[haiku-commits] haiku: hrev56173 - src/system/libroot/posix src/libs/bsd headers/posix headers/compatibility/bsd src/system/libroot/posix/musl/misc

  • From: waddlesplash <waddlesplash@xxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Thu, 9 Jun 2022 20:31:19 +0000 (UTC)

hrev56173 adds 4 changesets to branch 'master'
old head: 2b609f8f66853463253fa5a53a861c3e0726b0f1
new head: 0afb8a1b494ba36c5effd3430240b8e9439076c7
overview: 
https://git.haiku-os.org/haiku/log/?qt=range&q=0afb8a1b494b+%5E2b609f8f6685

----------------------------------------------------------------------------

092b6d4a98ea: libroot: Replace custom ffs implementation with musl's.
  
  It already has a per-arch implementation or a fallback in
  musl's own arch_atomic.h, which we already imported so we
  might as well leverage it.

22b1d791b2aa: libroot: Replace getsubopt implementation with musl's.
  
  It looks to be a bit more optimized, and one less thing for
  us to have to maintain from POSIX.

9b2434d26fb4: libroot: Replace ftw/nftw implemenations with their musl 
counterparts.
  
  Rewrite ftw.h to be a Haiku header instead of a BSD one while at it.

0afb8a1b494b: Move fts.h and functions from libroot to libbsd.
  
  These are BSD extensions, not POSIX functions. They were needed
  in libroot by the previous versions of the ftw/nftw implementations,
  but the musl versions do not need them, and so we can move them to
  libbsd.
  
  This is a minor ABI break, but hopefully whatever was using them
  in libroot also links to libbsd. If not, that's an easy enough fix.
  (These were only added to libroot in 2013.)

                              [ Augustin Cavalier <waddlesplash@xxxxxxxxx> ]

----------------------------------------------------------------------------

16 files changed, 220 insertions(+), 315 deletions(-)
headers/{posix => compatibility/bsd}/fts.h     |   6 +
headers/posix/ftw.h                            |  75 ++++-------
src/libs/bsd/Jamfile                           |   1 +
src/{system/libroot/posix => libs/bsd}/fts.c   |   0
src/system/libroot/posix/Jamfile               |   3 -
src/system/libroot/posix/ftw.c                 |  91 -------------
src/system/libroot/posix/musl/misc/Jamfile     |   5 +
src/system/libroot/posix/musl/misc/ffs.c       |   6 +
src/system/libroot/posix/musl/misc/ftw.c       |   9 ++
src/system/libroot/posix/musl/misc/getsubopt.c |  23 ++++
src/system/libroot/posix/musl/misc/nftw.c      | 142 +++++++++++++++++++++
src/system/libroot/posix/nftw.c                | 109 ----------------
src/system/libroot/posix/stdlib/Jamfile        |   1 -
src/system/libroot/posix/stdlib/getsubopt.cpp  |  39 ------
src/system/libroot/posix/string/Jamfile        |   1 -
src/system/libroot/posix/string/ffs.cpp        |  24 ----

############################################################################

Commit:      092b6d4a98ea7950eb5d3631bb0d02cd761f62d8
URL:         https://git.haiku-os.org/haiku/commit/?id=092b6d4a98ea
Author:      Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date:        Thu Jun  9 20:04:11 2022 UTC

libroot: Replace custom ffs implementation with musl's.

It already has a per-arch implementation or a fallback in
musl's own arch_atomic.h, which we already imported so we
might as well leverage it.

----------------------------------------------------------------------------

diff --git a/src/system/libroot/posix/musl/misc/Jamfile 
b/src/system/libroot/posix/musl/misc/Jamfile
index 18e9124faf..739adabb61 100644
--- a/src/system/libroot/posix/musl/misc/Jamfile
+++ b/src/system/libroot/posix/musl/misc/Jamfile
@@ -2,6 +2,7 @@ SubDir HAIKU_TOP src system libroot posix musl misc ;
 
 SubDirSysHdrs [ FDirName $(SUBDIR) .. include ] ;
 UseHeaders [ FDirName $(SUBDIR) .. internal ] ;
