From 3df6745c2b830ea35f6363c2ea78476f1bc9e899 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 12 Apr 2015 18:45:08 -0400 Subject: path-util.c: changes for rm-rf: never cross mount points Signed-off-by: Anthony G. Basile --- src/shared/path-util.c | 55 ++++++++++++++++++++++++++------------------------ src/shared/path-util.h | 1 + 2 files changed, 30 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/shared/path-util.c b/src/shared/path-util.c index 26703836ad..e478f6f43e 100644 --- a/src/shared/path-util.c +++ b/src/shared/path-util.c @@ -330,30 +330,17 @@ bool path_equal(const char *a, const char *b) { return path_compare(a, b) == 0; } -int path_is_mount_point(const char *t, bool allow_symlink) { - +int fd_is_mount_point(int fd) { union file_handle_union h = FILE_HANDLE_INIT; int mount_id = -1, mount_id_parent = -1; + bool nosupp = false; struct stat a, b; int r; - _cleanup_close_ int fd = -1; - bool nosupp = false; /* We are not actually interested in the file handles, but * name_to_handle_at() also passes us the mount ID, hence use * it but throw the handle away */ - if (path_equal(t, "/")) - return 1; - - fd = openat(AT_FDCWD, t, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|(allow_symlink ? 0 : O_PATH)); - if (fd < 0) { - if (errno == ENOENT) - return 0; - - return -errno; - } - r = name_to_handle_at(fd, "", &h.handle, &mount_id, AT_EMPTY_PATH); if (r < 0) { if (errno == ENOSYS) @@ -362,7 +349,9 @@ int path_is_mount_point(const char *t, bool allow_symlink) { goto fallback; else if (errno == EOPNOTSUPP) /* This kernel or file system does not support - * name_to_handle_at(), hence fallback to the + * name_to_handle_at(), hence let's see if the + * upper fs supports it (in which case it is a + * mount point), otherwise fallback to the * traditional stat() logic */ nosupp = true; else if (errno == ENOENT) @@ -371,29 +360,26 @@ int path_is_mount_point(const char *t, bool allow_symlink) { return -errno; } - h.handle.handle_bytes = MAX_HANDLE_SZ; r = name_to_handle_at(fd, "..", &h.handle, &mount_id_parent, 0); - if (r < 0) - if (errno == EOPNOTSUPP) + if (r < 0) { + if (errno == EOPNOTSUPP) { if (nosupp) /* Neither parent nor child do name_to_handle_at()? We have no choice but to fall back. */ goto fallback; else - /* The parent can't do name_to_handle_at() but - * the directory we are interested in can? - * Or the other way around? + /* The parent can't do name_to_handle_at() but the + * directory we are interested in can? * If so, it must be a mount point. */ return 1; - else + } else return -errno; - else + } else return mount_id != mount_id_parent; fallback: r = fstatat(fd, "", &a, AT_EMPTY_PATH); - if (r < 0) { if (errno == ENOENT) return 0; @@ -401,7 +387,6 @@ fallback: return -errno; } - r = fstatat(fd, "..", &b, 0); if (r < 0) return -errno; @@ -409,6 +394,24 @@ fallback: return a.st_dev != b.st_dev; } +int path_is_mount_point(const char *t, bool allow_symlink) { + _cleanup_close_ int fd = -1; + assert(t); + + if (path_equal(t, "/")) + return 1; + + fd = openat(AT_FDCWD, t, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|(allow_symlink ? 0 : O_PATH)); + if (fd < 0) { + if (errno == ENOENT) + return 0; + + return -errno; + } + + return fd_is_mount_point(fd); +} + bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) { bool changed = false; const char* const* i; diff --git a/src/shared/path-util.h b/src/shared/path-util.h index f5f5c36664..56d1c52ae2 100644 --- a/src/shared/path-util.h +++ b/src/shared/path-util.h @@ -34,6 +34,7 @@ bool path_equal(const char *a, const char *b) _pure_; char** path_strv_resolve(char **l, const char *prefix); char** path_strv_resolve_uniq(char **l, const char *prefix); +int fd_is_mount_point(int fd); int path_is_mount_point(const char *path, bool allow_symlink); bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update); -- cgit v1.2.3-54-g00ecf