From: Karel Zak Date: Tue, 10 Mar 2015 12:35:56 +0000 (+0100) Subject: libmount: cleanup fs root detection code X-Git-Tag: v2.27-rc1~377 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc06a01ec551ed2bcd397a5097165b4434179b34;p=thirdparty%2Futil-linux.git libmount: cleanup fs root detection code Signed-off-by: Karel Zak --- diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h index cdd57cc5be..5eefa71104 100644 --- a/libmount/src/mountP.h +++ b/libmount/src/mountP.h @@ -99,7 +99,6 @@ extern int mnt_get_uid(const char *username, uid_t *uid); extern int mnt_get_gid(const char *groupname, gid_t *gid); extern int mnt_in_group(gid_t gid); -extern char *mnt_get_fs_root(const char *path, const char *mountpoint); extern int mnt_open_uniq_filename(const char *filename, char **name); extern int mnt_has_regular_utab(const char **utab, int *writable); @@ -116,10 +115,6 @@ extern int mnt_table_set_parser_fltrcb( struct libmnt_table *tb, int (*cb)(struct libmnt_fs *, void *), void *data); -extern struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb, - struct libmnt_fs *fs, - unsigned long mountflags, - char **fsroot); extern int __mnt_table_parse_mtab(struct libmnt_table *tb, const char *filename, struct libmnt_table *u_tb); diff --git a/libmount/src/tab.c b/libmount/src/tab.c index cd20ad0b2b..09896ccb61 100644 --- a/libmount/src/tab.c +++ b/libmount/src/tab.c @@ -47,6 +47,7 @@ #include "strutils.h" #include "loopdev.h" #include "fileutils.h" +#include "canonicalize.h" int is_mountinfo(struct libmnt_table *tb) { @@ -1166,6 +1167,20 @@ struct libmnt_fs *mnt_table_find_devno(struct libmnt_table *tb, return NULL; } +static char *remove_mountpoint_from_path(const char *path, const char *mnt) +{ + char *res; + const char *p; + size_t sz; + + sz = strlen(mnt); + p = sz > 1 ? path + sz : path; + + res = *p ? strdup(p) : strdup("/"); + DBG(UTILS, ul_debug("%s fs-root is %s", path, res)); + return res; +} + /* * tb: /proc/self/mountinfo * fs: filesystem @@ -1185,7 +1200,8 @@ struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb, unsigned long mountflags, char **fsroot) { - char *root = NULL, *mnt = NULL; + char *root = NULL; + const char *mnt = NULL; const char *fstype; struct libmnt_fs *src_fs = NULL; @@ -1203,10 +1219,15 @@ struct libmnt_fs *mnt_table_get_fs_root(struct libmnt_table *tb, DBG(TAB, ul_debug("fs-root for bind")); src = xsrc = mnt_resolve_spec(mnt_fs_get_source(fs), tb->cache); - if (src) - mnt = mnt_get_mountpoint(src); + if (src) { + struct libmnt_fs *fs = mnt_table_find_mountpoint(tb, + src, MNT_ITER_BACKWARD); + if (fs) + mnt = mnt_fs_get_target(fs); + } + if (mnt) - root = mnt_get_fs_root(src, mnt); + root = remove_mountpoint_from_path(src, mnt); if (xsrc && !tb->cache) { free(xsrc); @@ -1273,11 +1294,9 @@ dflt: DBG(TAB, ul_debug("FS root result: %s", root)); - free(mnt); return src_fs; err: free(root); - free(mnt); return NULL; } @@ -1710,6 +1729,7 @@ done: return rc; } + int main(int argc, char *argv[]) { struct libmnt_test tss[] = { diff --git a/libmount/src/utils.c b/libmount/src/utils.c index 6f4d1a19f7..bc43a85cf5 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -1010,28 +1010,6 @@ err: return NULL; } -char *mnt_get_fs_root(const char *path, const char *mnt) -{ - char *m = (char *) mnt, *res; - const char *p; - size_t sz; - - if (!m) - m = mnt_get_mountpoint(path); - if (!m) - return NULL; - - sz = strlen(m); - p = sz > 1 ? path + sz : path; - - if (m != mnt) - free(m); - - res = *p ? strdup(p) : strdup("/"); - DBG(UTILS, ul_debug("%s fs-root is %s", path, res)); - return res; -} - /* * Search for @name kernel command parametr. * @@ -1176,17 +1154,6 @@ int test_mountpoint(struct libmnt_test *ts, int argc, char *argv[]) return 0; } -int test_fsroot(struct libmnt_test *ts, int argc, char *argv[]) -{ - char *path = canonicalize_path(argv[1]), - *mnt = path ? mnt_get_fs_root(path, NULL) : NULL; - - printf("%s: %s\n", argv[1], mnt ? : "unknown"); - free(mnt); - free(path); - return 0; -} - int test_filesystems(struct libmnt_test *ts, int argc, char *argv[]) { char **filesystems = NULL; @@ -1277,7 +1244,6 @@ int main(int argc, char *argv[]) { "--ends-with", test_endswith, " " }, { "--append-string", test_appendstr, " " }, { "--mountpoint", test_mountpoint, "" }, - { "--fs-root", test_fsroot, "" }, { "--cd-parent", test_chdir, "" }, { "--kernel-cmdline",test_kernel_cmdline, "