From: Karel Zak Date: Wed, 14 Aug 2024 14:28:48 +0000 (+0200) Subject: libmount: add API to read ID by statx() X-Git-Tag: v2.42-start~97^2~32 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2dfadc248ceb4f1874756cd11a93b268a6e553e2;p=thirdparty%2Futil-linux.git libmount: add API to read ID by statx() Signed-off-by: Karel Zak --- diff --git a/libmount/docs/libmount-sections.txt b/libmount/docs/libmount-sections.txt index a8a503144..b72229c58 100644 --- a/libmount/docs/libmount-sections.txt +++ b/libmount/docs/libmount-sections.txt @@ -430,6 +430,7 @@ mnt_get_mtab_path mnt_get_swaps_path mnt_guess_system_root mnt_has_regular_mtab +mnt_id_from_path mnt_mangle mnt_match_fstype mnt_tag_is_valid diff --git a/libmount/src/libmount.h.in b/libmount/src/libmount.h.in index 90f6a66c0..146240630 100644 --- a/libmount/src/libmount.h.in +++ b/libmount/src/libmount.h.in @@ -369,6 +369,8 @@ extern char *mnt_get_mountpoint(const char *path) extern int mnt_guess_system_root(dev_t devno, struct libmnt_cache *cache, char **path) __ul_attribute__((nonnull(3))); +extern int mnt_id_from_path(const char *path, uint64_t *uniq_id, int *id); + /* cache.c */ extern struct libmnt_cache *mnt_new_cache(void) __ul_attribute__((warn_unused_result)); diff --git a/libmount/src/libmount.sym b/libmount/src/libmount.sym index cdd76ba32..af7b2ac21 100644 --- a/libmount/src/libmount.sym +++ b/libmount/src/libmount.sym @@ -386,4 +386,5 @@ MOUNT_2_41 { mnt_fs_get_uniq_id; mnt_fs_get_parent_uniq_id; mnt_fs_set_uniq_id; + mnt_id_from_path; } MOUNT_2_40; diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index 370bcd9e7..b1374ceb2 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -136,6 +136,8 @@ extern int mnt_is_path(const char *target); extern int mnt_tmptgt_unshare(int *old_ns_fd); extern int mnt_tmptgt_cleanup(int old_ns_fd); +extern int mnt_id_from_fd(int fd, uint64_t *uniq_id, int *id); + /* tab.c */ extern int is_mountinfo(struct libmnt_table *tb); extern int mnt_table_set_parser_fltrcb( struct libmnt_table *tb, diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 94a877cf0..e5e376629 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -266,6 +266,63 @@ int mnt_is_readonly(const char *path) return 0; } +static int get_mnt_id( int fd, const char *path, + uint64_t *uniq_id, int *id) +{ +#if defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(HAVE_STRUCT_STATX_STX_MNT_ID) + int rc; + struct statx sx = { 0 }; + int flags = AT_STATX_DONT_SYNC | AT_NO_AUTOMOUNT; + + if (!path || !*path) + flags |= AT_EMPTY_PATH; + + if (id) { + rc = statx(fd, path ? path : "", flags, + STATX_MNT_ID, &sx); + if (rc) + return rc; + *id = sx.stx_mnt_id; + } + if (uniq_id) { + errno = 0; + rc = statx(fd, path ? path : "", flags, + STATX_MNT_ID_UNIQUE, &sx); + + if (rc && errno == EINVAL) + return -ENOSYS; /* *_ID_UNIQUE unsupported? */ + if (rc) + return rc; + *uniq_id = sx.stx_mnt_id; + } + return 0; +#else + return -ENOSYS; +#endif +} + +int mnt_id_from_fd(int fd, uint64_t *uniq_id, int *id) +{ + return get_mnt_id(fd, NULL, uniq_id, id); +} + +/** + * mnt_id_from_path: + * @path: mountpoint + * @uniq_id: returns STATX_MNT_ID_UNIQUE + * @id: returns STATX_MNT_ID + * + * Converts @path to ID. + * + * Returns: 0 on success, <0 on error + * + * Since: 2.41 + */ +int mnt_id_from_path(const char *path, uint64_t *uniq_id, int *id) +{ + return get_mnt_id(-1, path, uniq_id, id); +} + /** * mnt_mangle: * @str: string