Skip to contentSkip to conversation

libmount: expose mnt_get_mountpoint as external API

PATCH In review Dave Reisner 3 participants 6 messages Apr 26, 2012 – Apr 27, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libmount: expose mnt_get_mountpoint as external API

---
 libmount/src/libmount.h.in |    1 +
 libmount/src/libmount.sym  |    1 +
 libmount/src/mountP.h      |    1 -
 libmount/src/utils.c       |    9 +++++++++
 4 files changed, 11 insertions(+), 1 deletion(-)
libmount/src/libmount.h.in +1 -0
index 5c5edbb..e11c0c0 100644
@@ -146,6 +146,7 @@ extern const char *mnt_get_fstab_path(void);
 extern const char *mnt_get_swaps_path(void);
 extern const char *mnt_get_mtab_path(void);
 extern int mnt_has_regular_mtab(const char **mtab, int *writable);
+extern char *mnt_get_mountpoint(const char *path);
 
 /* cache.c */
 extern struct libmnt_cache *mnt_new_cache(void);
libmount/src/libmount.sym +1 -0
index ca7a305..d4d5b84 100644
@@ -237,6 +237,7 @@ global:
 	mnt_context_is_nohelpers;
 	mnt_table_find_devno;
 	mnt_table_parse_swaps;
+	mnt_get_mountpoint;
 	mnt_get_swaps_path;
 	mnt_fs_get_swaptype;
 	mnt_fs_get_size;
libmount/src/mountP.h +0 -1
index ee5e94f..c7d378e 100644
@@ -139,7 +139,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_mountpoint(const char *path);
 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);
libmount/src/utils.c +9 -0
index e740d83..b824edc 100644
@@ -785,6 +785,15 @@ int mnt_open_uniq_filename(const char *filename, char **name)
 	return fd < 0 ? -errno : fd;
 }
 
+/**
+ * mnt_get_mountpoint:
+ * @path: pathname
+ *
+ * This function finds the mountpoint that a given path resides in. @path
+ * should be canonicalized. The returned pointer should be freed by the caller.
+ *
+ * Returns: target of mounted device or NULL on error
+ */
 char *mnt_get_mountpoint(const char *path)
 {
 	char *mnt = strdup(path);
Signature · 1 line
1.7.10
#1 lore raw

PATCH 2/2 findmnt: add match_by_file to do within-device matching

Use the newly exported mnt_get_mountpoint to determine the device that a given file resides on, in case the supplied source or target is not explicitly a mount point.

http://www.spinics.net/lists/util-linux-ng/msg06081.html

---
 misc-utils/findmnt.c |   28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
misc-utils/findmnt.c +26 -2
index cc71a60..9db167f 100644
@@ -661,6 +661,30 @@ static int tab_is_tree(struct libmnt_table *tb)
 	return rc;
 }
 
+static int match_by_file(const char *name, struct libmnt_fs *fs,
+		int (*match_fn)(struct libmnt_fs*, const char*, struct libmnt_cache*))
+{
+	char *resolved, *mountpoint;
+	int match;
+
+	/* match exactly by name */
+	if (match_fn(fs, name, cache))
+		return 1;
+
+	/* maybe its a file within a mountpoint */
+	resolved = mnt_resolve_path(name, cache);
+	if (resolved) {
+		mountpoint = mnt_get_mountpoint(resolved);
+		if (mountpoint) {
+			match = match_fn(fs, mountpoint, cache);
+			free(mountpoint);
+			return match;
+		}
+	}
+
+	return 0;
+}
+
 /* filter function for libmount (mnt_table_find_next_fs()) */
 static int match_func(struct libmnt_fs *fs,
 		      void *data __attribute__ ((__unused__)))
@@ -670,11 +694,11 @@ static int match_func(struct libmnt_fs *fs,
 	void *md;
 
 	m = get_match(COL_TARGET);
-	if (m && !mnt_fs_match_target(fs, m, cache))
+	if (m && !match_by_file(m, fs, mnt_fs_match_target))
 		return rc;
 
 	m = get_match(COL_SOURCE);
-	if (m && !mnt_fs_match_source(fs, m, cache))
+	if (m && !match_by_file(m, fs, mnt_fs_match_source))
 		return rc;
 
 	m = get_match(COL_FSTYPE);
Signature · 1 line
1.7.10
#2 lore raw

Re: [PATCH 2/2] findmnt: add match_by_file to do within-device matching

Quoted 8 lines · Dave Reisner

Use the newly exported mnt_get_mountpoint to determine the device that a given file resides on, in case the supplied source or target is not explicitly a mount point.

http://www.spinics.net/lists/util-linux-ng/msg06081.html

---

On second thought, this isn't good -- it'll return results even when you're searching with --fstab (which is completely undesirable, imo).

Quoted 55 lines
 misc-utils/findmnt.c |   28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)
misc-utils/findmnt.c +26 -2
index cc71a60..9db167f 100644
@@ -661,6 +661,30 @@ static int tab_is_tree(struct libmnt_table *tb)
 	return rc;
 }
 
