[libvirt] [PATCH 09/23] util: introduce virFileDataSync

Daniel P. Berrangé berrange at redhat.com
Thu Jan 2 14:53:43 UTC 2020


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.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/libvirt_private.syms   |  1 +
 src/storage/storage_util.c |  4 ++--
 src/util/iohelper.c        |  2 +-
 src/util/virfile.c         | 11 +++++++++++
 src/util/virfile.h         |  2 ++
 5 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index d3184dbf5c..951ba7f0ca 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1976,6 +1976,7 @@ virFileChownFiles;
 virFileClose;
 virFileComparePaths;
 virFileCopyACLs;
+virFileDataSync;
 virFileDeleteTree;
 virFileDirectFdFlag;
 virFileExists;
diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c
index c1a6b44f4b..45ffd2206e 100644
--- a/src/storage/storage_util.c
+++ b/src/storage/storage_util.c
@@ -216,7 +216,7 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
         } while ((amtleft -= interval) > 0);
     }
 
-    if (fdatasync(fd) < 0) {
+    if (virFileDataSync(fd) < 0) {
         ret = -errno;
         virReportSystemError(errno, _("cannot sync data to file '%s'"),
                              vol->target.path);
@@ -2539,7 +2539,7 @@ storageBackendWipeLocal(const char *path,
         remaining -= written;
     }
 
-    if (fdatasync(fd) < 0) {
+    if (virFileDataSync(fd) < 0) {
         virReportSystemError(errno,
                              _("cannot sync data to volume with path '%s'"),
                              path);
diff --git a/src/util/iohelper.c b/src/util/iohelper.c
index a102a95abb..d864bbeaed 100644
--- a/src/util/iohelper.c
+++ b/src/util/iohelper.c
@@ -154,7 +154,7 @@ runIO(const char *path, int fd, int oflags)
     }
 
     /* Ensure all data is written */
-    if (fdatasync(fdout) < 0) {
+    if (virFileDataSync(fdout) < 0) {
         if (errno != EINVAL && errno != EROFS) {
             /* fdatasync() may fail on some special FDs, e.g. pipes */
             virReportSystemError(errno, _("unable to fsync %s"), fdoutname);
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 1784895575..c79fe86403 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -4432,3 +4432,14 @@ virFileGetXAttr(const char *path,
 
     return ret;
 }
+
+
+int
+virFileDataSync(int fd)
+{
+#if defined(__APPLE__) || defined(WIN32)
+    return g_fsync(fd);
+#else
+    return fdatasync(fd);
+#endif
+}
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 264b12c03d..c73f5252e4 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -372,3 +372,5 @@ int virFileSetXAttr(const char *path,
 int virFileRemoveXAttr(const char *path,
                        const char *name)
     G_GNUC_NO_INLINE;
+
+int virFileDataSync(int fd);
-- 
2.24.1




More information about the libvir-list mailing list