From ec7c579a737e257cd9acb6d052165daabbf9c8c4 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 19 Aug 2024 12:59:10 +0200 Subject: [PATCH] libmount: ifdef listmount and statmount stuff * introduce mnt_fs_try_statmount() macro to simplify mnt_fs_fetch_statmount() calls * define dummy API functions when HAVE_STATMOUNT_API undefined Signed-off-by: Karel Zak --- libmount/src/fs.c | 111 +++++++++++++++++++---------------- libmount/src/fs_statmount.c | 5 ++ libmount/src/mountP.h | 11 +++- libmount/src/tab.c | 4 +- libmount/src/tab_listmount.c | 50 +++++++++++++++- 5 files changed, 124 insertions(+), 57 deletions(-) diff --git a/libmount/src/fs.c b/libmount/src/fs.c index e466bca13..fdc249ed9 100644 --- a/libmount/src/fs.c +++ b/libmount/src/fs.c @@ -617,8 +617,9 @@ const char *mnt_fs_get_target(struct libmnt_fs *fs) { if (!fs) return NULL; - if (!fs->target && mnt_fs_want_statmount(fs, STATMOUNT_MNT_POINT)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_POINT); +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, target, STATMOUNT_MNT_POINT); +#endif return fs->target;; } @@ -665,9 +666,9 @@ int mnt_fs_get_propagation(struct libmnt_fs *fs, unsigned long *flags) { if (!fs || !flags) return -EINVAL; - if (!fs->propagation && mnt_fs_want_statmount(fs, STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, propagation, STATMOUNT_MNT_BASIC); +#endif if (!fs->propagation && fs->opt_fields) { /* * The optional fields format is incompatible with mount options @@ -719,9 +720,9 @@ int mnt_fs_is_pseudofs(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->fstype && mnt_fs_want_statmount(fs, STATMOUNT_FS_TYPE)) - mnt_fs_fetch_statmount(fs, STATMOUNT_FS_TYPE); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, fstype, STATMOUNT_FS_TYPE); +#endif return mnt_fs_get_flags(fs) & MNT_FS_PSEUDO ? 1 : 0; } @@ -735,9 +736,9 @@ int mnt_fs_is_netfs(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->fstype && mnt_fs_want_statmount(fs, STATMOUNT_FS_TYPE)) - mnt_fs_fetch_statmount(fs, STATMOUNT_FS_TYPE); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, fstype, STATMOUNT_FS_TYPE); +#endif return mnt_fs_get_flags(fs) & MNT_FS_NET ? 1 : 0; } @@ -766,10 +767,9 @@ const char *mnt_fs_get_fstype(struct libmnt_fs *fs) { if (!fs) return NULL; - - if (!fs->fstype && mnt_fs_want_statmount(fs, STATMOUNT_FS_TYPE)) - mnt_fs_fetch_statmount(fs, STATMOUNT_FS_TYPE); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, fstype, STATMOUNT_FS_TYPE); +#endif return fs->fstype; } @@ -892,9 +892,10 @@ char *mnt_fs_strdup_options(struct libmnt_fs *fs) return NULL; if (fs->optlist) sync_opts_from_optlist(fs, fs->optlist); - else if (!fs->optstr && mnt_fs_want_statmount(fs, STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + else + mnt_fs_try_statmount(fs, optstr, STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC); +#endif errno = 0; if (fs->optstr) return strdup(fs->optstr); @@ -922,9 +923,10 @@ const char *mnt_fs_get_options(struct libmnt_fs *fs) return NULL; if (fs->optlist) sync_opts_from_optlist(fs, fs->optlist); - else if (!fs->optstr && mnt_fs_want_statmount(fs, STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + else + mnt_fs_try_statmount(fs, optstr, STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC); +#endif return fs->optstr; } @@ -1094,9 +1096,10 @@ const char *mnt_fs_get_fs_options(struct libmnt_fs *fs) return NULL; if (fs->optlist) sync_opts_from_optlist(fs, fs->optlist); - else if (!fs->fs_optstr && mnt_fs_want_statmount(fs, STATMOUNT_SB_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_SB_BASIC); - +#ifdef HAVE_STATMOUNT_API + else + mnt_fs_try_statmount(fs, fs_optstr, STATMOUNT_SB_BASIC); +#endif return fs->fs_optstr; } @@ -1112,9 +1115,10 @@ const char *mnt_fs_get_vfs_options(struct libmnt_fs *fs) return NULL; if (fs->optlist) sync_opts_from_optlist(fs, fs->optlist); - else if (!fs->vfs_optstr && mnt_fs_want_statmount(fs, STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + else + mnt_fs_try_statmount(fs, vfs_optstr, STATMOUNT_MNT_BASIC); +#endif return fs->vfs_optstr; } @@ -1299,9 +1303,9 @@ const char *mnt_fs_get_root(struct libmnt_fs *fs) { if (!fs) return NULL; - if (!fs->root && mnt_fs_want_statmount(fs, STATMOUNT_MNT_ROOT)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_ROOT); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, root, STATMOUNT_MNT_ROOT); +#endif return fs->root; } @@ -1414,9 +1418,9 @@ int mnt_fs_get_id(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->id && mnt_fs_want_statmount(fs, STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, id, STATMOUNT_MNT_BASIC); +#endif return fs->id; } @@ -1435,9 +1439,9 @@ uint64_t mnt_fs_get_uniq_id(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->uniq_id && mnt_fs_want_statmount(fs, STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, uniq_id, STATMOUNT_MNT_BASIC); +#endif return fs->uniq_id; } @@ -1471,9 +1475,9 @@ int mnt_fs_get_parent_id(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->parent && mnt_fs_want_statmount(fs, STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, parent, STATMOUNT_MNT_BASIC); +#endif return fs->parent; } @@ -1489,9 +1493,9 @@ uint64_t mnt_fs_get_parent_uniq_id(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->uniq_parent && mnt_fs_want_statmount(fs, STATMOUNT_MNT_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_BASIC); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, uniq_parent, STATMOUNT_MNT_BASIC); +#endif return fs->uniq_parent; } @@ -1509,8 +1513,9 @@ uint64_t mnt_fs_get_ns(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->ns_id && mnt_fs_want_statmount(fs, STATMOUNT_MNT_NS_ID)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_NS_ID); +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, ns_id, STATMOUNT_MNT_NS_ID); +#endif return fs->ns_id; } @@ -1542,9 +1547,9 @@ dev_t mnt_fs_get_devno(struct libmnt_fs *fs) { if (!fs) return 0; - if (!fs->devno && mnt_fs_want_statmount(fs, STATMOUNT_SB_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_SB_BASIC); - +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, devno, STATMOUNT_SB_BASIC); +#endif return fs->devno; } @@ -1578,9 +1583,10 @@ int mnt_fs_get_option(struct libmnt_fs *fs, const char *name, if (fs->optlist) sync_opts_from_optlist(fs, fs->optlist); - else if (!fs->vfs_optstr && mnt_fs_want_statmount(fs, STATMOUNT_MNT_BASIC | STATMOUNT_SB_BASIC)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_BASIC | STATMOUNT_SB_BASIC); - +#ifdef HAVE_STATMOUNT_API + else + mnt_fs_try_statmount(fs, vfs_optstr, STATMOUNT_SB_BASIC | STATMOUNT_MNT_BASIC); +#endif if (fs->fs_optstr) rc = mnt_optstr_get_option(fs->fs_optstr, name, value, valsz); if (rc == 1 && fs->vfs_optstr) @@ -1686,8 +1692,9 @@ int mnt_fs_match_target(struct libmnt_fs *fs, const char *target, if (!fs || !target) return 0; - if (!fs->target && mnt_fs_want_statmount(fs, STATMOUNT_MNT_POINT)) - mnt_fs_fetch_statmount(fs, STATMOUNT_MNT_POINT); +#ifdef HAVE_STATMOUNT_API + mnt_fs_try_statmount(fs, target, STATMOUNT_MNT_BASIC); +#endif if (!fs->target) return 0; diff --git a/libmount/src/fs_statmount.c b/libmount/src/fs_statmount.c index 0fad5bd4b..d331e2476 100644 --- a/libmount/src/fs_statmount.c +++ b/libmount/src/fs_statmount.c @@ -29,6 +29,7 @@ */ struct libmnt_statmnt *mnt_new_statmnt(void) { +#ifdef HAVE_STATMOUNT_API struct libmnt_statmnt *sm; errno = 0; @@ -44,6 +45,10 @@ struct libmnt_statmnt *mnt_new_statmnt(void) sm->refcount = 1; DBG(STATMNT, ul_debugobj(sm, "alloc")); return sm; +#else + errno = ENOSYS; + return NULL; +#endif } /** diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index ee275f261..da4b60f39 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -283,8 +283,15 @@ struct libmnt_fs { #define MNT_FS_KERNEL (1 << 4) /* data from /proc/{mounts,self/mountinfo} */ #define MNT_FS_MERGED (1 << 5) /* already merged data from /run/mount/utab */ -#define mnt_fs_want_statmount(_x, _m) \ - ((_x)->stmnt && !(_x)->stmnt->disabled && !((_x)->stmnt_done & (_m))) +#ifdef HAVE_STATMOUNT_API +# define mnt_fs_try_statmount(FS, MEMBER, FLAGS) __extension__ ({ \ + if (!(FS)->MEMBER \ + && (FS)->stmnt \ + && !(FS)->stmnt->disabled \ + && !((FS)->stmnt_done & (FLAGS))) \ + mnt_fs_fetch_statmount((FS), (FLAGS)); }) +#endif + /* * fstab/mountinfo file diff --git a/libmount/src/tab.c b/libmount/src/tab.c index 7880d6491..ba4eef850 100644 --- a/libmount/src/tab.c +++ b/libmount/src/tab.c @@ -855,7 +855,7 @@ int mnt_table_next_fs(struct libmnt_table *tb, struct libmnt_iter *itr, struct l return -EINVAL; if (fs) *fs = NULL; - +#ifdef HAVE_STATMOUNT_API if (mnt_table_want_listmount(tb) && (list_empty(&tb->ents) || itr->p == itr->head)) { struct list_head *prev = NULL; @@ -871,7 +871,7 @@ int mnt_table_next_fs(struct libmnt_table *tb, struct libmnt_iter *itr, struct l MNT_ITER_ITERATE(itr); } } - +#endif if (!itr->head) MNT_ITER_INIT(itr, &tb->ents); if (itr->p != itr->head) { diff --git a/libmount/src/tab_listmount.c b/libmount/src/tab_listmount.c index 2d11af6e9..4bf86298a 100644 --- a/libmount/src/tab_listmount.c +++ b/libmount/src/tab_listmount.c @@ -11,6 +11,54 @@ */ #include "mountP.h" +#ifndef HAVE_STATMOUNT_API + +int mnt_table_listmount_set_id( + struct libmnt_table *tb __attribute__((__unused__)), + uint64_t id __attribute__((__unused__))) +{ + return -ENOSYS; +} + +int mnt_table_listmount_set_ns( + struct libmnt_table *tb __attribute__((__unused__)), + uint64_t ns __attribute__((__unused__))) +{ + return -ENOSYS; +} + +int mnt_table_listmount_set_stepsiz( + struct libmnt_table *tb __attribute__((__unused__)), + size_t sz __attribute__((__unused__))) +{ + return -ENOSYS; +} + +int mnt_table_enable_listmount( + struct libmnt_table *tb __attribute__((__unused__)), + int enable __attribute__((__unused__))) +{ + return -ENOSYS; +} + +int mnt_table_fetch_listmount(struct libmnt_table *tb __attribute__((__unused__))) +{ + return -ENOSYS; +} + +int mnt_table_reset_listmount(struct libmnt_table *tb __attribute__((__unused__))) +{ + return -ENOSYS; +} + +int mnt_table_next_lsmnt(struct libmnt_table *tb __attribute__((__unused__)), + int direction __attribute__((__unused__))) +{ + return -ENOSYS; +} + +#else /* HAVE_STATMOUNT_API */ + /* * This struct is not shared between multiple tables, so reference counting is * not used for it. @@ -371,4 +419,4 @@ int mnt_table_fetch_listmount(struct libmnt_table *tb) return rc; } - +#endif /* HAVE_STATMOUNT_API */ -- 2.47.2