From cc06a01ec551ed2bcd397a5097165b4434179b34 Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 10 Mar 2015 13:35:56 +0100
Subject: [PATCH] libmount: cleanup fs root detection code

Signed-off-by: Karel Zak <kzak@redhat.com>
---
 libmount/src/mountP.h                         |  5 ----
 libmount/src/tab.c                            | 32 ++++++++++++++++++++-----
 libmount/src/utils.c                          | 34 ---------------------------
 tests/expected/libmount/utils-fs-root         |  1 -
 tests/expected/libmount/utils-fs-root-subdir  |  1 -
 tests/expected/libmount/utils-fs-root-subdir2 |  1 -
 tests/ts/libmount/utils                       | 12 ----------
 7 files changed, 26 insertions(+), 60 deletions(-)
 delete mode 100644 tests/expected/libmount/utils-fs-root
 delete mode 100644 tests/expected/libmount/utils-fs-root-subdir
 delete mode 100644 tests/expected/libmount/utils-fs-root-subdir2

Index: util-linux-2.25/libmount/src/mountP.h
===================================================================
--- util-linux-2.25.orig/libmount/src/mountP.h
+++ util-linux-2.25/libmount/src/mountP.h
@@ -97,7 +97,6 @@ extern int mnt_get_uid(const char *usern
 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);
@@ -113,10 +112,6 @@ extern int mnt_table_set_parser_fltrcb(
 					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);
Index: util-linux-2.25/libmount/src/tab.c
===================================================================
--- util-linux-2.25.orig/libmount/src/tab.c
+++ util-linux-2.25/libmount/src/tab.c
@@ -47,6 +47,7 @@
 #include "strutils.h"
 #include "loopdev.h"
 #include "fileutils.h"
+#include "canonicalize.h"
 
 static int is_mountinfo(struct libmnt_table *tb)
 {
@@ -1219,6 +1220,20 @@ struct libmnt_fs *mnt_table_find_devno(s
 	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
@@ -1238,7 +1253,8 @@ struct libmnt_fs *mnt_table_get_fs_root(
 					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;
 
@@ -1256,10 +1272,15 @@ struct libmnt_fs *mnt_table_get_fs_root(
 		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);
@@ -1326,11 +1347,9 @@ dflt:
 
 	DBG(TAB, ul_debug("FS root result: %s", root));
 
-	free(mnt);
 	return src_fs;
 err:
 	free(root);
-	free(mnt);
 	return NULL;
 }
 
@@ -1764,6 +1783,7 @@ done:
 	return rc;
 }
 
+
 int main(int argc, char *argv[])
 {
 	struct libmnt_test tss[] = {
Index: util-linux-2.25/libmount/src/utils.c
===================================================================
--- util-linux-2.25.orig/libmount/src/utils.c
+++ util-linux-2.25/libmount/src/utils.c
@@ -1007,28 +1007,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.
  *
@@ -1159,17 +1137,6 @@ int test_mountpoint(struct libmnt_test *
 	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;
@@ -1260,7 +1227,6 @@ int main(int argc, char *argv[])
 	{ "--ends-with",     test_endswith,        "<string> <prefix>" },
 	{ "--append-string", test_appendstr,       "<string> <appendix>" },
 	{ "--mountpoint",    test_mountpoint,      "<path>" },
-	{ "--fs-root",       test_fsroot,          "<path>" },
 	{ "--cd-parent",     test_chdir,           "<path>" },
 	{ "--kernel-cmdline",test_kernel_cmdline,  "<option> | <option>=" },
 	{ "--mkdir",         test_mkdir,           "<path>" },
Index: util-linux-2.25/tests/expected/libmount/utils-fs-root
===================================================================
--- util-linux-2.25.orig/tests/expected/libmount/utils-fs-root
+++ /dev/null
@@ -1 +0,0 @@
-/proc: /
Index: util-linux-2.25/tests/expected/libmount/utils-fs-root-subdir
===================================================================
--- util-linux-2.25.orig/tests/expected/libmount/utils-fs-root-subdir
+++ /dev/null
@@ -1 +0,0 @@
-/proc/sys/kernel: /sys/kernel
Index: util-linux-2.25/tests/expected/libmount/utils-fs-root-subdir2
===================================================================
--- util-linux-2.25.orig/tests/expected/libmount/utils-fs-root-subdir2
+++ /dev/null
@@ -1 +0,0 @@
-/etc: /etc
Index: util-linux-2.25/tests/ts/libmount/utils
===================================================================
--- util-linux-2.25.orig/tests/ts/libmount/utils
+++ util-linux-2.25/tests/ts/libmount/utils
@@ -64,18 +64,6 @@ ts_init_subtest "mountpoint-root"
 ts_valgrind $TESTPROG --mountpoint / &> $TS_OUTPUT
 ts_finalize_subtest
 
-ts_init_subtest "fs-root"
-ts_valgrind $TESTPROG --fs-root /proc &> $TS_OUTPUT
-ts_finalize_subtest
-
-ts_init_subtest "fs-root-subdir"
-ts_valgrind $TESTPROG --fs-root /proc/sys/kernel &> $TS_OUTPUT
-ts_finalize_subtest
-
-ts_init_subtest "fs-root-subdir2"
-ts_valgrind $TESTPROG --fs-root /etc &> $TS_OUTPUT
-ts_finalize_subtest
-
 ts_init_subtest "kernel-cmdline"
 export LIBMOUNT_KERNEL_CMDLINE="$TS_SELF/files/kernel_cmdline"
 ts_valgrind $TESTPROG --kernel-cmdline selinux= &>> $TS_OUTPUT
