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(a)redhat.com>
---
src/util/virfile.c | 18 ++++++++++++------
src/util/virfile.h | 4 +++-
src/util/virxml.c | 19 ++++++++++++++-----
3 files changed, 29 insertions(+), 12 deletions(-)
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;
}
--
2.34.1