libfstools: fix foreachdir() to pass dir with a trailing slash
[project/fstools.git] / libfstools / overlay.c
index 6024586..d7dd74b 100644 (file)
@@ -91,15 +91,25 @@ foreachdir(const char *dir, int (*cb)(const char*))
        else
                sprintf(globdir, "%s/*", dir);
 
+       /* Include GLOB_MARK as callbacks expect a trailing slash */
        if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
                for (j = 0; j < gl.gl_pathc; j++) {
                        char *dir = gl.gl_pathv[j];
                        int len = strlen(gl.gl_pathv[j]);
+                       int err;
 
-                       if (len > 1 && dir[len - 1] == '/')
+                       /* Quick way of skipping files */
+                       if (dir[len - 1] != '/')
+                               continue;
+
+                       /* lstat needs path without a trailing slash */
+                       if (len > 1)
                                dir[len - 1] = '\0';
+                       err = lstat(gl.gl_pathv[j], &s);
+                       if (len > 1)
+                               dir[len - 1] = '/';
 
-                       if (!lstat(gl.gl_pathv[j], &s) && !S_ISLNK(s.st_mode))
+                       if (!err && !S_ISLNK(s.st_mode))
                                foreachdir(gl.gl_pathv[j], cb);
        }
        cb(dir);