util-lib: use trailing slash in chase_symlinks, fd_is_mount_point, path_is_mount_point

The kernel will reply with -ENOTDIR when we try to access a non-directory under
a name which ends with a slash. But our functions would strip the trailing slash
under various circumstances. Keep the trailing slash, so that

path_is_mount_point("/path/to/file/") return -ENOTDIR when /path/to/file/ is a file.

Tests are added for this change in behaviour.

Also, when called with a trailing slash, path_is_mount_point() would get
"" from basename(), and call name_to_handle_at(3, "", ...), and always
return -ENOENT. Now it'll return -ENOTDIR if the mount point is a file, and
true if it is a directory and a mount point.

v2:
- use strip_trailing_chars()

v3:
- instead of stripping trailing chars(), do the opposite — preserve them.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2017-10-31 11:08:30 +01:00
parent ca4d708dc4
commit b12d25a8d6
7 changed files with 115 additions and 18 deletions

View file

@ -399,6 +399,20 @@ static void test_file_in_same_dir(void) {
free(t);
}
static void test_last_path_component(void) {
assert_se(streq(last_path_component("a/b/c"), "c"));
assert_se(streq(last_path_component("a/b/c/"), "c/"));
assert_se(streq(last_path_component("/"), "/"));
assert_se(streq(last_path_component("//"), "/"));
assert_se(streq(last_path_component("///"), "/"));
assert_se(streq(last_path_component("."), "."));
assert_se(streq(last_path_component("./."), "."));
assert_se(streq(last_path_component("././"), "./"));
assert_se(streq(last_path_component("././/"), ".//"));
assert_se(streq(last_path_component("/foo/a"), "a"));
assert_se(streq(last_path_component("/foo/a/"), "a/"));
}
static void test_filename_is_valid(void) {
char foo[FILENAME_MAX+2];
int i;
@ -484,6 +498,7 @@ int main(int argc, char **argv) {
test_path_startswith();
test_prefix_root();
test_file_in_same_dir();
test_last_path_component();
test_filename_is_valid();
test_hidden_or_backup_file();
test_skip_dev_prefix();