linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 2/3] btrfs-progs: Add path_is_mp option for find_mount_root.
Date: Mon, 10 Feb 2014 15:28:29 +0800	[thread overview]
Message-ID: <1392017310-19951-3-git-send-email-quwenruo@cn.fujitsu.com> (raw)
In-Reply-To: <1392017310-19951-1-git-send-email-quwenruo@cn.fujitsu.com>

Add path_is_mp option for find_mount_root, allowing to treat path as a
mount point, if not found a restricted(*) match, will return -ENOENT.

*: stricted match allow only the last '/' differs since path completion
often addes a '/' in the end but mount points in /proc/self/mounts.
e.g /mnt/data and /mnt/data/ is a restricted match but
/mnt/data and /mnt/data/something is not a restricted match.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 cmds-receive.c   |  2 +-
 cmds-send.c      |  4 ++--
 cmds-subvolume.c |  2 +-
 utils.c          | 32 ++++++++++++++++++++++++--------
 utils.h          |  2 +-
 5 files changed, 29 insertions(+), 13 deletions(-)

diff --git a/cmds-receive.c b/cmds-receive.c
index cce37a7..810fd59 100644
--- a/cmds-receive.c
+++ b/cmds-receive.c
@@ -843,7 +843,7 @@ static int do_receive(struct btrfs_receive *r, const char *tomnt, int r_fd)
 		goto out;
 	}
 
-	ret = find_mount_root(dest_dir_full_path, &r->root_path);
+	ret = find_mount_root(dest_dir_full_path, &r->root_path, 0);
 	if (ret < 0) {
 		ret = -EINVAL;
 		fprintf(stderr, "ERROR: failed to determine mount point "
diff --git a/cmds-send.c b/cmds-send.c
index 9d49ce9..967f45a 100644
--- a/cmds-send.c
+++ b/cmds-send.c
@@ -349,7 +349,7 @@ static int init_root_path(struct btrfs_send *s, const char *subvol)
 	if (s->root_path)
 		goto out;
 
-	ret = find_mount_root(subvol, &s->root_path);
+	ret = find_mount_root(subvol, &s->root_path, 0);
 	if (ret < 0) {
 		ret = -EINVAL;
 		fprintf(stderr, "ERROR: failed to determine mount point "
@@ -584,7 +584,7 @@ int cmd_send(int argc, char **argv)
 			goto out;
 		}
 
-		ret = find_mount_root(subvol, &mount_root);
+		ret = find_mount_root(subvol, &mount_root, 0);
 		if (ret < 0) {
 			fprintf(stderr, "ERROR: find_mount_root failed on %s: "
 					"%s\n", subvol,
diff --git a/cmds-subvolume.c b/cmds-subvolume.c
index 0bd76f2..a78a535 100644
--- a/cmds-subvolume.c
+++ b/cmds-subvolume.c
@@ -931,7 +931,7 @@ static int cmd_subvol_show(int argc, char **argv)
 		goto out;
 	}
 
-	ret = find_mount_root(fullpath, &mnt);
+	ret = find_mount_root(fullpath, &mnt, 0);
 	if (ret < 0) {
 		fprintf(stderr, "ERROR: find_mount_root failed on %s: "
 				"%s\n", fullpath, strerror(-ret));
diff --git a/utils.c b/utils.c
index 8f06d4e..e39ad79 100644
--- a/utils.c
+++ b/utils.c
@@ -2111,7 +2111,13 @@ int lookup_ino_rootid(int fd, u64 *rootid)
 	return 0;
 }
 
-int find_mount_root(const char *path, char **mount_root)
+/*
+ * Find the mount root of a given path.
+ * Return 0 when found and restore the mount root into mount_root.
+ * If path_is_mp is set, path will be treated as mount point and compare
+ * in a restricted way.
+ */
+int find_mount_root(const char *path, char **mount_root, int path_is_mp)
 {
 	FILE *mnttab;
 	int fd;
@@ -2144,17 +2150,27 @@ int find_mount_root(const char *path, char **mount_root)
 	endmntent(mnttab);
 
 	if (!longest_match) {
-		fprintf(stderr,
-			"ERROR: Failed to find mount root for path %s.\n",
-			path);
-		return -ENOENT;
+		ret = -ENOENT;
+		goto out;
+	}
+
+	/* Only the last '/' in path may differs if path_is_mp */
+	if (path_is_mp && strlen(path) != strlen(longest_match)) {
+		if (strlen(path) != strlen(longest_match) + 1 ||
+		    path[strlen(path) - 1] != '/') {
+			ret = -ENOENT;
+			goto out;
+		}
 	}
 
 	ret = 0;
-	*mount_root = realpath(longest_match, NULL);
-	if (!*mount_root)
-		ret = -errno;
+	if (mount_root) {
+		*mount_root = realpath(longest_match, *mount_root);
+		if (!*mount_root)
+			ret = -errno;
+	}
 
+out:
 	free(longest_match);
 	return ret;
 }
diff --git a/utils.h b/utils.h
index e074732..b69712e 100644
--- a/utils.h
+++ b/utils.h
@@ -96,6 +96,6 @@ int ask_user(char *question);
 int lookup_ino_rootid(int fd, u64 *rootid);
 int btrfs_scan_lblkid(int update_kernel);
 int get_btrfs_mount(const char *dev, char *mp, size_t mp_size);
-int find_mount_root(const char *path, char **mount_root);
+int find_mount_root(const char *path, char **mount_root, int path_is_mp);
 
 #endif
-- 
1.8.5.4


  parent reply	other threads:[~2014-02-10  7:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-10  7:28 [PATCH 0/3] make 'btrfs fi show /mnt/point/' works with ending '/' character Qu Wenruo
2014-02-10  7:28 ` [PATCH 1/3] btrfs-progs: move find_mount_root to utils.[ch] Qu Wenruo
2014-02-10  7:28 ` Qu Wenruo [this message]
2014-02-10  7:28 ` [PATCH 3/3] btrfs-progs: reuse find_mount_root to determine arg type and so on Qu Wenruo
2014-02-12  2:18 ` [PATCH 0/3] make 'btrfs fi show /mnt/point/' works with ending '/' character Qu Wenruo
2014-02-12 14:31   ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1392017310-19951-3-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).