+static int match_by_file(const char *name, struct libmnt_fs *fs,
+		int (*match_fn)(struct libmnt_fs*, const char*, struct libmnt_cache*))
+{
+	char *resolved, *mountpoint;
+	int match;
+
+	/* match exactly by name */
+	if (match_fn(fs, name, cache))
+		return 1;
+
+	/* maybe its a file within a mountpoint */
+	resolved = mnt_resolve_path(name, cache);
+	if (resolved) {
+		mountpoint = mnt_get_mountpoint(resolved);
+		if (mountpoint) {
+			match = match_fn(fs, mountpoint, cache);
+			free(mountpoint);
+			return match;
+		}
+	}
+
+	return 0;
+}
+
 /* filter function for libmount (mnt_table_find_next_fs()) */
 static int match_func(struct libmnt_fs *fs,
 		      void *data __attribute__ ((__unused__)))
@@ -670,11 +694,11 @@ static int match_func(struct libmnt_fs *fs,
 	void *md;
 
 	m = get_match(COL_TARGET);
-	if (m && !mnt_fs_match_target(fs, m, cache))
+	if (m && !match_by_file(m, fs, mnt_fs_match_target))
 		return rc;
 
 	m = get_match(COL_SOURCE);
-	if (m && !mnt_fs_match_source(fs, m, cache))
+	if (m && !match_by_file(m, fs, mnt_fs_match_source))
 		return rc;
 
 	m = get_match(COL_FSTYPE);
Signature · 1 line
1.7.10
maintainer
#3 lore raw

Re: [PATCH 2/2] findmnt: add match_by_file to do within-device matching

Quoted 5 lines · Dave Reisner
Quoted 4 lines · Dave Reisner

Use the newly exported mnt_get_mountpoint to determine the device that a given file resides on, in case the supplied source or target is not explicitly a mount point.

Quoted 7 lines
Quoted 4 lines

http://www.spinics.net/lists/util-linux-ng/msg06081.html

---

On second thought, this isn't good -- it'll return results even when you're searching with --fstab (which is completely undesirable, imo).

I'll fix it, that's easy. Thanks.

Quoted 8 lines

misc-utils/findmnt.c static int match_func(struct libmnt_fs *fs, · line 701

Quoted 8 lines
misc-utils/findmnt.c +2 -2
 	m = get_match(COL_TARGET);
-	if (m && !mnt_fs_match_target(fs, m, cache))
+	if (m && !match_by_file(m, fs, mnt_fs_match_target))
 		return rc;
 
 	m = get_match(COL_SOURCE);
-	if (m && !mnt_fs_match_source(fs, m, cache))
+	if (m && !match_by_file(m, fs, mnt_fs_match_source))

why we need this functionality for SOURCE too?

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#4 lore raw

Re: [PATCH 2/2] findmnt: add match_by_file to do within-device matching

Quoted 4 lines · Karel Zak
Quoted 2 lines · Dave Reisner
Quoted 1 line · Dave Reisner

Use the newly exported mnt_get_mountpoint to determine the device

that a

Quoted 18 lines
Quoted 10 lines
Quoted 7 lines

given file resides on, in case the supplied source or target is not explicitly a mount point.

http://www.spinics.net/lists/util-linux-ng/msg06081.html

---

On second thought, this isn't good -- it'll return results even when you're searching with --fstab (which is completely undesirable, imo).

I'll fix it, that's easy. Thanks.

Quoted 5 lines
Quoted 5 lines
    m = get_match(COL_TARGET);
misc-utils/findmnt.c +1 -1
-   if (m && !mnt_fs_match_target(fs, m, cache))
+   if (m && !match_by_file(m, fs, mnt_fs_match_target))
            return rc;
Quoted 5 lines
Quoted 3 lines

misc-utils/findmnt.c static int match_func(struct libmnt_fs *fs, · line 701

Quoted 3 lines
    m = get_match(COL_SOURCE);
misc-utils/findmnt.c +1 -1
-   if (m && !mnt_fs_match_source(fs, m, cache))
+   if (m && !match_by_file(m, fs, mnt_fs_match_source))

why we need this functionality for SOURCE too?

Ah. Silly of me. We definitely don't need this.

Quoted 10 lines

Karel

--
Karel Zak <[email protected]>
http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

maintainer
#5 lore raw

Re: [PATCH 2/2] findmnt: add match_by_file to do within-device matching

In reply to Dave Reisner
Quoted 5 lines · Dave Reisner
Quoted 4 lines · Dave Reisner

Use the newly exported mnt_get_mountpoint to determine the device that a given file resides on, in case the supplied source or target is not explicitly a mount point.

Quoted 7 lines
Quoted 4 lines

http://www.spinics.net/lists/util-linux-ng/msg06081.html

---

On second thought, this isn't good -- it'll return results even when you're searching with --fstab (which is completely undesirable, imo).

I have restricted the functionality ;-(

- search with --kernel (this is default)

- target has to be explicitly defined by --target option or if the source is explicitly defined by MAJ:MIN, UUID=, ...

This restriction is required because findmnt(8) (like mount(8)) allows

findmnt <device>|<mountpoint>

and for example without this restriction, command

findmnt /dev/sdb1

returns info about /dev tmpfs (because mnt_get_mountpoint() returns "/dev" as a mountpoint for path "/dev/sdb1" ;-)

Note that source for mount(2) syscall could be whatever, including regular file, directory or any random string. All depends on filesystem driver(s). It means that we cannot make any decision about findmnt arguments if --target or --source are not explicitly given.

Examples:

$ findmnt /home/kzak/Pictures/
[nothing]
$ findmnt --target /home/kzak/Pictures
TARGET     SOURCE                                                FSTYPE OPTIONS
/home/kzak /dev/mapper/luks-10d813de-fa82-4f67-a86c-23d5d0e7c30e ext4  rw,relat

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com