linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] make 'btrfs fi show /mnt/point/' works with ending '/' character
@ 2014-02-10  7:28 Qu Wenruo
  2014-02-10  7:28 ` [PATCH 1/3] btrfs-progs: move find_mount_root to utils.[ch] Qu Wenruo
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Qu Wenruo @ 2014-02-10  7:28 UTC (permalink / raw)
  To: linux-btrfs

Before this patchset, 'btrfs fi show' can work with '/mnt/point' but not '/mnt/point/',
which is very annoying since tab completion will add '/' to a directory.

This patchset just reuse the find_mount_root function with some small modification to
ignore the last '/' only when needed.

Qu Wenruo (3):
  btrfs-progs: move find_mount_root to utils.[ch]
  btrfs-progs: Add path_is_mp option for find_mount_root.
  btrfs-progs: reuse find_mount_root to determine arg type and so on.

 cmds-filesystem.c |  7 +++++
 cmds-receive.c    |  2 +-
 cmds-send.c       | 53 ++-------------------------------
 cmds-subvolume.c  |  2 +-
 commands.h        |  1 -
 utils.c           | 88 +++++++++++++++++++++++++++++++++++++++++++++----------
 utils.h           |  1 +
 7 files changed, 86 insertions(+), 68 deletions(-)

-- 
1.8.5.4


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/3] btrfs-progs: move find_mount_root to utils.[ch]
  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 ` Qu Wenruo
  2014-02-10  7:28 ` [PATCH 2/3] btrfs-progs: Add path_is_mp option for find_mount_root Qu Wenruo
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2014-02-10  7:28 UTC (permalink / raw)
  To: linux-btrfs

Move find_mount_root to utils.[ch] for general use.

Signed-off-by: Qu Wenruo <quwenruo@cn.fuijitsu.com>
---
 cmds-send.c | 49 +------------------------------------------------
 commands.h  |  1 -
 utils.c     | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h     |  1 +
 4 files changed, 50 insertions(+), 49 deletions(-)

diff --git a/cmds-send.c b/cmds-send.c
index fc9a01e..9d49ce9 100644
--- a/cmds-send.c
+++ b/cmds-send.c
@@ -39,6 +39,7 @@
 #include "ioctl.h"
 #include "commands.h"
 #include "list.h"
+#include "utils.h"
 
 #include "send.h"
 #include "send-utils.h"
@@ -57,54 +58,6 @@ struct btrfs_send {
 	struct subvol_uuid_search sus;
 };
 
-int find_mount_root(const char *path, char **mount_root)
-{
-	FILE *mnttab;
-	int fd;
-	struct mntent *ent;
-	int len;
-	int ret;
-	int longest_matchlen = 0;
-	char *longest_match = NULL;
-
-	fd = open(path, O_RDONLY | O_NOATIME);
-	if (fd < 0)
-		return -errno;
-	close(fd);
-
-	mnttab = setmntent("/proc/self/mounts", "r");
-	if (!mnttab)
-		return -errno;
-
-	while ((ent = getmntent(mnttab))) {
-		len = strlen(ent->mnt_dir);
-		if (strncmp(ent->mnt_dir, path, len) == 0) {
-			/* match found */
-			if (longest_matchlen < len) {
-				free(longest_match);
-				longest_matchlen = len;
-				longest_match = strdup(ent->mnt_dir);
-			}
-		}
-	}
-	endmntent(mnttab);
-
-	if (!longest_match) {
-		fprintf(stderr,
-			"ERROR: Failed to find mount root for path %s.\n",
-			path);
-		return -ENOENT;
-	}
-
-	ret = 0;
-	*mount_root = realpath(longest_match, NULL);
-	if (!*mount_root)
-		ret = -errno;
-
-	free(longest_match);
-	return ret;
-}
-
 static int get_root_id(struct btrfs_send *s, const char *path, u64 *root_id)
 {
 	struct subvol_info *si;
diff --git a/commands.h b/commands.h
index 23c1201..db70043 100644
--- a/commands.h
+++ b/commands.h
@@ -126,5 +126,4 @@ int cmd_rescue(int argc, char **argv);
 int test_issubvolume(char *path);
 
 /* send.c */
-int find_mount_root(const char *path, char **mount_root);
 char *get_subvol_name(char *mnt, char *full_path);
diff --git a/utils.c b/utils.c
index 75b37f3..8f06d4e 100644
--- a/utils.c
+++ b/utils.c
@@ -2110,3 +2110,51 @@ int lookup_ino_rootid(int fd, u64 *rootid)
 
 	return 0;
 }
+
+int find_mount_root(const char *path, char **mount_root)
+{
+	FILE *mnttab;
+	int fd;
+	struct mntent *ent;
+	int len;
+	int ret;
+	int longest_matchlen = 0;
+	char *longest_match = NULL;
+
+	fd = open(path, O_RDONLY | O_NOATIME);
+	if (fd < 0)
+		return -errno;
+	close(fd);
+
+	mnttab = setmntent("/proc/self/mounts", "r");
+	if (!mnttab)
+		return -errno;
+
+	while ((ent = getmntent(mnttab))) {
+		len = strlen(ent->mnt_dir);
+		if (strncmp(ent->mnt_dir, path, len) == 0) {
+			/* match found */
+			if (longest_matchlen < len) {
+				free(longest_match);
+				longest_matchlen = len;
+				longest_match = strdup(ent->mnt_dir);
+			}
+		}
+	}
+	endmntent(mnttab);
+
+	if (!longest_match) {
+		fprintf(stderr,
+			"ERROR: Failed to find mount root for path %s.\n",
+			path);
+		return -ENOENT;
+	}
+
+	ret = 0;
+	*mount_root = realpath(longest_match, NULL);
+	if (!*mount_root)
+		ret = -errno;
+
+	free(longest_match);
+	return ret;
+}
diff --git a/utils.h b/utils.h
index 512c51b..e074732 100644
--- a/utils.h
+++ b/utils.h
@@ -96,5 +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);
 
 #endif
-- 
1.8.5.4


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 2/3] btrfs-progs: Add path_is_mp option for find_mount_root.
  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
  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
  3 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2014-02-10  7:28 UTC (permalink / raw)
  To: linux-btrfs

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH 3/3] btrfs-progs: reuse find_mount_root to determine arg type and so on.
  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 ` [PATCH 2/3] btrfs-progs: Add path_is_mp option for find_mount_root Qu Wenruo
