musl: add common glibc extention for nftw
[openwrt/openwrt.git] / toolchain / musl / patches / 600-nftw-support-common-gnu-extension.patch
1 From 6f1143425a3afc4eb5086e9c90e7efb3affd7cb7 Mon Sep 17 00:00:00 2001
2 From: Tony Ambardar <Tony.Ambardar@gmail.com>
3 Date: Sat, 11 Jul 2020 06:35:46 -0700
4 Subject: [PATCH 2/2] nftw: support common gnu extension
5
6 Signed-off-by: Tony Ambardar <Tony.Ambardar@gmail.com>
7 ---
8 include/ftw.h | 8 ++++++++
9 src/misc/nftw.c | 35 ++++++++++++++++++++++++++++++-----
10 2 files changed, 38 insertions(+), 5 deletions(-)
11
12 diff --git a/include/ftw.h b/include/ftw.h
13 index b15c062a..ce85deac 100644
14 --- a/include/ftw.h
15 +++ b/include/ftw.h
16 @@ -21,6 +21,14 @@ extern "C" {
17 #define FTW_CHDIR 4
18 #define FTW_DEPTH 8
19
20 +#ifdef _GNU_SOURCE
21 +#define FTW_ACTIONRETVAL 16
22 +#define FTW_CONTINUE 0
23 +#define FTW_STOP 1
24 +#define FTW_SKIP_SUBTREE 2
25 +#define FTW_SKIP_SIBLINGS 3
26 +#endif
27 +
28 struct FTW {
29 int base;
30 int level;
31 diff --git a/src/misc/nftw.c b/src/misc/nftw.c
32 index 8dcff7fe..0bb7b601 100644
33 --- a/src/misc/nftw.c
34 +++ b/src/misc/nftw.c
35 @@ -1,3 +1,4 @@
36 +#define _GNU_SOURCE
37 #include <ftw.h>
38 #include <dirent.h>
39 #include <fcntl.h>
40 @@ -72,8 +73,20 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
41 if (!fd_limit) close(dfd);
42 }
43
44 - if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
45 - return r;
46 + if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
47 + if (flags & FTW_ACTIONRETVAL)
48 + switch (r) {
49 + case FTW_SKIP_SUBTREE:
50 + h = NULL;
51 + case FTW_CONTINUE:
52 + break;
53 + case FTW_SKIP_SIBLINGS:
54 + case FTW_STOP:
55 + return r;
56 + }
57 + else
58 + return r;
59 + }
60
61 for (; h; h = h->chain)
62 if (h->dev == st.st_dev && h->ino == st.st_ino)
63 @@ -101,7 +114,10 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
64 strcpy(path+j+1, de->d_name);
65 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
66 closedir(d);
67 - return r;
68 + if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SIBLINGS)
69 + break;
70 + else
71 + return r;
72 }
73 }
74 closedir(d);
75 @@ -112,8 +128,16 @@ static int do_nftw(char *path, int (*fn)(const char *, const struct stat *, int,
76 }
77
78 path[l] = 0;
79 - if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
80 - return r;
81 + if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
82 + if (flags & FTW_ACTIONRETVAL)
83 + switch (r) {
84 + case FTW_SKIP_SIBLINGS:
85 + case FTW_STOP:
86 + return r;
87 + }
88 + else
89 + return r;
90 + }
91
92 return 0;
93 }
94 @@ -139,4 +163,5 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str
95 return r;
96 }
97
98 +#undef nftw64
99 weak_alias(nftw, nftw64);
100 --
101 2.17.1
102