From: Lennart Poettering Date: Tue, 25 Jun 2024 10:37:32 +0000 (+0200) Subject: mountpoint-util: add public helper for comparing file handles X-Git-Tag: v257-rc1~1003^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8da6413860f8a6cdcda67f03e629a0adf9392915;p=thirdparty%2Fsystemd.git mountpoint-util: add public helper for comparing file handles We already have the code, let's move it to a function of its own and export it. --- diff --git a/src/basic/mountpoint-util.c b/src/basic/mountpoint-util.c index 73b582f58f3..31a077fbdf2 100644 --- a/src/basic/mountpoint-util.c +++ b/src/basic/mountpoint-util.c @@ -178,6 +178,17 @@ static bool filename_possibly_with_slash_suffix(const char *s) { return filename_is_valid(copied); } +bool file_handle_equal(const struct file_handle *a, const struct file_handle *b) { + if (a == b) + return true; + if (!a != !b) + return false; + if (a->handle_type != b->handle_type) + return false; + + return memcmp_nn(a->f_handle, a->handle_bytes, b->f_handle, b->handle_bytes) == 0; +} + int fd_is_mount_point(int fd, const char *filename, int flags) { _cleanup_free_ struct file_handle *h = NULL, *h_parent = NULL; int mount_id = -1, mount_id_parent = -1; @@ -277,10 +288,7 @@ int fd_is_mount_point(int fd, const char *filename, int flags) { /* If the file handle for the directory we are interested in and its parent are identical, * we assume this is the root directory, which is a mount point. */ - - if (h->handle_type == h_parent->handle_type && - memcmp_nn(h->f_handle, h->handle_bytes, - h_parent->f_handle, h_parent->handle_bytes) == 0) + if (file_handle_equal(h_parent, h)) return 1; return mount_id != mount_id_parent; diff --git a/src/basic/mountpoint-util.h b/src/basic/mountpoint-util.h index 26afbb653de..c9d730e25c3 100644 --- a/src/basic/mountpoint-util.h +++ b/src/basic/mountpoint-util.h @@ -40,6 +40,8 @@ bool is_name_to_handle_at_fatal_error(int err); int name_to_handle_at_loop(int fd, const char *path, struct file_handle **ret_handle, int *ret_mnt_id, int flags); +bool file_handle_equal(const struct file_handle *a, const struct file_handle *b); + int path_get_mnt_id_at_fallback(int dir_fd, const char *path, int *ret); int path_get_mnt_id_at(int dir_fd, const char *path, int *ret); static inline int path_get_mnt_id(const char *path, int *ret) {