81c96ad76c3a761593ceba8ccb5013b84ac39f07
[openwrt/staging/chunkeey.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 --- a/include/ftw.h
13 +++ b/include/ftw.h
14 @@ -21,6 +21,14 @@ extern "C" {
15 #define FTW_CHDIR 4
16 #define FTW_DEPTH 8
17
18 +#ifdef _GNU_SOURCE
19 +#define FTW_ACTIONRETVAL 16
20 +#define FTW_CONTINUE 0
21 +#define FTW_STOP 1
22 +#define FTW_SKIP_SUBTREE 2
23 +#define FTW_SKIP_SIBLINGS 3
24 +#endif
25 +
26 struct FTW {
27 int base;
28 int level;
29 --- a/src/misc/nftw.c
30 +++ b/src/misc/nftw.c
31 @@ -1,3 +1,4 @@
32 +#define _GNU_SOURCE
33 #include <ftw.h>
34 #include <dirent.h>
35 #include <sys/stat.h>
36 @@ -63,8 +64,20 @@ static int do_nftw(char *path, int (*fn)
37 lev.base = k;
38 }
39
40 - if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
41 - return r;
42 + if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
43 + if (flags & FTW_ACTIONRETVAL)
44 + switch (r) {
45 + case FTW_SKIP_SUBTREE:
46 + h = NULL;
47 + case FTW_CONTINUE:
48 + break;
49 + case FTW_SKIP_SIBLINGS:
50 + case FTW_STOP:
51 + return r;
52 + }
53 + else
54 + return r;
55 + }
56
57 for (; h; h = h->chain)
58 if (h->dev == st.st_dev && h->ino == st.st_ino)
59 @@ -88,7 +101,10 @@ static int do_nftw(char *path, int (*fn)
60 strcpy(path+j+1, de->d_name);
61 if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
62 closedir(d);
63 - return r;
64 + if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SIBLINGS)
65 + break;
66 + else
67 + return r;
68 }
69 }
70 closedir(d);
71 @@ -98,8 +114,16 @@ static int do_nftw(char *path, int (*fn)
72 }
73
74 path[l] = 0;
75 - if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
76 - return r;
77 + if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
78 + if (flags & FTW_ACTIONRETVAL)
79 + switch (r) {
80 + case FTW_SKIP_SIBLINGS:
81 + case FTW_STOP:
82 + return r;
83 + }
84 + else
85 + return r;
86 + }
87
88 return 0;
89 }
90 @@ -125,4 +149,5 @@ int nftw(const char *path, int (*fn)(con
91 return r;
92 }
93
94 +#undef nftw64
95 weak_alias(nftw, nftw64);