600-nftw-support-common-gnu-extension.patch 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. From 6f1143425a3afc4eb5086e9c90e7efb3affd7cb7 Mon Sep 17 00:00:00 2001
  2. From: Tony Ambardar <[email protected]>
  3. Date: Sat, 11 Jul 2020 06:35:46 -0700
  4. Subject: [PATCH 2/2] nftw: support common gnu extension
  5. Signed-off-by: Tony Ambardar <[email protected]>
  6. ---
  7. include/ftw.h | 8 ++++++++
  8. src/misc/nftw.c | 35 ++++++++++++++++++++++++++++++-----
  9. 2 files changed, 38 insertions(+), 5 deletions(-)
  10. --- a/include/ftw.h
  11. +++ b/include/ftw.h
  12. @@ -21,6 +21,14 @@ extern "C" {
  13. #define FTW_CHDIR 4
  14. #define FTW_DEPTH 8
  15. +#ifdef _GNU_SOURCE
  16. +#define FTW_ACTIONRETVAL 16
  17. +#define FTW_CONTINUE 0
  18. +#define FTW_STOP 1
  19. +#define FTW_SKIP_SUBTREE 2
  20. +#define FTW_SKIP_SIBLINGS 3
  21. +#endif
  22. +
  23. struct FTW {
  24. int base;
  25. int level;
  26. --- a/src/misc/nftw.c
  27. +++ b/src/misc/nftw.c
  28. @@ -1,3 +1,4 @@
  29. +#define _GNU_SOURCE
  30. #include <ftw.h>
  31. #include <dirent.h>
  32. #include <fcntl.h>
  33. @@ -74,8 +75,20 @@ static int do_nftw(char *path, int (*fn)
  34. if (!fd_limit) close(dfd);
  35. }
  36. - if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
  37. - return r;
  38. + if (!(flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
  39. + if (flags & FTW_ACTIONRETVAL)
  40. + switch (r) {
  41. + case FTW_SKIP_SUBTREE:
  42. + h = NULL;
  43. + case FTW_CONTINUE:
  44. + break;
  45. + case FTW_SKIP_SIBLINGS:
  46. + case FTW_STOP:
  47. + return r;
  48. + }
  49. + else
  50. + return r;
  51. + }
  52. for (; h; h = h->chain)
  53. if (h->dev == st.st_dev && h->ino == st.st_ino)
  54. @@ -103,7 +116,10 @@ static int do_nftw(char *path, int (*fn)
  55. strcpy(path+j+1, de->d_name);
  56. if ((r=do_nftw(path, fn, fd_limit-1, flags, &new))) {
  57. closedir(d);
  58. - return r;
  59. + if ((flags & FTW_ACTIONRETVAL) && r == FTW_SKIP_SIBLINGS)
  60. + break;
  61. + else
  62. + return r;
  63. }
  64. }
  65. closedir(d);
  66. @@ -114,8 +130,16 @@ static int do_nftw(char *path, int (*fn)
  67. }
  68. path[l] = 0;
  69. - if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev)))
  70. - return r;
  71. + if ((flags & FTW_DEPTH) && (r=fn(path, &st, type, &lev))) {
  72. + if (flags & FTW_ACTIONRETVAL)
  73. + switch (r) {
  74. + case FTW_SKIP_SIBLINGS:
  75. + case FTW_STOP:
  76. + return r;
  77. + }
  78. + else
  79. + return r;
  80. + }
  81. return 0;
  82. }
  83. @@ -140,3 +164,5 @@ int nftw(const char *path, int (*fn)(con
  84. pthread_setcancelstate(cs, 0);
  85. return r;
  86. }
  87. +
  88. +#undef nftw64