From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 32641 invoked from network); 22 Jan 2022 14:43:49 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 22 Jan 2022 14:43:49 -0000 Received: (qmail 1367 invoked by uid 550); 22 Jan 2022 14:43:46 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 1332 invoked from network); 22 Jan 2022 14:43:45 -0000 Date: Sat, 22 Jan 2022 15:47:03 +0100 From: Ismael Luceno To: musl@lists.openwall.com Cc: Rich Felker Message-ID: References: <20220122132621.32485-1-ismael@iodev.co.uk> <20220122132621.32485-3-ismael@iodev.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220122132621.32485-3-ismael@iodev.co.uk> Subject: Re: [musl] [PATCH 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension) I noticed a small bug, I'll submit a v2. On 22/Jan/2022 14:26, Ismael Luceno wrote: > Signed-off-by: Ismael Luceno > --- > include/ftw.h | 7 +++++++ > src/misc/nftw.c | 32 ++++++++++++++++++++++---------- > 2 files changed, 29 insertions(+), 10 deletions(-) > > diff --git a/include/ftw.h b/include/ftw.h > index b15c062a8389..2e77dc76e11b 100644 > --- a/include/ftw.h > +++ b/include/ftw.h > @@ -21,6 +21,13 @@ extern "C" { > #define FTW_CHDIR 4 > #define FTW_DEPTH 8 > > +#define FTW_ACTIONRETVAL 16 > +/* return values for callback */ > +#define FTW_CONTINUE 0 > +#define FTW_STOP 1 > +#define FTW_SKIP_SUBTREE 2 > +#define FTW_SKIP_SIBLINGS 3 > + > struct FTW { > int base; > int level; > diff --git a/src/misc/nftw.c b/src/misc/nftw.c > index 0a35686e663d..4b934adcfb0c 100644 > --- a/src/misc/nftw.c > +++ b/src/misc/nftw.c > @@ -26,7 +26,7 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, > struct stat st; > struct history new; > int type; > - int r; > + int r, r2; > int dfd; > int err; > struct FTW lev; > @@ -72,12 +72,18 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, > if (!fd_limit) close(dfd); > } > > - if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) > - return r; > + if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) { > + if ((flags & FTW_ACTIONRETVAL)) { > + if (r == FTW_SKIP_SUBTREE) > + return 0; > + /* FTW_SKIP_SIBLINGS is saved for returning */ > + } else > + return r; > + } > > for (; h; h = h->chain) > if (h->dev == st.st_dev && h->ino == st.st_ino) > - return 0; > + return r; > > if ((type == FTW_D || type == FTW_DP) && fd_limit) { > if (dfd < 0) { > @@ -101,9 +107,12 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, > } > path[j]='/'; > strcpy(path+j+1, de->d_name); > - if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) { > + if ((r2=do_nftw(path, fn, fd_limit-1, flags, &new))) { > + if ((flags & FTW_ACTIONRETVAL) > + && r2 == FTW_SKIP_SIBLINGS) > + break; > closedir(d); > - return r; > + return r2; > } > } > closedir(d); > @@ -114,10 +123,13 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int, > } > > path[l] = 0; > - if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) > - return r; > - > - return 0; > + if (flags & FTW_DEPTH) { > + r = fn(path, &st, type, &lev); > + /* ignore FTW_SKIP_SUBTREE, the caller is broken */ > + if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SUBTREE) > + r = 0; > + } > + return r; > } > > int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags) > -- > 2.33.0 > -- Ismael Luceno http://iodev.co.uk/