[hackers] [sbase] libutil/recurse: only opendir if recursing || David Phillips

From: <git_AT_suckless.org>
Date: Sun, 1 Oct 2017 21:01:43 +0200 (CEST)

commit 69b9c2444b71babe012467b13aa408894b14a6cd
Author: David Phillips <david_AT_sighup.nz>
AuthorDate: Sun Oct 1 21:39:27 2017 +1300
Commit: Michael Forney <mforney_AT_mforney.org>
CommitDate: Sun Oct 1 09:34:47 2017 -0700

    libutil/recurse: only opendir if recursing
    
    Previous behaviour was to call opendir regardless of if we are actually going
    to be recursing into the directory. Additionally, some utilities that use
    DIRFIRST benefit from running the function pointed to by fn before the call
    to opendir. One such example is `chmod [-R] 777 dir` on a directory with mode
    000, where it will be expected for chmod to first give itself rwx before
    optionally listing the directory to traverse it.

diff --git a/libutil/recurse.c b/libutil/recurse.c
index e2b8a6e..169e031 100644
--- a/libutil/recurse.c
+++ b/libutil/recurse.c
_AT_@ -54,18 +54,17 @@ recurse(const char *path, void *data, struct recursor *r)
                 if (h->ino == st.st_ino && h->dev == st.st_dev)
                         return;
 
- if (!(dp = opendir(path))) {
- if (!(r->flags & SILENT)) {
- weprintf("opendir %s:", path);
- recurse_status = 1;
- }
- return;
- }
-
         if (!r->depth && (r->flags & DIRFIRST))
                 (r->fn)(path, &st, data, r);
 
         if (!r->maxdepth || r->depth + 1 < r->maxdepth) {
+ if (!(dp = opendir(path))) {
+ if (!(r->flags & SILENT)) {
+ weprintf("opendir %s:", path);
+ recurse_status = 1;
+ }
+ return;
+ }
                 while ((d = readdir(dp))) {
                         if (r->follow == 'H') {
                                 statf_name = "lstat";
_AT_@ -90,6 +89,7 @@ recurse(const char *path, void *data, struct recursor *r)
                                 r->depth--;
                         }
                 }
+ closedir(dp);
         }
 
         if (!r->depth) {
_AT_@ -102,6 +102,4 @@ recurse(const char *path, void *data, struct recursor *r)
                         free(h);
                 }
         }
-
- closedir(dp);
 }
Received on Sun Oct 01 2017 - 21:01:43 CEST

This archive was generated by hypermail 2.3.0 : Sun Oct 01 2017 - 21:12:21 CEST