Welcome to little lamb

Code » anopa » commit c3242c3

scan_dir(): Always set DT_REG or DT_DIR

author Olivier Brunel
2015-02-21 13:30:28 UTC
committer Olivier Brunel
2015-04-04 12:47:34 UTC
parent 738bd8ef86cfbdf7d7ca2274ceb93764067132f0

scan_dir(): Always set DT_REG or DT_DIR

Since we stat() if DT_UNKNOWN because of the files_only flag, let's
update the direntry so the iterator doesn't have to stat() again.

src/anopa/aa-enable.c +1 -1
src/anopa/aa-stop.c +1 -10
src/libanopa/scan_dir.c +6 -2

diff --git a/src/anopa/aa-enable.c b/src/anopa/aa-enable.c
index 06245a2..98551f5 100644
--- a/src/anopa/aa-enable.c
+++ b/src/anopa/aa-enable.c
@@ -116,7 +116,7 @@ enable_service (const char *name, int from_next)
 static int
 it_list (direntry *d, void *data)
 {
-    if (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN)
+    if (d->d_type != DT_DIR)
         enable_service (d->d_name, 0);
     else
     {
diff --git a/src/anopa/aa-stop.c b/src/anopa/aa-stop.c
index 7738f6e..0aff6e1 100644
--- a/src/anopa/aa-stop.c
+++ b/src/anopa/aa-stop.c
@@ -134,17 +134,8 @@ add_service (const char *name)
 static int
 it_stop (direntry *d, void *data)
 {
-    if (*d->d_name == '.' || (d->d_type != DT_DIR && d->d_type != DT_UNKNOWN))
+    if (*d->d_name == '.' || d->d_type != DT_DIR)
         return 0;
-    else if (d->d_type == DT_UNKNOWN)
-    {
-        struct stat st;
-
-        if (stat (d->d_name, &st) < 0)
-            return 0;
-        if (!S_ISDIR (st.st_mode))
-            return 0;
-    }
 
     tain_now_g ();
     add_service (d->d_name);
diff --git a/src/libanopa/scan_dir.c b/src/libanopa/scan_dir.c
index 34d107e..e0bf2e5 100644
--- a/src/libanopa/scan_dir.c
+++ b/src/libanopa/scan_dir.c
@@ -48,10 +48,14 @@ aa_scan_dir (stralloc *sa, int files_only, aa_sd_it_fn iterator, void *data)
             rr = stat (sa->s, &st);
             sa->len = l;
             sa->s[l - 1] = '\0';
-            if (rr != 0 || (!S_ISREG (st.st_mode) && (files_only || !S_ISDIR (st.st_mode))))
+            if (rr != 0)
                 continue;
+            if (S_ISREG (st.st_mode))
+                d->d_type = DT_REG;
+            else if (S_ISDIR (st.st_mode))
+                d->d_type = DT_DIR;
         }
-        else if (d->d_type != DT_REG && (files_only || d->d_type != DT_DIR))
+        if (d->d_type != DT_REG && (files_only || d->d_type != DT_DIR))
             continue;
 
         r = iterator (d, data);