mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-26 18:41:17 +00:00
virFileRewrite: Move error reporting into callback
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>
This commit is contained in:
parent
04ed251850
commit
d295c14258
@ -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;
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user