Merge pull request #5809 from keszybz/glob-safe

Implement `safe_glob` that ignores "." and ".."
This commit is contained in:
Martin Pitt
2017-04-29 21:19:24 +02:00
committed by GitHub
9 changed files with 175 additions and 59 deletions
+66
View File
@@ -18,12 +18,17 @@
***/
#include <fcntl.h>
#include <glob.h>
#include <sys/stat.h>
#include <unistd.h>
#include "alloc-util.h"
#include "dirent-util.h"
#include "fileio.h"
#include "fs-util.h"
#include "glob-util.h"
#include "macro.h"
#include "rm-rf.h"
static void test_glob_exists(void) {
char name[] = "/tmp/test-glob_exists.XXXXXX";
@@ -43,8 +48,69 @@ static void test_glob_exists(void) {
assert_se(r == 0);
}
static void test_glob_no_dot(void) {
char template[] = "/tmp/test-glob-util.XXXXXXX";
const char *fn;
_cleanup_globfree_ glob_t g = {
.gl_closedir = (void (*)(void *)) closedir,
.gl_readdir = (struct dirent *(*)(void *)) readdir_no_dot,
.gl_opendir = (void *(*)(const char *)) opendir,
.gl_lstat = lstat,
.gl_stat = stat,
};
int r;
assert_se(mkdtemp(template));
fn = strjoina(template, "/*");
r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
assert_se(r == GLOB_NOMATCH);
fn = strjoina(template, "/.*");
r = glob(fn, GLOB_NOSORT|GLOB_BRACE|GLOB_ALTDIRFUNC, NULL, &g);
assert_se(r == GLOB_NOMATCH);
(void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL);
}
static void test_safe_glob(void) {
char template[] = "/tmp/test-glob-util.XXXXXXX";
const char *fn, *fn2, *fname;
_cleanup_globfree_ glob_t g = {};
int r;
assert_se(mkdtemp(template));
fn = strjoina(template, "/*");
r = safe_glob(fn, 0, &g);
assert_se(r == -ENOENT);
fn2 = strjoina(template, "/.*");
r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g);
assert_se(r == -ENOENT);
fname = strjoina(template, "/.foobar");
assert_se(touch(fname) == 0);
r = safe_glob(fn, 0, &g);
assert_se(r == -ENOENT);
r = safe_glob(fn2, GLOB_NOSORT|GLOB_BRACE, &g);
assert_se(r == 0);
assert_se(g.gl_pathc == 1);
assert_se(streq(g.gl_pathv[0], fname));
assert_se(g.gl_pathv[1] == NULL);
(void) rm_rf(template, REMOVE_ROOT|REMOVE_PHYSICAL);
}
int main(void) {
test_glob_exists();
test_glob_no_dot();
test_safe_glob();
return 0;
}
+34
View File
@@ -27,6 +27,7 @@
#include "mount-util.h"
#include "path-util.h"
#include "rm-rf.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "util.h"
@@ -104,6 +105,38 @@ static void test_path(void) {
assert_se(!path_equal_ptr(NULL, "/a"));
}
static void test_path_equal_root(void) {
/* Nail down the details of how path_equal("/", ...) works. */
assert_se(path_equal("/", "/"));
assert_se(path_equal("/", "//"));
assert_se(!path_equal("/", "/./"));
assert_se(!path_equal("/", "/../"));
assert_se(!path_equal("/", "/.../"));
/* Make sure that files_same works as expected. */
assert_se(files_same("/", "/") > 0);
assert_se(files_same("/", "//") > 0);
assert_se(files_same("/", "/./") > 0);
assert_se(files_same("/", "/../") > 0);
assert_se(files_same("/", "/.../") == -ENOENT);
/* The same for path_equal_or_files_same. */
assert_se(path_equal_or_files_same("/", "/"));
assert_se(path_equal_or_files_same("/", "//"));
assert_se(path_equal_or_files_same("/", "/./"));
assert_se(path_equal_or_files_same("/", "/../"));
assert_se(!path_equal_or_files_same("/", "/.../"));
}
static void test_find_binary(const char *self) {
char *p;
@@ -551,6 +584,7 @@ int main(int argc, char **argv) {
log_open();
test_path();
test_path_equal_root();
test_find_binary(argv[0]);
test_prefixes();
test_path_join();