mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-10 01:17:44 +03:00
Merge pull request #20618 from yuwata/path-find-component
use path_find_{first,last}_component() at more several places
This commit is contained in:
commit
dd4c15296c
@ -45,50 +45,39 @@ int unlink_noerrno(const char *path) {
|
||||
}
|
||||
|
||||
int rmdir_parents(const char *path, const char *stop) {
|
||||
size_t l;
|
||||
int r = 0;
|
||||
char *p;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(stop);
|
||||
|
||||
l = strlen(path);
|
||||
if (!path_is_safe(path))
|
||||
return -EINVAL;
|
||||
|
||||
/* Skip trailing slashes */
|
||||
while (l > 0 && path[l-1] == '/')
|
||||
l--;
|
||||
if (!path_is_safe(stop))
|
||||
return -EINVAL;
|
||||
|
||||
while (l > 0) {
|
||||
char *t;
|
||||
p = strdupa(path);
|
||||
|
||||
/* Skip last component */
|
||||
while (l > 0 && path[l-1] != '/')
|
||||
l--;
|
||||
for (;;) {
|
||||
char *slash = NULL;
|
||||
|
||||
/* Skip trailing slashes */
|
||||
while (l > 0 && path[l-1] == '/')
|
||||
l--;
|
||||
|
||||
if (l <= 0)
|
||||
break;
|
||||
|
||||
t = strndup(path, l);
|
||||
if (!t)
|
||||
return -ENOMEM;
|
||||
|
||||
if (path_startswith(stop, t)) {
|
||||
free(t);
|
||||
/* skip the last component. */
|
||||
r = path_find_last_component(p, /* accept_dot_dot= */ false, (const char **) &slash, NULL);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
if (slash == p)
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = rmdir(t);
|
||||
free(t);
|
||||
assert(*slash == '/');
|
||||
*slash = '\0';
|
||||
|
||||
if (r < 0)
|
||||
if (errno != ENOENT)
|
||||
return -errno;
|
||||
if (path_startswith_full(stop, p, /* accept_dot_dot= */ false))
|
||||
return 0;
|
||||
|
||||
if (rmdir(p) < 0 && errno != ENOENT)
|
||||
return -errno;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char *newpath) {
|
||||
|
@ -95,57 +95,65 @@ int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags f
|
||||
}
|
||||
|
||||
int mkdir_parents_internal(const char *prefix, const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags, mkdir_func_t _mkdir) {
|
||||
const char *p, *e;
|
||||
const char *p, *e = NULL;
|
||||
int r;
|
||||
|
||||
assert(path);
|
||||
assert(_mkdir != mkdir);
|
||||
|
||||
if (prefix && !path_startswith(path, prefix))
|
||||
if (prefix) {
|
||||
p = path_startswith_full(path, prefix, /* accept_dot_dot= */ false);
|
||||
if (!p)
|
||||
return -ENOTDIR;
|
||||
} else
|
||||
p = path;
|
||||
|
||||
if (isempty(p))
|
||||
return 0;
|
||||
|
||||
if (!path_is_safe(p))
|
||||
return -ENOTDIR;
|
||||
|
||||
/* return immediately if directory exists */
|
||||
e = strrchr(path, '/');
|
||||
if (!e)
|
||||
r = path_find_last_component(p, /* accept_dot_dot= */ false, &e, NULL);
|
||||
if (r <= 0) /* r == 0 means path is equivalent to prefix. */
|
||||
return r;
|
||||
if (e == p)
|
||||
return 0;
|
||||
|
||||
if (e == path)
|
||||
return 0;
|
||||
assert(e > p);
|
||||
assert(*e == '/');
|
||||
|
||||
p = strndupa(path, e - path);
|
||||
r = is_dir(p, true);
|
||||
/* drop the last component */
|
||||
path = strndupa(path, e - path);
|
||||
r = is_dir(path, true);
|
||||
if (r > 0)
|
||||
return 0;
|
||||
if (r == 0)
|
||||
return -ENOTDIR;
|
||||
|
||||
/* create every parent directory in the path, except the last component */
|
||||
p = path + strspn(path, "/");
|
||||
for (;;) {
|
||||
char t[strlen(path) + 1];
|
||||
for (p = path;;) {
|
||||
char *s;
|
||||
int n;
|
||||
|
||||
e = p + strcspn(p, "/");
|
||||
p = e + strspn(e, "/");
|
||||
n = path_find_first_component(&p, /* accept_dot_dot= */ false, (const char **) &s);
|
||||
if (n <= 0)
|
||||
return n;
|
||||
|
||||
/* Is this the last component? If so, then we're done */
|
||||
if (*p == 0)
|
||||
return 0;
|
||||
assert(p);
|
||||
assert(s >= path);
|
||||
assert(IN_SET(s[n], '/', '\0'));
|
||||
|
||||
memcpy(t, path, e - path);
|
||||
t[e-path] = 0;
|
||||
s[n] = '\0';
|
||||
|
||||
if (prefix && path_startswith(prefix, t))
|
||||
continue;
|
||||
|
||||
if (!uid_is_valid(uid) && !gid_is_valid(gid) && flags == 0) {
|
||||
r = _mkdir(t, mode);
|
||||
if (r < 0 && r != -EEXIST)
|
||||
return r;
|
||||
} else {
|
||||
r = mkdir_safe_internal(t, mode, uid, gid, flags, _mkdir);
|
||||
if (!prefix || !path_startswith_full(prefix, path, /* accept_dot_dot= */ false)) {
|
||||
r = mkdir_safe_internal(path, mode, uid, gid, flags, _mkdir);
|
||||
if (r < 0 && r != -EEXIST)
|
||||
return r;
|
||||
}
|
||||
|
||||
s[n] = *p == '\0' ? '\0' : '/';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -865,6 +865,60 @@ static void test_conservative_rename(void) {
|
||||
assert_se(access(q, F_OK) < 0 && errno == ENOENT);
|
||||
}
|
||||
|
||||
static void test_rmdir_parents_one(
|
||||
const char *prefix,
|
||||
const char *path,
|
||||
const char *stop,
|
||||
int expected,
|
||||
const char *test_exist,
|
||||
const char *test_nonexist_subdir) {
|
||||
|
||||
const char *p, *s;
|
||||
|
||||
log_debug("/* %s(%s, %s) */", __func__, path, stop);
|
||||
|
||||
p = strjoina(prefix, path);
|
||||
s = strjoina(prefix, stop);
|
||||
|
||||
if (expected >= 0)
|
||||
assert_se(mkdir_parents(p, 0700) >= 0);
|
||||
|
||||
assert_se(rmdir_parents(p, s) == expected);
|
||||
|
||||
if (expected >= 0) {
|
||||
const char *e, *f;
|
||||
|
||||
e = strjoina(prefix, test_exist);
|
||||
f = strjoina(e, test_nonexist_subdir);
|
||||
|
||||
assert_se(access(e, F_OK) >= 0);
|
||||
assert_se(access(f, F_OK) < 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_rmdir_parents(void) {
|
||||
char *temp;
|
||||
|
||||
log_info("/* %s */", __func__);
|
||||
|
||||
temp = strjoina(arg_test_dir ?: "/tmp", "/test-rmdir.XXXXXX");
|
||||
assert_se(mkdtemp(temp));
|
||||
|
||||
test_rmdir_parents_one(temp, "/aaa/../hoge/foo", "/hoge/foo", -EINVAL, NULL, NULL);
|
||||
test_rmdir_parents_one(temp, "/aaa/bbb/ccc", "/hoge/../aaa", -EINVAL, NULL, NULL);
|
||||
|
||||
test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc/ddd", 0, "/aaa/bbb/ccc/ddd", "/eee");
|
||||
test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb/ccc", 0, "/aaa/bbb/ccc", "/ddd");
|
||||
test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/bbb", 0, "/aaa/bbb", "/ccc");
|
||||
test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa", 0, "/aaa", "/bbb");
|
||||
test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/", 0, "/", "/aaa");
|
||||
|
||||
test_rmdir_parents_one(temp, "/aaa/bbb/ccc/ddd/eee", "/aaa/hoge/foo", 0, "/aaa", "/bbb");
|
||||
test_rmdir_parents_one(temp, "/aaa////bbb/.//ccc//ddd/eee///./.", "///././aaa/.", 0, "/aaa", "/bbb");
|
||||
|
||||
assert_se(rm_rf(temp, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
test_setup_logging(LOG_INFO);
|
||||
|
||||
@ -883,6 +937,7 @@ int main(int argc, char *argv[]) {
|
||||
test_rename_noreplace();
|
||||
test_chmod_and_chown();
|
||||
test_conservative_rename();
|
||||
test_rmdir_parents();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ static int run(int argc, char *argv[]) {
|
||||
} else {
|
||||
if (unlink(devname) < 0)
|
||||
return log_error_errno(errno, "unlink('%s') failed: %m", devname);
|
||||
(void) rmdir_parents(devname, "/");
|
||||
(void) rmdir_parents(devname, "/dev");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user