+UseHeaders [ FDirName $(SUBDIR) .. arch $(TARGET_KERNEL_ARCH_DIR) ] ;
 
 local architectureObject ;
 for architectureObject in [ MultiArchSubDirSetup ] {
@@ -10,6 +11,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
 
                MergeObject <$(architecture)>posix_musl_misc.o :
                        a64l.c
+                       ffs.c
                        ;
        }
 }
diff --git a/src/system/libroot/posix/musl/misc/ffs.c 
b/src/system/libroot/posix/musl/misc/ffs.c
new file mode 100644
index 0000000000..240b3a101d
--- /dev/null
+++ b/src/system/libroot/posix/musl/misc/ffs.c
@@ -0,0 +1,6 @@
+#include "atomic.h"
+
+int ffs(int i)
+{
+       return i ? a_ctz_l(i)+1 : 0;
+}
diff --git a/src/system/libroot/posix/string/Jamfile 
b/src/system/libroot/posix/string/Jamfile
index 609c0fdb4c..105ff21c63 100644
--- a/src/system/libroot/posix/string/Jamfile
+++ b/src/system/libroot/posix/string/Jamfile
@@ -20,7 +20,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
                        bcmp.c
                        bcopy.c
                        bzero.c
-                       ffs.cpp
                        memccpy.c
                        memchr.c
                        memcmp.c
diff --git a/src/system/libroot/posix/string/ffs.cpp 
b/src/system/libroot/posix/string/ffs.cpp
deleted file mode 100644
index 0005f34afe..0000000000
--- a/src/system/libroot/posix/string/ffs.cpp
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2020, Adrien Destugues <pulkomandy@xxxxxxxxxxxxx>.
- * Distributed under the terms of the MIT License.
- */
-
-
-// find first (least significant) set bit
-extern "C" int
-ffs(int value)
-{
-#ifdef __riscv
-       // TODO: As of this writing, gcc 8.x seems
-       // to have an issue with infinite recursion.
-       // Re-examine in future GCC updates
-       int bit;
-       if (value == 0)
-               return 0;
-       for (bit = 1; !(value & 1); bit++)
-               value >>= 1;
-       return bit;
-#else
-       return __builtin_ffs(value);
-#endif
-}

############################################################################

Commit:      22b1d791b2aa81e134295b60a2471fb621793c37
URL:         https://git.haiku-os.org/haiku/commit/?id=22b1d791b2aa
Author:      Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date:        Thu Jun  9 20:06:56 2022 UTC

libroot: Replace getsubopt implementation with musl's.

It looks to be a bit more optimized, and one less thing for
us to have to maintain from POSIX.

----------------------------------------------------------------------------

diff --git a/src/system/libroot/posix/musl/misc/Jamfile 
b/src/system/libroot/posix/musl/misc/Jamfile
index 739adabb61..e8e6c29e8b 100644
--- a/src/system/libroot/posix/musl/misc/Jamfile
+++ b/src/system/libroot/posix/musl/misc/Jamfile
@@ -12,6 +12,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
                MergeObject <$(architecture)>posix_musl_misc.o :
                        a64l.c
                        ffs.c
+                       getsubopt.c
                        ;
        }
 }
diff --git a/src/system/libroot/posix/musl/misc/getsubopt.c 
b/src/system/libroot/posix/musl/misc/getsubopt.c
new file mode 100644
index 0000000000..53ee9573f2
--- /dev/null
+++ b/src/system/libroot/posix/musl/misc/getsubopt.c
@@ -0,0 +1,23 @@
+#include <stdlib.h>
+#include <string.h>
+
+int getsubopt(char **opt, char *const *keys, char **val)
+{
+       char *s = *opt;
+       int i;
+
+       *val = NULL;
+       *opt = strchr(s, ',');
+       if (*opt) *(*opt)++ = 0;
+       else *opt = s + strlen(s);
+
+       for (i=0; keys[i]; i++) {
+               size_t l = strlen(keys[i]);
+               if (strncmp(keys[i], s, l)) continue;
+               if (s[l] == '=')
+                       *val = s + l + 1;
+               else if (s[l]) continue;
+               return i;
+       }
+       return -1;
+}
diff --git a/src/system/libroot/posix/stdlib/Jamfile 
b/src/system/libroot/posix/stdlib/Jamfile
index e97b601fee..2beea8ef8c 100644
--- a/src/system/libroot/posix/stdlib/Jamfile
+++ b/src/system/libroot/posix/stdlib/Jamfile
@@ -21,7 +21,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
                        div.c
                        env.cpp
                        exit.cpp
