From nobody Fri May 16 04:00:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15016242967221002.6522838011917; Tue, 1 Aug 2017 14:51:36 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8D08FE977F; Tue, 1 Aug 2017 21:51:34 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 300DA61B8B; Tue, 1 Aug 2017 21:51:34 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 899E74BB79; Tue, 1 Aug 2017 21:51:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkWv9007911 for ; Tue, 1 Aug 2017 17:46:32 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9D25218174; Tue, 1 Aug 2017 21:46:32 +0000 (UTC) Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 95BB018205 for ; Tue, 1 Aug 2017 21:46:32 +0000 (UTC) Received: from mail-qt0-f196.google.com (mail-qt0-f196.google.com [209.85.216.196]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F3D49159526 for ; Tue, 1 Aug 2017 21:46:24 +0000 (UTC) Received: by mail-qt0-f196.google.com with SMTP id t37so2931332qtg.2 for ; Tue, 01 Aug 2017 14:46:24 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.22 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:22 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 8D08FE977F Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 8D08FE977F Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aM0g0j7U" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F3D49159526 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com F3D49159526 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=wNL+lS+AcvkOsSdWms0l5HfnhkogvuB2kaWcygSaNok=; b=aM0g0j7UmkOrdIjt5Ed4ZOK2UUEmYzBH0el2PBw2xX3d0ljPbsFfcjHdNquzjm7L5C UmkWOag6BNHlYQ0CG/j6nO39d7kRnOIILODpWzt1dIn4BkdRXut7wPlxZUNRMIUWwARe 3KR5iEg7xfpL0Z4D4JXgth8wVfw04gNBzPt+6vdnDb8VVgDQgvV/A5tJcZGzOYyB9LnZ c+lEmmmWA4Fqesa8NGUvDei/BP90wnYZhrSmMx2o2G9X4bmJcmVM6a28my0L2LSwd2bJ JkIJXxU6ssyJnEL5EpE09UnqgbWU2CmSf+Efm1mT8iGG9doa4uUXDe2KtHm+enVD0uHn AJfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=wNL+lS+AcvkOsSdWms0l5HfnhkogvuB2kaWcygSaNok=; b=TpxtnYH5L63IAiNR289yuUaHyjeFfdPGS9YZq9Ko3f8A4vSPpA9hCXhL0fKhP9c1iV cjw3J10R9MC+axXBpQ8HMt4SPmPxVShxRQBjY9fMCJXKzGZqowcC8qyqZsv045LOkaYr NMUleqZC+GikwnGC1GKRxtQoDmZF3CBZzEGzZgNJiw9dOim0sKy35dNvyY6ehXd1oZYe Eig7pa2g+lvURqzVQPgkBjQt21ufXZBAeICzmpOGDSdDD0JvWVfingc3U5c9e7YG6NOU kKmv8BRcAA/ZR1AOTucAZEvp9e2z1sIaJbC+Onaqd6xFQ0LIEwIBBcR95IJEiLHTuf/L aICA== X-Gm-Message-State: AIVw110X4EhA2t0x9PWQOmBff4SM/EIjSr2LJICqaCiPUxcQzGZ/LBR9 5SAyXh2Jr6xxQBSWmEQ= X-Received: by 10.200.34.162 with SMTP id f31mr27206567qta.132.1501623983602; Tue, 01 Aug 2017 14:46:23 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:11 -0400 Message-Id: <20170801214614.8915-11-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:25 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 01 Aug 2017 21:46:25 +0000 (UTC) for IP:'209.85.216.196' DOMAIN:'mail-qt0-f196.google.com' HELO:'mail-qt0-f196.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.216.196 mail-qt0-f196.google.com 209.85.216.196 mail-qt0-f196.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 10/13] Split up the bindings for libvirt snapshot API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 01 Aug 2017 21:51:35 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 1 + src/libvirt-php.c | 257 +--------------------------------------------= ---- src/libvirt-php.h | 18 +--- src/libvirt-snapshot.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-snapshot.h | 58 +++++++++++ 5 files changed, 305 insertions(+), 272 deletions(-) create mode 100644 src/libvirt-snapshot.c create mode 100644 src/libvirt-snapshot.h diff --git a/src/Makefile.am b/src/Makefile.am index 32b23cf..1b78011 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -26,6 +26,7 @@ libvirt_php_la_SOURCES =3D \ libvirt-node.c libvirt-node.h \ libvirt-stream.c libvirt-stream.h \ libvirt-domain.c libvirt-domain.h \ + libvirt-snapshot.c libvirt-snapshot.h \ libvirt-network.c libvirt-network.h \ libvirt-storage.c libvirt-storage.h libvirt_php_la_CFLAGS =3D \ diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 1f0d058..8814e54 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -24,6 +24,7 @@ #include "libvirt-node.h" #include "libvirt-stream.h" #include "libvirt-domain.h" +#include "libvirt-snapshot.h" #include "libvirt-network.h" #include "libvirt-storage.h" =20 @@ -40,7 +41,6 @@ const char *features_binaries[] =3D { NULL }; =20 /* ZEND thread safe per request globals definition */ int le_libvirt_nodedev; -int le_libvirt_snapshot; int le_libvirt_nwfilter; =20 ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) @@ -477,13 +477,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE_LIBVIRT_CONNECTION PHP_FE_LIBVIRT_STREAM PHP_FE_LIBVIRT_DOMAIN - /* Domain snapshot functions */ - PHP_FE(libvirt_domain_has_current_snapshot, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_create, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_get_xml, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_revert, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_delete, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_domain_snapshot_lookup_by_name, arginfo_libvirt_domain_= snapshot_lookup_by_name) + PHP_FE_LIBVIRT_SNAPSHOT PHP_FE_LIBVIRT_STORAGE PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE @@ -503,7 +497,6 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uu= id) PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) /* List functions */ - PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) @@ -1213,33 +1206,6 @@ static void php_libvirt_nodedev_dtor(virt_resource *= rsrc TSRMLS_DC) } } =20 -/* Destructor for snapshot resource */ -static void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_snapshot *snapshot =3D (php_libvirt_snapshot *)rsrc->ptr; - int rv =3D 0; - - if (snapshot !=3D NULL) { - if (snapshot->snapshot !=3D NULL) { - if (!check_resource_allocation(NULL, INT_RESOURCE_SNAPSHOT, sn= apshot->snapshot TSRMLS_CC)) { - snapshot->snapshot =3D NULL; - efree(snapshot); - return; - } - rv =3D virDomainSnapshotFree(snapshot->snapshot); - if (rv !=3D 0) { - DPRINTF("%s: virDomainSnapshotFree(%p) returned %d\n", __F= UNCTION__, snapshot->snapshot, rv); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnap= shotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virDomainSnapshotFree(%p) completed successfu= lly\n", __FUNCTION__, snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, snapshot->d= omain->conn->conn, snapshot->snapshot, 0 TSRMLS_CC); - } - snapshot->snapshot =3D NULL; - } - efree(snapshot); - } -} - /* Destructor for nwfilter resource */ static void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1606,19 +1572,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) RETURN_FALSE; = \ } while (0) =20 -#define GET_SNAPSHOT_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(snapshot, php_libvirt_snapshot*, &zsnapshot, P= HP_LIBVIRT_SNAPSHOT_RES_NAME, le_libvirt_snapshot);\ - if ((snapshot =3D=3D NULL) || (snapshot->snapshot =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_NWFILTER_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -2606,212 +2559,6 @@ void parse_array(zval *arr, tVMDisk *disk, tVMNetwo= rk *network) } VIRT_FOREACH_END(); } =20 -/* - * Function name: libvirt_domain_has_current_snapshot - * Since version: 0.4.1(-2) - * Description: Function is used to get the information whether domain= has the current snapshot - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) - */ -PHP_FUNCTION(libvirt_domain_has_current_snapshot) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int retval; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); - if (retval <=3D 0) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_lookup_by_name - * Since version: 0.4.1(-2) - * Description: This functions is used to lookup for the snapshot by i= t's name - * Arguments: @res [resource]: libvirt domain resource - * @name [string]: name of the snapshot to get the resour= ce - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - strsize_t name_len; - char *name =3D NULL; - zend_long flags =3D 0; - php_libvirt_snapshot *res_snapshot; - virDomainSnapshotPtr snapshot =3D NULL; - - GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); - - if ((name =3D=3D NULL) || (name_len < 1)) - RETURN_FALSE; - snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_create - * Since version: 0.4.1(-2) - * Description: This function creates the domain snapshot for the doma= in identified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: domain snapshot resource - */ -PHP_FUNCTION(libvirt_domain_snapshot_create) -{ - php_libvirt_domain *domain =3D NULL; - php_libvirt_snapshot *res_snapshot; - zval *zdomain; - virDomainSnapshotPtr snapshot =3D NULL; - zend_long flags =3D 0; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); - DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); - if (snapshot =3D=3D NULL) - RETURN_FALSE; - - res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); - res_snapshot->domain =3D domain; - res_snapshot->snapshot =3D snapshot; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); - resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); -} - -/* - * Function name: libvirt_domain_snapshot_get_xml - * Since version: 0.4.1(-2) - * Description: Function is used to get the XML description of the sna= pshot identified by it's resource - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: XML description string for the snapshot - */ -PHP_FUNCTION(libvirt_domain_snapshot_get_xml) -{ - char *xml; - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); - if (xml =3D=3D NULL) - RETURN_FALSE; - - VIRT_RETVAL_STRING(xml); - free(xml); -} - -/* - * Function name: libvirt_domain_snapshot_revert - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: libvirt snapshot flags - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_revert) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - int ret; - zend_long flags =3D 0; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); - DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); - if (ret =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_domain_snapshot_delete - * Since version: 0.4.1(-2) - * Description: Function is used to revert the domain state to the sta= te identified by the snapshot - * Arguments: @res [resource]: libvirt snapshot resource - * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_domain_snapshot_delete) -{ - zval *zsnapshot; - php_libvirt_snapshot *snapshot; - zend_long flags =3D 0; - int retval; - - GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); - - retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); - DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); - if (retval =3D=3D -1) - RETURN_FALSE; - RETURN_TRUE; -} - -/* - * Function name: libvirt_list_domain_snapshots - * Since version: 0.4.1(-2) - * Description: Function is used to list domain snapshots for the doma= in specified by it's resource - * Arguments: @res [resource]: libvirt domain resource - * @flags [int]: libvirt snapshot flags - * Returns: libvirt domain snapshot names array - */ -PHP_FUNCTION(libvirt_list_domain_snapshots) -{ - php_libvirt_domain *domain =3D NULL; - zval *zdomain; - int count =3D -1; - int expectedcount =3D -1; - char **names; - zend_long flags =3D 0; - int i; - - GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); - - expectedcount =3D virDomainSnapshotNum(domain->domain, flags); - DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); - - if (expectedcount !=3D -1) { - names =3D (char **)emalloc(expectedcount * sizeof(char *)); - count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); - } - - if ((count !=3D expectedcount) || (count < 0)) { - RETURN_FALSE; - } else { - array_init(return_value); - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - } - efree(names); -} - /* Listing functions */ /* * Function name: libvirt_list_nodedevs diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 1505878..ca10d23 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -129,7 +129,6 @@ typedef uint64_t arch_uint; =20 /* Internal resource identifier objects */ #define INT_RESOURCE_NODEDEV 0x08 -#define INT_RESOURCE_SNAPSHOT 0x40 #define INT_RESOURCE_NWFILTER 0x60 =20 typedef struct tTokenizer { @@ -159,16 +158,10 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; =20 -/* TODO: temporary forward declarations until other parts are "modularized= " */ +/* TODO: temporary forward declaration until other parts are "modularized"= */ typedef struct _php_libvirt_connection php_libvirt_connection; -typedef struct _php_libvirt_domain php_libvirt_domain; =20 /* Libvirt-php types */ -typedef struct _php_libvirt_snapshot { - virDomainSnapshotPtr snapshot; - php_libvirt_domain* domain; -} php_libvirt_snapshot; - typedef struct _php_libvirt_nodedev { virNodeDevicePtr device; php_libvirt_connection* conn; @@ -232,7 +225,6 @@ long get_next_free_numeric_value(virDomainPtr domain, c= har *xpath); int get_subnet_bits(char *ip); =20 #define PHP_LIBVIRT_NODEDEV_RES_NAME "Libvirt node device" -#define PHP_LIBVIRT_SNAPSHOT_RES_NAME "Libvirt domain snapshot" #define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" =20 PHP_MINIT_FUNCTION(libvirt); @@ -243,13 +235,6 @@ PHP_MINFO_FUNCTION(libvirt); =20 /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* Domain snapshot functions */ -PHP_FUNCTION(libvirt_domain_has_current_snapshot); -PHP_FUNCTION(libvirt_domain_snapshot_create); -PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name); -PHP_FUNCTION(libvirt_domain_snapshot_get_xml); -PHP_FUNCTION(libvirt_domain_snapshot_revert); -PHP_FUNCTION(libvirt_domain_snapshot_delete); /* Nodedev functions */ PHP_FUNCTION(libvirt_nodedev_get); PHP_FUNCTION(libvirt_nodedev_capabilities); @@ -269,7 +254,6 @@ PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid); PHP_FUNCTION(libvirt_list_nodedevs); PHP_FUNCTION(libvirt_list_all_nwfilters); PHP_FUNCTION(libvirt_list_nwfilters); -PHP_FUNCTION(libvirt_list_domain_snapshots); /* Common functions */ PHP_FUNCTION(libvirt_version); PHP_FUNCTION(libvirt_check_version); diff --git a/src/libvirt-snapshot.c b/src/libvirt-snapshot.c new file mode 100644 index 0000000..c4c8d81 --- /dev/null +++ b/src/libvirt-snapshot.c @@ -0,0 +1,243 @@ +/* + * libvirt-snapshot.c: The PHP bindings to libvirt domain snapshot API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-snapshot.h" + +DEBUG_INIT("snapshot"); + +void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_snapshot *snapshot =3D (php_libvirt_snapshot *)rsrc->ptr; + int rv =3D 0; + + if (snapshot !=3D NULL) { + if (snapshot->snapshot !=3D NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_SNAPSHOT, sn= apshot->snapshot TSRMLS_CC)) { + snapshot->snapshot =3D NULL; + efree(snapshot); + return; + } + rv =3D virDomainSnapshotFree(snapshot->snapshot); + if (rv !=3D 0) { + DPRINTF("%s: virDomainSnapshotFree(%p) returned %d\n", __F= UNCTION__, snapshot->snapshot, rv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virDomainSnap= shotFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virDomainSnapshotFree(%p) completed successfu= lly\n", __FUNCTION__, snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, snapshot->d= omain->conn->conn, snapshot->snapshot, 0 TSRMLS_CC); + } + snapshot->snapshot =3D NULL; + } + efree(snapshot); + } +} + +/* + * Function name: libvirt_domain_has_current_snapshot + * Since version: 0.4.1(-2) + * Description: Function is used to get the information whether domain= has the current snapshot + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: libvirt snapshot flags + * Returns: TRUE is domain has the current snapshot, otherwise FAL= SE (you may need to check for error using libvirt_get_last_error()) + */ +PHP_FUNCTION(libvirt_domain_has_current_snapshot) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int retval; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + retval =3D virDomainHasCurrentSnapshot(domain->domain, flags); + if (retval <=3D 0) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_snapshot_lookup_by_name + * Since version: 0.4.1(-2) + * Description: This functions is used to lookup for the snapshot by i= t's name + * Arguments: @res [resource]: libvirt domain resource + * @name [string]: name of the snapshot to get the resour= ce + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource + */ +PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + strsize_t name_len; + char *name =3D NULL; + zend_long flags =3D 0; + php_libvirt_snapshot *res_snapshot; + virDomainSnapshotPtr snapshot =3D NULL; + + GET_DOMAIN_FROM_ARGS("rs|l", &zdomain, &name, &name_len, &flags); + + if ((name =3D=3D NULL) || (name_len < 1)) + RETURN_FALSE; + snapshot=3DvirDomainSnapshotLookupByName(domain->domain, name, flags); + if (snapshot =3D=3D NULL) + RETURN_FALSE; + + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); +} + +/* + * Function name: libvirt_domain_snapshot_create + * Since version: 0.4.1(-2) + * Description: This function creates the domain snapshot for the doma= in identified by it's resource + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: libvirt snapshot flags + * Returns: domain snapshot resource + */ +PHP_FUNCTION(libvirt_domain_snapshot_create) +{ + php_libvirt_domain *domain =3D NULL; + php_libvirt_snapshot *res_snapshot; + zval *zdomain; + virDomainSnapshotPtr snapshot =3D NULL; + zend_long flags =3D 0; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + snapshot =3D virDomainSnapshotCreateXML(domain->domain, "", flags); + DPRINTF("%s: virDomainSnapshotCreateXML(%p, ) returned %p\n", PHP= FUNC, domain->domain, snapshot); + if (snapshot =3D=3D NULL) + RETURN_FALSE; + + res_snapshot =3D (php_libvirt_snapshot *)emalloc(sizeof(php_libvirt_sn= apshot)); + res_snapshot->domain =3D domain; + res_snapshot->snapshot =3D snapshot; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_snapshot->snapshot); + resource_change_counter(INT_RESOURCE_SNAPSHOT, domain->conn->conn, res= _snapshot->snapshot, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_snapshot, le_libvirt_snapshot); +} + +/* + * Function name: libvirt_domain_snapshot_get_xml + * Since version: 0.4.1(-2) + * Description: Function is used to get the XML description of the sna= pshot identified by it's resource + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags + * Returns: XML description string for the snapshot + */ +PHP_FUNCTION(libvirt_domain_snapshot_get_xml) +{ + char *xml; + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + zend_long flags =3D 0; + + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); + + xml =3D virDomainSnapshotGetXMLDesc(snapshot->snapshot, flags); + if (xml =3D=3D NULL) + RETURN_FALSE; + + VIRT_RETVAL_STRING(xml); + free(xml); +} + +/* + * Function name: libvirt_domain_snapshot_revert + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: libvirt snapshot flags + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_snapshot_revert) +{ + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + int ret; + zend_long flags =3D 0; + + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); + + ret =3D virDomainRevertToSnapshot(snapshot->snapshot, flags); + DPRINTF("%s: virDomainRevertToSnapshot(%p, 0) returned %d\n", PHPFUNC,= snapshot->snapshot, ret); + if (ret =3D=3D -1) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_domain_snapshot_delete + * Since version: 0.4.1(-2) + * Description: Function is used to revert the domain state to the sta= te identified by the snapshot + * Arguments: @res [resource]: libvirt snapshot resource + * @flags [int]: 0 to delete just snapshot, VIR_SNAPSHOT_= DELETE_CHILDREN to delete snapshot children as well + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_domain_snapshot_delete) +{ + zval *zsnapshot; + php_libvirt_snapshot *snapshot; + zend_long flags =3D 0; + int retval; + + GET_SNAPSHOT_FROM_ARGS("r|l", &zsnapshot, &flags); + + retval =3D virDomainSnapshotDelete(snapshot->snapshot, flags); + DPRINTF("%s: virDomainSnapshotDelete(%p, %d) returned %d\n", PHPFUNC, = snapshot->snapshot, (int) flags, retval); + if (retval =3D=3D -1) + RETURN_FALSE; + RETURN_TRUE; +} + +/* + * Function name: libvirt_list_domain_snapshots + * Since version: 0.4.1(-2) + * Description: Function is used to list domain snapshots for the doma= in specified by it's resource + * Arguments: @res [resource]: libvirt domain resource + * @flags [int]: libvirt snapshot flags + * Returns: libvirt domain snapshot names array + */ +PHP_FUNCTION(libvirt_list_domain_snapshots) +{ + php_libvirt_domain *domain =3D NULL; + zval *zdomain; + int count =3D -1; + int expectedcount =3D -1; + char **names; + zend_long flags =3D 0; + int i; + + GET_DOMAIN_FROM_ARGS("r|l", &zdomain, &flags); + + expectedcount =3D virDomainSnapshotNum(domain->domain, flags); + DPRINTF("%s: virDomainSnapshotNum(%p, 0) returned %d\n", PHPFUNC, doma= in->domain, expectedcount); + + if (expectedcount !=3D -1) { + names =3D (char **)emalloc(expectedcount * sizeof(char *)); + count =3D virDomainSnapshotListNames(domain->domain, names, expect= edcount, 0); + } + + if ((count !=3D expectedcount) || (count < 0)) { + RETURN_FALSE; + } else { + array_init(return_value); + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + } + efree(names); +} diff --git a/src/libvirt-snapshot.h b/src/libvirt-snapshot.h new file mode 100644 index 0000000..e6092aa --- /dev/null +++ b/src/libvirt-snapshot.h @@ -0,0 +1,58 @@ +/* + * libvirt-snapshot.h: The PHP bindings to libvirt domain snapshot API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_SNAPSHOT_H__ +# define __LIBVIRT_SNAPSHOT_H__ + +# include "libvirt-domain.h" + +# define PHP_LIBVIRT_SNAPSHOT_RES_NAME "Libvirt domain snapshot" +# define INT_RESOURCE_SNAPSHOT 0x40 + +# define PHP_FE_LIBVIRT_SNAPSHOT = \ + PHP_FE(libvirt_domain_has_current_snapshot, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_lookup_by_name, arginfo_libvirt_domain_= snapshot_lookup_by_name) \ + PHP_FE(libvirt_domain_snapshot_create, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_get_xml, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_revert, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_domain_snapshot_delete, arginfo_libvirt_conn_op= tflags) \ + PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_op= tflags) + +# define GET_SNAPSHOT_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(snapshot, php_libvirt_snapshot*, &zsnapshot, = \ + PHP_LIBVIRT_SNAPSHOT_RES_NAME, = \ + le_libvirt_snapshot); = \ + if (snapshot =3D=3D NULL || snapshot->snapshot =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) = \ + +int le_libvirt_snapshot; + +typedef struct _php_libvirt_snapshot { + virDomainSnapshotPtr snapshot; + php_libvirt_domain* domain; +} php_libvirt_snapshot; + +void php_libvirt_snapshot_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_domain_has_current_snapshot); +PHP_FUNCTION(libvirt_domain_snapshot_lookup_by_name); +PHP_FUNCTION(libvirt_domain_snapshot_create); +PHP_FUNCTION(libvirt_domain_snapshot_get_xml); +PHP_FUNCTION(libvirt_domain_snapshot_revert); +PHP_FUNCTION(libvirt_domain_snapshot_delete); +PHP_FUNCTION(libvirt_list_domain_snapshots); + +#endif --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list