path-util: rework file_in_same_dir() on top of path_extract_directory()

Let's port one more over.

Note that this changes behaviour of file_in_same_dir() in some regards.
Specifically, a trailing slash of the input path will be treated
differently: previously we'd operate below that dir then, instead of the
parent. I think that makes little sense however, and I think the code
using this function doesn't expect that either.

Moroever, addresses some corner cases if the path is specified as "/" or
".", i.e. where e cannot extract a parent. These will now be treated as
error, which I think is much cleaner.
This commit is contained in:
Lennart Poettering
2023-01-24 18:13:27 +01:00
parent 5b532e14e3
commit 162f6477c6
6 changed files with 93 additions and 86 deletions
+8 -12
View File
@@ -600,23 +600,19 @@ TEST(prefix_root) {
TEST(file_in_same_dir) {
char *t;
t = file_in_same_dir("/", "a");
assert_se(file_in_same_dir("/", "a", &t) == -EADDRNOTAVAIL);
assert_se(file_in_same_dir("/", "/a", &t) >= 0);
assert_se(streq(t, "/a"));
free(t);
t = file_in_same_dir("/", "/a");
assert_se(streq(t, "/a"));
assert_se(file_in_same_dir("", "a", &t) == -EINVAL);
assert_se(file_in_same_dir("a/", "x", &t) >= 0);
assert_se(streq(t, "x"));
free(t);
t = file_in_same_dir("", "a");
assert_se(streq(t, "a"));
free(t);
t = file_in_same_dir("a/", "a");
assert_se(streq(t, "a/a"));
free(t);
t = file_in_same_dir("bar/foo", "bar");
assert_se(file_in_same_dir("bar/foo", "bar", &t) >= 0);
assert_se(streq(t, "bar/bar"));
free(t);
}