[libvirt] [PATCH 1/2] virFileFsType: get filesystem type of a given path

Oskari Saarenmaa os at ohmu.fi
Tue Sep 10 18:57:57 UTC 2013


This can be used by storage pools to figure out which actions are available
on various paths (for example subvolumes when running on btrfs.)

Signed-off-by: Oskari Saarenmaa <os at ohmu.fi>
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virfile.h       |  1 +
 3 files changed, 49 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 35f0f1b..d0238cf 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1370,6 +1370,7 @@ virFileExists;
 virFileFclose;
 virFileFdopen;
 virFileFindMountPoint;
+virFileFsType;
 virFileHasSuffix;
 virFileIsAbsPath;
 virFileIsDir;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index feac3c9..44871d6 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1133,6 +1133,45 @@ cleanup:
     return ret;
 }
 
+/* search /proc/mounts for the filesystem type of the given path;
+ * return pointer to malloc'ed string of type if found, otherwise
+ * return NULL.
+ */
+char *
+virFileFsType(const char *path)
+{
+    FILE *f;
+    struct mntent mb;
+    char mntbuf[1024];
+    char *real = NULL, *ret = NULL;
+    size_t lookup_len, longest = 0;
+
+    if ((real = realpath(path, NULL)) == NULL)
+        return NULL;
+    lookup_len = strlen(real);
+
+    f = setmntent("/proc/mounts", "r");
+    if (!f) {
+        VIR_FREE(real);
+        return NULL;
+    }
+
+    while (getmntent_r(f, &mb, mntbuf, sizeof(mntbuf))) {
+        size_t mnt_dir_len = strlen(mb.mnt_dir);
+        if (lookup_len >= mnt_dir_len && mnt_dir_len >= longest) {
+            if (memcmp(mb.mnt_dir, real, mnt_dir_len) == 0) {
+                longest = mnt_dir_len;
+                VIR_FREE(ret);
+                ignore_value(VIR_STRDUP_QUIET(ret, mb.mnt_type));
+            }
+        }
+    }
+    endmntent(f);
+    VIR_FREE(real);
+
+    return ret;
+}
+
 #else /* defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */
 
 char *
@@ -1143,6 +1182,14 @@ virFileFindMountPoint(const char *type ATTRIBUTE_UNUSED)
     return NULL;
 }
 
+char *
+virFileFsType(const char *path)
+{
+    errno = ENOSYS;
+
+    return NULL;
+}
+
 #endif /* defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R */
 
 int
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 72d35ce..3c01247 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -221,6 +221,7 @@ int virFileOpenTty(int *ttymaster,
                    int rawmode);
 
 char *virFileFindMountPoint(const char *type);
+char *virFileFsType(const char *path);
 
 void virFileWaitForDevices(void);
 
-- 
1.8.3.1




More information about the libvir-list mailing list