1818***/
1919
2020#include <fcntl.h>
21+ #include <glob.h>
22+ #include <sys/stat.h>
2123#include <unistd.h>
2224
2325#include "alloc-util.h"
26+ #include "dirent-util.h"
2427#include "fileio.h"
28+ #include "fs-util.h"
2529#include "glob-util.h"
2630#include "macro.h"
31+ #include "rm-rf.h"
2732
2833static void test_glob_exists (void ) {
2934 char name [] = "/tmp/test-glob_exists.XXXXXX" ;
@@ -43,8 +48,69 @@ static void test_glob_exists(void) {
4348 assert_se (r == 0 );
4449}
4550
51+ static void test_glob_no_dot (void ) {
52+ char template [] = "/tmp/test-glob-util.XXXXXXX" ;
53+ const char * fn ;
54+
55+ _cleanup_globfree_ glob_t g = {
56+ .gl_closedir = (void (* )(void * )) closedir ,
57+ .gl_readdir = (struct dirent * (* )(void * )) readdir_no_dot ,
58+ .gl_opendir = (void * (* )(const char * )) opendir ,
59+ .gl_lstat = lstat ,
60+ .gl_stat = stat ,
61+ };
62+
63+ int r ;
64+
65+ assert_se (mkdtemp (template ));
66+
67+ fn = strjoina (template , "/*" );
68+ r = glob (fn , GLOB_NOSORT |GLOB_BRACE |GLOB_ALTDIRFUNC , NULL , & g );
69+ assert_se (r == GLOB_NOMATCH );
70+
71+ fn = strjoina (template , "/.*" );
72+ r = glob (fn , GLOB_NOSORT |GLOB_BRACE |GLOB_ALTDIRFUNC , NULL , & g );
73+ assert_se (r == GLOB_NOMATCH );
74+
75+ (void ) rm_rf (template , REMOVE_ROOT |REMOVE_PHYSICAL );
76+ }
77+
78+ static void test_safe_glob (void ) {
79+ char template [] = "/tmp/test-glob-util.XXXXXXX" ;
80+ const char * fn , * fn2 , * fname ;
81+
82+ _cleanup_globfree_ glob_t g = {};
83+ int r ;
84+
85+ assert_se (mkdtemp (template ));
86+
87+ fn = strjoina (template , "/*" );
88+ r = safe_glob (fn , 0 , & g );
89+ assert_se (r == - ENOENT );
90+
91+ fn2 = strjoina (template , "/.*" );
92+ r = safe_glob (fn2 , GLOB_NOSORT |GLOB_BRACE , & g );
93+ assert_se (r == - ENOENT );
94+
95+ fname = strjoina (template , "/.foobar" );
96+ assert_se (touch (fname ) == 0 );
97+
98+ r = safe_glob (fn , 0 , & g );
99+ assert_se (r == - ENOENT );
100+
101+ r = safe_glob (fn2 , GLOB_NOSORT |GLOB_BRACE , & g );
102+ assert_se (r == 0 );
103+ assert_se (g .gl_pathc == 1 );
104+ assert_se (streq (g .gl_pathv [0 ], fname ));
105+ assert_se (g .gl_pathv [1 ] == NULL );
106+
107+ (void ) rm_rf (template , REMOVE_ROOT |REMOVE_PHYSICAL );
108+ }
109+
46110int main (void ) {
47111 test_glob_exists ();
112+ test_glob_no_dot ();
113+ test_safe_glob ();
48114
49115 return 0 ;
50116}
0 commit comments