Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1 | From f8a239b182158ca0a537ba053cb0e6bad9c3a2fb 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 07/24] 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 | --- |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 19 | src/basic/glob-util.c | 13 ++++++++++++- |
| 20 | src/test/test-glob-util.c | 16 ++++++++++++++++ |
| 21 | src/tmpfiles/tmpfiles.c | 10 ++++++++++ |
| 22 | 3 files changed, 38 insertions(+), 1 deletion(-) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 23 | |
| 24 | diff --git a/src/basic/glob-util.c b/src/basic/glob-util.c |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 25 | index 32c53f8..ae358d9 100644 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 26 | --- a/src/basic/glob-util.c |
| 27 | +++ b/src/basic/glob-util.c |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 28 | @@ -13,6 +13,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 29 | #include "path-util.h" |
| 30 | #include "strv.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 31 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 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 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 37 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 38 | static void closedir_wrapper(void* v) { |
| 39 | (void) closedir(v); |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 40 | } |
| 41 | @@ -20,6 +26,7 @@ static void closedir_wrapper(void* v) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 42 | int safe_glob(const char *path, int flags, glob_t *pglob) { |
| 43 | int k; |
| 44 | |
| 45 | +#ifdef GLOB_ALTDIRFUNC |
| 46 | /* We want to set GLOB_ALTDIRFUNC ourselves, don't allow it to be set. */ |
| 47 | assert(!(flags & GLOB_ALTDIRFUNC)); |
| 48 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 49 | @@ -33,10 +40,14 @@ int safe_glob(const char *path, int flags, glob_t *pglob) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 50 | pglob->gl_lstat = lstat; |
| 51 | if (!pglob->gl_stat) |
| 52 | pglob->gl_stat = stat; |
| 53 | +#endif |
| 54 | |
| 55 | errno = 0; |
| 56 | +#ifdef GLOB_ALTDIRFUNC |
| 57 | k = glob(path, flags | GLOB_ALTDIRFUNC, NULL, pglob); |
| 58 | - |
| 59 | +#else |
| 60 | + k = glob(path, flags, NULL, pglob); |
| 61 | +#endif |
| 62 | if (k == GLOB_NOMATCH) |
| 63 | return -ENOENT; |
| 64 | if (k == GLOB_NOSPACE) |
| 65 | diff --git a/src/test/test-glob-util.c b/src/test/test-glob-util.c |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 66 | index b4f4144..f0d474e 100644 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 67 | --- a/src/test/test-glob-util.c |
| 68 | +++ b/src/test/test-glob-util.c |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 69 | @@ -13,6 +13,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 70 | #include "rm-rf.h" |
| 71 | #include "tmpfile-util.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 72 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 73 | +/* Don't fail if the standard library |
| 74 | + * doesn't provide brace expansion */ |
| 75 | +#ifndef GLOB_BRACE |
| 76 | +#define GLOB_BRACE 0 |
| 77 | +#endif |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 78 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 79 | static void test_glob_exists(void) { |
| 80 | char name[] = "/tmp/test-glob_exists.XXXXXX"; |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 81 | int fd = -1; |
| 82 | @@ -40,11 +46,13 @@ static void test_glob_no_dot(void) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 83 | const char *fn; |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 84 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 85 | _cleanup_globfree_ glob_t g = { |
| 86 | +#ifdef GLOB_ALTDIRFUNC |
| 87 | .gl_closedir = closedir_wrapper, |
| 88 | .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot, |
| 89 | .gl_opendir = (void *(*)(const char *)) opendir, |
| 90 | .gl_lstat = lstat, |
| 91 | .gl_stat = stat, |
| 92 | +#endif |
| 93 | }; |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 94 | |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 95 | int r; |
| 96 | @@ -52,11 +60,19 @@ static void test_glob_no_dot(void) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 97 | assert_se(mkdtemp(template)); |
| 98 | |
| 99 | fn = strjoina(template, "/*"); |
| 100 | +#ifdef GLOB_ALTDIRFUNC |
| 101 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 102 | +#else |
| 103 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 104 | +#endif |
| 105 | assert_se(r == GLOB_NOMATCH); |
| 106 | |
| 107 | fn = strjoina(template, "/.*"); |
| 108 | +#ifdef GLOB_ALTDIRFUNC |
| 109 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); |
| 110 | +#else |
| 111 | + r = glob(fn, GLOB_NOSORT|GLOB_BRACE, NULL, &g); |
| 112 | +#endif |
| 113 | assert_se(r == GLOB_NOMATCH); |
| 114 | |
| 115 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); |
| 116 | diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 117 | index d9d1cc1..477d1e3 100644 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 118 | --- a/src/tmpfiles/tmpfiles.c |
| 119 | +++ b/src/tmpfiles/tmpfiles.c |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 120 | @@ -63,6 +63,12 @@ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 121 | #include "umask-util.h" |
| 122 | #include "user-util.h" |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 123 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 124 | +/* Don't fail if the standard library |
| 125 | + * doesn't provide brace expansion */ |
| 126 | +#ifndef GLOB_BRACE |
| 127 | +#define GLOB_BRACE 0 |
| 128 | +#endif |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 129 | + |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 130 | /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates |
| 131 | * them in the file system. This is intended to be used to create |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 132 | * properly owned directories beneath /tmp, /var/tmp, /run, which are |
| 133 | @@ -1839,7 +1845,9 @@ finish: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 134 | |
| 135 | static int glob_item(Item *i, action_t action) { |
| 136 | _cleanup_globfree_ glob_t g = { |
| 137 | +#ifdef GLOB_ALTDIRFUNC |
| 138 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 139 | +#endif |
| 140 | }; |
| 141 | int r = 0, k; |
| 142 | char **fn; |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 143 | @@ -1859,7 +1867,9 @@ static int glob_item(Item *i, action_t action) { |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 144 | |
| 145 | static int glob_item_recursively(Item *i, fdaction_t action) { |
| 146 | _cleanup_globfree_ glob_t g = { |
| 147 | +#ifdef GLOB_ALTDIRFUNC |
| 148 | .gl_opendir = (void *(*)(const char *)) opendir_nomod, |
| 149 | +#endif |
| 150 | }; |
| 151 | int r = 0, k; |
| 152 | char **fn; |
| 153 | -- |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 154 | 2.11.0 |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 155 | |