-                       getsubopt.cpp
                        heapsort.c
                        merge.c
                        mktemp.c
diff --git a/src/system/libroot/posix/stdlib/getsubopt.cpp 
b/src/system/libroot/posix/stdlib/getsubopt.cpp
deleted file mode 100644
index 9346bcef32..0000000000
--- a/src/system/libroot/posix/stdlib/getsubopt.cpp
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * Copyright 2010, Oliver Tappe <zooey@xxxxxxxxxxxxxxx>.
- * Distributed under the terms of the MIT License.
- */
-
-
-#include <stdlib.h>
-#include <string.h>
-
-
-int
-getsubopt(char** optionPtr, char* const* keys, char** valuePtr)
-{
-       if (optionPtr == NULL || valuePtr == NULL)
-               return -1;
-
-       char* option = *optionPtr;
-       if (*option == '\0')
-               return -1;
-
-       char* startOfNextOption = strchr(option, ',');
-       if (startOfNextOption != NULL)
-               *startOfNextOption++ = '\0';
-       else
-               startOfNextOption = option + strlen(option);
-       *optionPtr = startOfNextOption;
-
-       char* startOfValue = strchr(option, '=');
-       if (startOfValue != NULL)
-               *startOfValue++ = '\0';
-       *valuePtr = startOfValue;
-
-       for (int keyIndex = 0; keys[keyIndex] != NULL; ++keyIndex) {
-               if (strcmp(option, keys[keyIndex]) == 0)
-                       return keyIndex;
-       }
-
-       return -1;
-}

############################################################################

Commit:      9b2434d26fb46dc01227d617319f4ddb240a6472
URL:         https://git.haiku-os.org/haiku/commit/?id=9b2434d26fb4
Author:      Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date:        Thu Jun  9 20:24:11 2022 UTC

libroot: Replace ftw/nftw implemenations with their musl counterparts.

Rewrite ftw.h to be a Haiku header instead of a BSD one while at it.

----------------------------------------------------------------------------

diff --git a/headers/posix/ftw.h b/headers/posix/ftw.h
index a42f74d6f2..be2a2799c3 100644
--- a/headers/posix/ftw.h
+++ b/headers/posix/ftw.h
@@ -1,63 +1,44 @@
-/*     $OpenBSD: ftw.h,v 1.1 2003/07/21 21:13:18 millert Exp $ */
-
 /*
- * Copyright (c) 2003 Todd C. Miller <Todd.Miller@xxxxxxxxxxxxx>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F39502-99-1-0512.
- *
- * $FreeBSD$
+ * Copyright 2022, Haiku, Inc. All rights reserved.
+ * Distributed under the terms of the MIT License.
  */
+#ifndef        _FTW_H_
+#define        _FTW_H_
 
-#ifndef        _FTW_H
-#define        _FTW_H
 
-#include <sys/cdefs.h>
-#include <sys/types.h>
+#include <features.h>
 #include <sys/stat.h>
 
-/*
- * Valid flags for the 3rd argument to the function that is passed as the
- * second argument to ftw(3) and nftw(3).  Say it three times fast!
- */
-#define        FTW_F           0       /* File.  */
-#define        FTW_D           1       /* Directory.  */
-#define        FTW_DNR         2       /* Directory without read permission.  
*/
-#define        FTW_DP          3       /* Directory with subdirectories 
visited.  */
-#define        FTW_NS          4       /* Unknown type; stat() failed.  */
-#define        FTW_SL          5       /* Symbolic link.  */
-#define        FTW_SLN         6       /* Sym link that names a nonexistent 
file.  */
 
