public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Dave Reisner <dreisner@archlinux.org>
To: util-linux@vger.kernel.org
Cc: Dave Reisner <dreisner@archlinux.org>
Subject: [PATCH 2/2] findmnt: add match_by_file to do within-device matching
Date: Wed, 25 Apr 2012 20:30:52 -0400	[thread overview]
Message-ID: <1335400252-17929-2-git-send-email-dreisner@archlinux.org> (raw)
In-Reply-To: <1335400252-17929-1-git-send-email-dreisner@archlinux.org>

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

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
---
 misc-utils/findmnt.c |   28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/misc-utils/findmnt.c b/misc-utils/findmnt.c
index cc71a60..9db167f 100644
--- a/misc-utils/findmnt.c
+++ b/misc-utils/findmnt.c
@@ -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);
-- 
1.7.10


  reply	other threads:[~2012-04-26  0:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-26  0:30 [PATCH 1/2] libmount: expose mnt_get_mountpoint as external API Dave Reisner
2012-04-26  0:30 ` Dave Reisner [this message]
2012-04-26  2:16   ` [PATCH 2/2] findmnt: add match_by_file to do within-device matching Dave Reisner
2012-04-26  7:46     ` Karel Zak
2012-04-26 10:45       ` Dave Reisner
2012-04-27 12:28     ` Karel Zak

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=1335400252-17929-2-git-send-email-dreisner@archlinux.org \
    --to=dreisner@archlinux.org \
    --cc=util-linux@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