[PATCH 3/5] qemu: implement SnapshotDeleteByName

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Refactor orgininal qemuDomainSnapshotDelete() into a common
function qemuDomainSnapshotDeleteImpl() which is used by both
APIs SnapshotDelete() and SnapshotDeleteByName()
---
 src/qemu/qemu_driver.c | 102 ++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 83 insertions(+), 19 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index c886378..152a1fd 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -12847,30 +12847,19 @@ qemuDomainSnapshotReparentChildren(void *payload,
                                                rep->cfg->snapshotDir);
 }
 
-
-static int qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot,
-                                    unsigned int flags)
+static int
+qemuDomainSnapshotDeleteImpl(virQEMUDriverPtr driver,
+                             virDomainObjPtr *vmptr,
+                             virDomainSnapshotObjPtr snap,
+                             unsigned int flags)
 {
-    virQEMUDriverPtr driver = snapshot->domain->conn->privateData;
-    virDomainObjPtr vm = NULL;
+    virDomainObjPtr vm = *vmptr;
     int ret = -1;
-    virDomainSnapshotObjPtr snap = NULL;
     virQEMUSnapRemove rem;
     virQEMUSnapReparent rep;
     bool metadata_only = !!(flags & VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY);
     int external = 0;
-    virQEMUDriverConfigPtr cfg = NULL;
-
-    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
-                  VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY |
-                  VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY, -1);
-
-    if (!(vm = qemuDomObjFromSnapshot(snapshot)))
-        return -1;
-
-    cfg = virQEMUDriverGetConfig(driver);
-    if (!(snap = qemuSnapObjFromSnapshot(vm, snapshot)))
-        goto cleanup;
+    virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
 
     if (!metadata_only) {
         if (!(flags & VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY) &&
@@ -12948,12 +12937,86 @@ endjob:
         vm = NULL;
 
 cleanup:
+    virObjectUnref(cfg);
+    return ret;
+}
+
+
+static int qemuDomainSnapshotDelete(virDomainSnapshotPtr snapshot,
+                                    unsigned int flags)
+{
+    virQEMUDriverPtr driver = snapshot->domain->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    virDomainSnapshotObjPtr snap = NULL;
+    int ret = -1;
+
+    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
+                  VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY |
+                  VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY, -1);
+
+    if (!(vm = qemuDomObjFromSnapshot(snapshot)))
+        return -1;
+
+    if (!(snap = qemuSnapObjFromSnapshot(vm, snapshot)))
+        goto cleanup;
+
+    ret = qemuDomainSnapshotDeleteImpl(driver, &vm, snap, flags);
+
+cleanup:
     if (vm)
         virObjectUnlock(vm);
-    virObjectUnref(cfg);
     return ret;
 }
 
+
+static int
+qemuDomainSnapshotDeleteByName(virDomainPtr domain,
+                               const char *name,
+                               unsigned int flags)
+{
+    virQEMUDriverPtr driver = domain->conn->privateData;
+    virDomainObjPtr vm = NULL;
+    virDomainSnapshotObjPtr snap = NULL;
+    const char *snapname = name;
+    int ret = -1;
+
+    virCheckFlags(VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN |
+                  VIR_DOMAIN_SNAPSHOT_DELETE_METADATA_ONLY |
+                  VIR_DOMAIN_SNAPSHOT_DELETE_CHILDREN_ONLY |
+                  VIR_DOMAIN_SNAPSHOT_DELETE_CURRENT, -1);
+
+    if (!(flags & VIR_DOMAIN_SNAPSHOT_DELETE_CURRENT) && !snapname) {
+        virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT, "%s",
+                       _("cannot determine snapshot object"));
+        goto cleanup;
+    }
+
+    if (!(vm = qemuDomObjFromDomain(domain)))
+        goto cleanup;
+
+    if (flags & VIR_DOMAIN_SNAPSHOT_DELETE_CURRENT) {
+        if (!vm->current_snapshot) {
+            virReportError(VIR_ERR_NO_DOMAIN_SNAPSHOT, "%s",
+                           _("the domain does not have a current snapshot"));
+            goto cleanup;
+        }
+
+        snapname = vm->current_snapshot->def->name;
+        flags &= ~VIR_DOMAIN_SNAPSHOT_DELETE_CURRENT;
+    }
+
+    if (!(snap = qemuSnapObjFromName(vm, snapname)))
+        goto cleanup;
+
+    ret = qemuDomainSnapshotDeleteImpl(driver, &vm, snap, flags);
+
+cleanup:
+    if (vm)
+        virObjectUnlock(vm);
+    return ret;
+}
+
+
 static int qemuDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
                                         char **result, unsigned int flags)
 {
@@ -15333,6 +15396,7 @@ static virDriver qemuDriver = {
     .domainSnapshotHasMetadata = qemuDomainSnapshotHasMetadata, /* 0.9.13 */
     .domainRevertToSnapshot = qemuDomainRevertToSnapshot, /* 0.8.0 */
     .domainSnapshotDelete = qemuDomainSnapshotDelete, /* 0.8.0 */
+    .domainSnapshotDeleteByName = qemuDomainSnapshotDeleteByName, /* 1.0.7 */
     .domainQemuMonitorCommand = qemuDomainQemuMonitorCommand, /* 0.8.3 */
     .domainQemuAttach = qemuDomainQemuAttach, /* 0.9.4 */
     .domainQemuAgentCommand = qemuDomainQemuAgentCommand, /* 0.10.0 */
-- 
1.8.1.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]