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 15340 invoked from network); 24 Jan 2022 00:48:01 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 24 Jan 2022 00:48:01 -0000 Received: (qmail 12283 invoked by uid 550); 24 Jan 2022 00:47:59 -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 12243 invoked from network); 24 Jan 2022 00:47:58 -0000 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=5UFYxdSAAFP822D9mGTnkPEDNZ9qmMcyJEUYD48/VS0=; b=K1v45NKxS/adobU7IbIp8iz2lDJuAtkjJQIdvTUdSURMAlH4OYJaZOV4EB8UuHGriq 12hGenWbZRUqL7rLrQ+C7+kT7gBafu8zCSWC102aDjfF/ahXaaXOCBeyFNJTf+YNd8iM fUScJxmZkoGl8rhklheaXGMz5CfezV+CcM0HPIfVQESKb0MJ1DhADmj8dAPshLJgyZuU 4/4/v1PHfnhF5EtieVe1M88Df5ZveFyhh/xmXMtajGSuXbPs9mOXFnWXGMZpT6fXoZf3 f68mrGptTEkjO5KOp90C/kW2Pr1UbbbsDaFR/vxEimvLY6C0TnZU8pp4S2+hGgYUGZxd lcrg== X-Gm-Message-State: AOAM532+HQCaVqDEuYo8Aj1F2zTnqrZZSsd7kBOsyAyNEXaN1B3QGqZx c1BE+TiUFhcXbI3EPgxI2flnXIwA8827CNu5p++bpfAbwY6xxRbfF77rwCWKqw6igakmxO7MEgr PFVijv+eA1DXv5NzYIS0= X-Received: by 2002:a05:6a00:1308:b0:4c5:e231:afd4 with SMTP id j8-20020a056a00130800b004c5e231afd4mr12277176pfu.34.1642985265537; Sun, 23 Jan 2022 16:47:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJwizvi6IzPpNyTuqG4MM8MBXqP2POLDkwHFAgK6ycc4YEVolpV7z0eg6woSM03OjYq9qoJtWg== X-Received: by 2002:a05:6a00:1308:b0:4c5:e231:afd4 with SMTP id j8-20020a056a00130800b004c5e231afd4mr12277162pfu.34.1642985265112; Sun, 23 Jan 2022 16:47:45 -0800 (PST) Date: Mon, 24 Jan 2022 09:47:33 +0900 From: Dominique MARTINET To: musl@lists.openwall.com Cc: Rich Felker , Ismael Luceno Message-ID: References: <20220123155955.16484-1-ismael@iodev.co.uk> <20220123155955.16484-3-ismael@iodev.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220123155955.16484-3-ismael@iodev.co.uk> Subject: Re: [musl] [PATCH v3 2/2] nftw: implement FTW_ACTIONRETVAL (GNU extension) This didn't get much traction when I submitted one last year: https://www.openwall.com/lists/musl/2021/03/26/1 (and there were at least a couple other occurences I could find at the time) But given it keeps getting resubmitted I assume we can say that confirms there's demand for it? Ismael Luceno wrote on Sun, Jan 23, 2022 at 04:59:55PM +0100: > 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 FWIW since this is a GNU extension I only had defined these in a #ifdef _GNU_SOURCE block, but this had the ugly side effect of having to redefine SUBTREE/SIBLINGS in the .c so I'm not sure which is better. > + > struct FTW { > int base; > int level; > diff --git a/src/misc/nftw.c b/src/misc/nftw.c > index 7569a657e54e..382169f66b8b 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,19 @@ 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; > + r = 0; > + if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) { > + if ((flags & FTW_ACTIONRETVAL)) { > + if (r == FTW_STOP) return FTW_STOP; > + if (r == FTW_SKIP_SUBTREE) return 0; > + /* other values are saved for when returning */ Hm, I'd naively think you would want to return immediately the other values as well, so the else below is wrong? But I didn't take long enough to check what e.g. a SKIP_SIBLINGS would mean here, the construction just looks a bit odd to me. > + } 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) { > @@ -107,9 +114,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); > @@ -120,10 +130,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 (too late), the caller is broken */ > + if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SUBTREE) > + return 0; IIRC the glibc implementation also ignores FTW_SKIP_SIBLINGS in that case (nftw returns 0), I'm not sure how much of a 1-to-1 implementation we want -- I had implemented my version through a black-box approach with a client exercising all kind of different code paths as for a gnu extension I'd assume glibc to be the reference. I haven't taken the time to rerun that comparison (the test client is in a comment in my patch), but I probably will if this looks like it would get merged unless someone else did. > + } > + return r; > } > > int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, struct FTW *), int fd_limit, int flags) -- Dominique