-/*
- * Flags for use as the 4th argument to nftw(3).  These may be ORed together.
- */
-#define        FTW_PHYS        0x01    /* Physical walk, don't follow sym 
links.  */
-#define        FTW_MOUNT       0x02    /* The walk does not cross a mount 
point.  */
-#define        FTW_DEPTH       0x04    /* Subdirs visited before the dir 
itself. */
-#define        FTW_CHDIR       0x08    /* Change to a directory before reading 
it. */
+#define        FTW_F           0
+#define        FTW_D           1
+#define        FTW_DNR         2
+#define        FTW_DP          3
+#define        FTW_NS          4
+#define        FTW_SL          5
+#define        FTW_SLN         6
+
+#define        FTW_PHYS        0x01
+#define        FTW_MOUNT       0x02
+#define        FTW_DEPTH       0x04
+#define        FTW_CHDIR       0x08
 
 struct FTW {
        int base;
        int level;
 };
 
-__BEGIN_DECLS
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 int    ftw(const char *, int (*)(const char *, const struct stat *, int), int);
 int    nftw(const char *, int (*)(const char *, const struct stat *, int,
-           struct FTW *), int, int);
-__END_DECLS
+       struct FTW *), int, int);
+
+#ifdef __cplusplus
+}
+#endif
 
-#endif /* !_FTW_H */
+#endif /* _FTW_H_ */
diff --git a/src/system/libroot/posix/Jamfile b/src/system/libroot/posix/Jamfile
index d91c0d1a04..c8322d107b 100644
--- a/src/system/libroot/posix/Jamfile
+++ b/src/system/libroot/posix/Jamfile
@@ -33,11 +33,9 @@ for architectureObject in [ MultiArchSubDirSetup ] {
                        errno.c
                        fcntl.cpp
                        fts.c
-                       ftw.c
                        glob.c
                        inttypes.c
                        libgen.cpp
-                       nftw.c
                        poll.cpp
                        $(PWD_BACKEND)
                        scheduler.cpp
diff --git a/src/system/libroot/posix/ftw.c b/src/system/libroot/posix/ftw.c
deleted file mode 100644
index 67ff8c20e1..0000000000
--- a/src/system/libroot/posix/ftw.c
+++ /dev/null
@@ -1,91 +0,0 @@
-/*     $OpenBSD: ftw.c,v 1.5 2005/08/08 08:05:34 espie Exp $   */
-
-/*
- * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@xxxxxxxxxxxxx>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F39502-99-1-0512.
- */
-
-#include <sys/cdefs.h>
-// __FBSDID("$FreeBSD$");
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fts.h>
-#include <ftw.h>
-
-int
-ftw(const char *path, int (*fn)(const char *, const struct stat *, int),
-    int nfds)
-{
-       char * const paths[2] = { (char *)path, NULL };
-       FTSENT *cur;
-       FTS *ftsp;
-       int error = 0, fnflag, sverrno;
-
-       /* XXX - nfds is currently unused */
-       if (nfds < 1) {
-               errno = EINVAL;
-               return (-1);
-       }
-
-       ftsp = fts_open(paths, FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR, NULL);
-       if (ftsp == NULL)
-               return (-1);
-       while ((cur = fts_read(ftsp)) != NULL) {
-               switch (cur->fts_info) {
-               case FTS_D:
-                       fnflag = FTW_D;
-                       break;
-               case FTS_DNR:
-                       fnflag = FTW_DNR;
-                       break;
-               case FTS_DP:
-                       /* we only visit in preorder */
-                       continue;
-               case FTS_F:
-               case FTS_DEFAULT:
-                       fnflag = FTW_F;
-                       break;
-               case FTS_NS:
-               case FTS_NSOK:
-               case FTS_SLNONE:
-                       fnflag = FTW_NS;
-                       break;
-               case FTS_SL:
-                       fnflag = FTW_SL;
-                       break;
-               case FTS_DC:
-                       errno = ELOOP;
-                       /* FALLTHROUGH */
-               default:
-                       error = -1;
-                       goto done;
-               }
-               error = fn(cur->fts_path, cur->fts_statp, fnflag);
-               if (error != 0)
-                       break;
-       }
-done:
-       sverrno = errno;
-       if (fts_close(ftsp) != 0 && error == 0)
-               error = -1;
-       else
-               errno = sverrno;
-       return (error);
-}
diff --git a/src/system/libroot/posix/musl/misc/Jamfile 
b/src/system/libroot/posix/musl/misc/Jamfile
index e8e6c29e8b..4a4d7fb998 100644
--- a/src/system/libroot/posix/musl/misc/Jamfile
+++ b/src/system/libroot/posix/musl/misc/Jamfile
@@ -12,7 +12,9 @@ for architectureObject in [ MultiArchSubDirSetup ] {
                MergeObject <$(architecture)>posix_musl_misc.o :
                        a64l.c
                        ffs.c
+                       ftw.c
                        getsubopt.c
+                       nftw.c
                        ;
        }
 }
