[PATCH 4/5] virFileRewrite: Allow callback report errors

Michal Privoznik mprivozn at redhat.com
Thu Feb 10 11:13:25 UTC 2022


Sometimes it may be handy for the callback to report error, even
though our current callbacks are trivial. Let's report an error
only if callback returns a well known value, otherwise assume it
reported error message on its own.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/util/virfile.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index f99e7f95e1..dd065a537c 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -499,6 +499,10 @@ int virFileUnlock(int fd G_GNUC_UNUSED,
  * @uid:@gid (pass -1 for current uid/gid) and written by
  * @rewrite callback.
  *
+ * A negative value returned by @rewrite callback is treated as
+ * error and if the value is different to -1 then it's the
+ * callback's responsibility to report error.
+ *
  * Returns: 0 on success,
  *         -1 otherwise (with error reported)
  */
@@ -512,6 +516,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 +529,11 @@ virFileRewrite(const char *path,
         goto cleanup;
     }
 
-    if (rewrite(fd, opaque) < 0) {
-        virReportSystemError(errno, _("cannot write data to file '%s'"),
-                             newfile);
+    if ((rc = rewrite(fd, opaque)) < 0) {
+        if (rc == -1) {
+            virReportSystemError(errno, _("cannot write data to file '%s'"),
+                                 newfile);
+        }
         goto cleanup;
     }
 
-- 
2.34.1




More information about the libvir-list mailing list