@ 2014-02-10  7:28 ` Qu Wenruo
  2014-02-12  2:18 ` [PATCH 0/3] make 'btrfs fi show /mnt/point/' works with ending '/' character Qu Wenruo
  3 siblings, 0 replies; 6+ messages in thread
From: Qu Wenruo @ 2014-02-10  7:28 UTC (permalink / raw)
  To: linux-btrfs

Since find_mount_root now can check mount point quiet good, reuse it to
determing arg type and get real mount point

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 cmds-filesystem.c |  7 +++++++
 utils.c           | 24 +++++++++---------------
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 43e1cf3..bc62d51 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -532,6 +532,7 @@ static int cmd_show(int argc, char **argv)
 	struct btrfs_fs_devices *fs_devices;
 	struct list_head *cur_uuid;
 	char *search = NULL;
+	char *real_mp = NULL;
 	int ret;
 	int where = BTRFS_SCAN_LBLKID;
 	int type = 0;
@@ -607,6 +608,11 @@ static int cmd_show(int argc, char **argv)
 				}
 			}
 		}
+
+		if (type == BTRFS_ARG_MNTPOINT) {
+			find_mount_root(search, &real_mp, 1);
+			search = real_mp;
+		}
 	}
 
 	if (where == BTRFS_SCAN_DEV)
@@ -646,6 +652,7 @@ devs_only:
 
 out:
 	printf("%s\n", BTRFS_BUILD_VERSION);
+	free(real_mp);
 	free_seen_fsid();
 	return ret;
 }
diff --git a/utils.c b/utils.c
index e39ad79..297f18d 100644
--- a/utils.c
+++ b/utils.c
@@ -700,26 +700,20 @@ int is_block_device(const char *path)
 
 /*
  * check if given path is a mount point
- * return 1 if yes. 0 if no. -1 for error
+ * return 1 if yes. 0 if no. <0 for error
  */
 int is_mount_point(const char *path)
 {
-	FILE *f;
-	struct mntent *mnt;
-	int ret = 0;
-
-	f = setmntent("/proc/self/mounts", "r");
-	if (f == NULL)
-		return -1;
+	int ret;
 
-	while ((mnt = getmntent(f)) != NULL) {
-		if (strcmp(mnt->mnt_dir, path))
-			continue;
-		ret = 1;
-		break;
+	ret = find_mount_root(path, NULL, 1);
+	if (ret < 0) {
+		if (ret != -ENOENT)
+			return 0;
+		else
+			return ret;
 	}
-	endmntent(f);
-	return ret;
+	return 1;
 }
 
 /*
-- 
1.8.5.4


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/3] make 'btrfs fi show /mnt/point/' works with ending '/' character
  2014-02-10  7:28 [PATCH 0/3] make 'btrfs fi show /mnt/point/' works with ending '/' character Qu Wenruo
                   ` (2 preceding siblings ...)
  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 ` Qu Wenruo
  2014-02-12 14:31   ` David Sterba
  3 siblings, 1 reply; 6+ messages in thread
From: Qu Wenruo @ 2014-02-12  2:18 UTC (permalink / raw)
  To: linux-btrfs

Please ignore this patchset since adding a new option to find_mount_root
is not the best method to
solve the problem.

I'll send a better version patch to fix it.

Thanks
Qu

On mon, 10 Feb 2014 15:28:27 +0800, Qu Wenruo wrote:
> Before this patchset, 'btrfs fi show' can work with '/mnt/point' but not '/mnt/point/',
> which is very annoying since tab completion will add '/' to a directory.
>
> This patchset just reuse the find_mount_root function with some small modification to
> ignore the last '/' only when needed.
>
> Qu Wenruo (3):
>   btrfs-progs: move find_mount_root to utils.[ch]
>   btrfs-progs: Add path_is_mp option for find_mount_root.
>   btrfs-progs: reuse find_mount_root to determine arg type and so on.
>
>  cmds-filesystem.c |  7 +++++
>  cmds-receive.c    |  2 +-
>  cmds-send.c       | 53 ++-------------------------------
>  cmds-subvolume.c  |  2 +-
>  commands.h        |  1 -
>  utils.c           | 88 +++++++++++++++++++++++++++++++++++++++++++++----------
>  utils.h           |  1 +
>  7 files changed, 86 insertions(+), 68 deletions(-)
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 0/3] make 'btrfs fi show /mnt/point/' works with ending '/' character
  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
  0 siblings, 0 replies; 6+ messages in thread
From: David Sterba @ 2014-02-12 14:31 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Wed, Feb 12, 2014 at 10:18:26AM +0800, Qu Wenruo wrote:
> Please ignore this patchset since adding a new option to
> find_mount_root is not the best method to solve the problem.

I'll merge the first patch, it's moving a utility finction to a file
where it IMO belongs.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-02-12 14:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH 2/3] btrfs-progs: Add path_is_mp option for find_mount_root Qu Wenruo
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

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).