0007-don-t-fail-if-GLOB_BRACE-and-GLOB_ALTDIRFUNC-is-not-.patch 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. From 74c664bcd6b9a5fcf3466310c07f608d12456f7f Mon Sep 17 00:00:00 2001
  2. From: Chen Qi <Qi.Chen@windriver.com>
  3. Date: Mon, 25 Feb 2019 14:56:21 +0800
  4. Subject: [PATCH] don't fail if GLOB_BRACE and GLOB_ALTDIRFUNC is not defined
  5. If the standard library doesn't provide brace
  6. expansion users just won't get it.
  7. Dont use GNU GLOB extentions on non-glibc systems
  8. Conditionalize use of GLOB_ALTDIRFUNC
  9. Upstream-Status: Inappropriate [musl specific]
  10. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  11. Signed-off-by: Chen Qi <Qi.Chen@windriver.com>
  12. [rebased for systemd 243]
  13. Signed-off-by: Scott Murray <scott.murray@konsulko.com>
  14. ---
  15. src/basic/glob-util.c | 12 ++++++++++++
  16. src/test/test-glob-util.c | 16 ++++++++++++++++
  17. src/tmpfiles/tmpfiles.c | 10 ++++++++++
  18. 3 files changed, 38 insertions(+)
  19. --- a/src/basic/glob-util.c
  20. +++ b/src/basic/glob-util.c
  21. @@ -12,6 +12,12 @@
  22. #include "path-util.h"
  23. #include "strv.h"
  24. +/* Don't fail if the standard library
  25. + * doesn't provide brace expansion */
  26. +#ifndef GLOB_BRACE
  27. +#define GLOB_BRACE 0
  28. +#endif
  29. +
  30. static void closedir_wrapper(void* v) {
  31. (void) closedir(v);
  32. }
  33. @@ -19,6 +25,7 @@ static void closedir_wrapper(void* v) {
  34. int safe_glob(const char *path, int flags, glob_t *pglob) {
  35. int k;
  36. +#ifdef GLOB_ALTDIRFUNC
  37. /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */
  38. assert(!(flags & GLOB_ALTDIRFUNC));
  39. @@ -32,9 +39,14 @@ int safe_glob(const char *path, int flag
  40. pglob->gl_lstat = lstat;
  41. if (!pglob->gl_stat)
  42. pglob->gl_stat = stat;
  43. +#endif
  44. errno = 0;
  45. +#ifdef GLOB_ALTDIRFUNC
  46. k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob);
  47. +#else
  48. + k = glob(path, flags, NULL, pglob);
  49. +#endif
  50. if (k == GLOB_NOMATCH)
  51. return -ENOENT;
  52. if (k == GLOB_NOSPACE)
  53. --- a/src/test/test-glob-util.c
  54. +++ b/src/test/test-glob-util.c
  55. @@ -34,6 +34,12 @@ TEST(glob_first) {
  56. assert_se(first == NULL);
  57. }
  58. +/* Don't fail if the standard library
  59. + * doesn't provide brace expansion */
  60. +#ifndef GLOB_BRACE
  61. +#define GLOB_BRACE 0
  62. +#endif
  63. +
  64. TEST(glob_exists) {
  65. char name[] = "/tmp/test-glob_exists.XXXXXX";
  66. int fd = -1;
  67. @@ -61,11 +67,13 @@ TEST(glob_no_dot) {
  68. const char *fn;
  69. _cleanup_globfree_ glob_t g = {
  70. +#ifdef GLOB_ALTDIRFUNC
  71. .gl_closedir = closedir_wrapper,
  72. .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot,
  73. .gl_opendir = (void *(*)(const char *)) opendir,
  74. .gl_lstat = lstat,
  75. .gl_stat = stat,
  76. +#endif
  77. };
  78. int r;
  79. @@ -73,11 +81,19 @@ TEST(glob_no_dot) {
  80. assert_se(mkdtemp(template));
  81. fn = strjoina(template, "/*");
  82. +#ifdef GLOB_ALTDIRFUNC
  83. r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
  84. +#else
  85. + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
  86. +#endif
  87. assert_se(r == GLOB_NOMATCH);
  88. fn = strjoina(template, "/.*");
  89. +#ifdef GLOB_ALTDIRFUNC
  90. r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
  91. +#else
  92. + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g);
  93. +#endif
  94. assert_se(r == GLOB_NOMATCH);
  95. (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL);
  96. --- a/src/tmpfiles/tmpfiles.c
  97. +++ b/src/tmpfiles/tmpfiles.c
  98. @@ -71,6 +71,12 @@
  99. #include "user-util.h"
  100. #include "virt.h"
  101. +/* Don't fail if the standard library
  102. + * doesn't provide brace expansion */
  103. +#ifndef GLOB_BRACE
  104. +#define GLOB_BRACE 0
  105. +#endif
  106. +
  107. /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
  108. * them in the file system. This is intended to be used to create
  109. * properly owned directories beneath /tmp, /var/tmp, /run, which are
  110. @@ -2174,7 +2180,9 @@ finish:
  111. static int glob_item(Item *i, action_t action) {
  112. _cleanup_globfree_ glob_t g = {
  113. +#ifdef GLOB_ALTDIRFUNC
  114. .gl_opendir = (void *(*)(const char *)) opendir_nomod,
  115. +#endif
  116. };
  117. int r = 0, k;
  118. @@ -2194,7 +2202,9 @@ static int glob_item(Item *i, action_t a
  119. static int glob_item_recursively(Item *i, fdaction_t action) {
  120. _cleanup_globfree_ glob_t g = {
  121. +#ifdef GLOB_ALTDIRFUNC
  122. .gl_opendir = (void *(*)(const char *)) opendir_nomod,
  123. +#endif
  124. };
  125. int r = 0, k;