diff --git a/src/util/virfile.c b/src/util/virfile.c index f99e7f95e1..5d6f14ba7e 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -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; } diff --git a/src/util/virfile.h b/src/util/virfile.h index 34184b32aa..b04386f6e6 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -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, diff --git a/src/util/virxml.c b/src/util/virxml.c index a55eb9629b..268aad1d20 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -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; }