now that we introduced virQEMUSaveFd, use it in the creation of a
new save image.

Signed-off-by: Claudio Fontana <cfont...@suse.de>
---
 src/qemu/qemu_saveimage.c | 54 ++++++++++++---------------------------
 1 file changed, 17 insertions(+), 37 deletions(-)

diff --git a/src/qemu/qemu_saveimage.c b/src/qemu/qemu_saveimage.c
index 67c93e3865..91fba7bd7d 100644
--- a/src/qemu/qemu_saveimage.c
+++ b/src/qemu/qemu_saveimage.c
@@ -380,41 +380,33 @@ qemuSaveImageCreate(virQEMUDriver *driver,
                     virDomainAsyncJob asyncJob)
 {
     g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
-    bool needUnlink = false;
+    virQEMUSaveFd saveFd = QEMU_SAVEFD_INVALID;
+    unsigned int oflags = O_WRONLY | O_TRUNC | O_CREAT;
     int ret = -1;
-    int fd = -1;
-    int directFlag = 0;
-    virFileWrapperFd *wrapperFd = NULL;
-    unsigned int wrapperFlags = VIR_FILE_WRAPPER_NON_BLOCKING;
 
-    /* Obtain the file handle.  */
-    if ((flags & VIR_DOMAIN_SAVE_BYPASS_CACHE)) {
-        wrapperFlags |= VIR_FILE_WRAPPER_BYPASS_CACHE;
-        directFlag = virFileDirectFdFlag();
-        if (directFlag < 0) {
+    if (flags & VIR_DOMAIN_SAVE_BYPASS_CACHE) {
+        if (virFileDirectFdFlag() < 0) {
             virReportError(VIR_ERR_OPERATION_FAILED, "%s",
                            _("bypass cache unsupported by this system"));
-            goto cleanup;
+            return -1;
         }
+        oflags |= O_DIRECT;
     }
 
-    fd = virQEMUFileOpenAs(cfg->user, cfg->group, false, path,
-                           O_WRONLY | O_TRUNC | O_CREAT | directFlag,
-                           &needUnlink);
-    if (fd < 0)
+    if (virQEMUSaveFdInit(&saveFd, path, 0, oflags, cfg) < 0) {
         goto cleanup;
-
-    if (qemuSecuritySetImageFDLabel(driver->securityManager, vm->def, fd) < 0)
+    }
+    if (qemuSecuritySetImageFDLabel(driver->securityManager, vm->def, 
saveFd.fd) < 0)
         goto cleanup;
 
-    if (!(wrapperFd = virFileWrapperFdNew(&fd, path, wrapperFlags)))
+    if (virQEMUSaveDataWrite(data, saveFd.fd, saveFd.path) < 0)
         goto cleanup;
 
-    if (virQEMUSaveDataWrite(data, fd, path) < 0)
-        goto cleanup;
 
     /* Perform the migration */
-    if (qemuMigrationSrcToFile(driver, vm, fd, compressor, asyncJob) < 0)
+    if (qemuMigrationSrcToFile(driver, vm, saveFd.fd, compressor, asyncJob) < 
0)
+        goto cleanup;
+    if (virQEMUSaveFdClose(&saveFd, vm) < 0)
         goto cleanup;
 
     /* Touch up file header to mark image complete. */
@@ -423,29 +415,17 @@ qemuSaveImageCreate(virQEMUDriver *driver,
      * up to seek backwards on wrapperFd.  The reopened fd will
      * trigger a single page of file system cache pollution, but
      * that's acceptable.  */
-    if (VIR_CLOSE(fd) < 0) {
-        virReportSystemError(errno, _("unable to close %s"), path);
-        goto cleanup;
-    }
 
-    if (qemuDomainFileWrapperFDClose(vm, wrapperFd) < 0)
-        goto cleanup;
-
-    if ((fd = qemuDomainOpenFile(cfg, vm->def, path, O_WRONLY, NULL)) < 0 ||
-        virQEMUSaveDataFinish(data, &fd, path) < 0)
+    if ((saveFd.fd = qemuDomainOpenFile(cfg, vm->def, saveFd.path, O_WRONLY, 
NULL)) < 0 ||
+        virQEMUSaveDataFinish(data, &saveFd.fd, saveFd.path) < 0)
         goto cleanup;
 
     ret = 0;
 
- cleanup:
-    VIR_FORCE_CLOSE(fd);
-    if (qemuDomainFileWrapperFDClose(vm, wrapperFd) < 0)
-        ret = -1;
-    virFileWrapperFdFree(wrapperFd);
 
-    if (ret < 0 && needUnlink)
-        unlink(path);
+ cleanup:
 
+    ret = virQEMUSaveFdFini(&saveFd, vm, ret);
     return ret;
 }
 
-- 
2.35.3

Reply via email to