diff --git a/src/system/libroot/posix/musl/misc/ftw.c 
b/src/system/libroot/posix/musl/misc/ftw.c
new file mode 100644
index 0000000000..e757fc6f0c
--- /dev/null
+++ b/src/system/libroot/posix/musl/misc/ftw.c
@@ -0,0 +1,9 @@
+#include <ftw.h>
+
+int ftw(const char *path, int (*fn)(const char *, const struct stat *, int), 
int fd_limit)
+{
+       /* The following cast assumes that calling a function with one
+        * argument more than it needs behaves as expected. This is
+        * actually undefined, but works on all real-world machines. */
+       return nftw(path, (int (*)())fn, fd_limit, FTW_PHYS);
+}
diff --git a/src/system/libroot/posix/musl/misc/nftw.c 
b/src/system/libroot/posix/musl/misc/nftw.c
new file mode 100644
index 0000000000..cf55ec5a68
--- /dev/null
+++ b/src/system/libroot/posix/musl/misc/nftw.c
@@ -0,0 +1,142 @@
+#include <ftw.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <limits.h>
+#include <pthread.h>
+
+struct history
+{
+       struct history *chain;
+       dev_t dev;
+       ino_t ino;
+       int level;
+       int base;
+};
+
+#undef dirfd
+#define dirfd(d) (*(int *)d)
+
+static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, 
int, struct FTW *), int fd_limit, int flags, struct history *h)
+{
+       size_t l = strlen(path), j = l && path[l-1]=='/' ? l-1 : l;
+       struct stat st;
+       struct history new;
+       int type;
+       int r;
+       int dfd;
+       int err;
+       struct FTW lev;
+
+       if ((flags & FTW_PHYS) ? lstat(path, &st) : stat(path, &st) < 0) {
+               if (!(flags & FTW_PHYS) && errno==ENOENT && !lstat(path, &st))
+                       type = FTW_SLN;
+               else if (errno != EACCES) return -1;
+               else type = FTW_NS;
+       } else if (S_ISDIR(st.st_mode)) {
+               if (flags & FTW_DEPTH) type = FTW_DP;
+               else type = FTW_D;
+       } else if (S_ISLNK(st.st_mode)) {
+               if (flags & FTW_PHYS) type = FTW_SL;
+               else type = FTW_SLN;
+       } else {
+               type = FTW_F;
+       }
+
+       if ((flags & FTW_MOUNT) && h && st.st_dev != h->dev)
+               return 0;
+
+       new.chain = h;
+       new.dev = st.st_dev;
+       new.ino = st.st_ino;
+       new.level = h ? h->level+1 : 0;
+       new.base = j+1;
+
+       lev.level = new.level;
+       if (h) {
+               lev.base = h->base;
+       } else {
+               size_t k;
+               for (k=j; k && path[k]=='/'; k--);
+               for (; k && path[k-1]!='/'; k--);
+               lev.base = k;
+       }
+
+       if (type == FTW_D || type == FTW_DP) {
+               dfd = open(path, O_RDONLY);
+               err = errno;
+               if (dfd < 0 && err == EACCES) type = FTW_DNR;
+               if (!fd_limit) close(dfd);
+       }
+
+       if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
+               return r;
+
+       for (; h; h = h->chain)
+               if (h->dev == st.st_dev && h->ino == st.st_ino)
+                       return 0;
+
+       if ((type == FTW_D || type == FTW_DP) && fd_limit) {
+               if (dfd < 0) {
+                       errno = err;
+                       return -1;
+               }
+               {
+               DIR *d = fdopendir(dfd);
+               if (d) {
+                       struct dirent *de;
+                       while ((de = readdir(d))) {
+                               if (de->d_name[0] == '.'
+                                && (!de->d_name[1]
+                                 || (de->d_name[1]=='.'
+                                  && !de->d_name[2]))) continue;
+                               if (strlen(de->d_name) >= PATH_MAX-l) {
+                                       errno = ENAMETOOLONG;
+                                       closedir(d);
+                                       return -1;
+                               }
+                               path[j]='/';
+                               strcpy(path+j+1, de->d_name);
+                               if ((r=do_nftw(path, fn, fd_limit-1, flags, 
&new))) {
+                                       closedir(d);
+                                       return r;
+                               }
+                       }
+                       closedir(d);
+               } else {
+                       close(dfd);
+                       return -1;
+               }
+               }
+       }
+
+       path[l] = 0;
+       if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
+               return r;
+
+       return 0;
+}
+
+int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, 
struct FTW *), int fd_limit, int flags)
+{
+       int r, cs;
+       size_t l;
+       char pathbuf[PATH_MAX+1];
+
+       if (fd_limit <= 0) return 0;
+
+       l = strlen(path);
+       if (l > PATH_MAX) {
+               errno = ENAMETOOLONG;
+               return -1;
+       }
+       memcpy(pathbuf, path, l+1);
+
+       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+       r = do_nftw(pathbuf, fn, fd_limit, flags, NULL);
+       pthread_setcancelstate(cs, 0);
+       return r;
+}
diff --git a/src/system/libroot/posix/nftw.c b/src/system/libroot/posix/nftw.c
deleted file mode 100644
index 88e8c09074..0000000000
--- a/src/system/libroot/posix/nftw.c
+++ /dev/null
@@ -1,109 +0,0 @@
-/*     $OpenBSD: nftw.c,v 1.7 2006/03/31 19:41:44 millert Exp $        */
-
-/*
- * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@xxxxxxxxxxxxx>
- *
- * Permission to use, copy, modify, and distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- * Sponsored in part by the Defense Advanced Research Projects
- * Agency (DARPA) and Air Force Research Laboratory, Air Force
- * Materiel Command, USAF, under agreement number F39502-99-1-0512.
- */
-
-#include <sys/cdefs.h>
-//__FBSDID("$FreeBSD$");
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <errno.h>
-#include <fts.h>
-#include <ftw.h>
-
-int
-nftw(const char *path, int (*fn)(const char *, const struct stat *, int,
-     struct FTW *), int nfds, int ftwflags)
-{
-       char * const paths[2] = { (char *)path, NULL };
-       struct FTW ftw;
-       FTSENT *cur;
-       FTS *ftsp;
-       int error = 0, ftsflags, fnflag, postorder, sverrno;
-
-       /* XXX - nfds is currently unused */
-       if (nfds < 1) {
-               errno = EINVAL;
-               return (-1);
-       }
-
-       ftsflags = FTS_COMFOLLOW;
-       if (!(ftwflags & FTW_CHDIR))
-               ftsflags |= FTS_NOCHDIR;
-       if (ftwflags & FTW_MOUNT)
-               ftsflags |= FTS_XDEV;
-       if (ftwflags & FTW_PHYS)
-               ftsflags |= FTS_PHYSICAL;
-       else
-               ftsflags |= FTS_LOGICAL;
-       postorder = (ftwflags & FTW_DEPTH) != 0;
-       ftsp = fts_open(paths, ftsflags, NULL);
-       if (ftsp == NULL)
-               return (-1);
-       while ((cur = fts_read(ftsp)) != NULL) {
-               switch (cur->fts_info) {
-               case FTS_D:
-                       if (postorder)
-                               continue;
-                       fnflag = FTW_D;
-                       break;
-               case FTS_DC:
-                       continue;
-               case FTS_DNR:
-                       fnflag = FTW_DNR;
-                       break;
-               case FTS_DP:
-                       if (!postorder)
-                               continue;
-                       fnflag = FTW_DP;
-                       break;
-               case FTS_F:
-               case FTS_DEFAULT:
-                       fnflag = FTW_F;
-                       break;
-               case FTS_NS:
-               case FTS_NSOK:
-                       fnflag = FTW_NS;
-                       break;
-               case FTS_SL:
-                       fnflag = FTW_SL;
-                       break;
-               case FTS_SLNONE:
-                       fnflag = FTW_SLN;
-                       break;
-               default:
-                       error = -1;
-                       goto done;
-               }
-               ftw.base = cur->fts_pathlen - cur->fts_namelen;
-               ftw.level = cur->fts_level;
-               error = fn(cur->fts_path, cur->fts_statp, fnflag, &ftw);
-               if (error != 0)
-                       break;
-       }
-done:
-       sverrno = errno;
-       if (fts_close(ftsp) != 0 && error == 0)
-               error = -1;
-       else
-               errno = sverrno;
-       return (error);
-}

