* 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)
g_autofree char *newfile = NULL;
int fd = -1;
int ret = -1;
+ int rc;
newfile = g_strdup_printf("%s.new", 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;
}
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;
}
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,
};
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;
}