]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6a4f4a0f RC |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <fcntl.h> | |
48d7c648 ZJS |
22 | #include <glob.h> |
23 | #include <sys/stat.h> | |
6a4f4a0f RC |
24 | #include <unistd.h> |
25 | ||
26 | #include "alloc-util.h" | |
48d7c648 | 27 | #include "dirent-util.h" |
6a4f4a0f | 28 | #include "fileio.h" |
48d7c648 | 29 | #include "fs-util.h" |
6a4f4a0f RC |
30 | #include "glob-util.h" |
31 | #include "macro.h" | |
48d7c648 | 32 | #include "rm-rf.h" |
6a4f4a0f RC |
33 | |
34 | static void test_glob_exists(void) { | |
35 | char name[] = "/tmp/test-glob_exists.XXXXXX"; | |
36 | int fd = -1; | |
37 | int r; | |
38 | ||
646853bd | 39 | fd = mkostemp_safe(name); |
6a4f4a0f RC |
40 | assert_se(fd >= 0); |
41 | close(fd); | |
42 | ||
43 | r = glob_exists("/tmp/test-glob_exists*"); | |
44 | assert_se(r == 1); | |
45 | ||
46 | r = unlink(name); | |
47 | assert_se(r == 0); | |
48 | r = glob_exists("/tmp/test-glob_exists*"); | |
49 | assert_se(r == 0); | |
50 | } | |
51 | ||
48d7c648 ZJS |
52 | static void test_glob_no_dot(void) { |
53 | char template[] = "/tmp/test-glob-util.XXXXXXX"; | |
54 | const char *fn; | |
55 | ||
56 | _cleanup_globfree_ glob_t g = { | |
57 | .gl_closedir = (void (*)(void *)) closedir, | |
58 | .gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot, | |
59 | .gl_opendir = (void *(*)(const char *)) opendir, | |
60 | .gl_lstat = lstat, | |
61 | .gl_stat = stat, | |
62 | }; | |
63 | ||
64 | int r; | |
65 | ||
66 | assert_se(mkdtemp(template)); | |
67 | ||
68 | fn = strjoina(template, "/*"); | |
69 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); | |
70 | assert_se(r == GLOB_NOMATCH); | |
71 | ||
72 | fn = strjoina(template, "/.*"); | |
73 | r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g); | |
74 | assert_se(r == GLOB_NOMATCH); | |
75 | ||
76 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); | |
77 | } | |
78 | ||
79 | static void test_safe_glob(void) { | |
80 | char template[] = "/tmp/test-glob-util.XXXXXXX"; | |
81 | const char *fn, *fn2, *fname; | |
82 | ||
83 | _cleanup_globfree_ glob_t g = {}; | |
84 | int r; | |
85 | ||
86 | assert_se(mkdtemp(template)); | |
87 | ||
88 | fn = strjoina(template, "/*"); | |
89 | r = safe_glob(fn, 0, &g); | |
90 | assert_se(r == -ENOENT); | |
91 | ||
92 | fn2 = strjoina(template, "/.*"); | |
93 | r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g); | |
94 | assert_se(r == -ENOENT); | |
95 | ||
96 | fname = strjoina(template, "/.foobar"); | |
97 | assert_se(touch(fname) == 0); | |
98 | ||
99 | r = safe_glob(fn, 0, &g); | |
100 | assert_se(r == -ENOENT); | |
101 | ||
102 | r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g); | |
103 | assert_se(r == 0); | |
104 | assert_se(g.gl_pathc == 1); | |
105 | assert_se(streq(g.gl_pathv[0], fname)); | |
106 | assert_se(g.gl_pathv[1] == NULL); | |
107 | ||
108 | (void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL); | |
109 | } | |
110 | ||
6a4f4a0f RC |
111 | int main(void) { |
112 | test_glob_exists(); | |
48d7c648 ZJS |
113 | test_glob_no_dot(); |
114 | test_safe_glob(); | |
6a4f4a0f RC |
115 | |
116 | return 0; | |
117 | } |