############################################################################

Revision:    hrev56173
Commit:      0afb8a1b494ba36c5effd3430240b8e9439076c7
URL:         https://git.haiku-os.org/haiku/commit/?id=0afb8a1b494b
Author:      Augustin Cavalier <waddlesplash@xxxxxxxxx>
Date:        Thu Jun  9 20:30:40 2022 UTC

Move fts.h and functions from libroot to libbsd.

These are BSD extensions, not POSIX functions. They were needed
in libroot by the previous versions of the ftw/nftw implementations,
but the musl versions do not need them, and so we can move them to
libbsd.

This is a minor ABI break, but hopefully whatever was using them
in libroot also links to libbsd. If not, that's an easy enough fix.
(These were only added to libroot in 2013.)

----------------------------------------------------------------------------

diff --git a/headers/posix/fts.h b/headers/compatibility/bsd/fts.h
similarity index 98%
rename from headers/posix/fts.h
rename to headers/compatibility/bsd/fts.h
index 2f3780b715..1b7467740e 100644
--- a/headers/posix/fts.h
+++ b/headers/compatibility/bsd/fts.h
@@ -33,6 +33,10 @@
 #ifndef        _FTS_H_
 #define        _FTS_H_
 
+#include <features.h>
+
+#ifdef _DEFAULT_SOURCE
+
 #include <sys/cdefs.h>
 
 typedef struct {
@@ -135,4 +139,6 @@ int  fts_set(FTS *, FTSENT *, int);
 void    fts_set_clientptr(FTS *, void *);
 __END_DECLS
 
+#endif /* _DEFAULT_SOURCE */
+
 #endif /* !_FTS_H_ */
diff --git a/src/libs/bsd/Jamfile b/src/libs/bsd/Jamfile
index 0b3c70d046..3f22999240 100644
--- a/src/libs/bsd/Jamfile
+++ b/src/libs/bsd/Jamfile
@@ -13,6 +13,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
                        err.c
                        explicit_bzero.c
                        fgetln.c
+                       fts.c
                        getpass.c
                        issetugid.c
                        lutimes.c
diff --git a/src/system/libroot/posix/fts.c b/src/libs/bsd/fts.c
similarity index 100%
rename from src/system/libroot/posix/fts.c
rename to src/libs/bsd/fts.c
diff --git a/src/system/libroot/posix/Jamfile b/src/system/libroot/posix/Jamfile
index c8322d107b..3271dce2c5 100644
--- a/src/system/libroot/posix/Jamfile
+++ b/src/system/libroot/posix/Jamfile
@@ -32,7 +32,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
                        dirent.c
                        errno.c
                        fcntl.cpp
-                       fts.c
                        glob.c
                        inttypes.c
                        libgen.cpp


Other related posts:

  • » [haiku-commits] haiku: hrev56173 - src/system/libroot/posix src/libs/bsd headers/posix headers/compatibility/bsd src/system/libroot/posix/musl/misc - waddlesplash