util: introduce virFileDataSync

A wrapper that calls g_fsync on Win32/macOS and fdatasync
elsewhere. g_fsync is a stronger flush than we need but it
satisfies the caller's requirements & matches the approach
gnulib takes.

Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2019-12-24 15:14:13 +00:00
parent dac174ee07
commit ea7b20a263
5 changed files with 17 additions and 3 deletions

View File

@ -1976,6 +1976,7 @@ virFileChownFiles;
virFileClose; virFileClose;
virFileComparePaths; virFileComparePaths;
virFileCopyACLs; virFileCopyACLs;
virFileDataSync;
virFileDeleteTree; virFileDeleteTree;
virFileDirectFdFlag; virFileDirectFdFlag;
virFileExists; virFileExists;

View File

@ -216,7 +216,7 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
} while ((amtleft -= interval) > 0); } while ((amtleft -= interval) > 0);
} }
if (fdatasync(fd) < 0) { if (virFileDataSync(fd) < 0) {
ret = -errno; ret = -errno;
virReportSystemError(errno, _("cannot sync data to file '%s'"), virReportSystemError(errno, _("cannot sync data to file '%s'"),
vol->target.path); vol->target.path);
@ -2539,7 +2539,7 @@ storageBackendWipeLocal(const char *path,
remaining -= written; remaining -= written;
} }
if (fdatasync(fd) < 0) { if (virFileDataSync(fd) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot sync data to volume with path '%s'"), _("cannot sync data to volume with path '%s'"),
path); path);

View File

@ -154,7 +154,7 @@ runIO(const char *path, int fd, int oflags)
} }
/* Ensure all data is written */ /* Ensure all data is written */
if (fdatasync(fdout) < 0) { if (virFileDataSync(fdout) < 0) {
if (errno != EINVAL && errno != EROFS) { if (errno != EINVAL && errno != EROFS) {
/* fdatasync() may fail on some special FDs, e.g. pipes */ /* fdatasync() may fail on some special FDs, e.g. pipes */
virReportSystemError(errno, _("unable to fsync %s"), fdoutname); virReportSystemError(errno, _("unable to fsync %s"), fdoutname);

View File

@ -4432,3 +4432,14 @@ virFileGetXAttr(const char *path,
return ret; return ret;
} }
int
virFileDataSync(int fd)
{
#if defined(__APPLE__) || defined(WIN32)
return g_fsync(fd);
#else
return fdatasync(fd);
#endif
}

View File

@ -372,3 +372,5 @@ int virFileSetXAttr(const char *path,
int virFileRemoveXAttr(const char *path, int virFileRemoveXAttr(const char *path,
const char *name) const char *name)
G_GNUC_NO_INLINE; G_GNUC_NO_INLINE;
int virFileDataSync(int fd);