]> xenbits.xensource.com Git - libvirt.git/commitdiff
Add virFileIsMountPoint function
authorDaniel P. Berrange <berrange@redhat.com>
Mon, 7 Oct 2013 11:51:58 +0000 (12:51 +0100)
committerDaniel P. Berrange <berrange@redhat.com>
Tue, 5 Nov 2013 07:51:47 +0000 (15:51 +0800)
Add a function for efficiently checking if a path is a filesystem
mount point.

NB will not work for bind mounts, only true filesystem mounts.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index e7b9a59c0eaa27a271db2879a253ce5407e09354..ad8a79d7dc23d3cbe05bfdf00acea185e8ee106a 100644 (file)
@@ -1196,6 +1196,7 @@ virFileIsAbsPath;
 virFileIsDir;
 virFileIsExecutable;
 virFileIsLink;
+virFileIsMountPoint;
 virFileLinkPointsTo;
 virFileLock;
 virFileLoopDeviceAssociate;
index 3a9980cf4e4e4c7c2fc68402404f5e01983e34c5..4882006d98a092271ef1fdbc3c2ea9dc7f0b0121 100644 (file)
@@ -1518,6 +1518,58 @@ virFileIsExecutable(const char *file)
     return false;
 }
 
+
+/*
+ * Check that a file refers to a mount point. Trick is that for
+ * a mount point, the st_dev field will differ from the parent
+ * directory.
+ *
+ * Note that this will not detect bind mounts of dirs/files,
+ * only true filesystem mounts.
+ */
+int virFileIsMountPoint(const char *file)
+{
+    char *parent = NULL;
+    int ret = -1;
+    struct stat sb1, sb2;
+
+    if (!(parent = mdir_name(file))) {
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    VIR_DEBUG("Comparing '%s' to '%s'", file, parent);
+
+    if (stat(file, &sb1) < 0) {
+        if (errno == ENOENT)
+            ret = 0;
+        else
+            virReportSystemError(errno,
+                                 _("Cannot stat '%s'"),
+                                 file);
+        goto cleanup;
+    }
+
+    if (stat(parent, &sb2) < 0) {
+        virReportSystemError(errno,
+                             _("Cannot stat '%s'"),
+                             parent);
+        goto cleanup;
+    }
+
+    if (!S_ISDIR(sb1.st_mode)) {
+        ret = 0;
+        goto cleanup;
+    }
+
+    ret = sb1.st_dev != sb2.st_dev;
+    VIR_DEBUG("Is mount %d", ret);
+
+ cleanup:
+    VIR_FREE(parent);
+    return ret;
+}
+
 #ifndef WIN32
 /* Check that a file is accessible under certain
  * user & gid.
index 72d35ce4ebe9b76d9ddc489838c9f98b291119bb..ff84719ccfcba57e0bc7ea14d44b5bf4e1fe8748 100644 (file)
@@ -156,6 +156,8 @@ bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1);
 bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1);
 bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
 
+int virFileIsMountPoint(const char *file) ATTRIBUTE_NONNULL(1);
+
 char *virFileSanitizePath(const char *path);
 
 enum {