]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virFileRewrite: Move error reporting into callback
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 9 Feb 2022 12:43:19 +0000 (13:43 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 11 Feb 2022 12:57:44 +0000 (13:57 +0100)
When rewriting a file using virFileRewrite() and error occurs
while writing into a temporary file it's actually the callback
that can report the most accurate error. Move error reporting
into very few callback we have currently. Those callbacks are
trivial so the benefit of this change is not obvious, but this
will change shortly when slightly more complicated callback is
introduced.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/util/virfile.c
src/util/virfile.h
src/util/virxml.c

index f99e7f95e15c76baa58b2622bf782b6e16effd62..5d6f14ba7e4acb113ce00b15c4c57926dad66aea 100644 (file)
@@ -497,7 +497,8 @@ int virFileUnlock(int fd G_GNUC_UNUSED,
  * temporary file on a side and renaming it to the desired name.
  * The temporary file is created using supplied @mode and
  * @uid:@gid (pass -1 for current uid/gid) and written by
- * @rewrite callback.
+ * @rewrite callback. It's callback's responsibility to report
+ * errors.
  *
  * Returns: 0 on success,
  *         -1 otherwise (with error reported)
@@ -512,6 +513,7 @@ virFileRewrite(const char *path,
     g_autofree char *newfile = NULL;
     int fd = -1;
     int ret = -1;
+    int rc;
 
     newfile = g_strdup_printf("%s.new", path);
 
@@ -524,9 +526,7 @@ virFileRewrite(const char *path,
         goto cleanup;
     }
 
-    if (rewrite(fd, opaque) < 0) {
-        virReportSystemError(errno, _("cannot write data to file '%s'"),
-                             newfile);
+    if ((rc = rewrite(fd, newfile, opaque)) < 0) {
         goto cleanup;
     }
 
@@ -558,12 +558,18 @@ virFileRewrite(const char *path,
 
 
 static int
-virFileRewriteStrHelper(int fd, const void *opaque)
+virFileRewriteStrHelper(int fd,
+                        const char *path,
+                        const void *opaque)
 {
     const char *data = opaque;
 
-    if (safewrite(fd, data, strlen(data)) < 0)
+    if (safewrite(fd, data, strlen(data)) < 0) {
+        virReportSystemError(errno,
+                             _("cannot write data to file '%s'"),
+                             path);
         return -1;
+    }
 
     return 0;
 }
index 34184b32aa057e227650477613cada4af110cd2e..b04386f6e67236457ed4be578af314e3273b9497 100644 (file)
@@ -123,7 +123,9 @@ int virFileLock(int fd, bool shared, off_t start, off_t len, bool waitForLock)
 int virFileUnlock(int fd, off_t start, off_t len)
     G_GNUC_NO_INLINE;
 
-typedef int (*virFileRewriteFunc)(int fd, const void *opaque);
+typedef int (*virFileRewriteFunc)(int fd,
+                                  const char *path,
+                                  const void *opaque);
 int virFileRewrite(const char *path,
                    mode_t mode,
                    uid_t uid, gid_t gid,
index a55eb9629b62388325522832ef4c8432b905df0c..268aad1d20ee125bd4a123c066a9f7ee91bbe144 100644 (file)
@@ -1172,17 +1172,26 @@ struct virXMLRewriteFileData {
 };
 
 static int
-virXMLRewriteFile(int fd, const void *opaque)
+virXMLRewriteFile(int fd,
+                  const char *path,
+                  const void *opaque)
 {
     const struct virXMLRewriteFileData *data = opaque;
 
-    if (data->warnCommand) {
-        if (virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0)
-            return -1;
+    if (data->warnCommand &&
+        virXMLEmitWarning(fd, data->warnName, data->warnCommand) < 0) {
+        virReportSystemError(errno,
+                             _("cannot write data to file '%s'"),
+                             path);
+        return -1;
     }
 
-    if (safewrite(fd, data->xml, strlen(data->xml)) < 0)
+    if (safewrite(fd, data->xml, strlen(data->xml)) < 0) {
+        virReportSystemError(errno,
+                             _("cannot write data to file '%s'"),
+                             path);
         return -1;
+    }
 
     return 0;
 }