diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-04-17 05:04:27 +0900 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-04-19 03:28:34 +0900 |
commit | c0552b359c5b5dc0c130b831ce0067af99149f27 (patch) | |
tree | 2b1e0fdbdba4f22a769af2fb53a824a020a7a435 /src/test | |
parent | d81fc152547769854835f471c97db8e7d066373e (diff) | |
download | systemd-c0552b359c5b5dc0c130b831ce0067af99149f27.tar.gz |
chase: make chaseat() provides absolute path also when dir_fd points to the root directory
Usually, we pass the file descriptor of the root directory to chaseat()
when `--root=` is not specified. Previously, even in such case, the
result was relative, and we need to prefix the path with "/" when we
want to pass the path to other functions that do not support dir_fd, or
log or show the path. That's inconvenient.
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-chase.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/test/test-chase.c b/src/test/test-chase.c index 1e98f5c6ed..52ea21a54c 100644 --- a/src/test/test-chase.c +++ b/src/test/test-chase.c @@ -442,6 +442,34 @@ TEST(chaseat) { assert_se(streq(result, "/usr")); result = mfree(result); + /* If the file descriptor points to the root directory, the result will be absolute. */ + + fd = open("/", O_CLOEXEC | O_DIRECTORY | O_PATH); + assert_se(fd >= 0); + + assert_se(chaseat(fd, p, 0, &result, NULL) >= 0); + assert_se(streq(result, "/usr")); + result = mfree(result); + + assert_se(chaseat(fd, p, CHASE_AT_RESOLVE_IN_ROOT, &result, NULL) >= 0); + assert_se(streq(result, "/usr")); + result = mfree(result); + + fd = safe_close(fd); + + /* If the file descriptor does not point to the root directory, the result will be relative. */ + + assert_se(chaseat(tfd, "abc", CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL) == -ENOENT); + assert_se(chaseat(tfd, "/abc", CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL) == -ENOENT); + + assert_se(chaseat(tfd, "abc", CHASE_AT_RESOLVE_IN_ROOT | CHASE_NONEXISTENT, &result, NULL) >= 0); + assert_se(streq(result, "usr")); + result = mfree(result); + + assert_se(chaseat(tfd, "/abc", CHASE_AT_RESOLVE_IN_ROOT | CHASE_NONEXISTENT, &result, NULL) >= 0); + assert_se(streq(result, "usr")); + result = mfree(result); + /* Test that absolute path or not are the same when resolving relative to a directory file * descriptor and that we always get a relative path back. */ |