chiark / gitweb /
Prep v234: Apply missing upstream fixes in src/basic (1/6)
[elogind.git] / src / basic / conf-files.c
index da8745b284d7462d9a3a03297b0784aff9bf09ce..b8f0f5d03d227e370d0db4b6a5aa07e152b89016 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <string.h>
+#include <dirent.h>
 #include <errno.h>
-#include <stdlib.h>
+#include <stdarg.h>
 #include <stdio.h>
-#include <dirent.h>
+#include <stdlib.h>
+#include <string.h>
 
+#include "conf-files.h"
+#include "dirent-util.h"
+#include "fd-util.h"
+#include "hashmap.h"
+#include "log.h"
 #include "macro.h"
-#include "util.h"
 #include "missing.h"
-#include "log.h"
-#include "strv.h"
 #include "path-util.h"
-#include "hashmap.h"
-#include "conf-files.h"
+#include "string-util.h"
+#include "strv.h"
+#include "util.h"
 
 static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
         _cleanup_closedir_ DIR *dir = NULL;
         const char *dirpath;
+        struct dirent *de;
         int r;
 
         assert(path);
-        assert(suffix);
 
         dirpath = prefix_roota(root, path);
 
@@ -51,22 +53,13 @@ static int files_add(Hashmap *h, const char *root, const char *path, const char
                 return -errno;
         }
 
-        for (;;) {
-                struct dirent *de;
+        FOREACH_DIRENT(de, dir, return -errno) {
                 char *p;
 
-                errno = 0;
-                de = readdir(dir);
-                if (!de && errno != 0)
-                        return -errno;
-
-                if (!de)
-                        break;
-
                 if (!dirent_is_file_with_suffix(de, suffix))
                         continue;
 
-                p = strjoin(dirpath, "/", de->d_name, NULL);
+                p = strjoin(dirpath, "/", de->d_name);
                 if (!p)
                         return -ENOMEM;
 
@@ -100,7 +93,6 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
         int r;
 
         assert(strv);
-        assert(suffix);
 
         /* This alters the dirs string array */
         if (!path_strv_resolve_uniq(dirs, root))
@@ -112,17 +104,15 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
 
         STRV_FOREACH(p, dirs) {
                 r = files_add(fh, root, *p, suffix);
-                if (r == -ENOMEM) {
+                if (r == -ENOMEM)
                         return r;
-                } else if (r < 0)
-                        log_debug_errno(r, "Failed to search for files in %s: %m",
-                                        *p);
+                if (r < 0)
+                        log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
         }
 
         files = hashmap_get_strv(fh);
-        if (files == NULL) {
+        if (!files)
                 return -ENOMEM;
-        }
 
         qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
         *strv = files;
@@ -134,7 +124,6 @@ int conf_files_list_strv(char ***strv, const char *suffix, const char *root, con
         _cleanup_strv_free_ char **copy = NULL;
 
         assert(strv);
-        assert(suffix);
 
         copy = strv_copy((char**) dirs);
         if (!copy)
@@ -148,7 +137,6 @@ int conf_files_list(char ***strv, const char *suffix, const char *root, const ch
         va_list ap;
 
         assert(strv);
-        assert(suffix);
 
         va_start(ap, dir);
         dirs = strv_new_ap(dir, ap);
@@ -164,7 +152,6 @@ int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, c
         _cleanup_strv_free_ char **dirs = NULL;
 
         assert(strv);
-        assert(suffix);
 
         dirs = strv_split_nulstr(d);
         if (!dirs)