]> xenbits.xensource.com Git - libvirt.git/commitdiff
storage: Extract error reporting for broken chains
authorPeter Krempa <pkrempa@redhat.com>
Mon, 16 Oct 2017 11:53:30 +0000 (13:53 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 3 Nov 2017 08:15:41 +0000 (09:15 +0100)
Simplify reporting the error if backing chain is broken for further
callers by extracting it into a separate function.

src/storage/storage_source.c
src/storage/storage_source.h

index cced5308c76178eff96348f62362c48d6a9bf998..4586ef4ad44abc7db336e4f9ce44db1626ff2e28 100644 (file)
@@ -404,6 +404,38 @@ virStorageFileChown(const virStorageSource *src,
 }
 
 
+/**
+ * virStorageFileReportBrokenChain:
+ *
+ * @errcode: errno when accessing @src
+ * @src: inaccessible file in the backing chain of @parent
+ * @parent: root virStorageSource being checked
+ *
+ * Reports the correct error message if @src is missing in the backing chain
+ * for @parent.
+ */
+void
+virStorageFileReportBrokenChain(int errcode,
+                                virStorageSourcePtr src,
+                                virStorageSourcePtr parent)
+{
+    unsigned int access_user = src->drv->uid;
+    unsigned int access_group = src->drv->gid;
+
+    if (src == parent) {
+        virReportSystemError(errcode,
+                             _("Cannot access storage file '%s' "
+                               "(as uid:%u, gid:%u)"),
+                             src->path, access_user, access_group);
+    } else {
+        virReportSystemError(errcode,
+                             _("Cannot access backing file '%s' "
+                               "of storage file '%s' (as uid:%u, gid:%u)"),
+                             src->path, parent->path, access_user, access_group);
+    }
+}
+
+
 /* Recursive workhorse for virStorageFileGetMetadata.  */
 static int
 virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
@@ -433,20 +465,7 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
         return -1;
 
     if (virStorageFileAccess(src, F_OK) < 0) {
-        if (src == parent) {
-            virReportSystemError(errno,
-                                 _("Cannot access storage file '%s' "
-                                   "(as uid:%u, gid:%u)"),
-                                 src->path, (unsigned int)uid,
-                                 (unsigned int)gid);
-        } else {
-            virReportSystemError(errno,
-                                 _("Cannot access backing file '%s' "
-                                   "of storage file '%s' (as uid:%u, gid:%u)"),
-                                 src->path, parent->path,
-                                 (unsigned int)uid, (unsigned int)gid);
-        }
-
+        virStorageFileReportBrokenChain(errno, src, parent);
         goto cleanup;
     }
 
index 320ea3cab7af558270092388e174b57b79fed1c2..0640c138edab4496d6036ff5d0ddbb50e0cbabac 100644 (file)
@@ -52,4 +52,8 @@ int virStorageFileGetMetadata(virStorageSourcePtr src,
 char *virStorageFileGetBackingStoreStr(virStorageSourcePtr src)
     ATTRIBUTE_NONNULL(1);
 
+void virStorageFileReportBrokenChain(int errcode,
+                                     virStorageSourcePtr src,
+                                     virStorageSourcePtr parent);
+
 #endif /* __VIR_STORAGE_SOURCE_H__ */