Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame^] | 1 | From 848e711d719a6d987bc7d14183e1c7b1f1c91c56 Mon Sep 17 00:00:00 2001 |
| 2 | From: Chen Qi <Qi.Chen@windriver.com> |
| 3 | Date: Fri, 29 Jun 2018 17:10:29 +0800 |
| 4 | Subject: [PATCH 08/19] don't fail if GLOB_BRACE and GLOB_ALTDIRFUNC is not |
| 5 | defined |
| 6 | |
| 7 | If the standard library doesn't provide brace |
| 8 | expansion users just won't get it. |
| 9 | |
| 10 | Dont use GNU GLOB extentions on non-glibc systems |
| 11 | |
| 12 | Conditionalize use of GLOB_ALTDIRFUNC |
| 13 | |
| 14 | Upstream-Status: Inappropriate [musl specific] |
| 15 | |
| 16 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 17 | Signed-off-by: Chen Qi <Qi.Chen@windriver.com> |
| 18 | --- |
| 19 | src/basic/glob-util.c | 13 +++++++++++-- |
| 20 | src/test/test-glob-util.c | 15 +++++++++++++++ |
| 21 | src/tmpfiles/tmpfiles.c | 10 ++++++++++ |
| 22 | 3 files changed, 36 insertions(+), 2 deletions(-) |
| 23 | |
| 24 | diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c |
| 25 | index 9fac676f2..8adb9559e 100644 |
| 26 | --- a/src/basic/glob-util.c |
| 27 | +++ b/src/basic/glob-util.c |
| 28 | @@ -10,6 +10,11 @@ |
| 29 | #include "macro.h" |
| 30 | #include "path-util.h" |
| 31 | #include "strv.h" |
| 32 | +/* Don't fail if the standard library |
| 33 | + * doesn't provide brace expansion */ |
| 34 | +#ifndef GLOB_BRACE |
| 35 | +#define GLOB_BRACE 0 |
| 36 | +#endif |
| 37 | |
| 38 | static void closedir_wrapper(void* v) { |
| 39 | (void) closedir(v); |
| 40 | @@ -18,6 +23,7 @@ static void closedir_wrapper(void* v) { |
| 41 | int safe_glob(const char *path, int flags, glob_t *pglob) { |
| 42 | int k; |
| 43 | |
| 44 | +#ifdef GLOB_ALTDIRFUNC |
| 45 | /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */ |
| 46 | assert(!(flags & GLOB_ALTDIRFUNC)); |
| 47 | |
| 48 | @@ -31,10 +37,13 @@ int safe_glob(const char *path, int flags, glob_t *pglob) { |
| 49 | pglob->gl_lstat = lstat; |
| 50 | if (!pglob->gl_stat) |
| 51 | pglob->gl_stat = stat; |
| 52 | - |
| 53 | +#endif |
| 54 | errno = 0; |
| 55 | +#ifdef GLOB_ALTDIRFUNC |
| 56 | k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob); |
| 57 | - |
| 58 | +#else |
| 59 | + k = glob(path, flags, NULL, pglob); |
| 60 | +#endif |
| 61 | if (k == GLOB_NOMATCH) |
| 62 | return -ENOENT; |
| 63 | if (k == GLOB_NOSPACE) |
| 64 | diff --git a/src/test/test-glob-util.c b/src/test/test-glob-util.c |
| 65 | index d78d6223c..f5943b26d 100644 |
| 66 | --- a/src/test/test-glob-util.c |
| 67 | +++ b/src/test/test-glob-util.c |
| 68 | @@ -12,6 +12,11 @@ |
| 69 | #include "glob-util.h" |
| 70 | #include "macro.h" |
| 71 | #include "rm-rf.h" |
| 72 | +/* Don't fail if the standard library |
| 73 | + * doesn't provide brace expansion */ |
| 74 | +#ifndef GLOB_BRACE |
| 75 | +#define GLOB_BRACE 0 |
| 76 | +#endif |
| 77 | |
| 78 | static void test_glob_exists(void) { |
| 79 | char name[] = "/tmp/test-glob_exists.XXXXXX"; |
| 80 | @@ -40,11 +45,13 @@ static void test_glob_no_dot(void) { |
| 81 | const char *fn; |
| 82 | |
| 83 | _cleanup_globfree_ glob_t g = { |
| 84 | +#ifdef GLOB_ALTDIRFUNC |
| 85 | .gl_closedir = closedir_wrapper, |
| 86 | .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot, |
| 87 | .gl_opendir = (void *(*)(const char *)) opendir, |
| 88 | .gl_lstat = lstat, |
| 89 | .gl_stat = stat, |
| 90 | +#endif |
| 91 | }; |
| 92 | |
| 93 | int r; |
| 94 | @@ -52,11 +59,19 @@ static void test_glob_no_dot(void) { |
| 95 | assert_se(mkdtemp(template)); |
| 96 | |
| 97 | fn = strjoina(template, "/*"); |
| 98 | +#ifdef GLOB_ALTDIRFUNC |
| 99 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 100 | +#else |
| 101 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 102 | +#endif |
| 103 | assert_se(r == GLOB_NOMATCH); |
| 104 | |
| 105 | fn = strjoina(template, "/.*"); |
| 106 | +#ifdef GLOB_ALTDIRFUNC |
| 107 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 108 | +#else |
| 109 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 110 | +#endif |
| 111 | assert_se(r == GLOB_NOMATCH); |
| 112 | |
| 113 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 114 | diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c |
| 115 | index 927de35f3..5f0283da5 100644 |
| 116 | --- a/src/tmpfiles/tmpfiles.c |
| 117 | +++ b/src/tmpfiles/tmpfiles.c |
| 118 | @@ -60,6 +60,12 @@ |
| 119 | #include "user-util.h" |
| 120 | #include "util.h" |
| 121 | |
| 122 | +/* Don't fail if the standard library |
| 123 | + * doesn't provide brace expansion */ |
| 124 | +#ifndef GLOB_BRACE |
| 125 | +#define GLOB_BRACE 0 |
| 126 | +#endif |
| 127 | + |
| 128 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates |
| 129 | * them in the file system. This is intended to be used to create |
| 130 | * properly owned directories beneath /tmp, /var/tmp, /run, which are |
| 131 | @@ -1345,7 +1351,9 @@ finish: |
| 132 | |
| 133 | static int glob_item(Item *i, action_t action) { |
| 134 | _cleanup_globfree_ glob_t g = { |
| 135 | +#ifdef GLOB_ALTDIRFUNC |
| 136 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 137 | +#endif |
| 138 | }; |
| 139 | int r = 0, k; |
| 140 | char **fn; |
| 141 | @@ -1365,7 +1373,9 @@ static int glob_item(Item *i, action_t action) { |
| 142 | |
| 143 | static int glob_item_recursively(Item *i, fdaction_t action) { |
| 144 | _cleanup_globfree_ glob_t g = { |
| 145 | +#ifdef GLOB_ALTDIRFUNC |
| 146 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 147 | +#endif |
| 148 | }; |
| 149 | int r = 0, k; |
| 150 | char **fn; |
| 151 | -- |
| 152 | 2.11.0 |
| 153 | |