summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorQu Wenruo <quwenruo@cn.fujitsu.com>2014-02-10 15:28:28 +0800
committerChris Mason <clm@fb.com>2014-03-21 06:23:12 -0700
commit14ef4f569572ad34254186f1a503c39b735cc167 (patch)
treea14bb06a2be13be0747761f87fc3cc9b67256c4c /utils.c
parente7839bced72b27bddefe3dbc8ffbcd640edae509 (diff)
btrfs-progs: move find_mount_root to utils.[ch]
Move find_mount_root to utils.[ch] for general use. Signed-off-by: Qu Wenruo <quwenruo@cn.fuijitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <clm@fb.com>
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index acd27b80..8006e49b 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;
+}