[libvirt] [PATCH 09/12] storage: Cleanup failures in virStorageBackendCreateRaw

John Ferlan jferlan at redhat.com
Fri Oct 9 13:34:08 UTC 2015


If the volume target path doesn't exist prior to calling virFileOpenAs and
we have a successful call, then we know we've had a successful creation.
For any other failures in the function we should clean up after ourselves
by using virFileDelete if we're about to return failure.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/storage/storage_backend.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c
index 7d0de63..32f85ac 100644
--- a/src/storage/storage_backend.c
+++ b/src/storage/storage_backend.c
@@ -485,6 +485,8 @@ virStorageBackendCreateRaw(virConnectPtr conn ATTRIBUTE_UNUSED,
     int operation_flags;
     bool reflink_copy = false;
     mode_t open_mode = VIR_STORAGE_DEFAULT_VOL_PERM_MODE;
+    bool exists = false;
+    bool created = false;
 
     virCheckFlags(VIR_STORAGE_VOL_CREATE_PREALLOC_METADATA |
                   VIR_STORAGE_VOL_CREATE_REFLINK,
@@ -520,6 +522,8 @@ virStorageBackendCreateRaw(virConnectPtr conn ATTRIBUTE_UNUSED,
     if (vol->target.perms->mode != (mode_t) -1)
         open_mode = vol->target.perms->mode;
 
+    if (virFileExists(vol->target.path))
+        exists = true;
     if ((fd = virFileOpenAs(vol->target.path,
                             O_RDWR | O_CREAT | O_EXCL,
                             open_mode,
@@ -531,6 +535,8 @@ virStorageBackendCreateRaw(virConnectPtr conn ATTRIBUTE_UNUSED,
                              vol->target.path);
         goto cleanup;
     }
+    if (!exists)
+        created = true;
 
     if (vol->target.nocow) {
 #ifdef __linux__
@@ -557,6 +563,10 @@ virStorageBackendCreateRaw(virConnectPtr conn ATTRIBUTE_UNUSED,
         ret = -1;
 
  cleanup:
+    if (ret < 0 && created)
+        ignore_value(virFileRemove(vol->target.path,
+                                   vol->target.perms->uid,
+                                   vol->target.perms->gid));
     VIR_FORCE_CLOSE(fd);
     return ret;
 }
-- 
2.1.0




More information about the libvir-list mailing list