From nobody Fri May 16 04:12:45 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 150178588940468.38662075828779; Thu, 3 Aug 2017 11:44:49 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 15CE7859FA; Thu, 3 Aug 2017 18:44:47 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BA9A961F22; Thu, 3 Aug 2017 18:44:46 +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 32EBC180B467; Thu, 3 Aug 2017 18:44:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v73IZQSt026082 for ; Thu, 3 Aug 2017 14:35:26 -0400 Received: by smtp.corp.redhat.com (Postfix) id D0F875D760; Thu, 3 Aug 2017 18:35:26 +0000 (UTC) Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C6E7D5D6A4 for ; Thu, 3 Aug 2017 18:35:26 +0000 (UTC) Received: from mail-qt0-f195.google.com (mail-qt0-f195.google.com [209.85.216.195]) (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 2430F4A703 for ; Thu, 3 Aug 2017 18:35:19 +0000 (UTC) Received: by mail-qt0-f195.google.com with SMTP id u19so2136185qtc.0 for ; Thu, 03 Aug 2017 11:35:19 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id b142sm23906990qka.37.2017.08.03.11.35.17 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 03 Aug 2017 11:35:17 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 15CE7859FA Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx02.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 15CE7859FA Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IOe4WNO2" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 2430F4A703 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 2430F4A703 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=pQ4Az4DdngOkS8RxtNjd5FQDmLqE2dQUudaXSSmj42E=; b=IOe4WNO2d1uhI0M04pVmLZJh1FZsgtT9NX4FraatqEx0Ri6TRP13mV3hi3D1wTIYJV RCmE7nmpabZ8Qc8psM8sg8gu+ObISjIZ3HkDz29TSiRF+o14Ya6V2Eh3N1+1U4NBAID1 xcqaFdiYRWcHpIDPNB2FAvJ+mZmiIq6fzuQovYl++wKcUIT7ITNlTt96+Uf2XiwOYhLj HIjFxpDfuyVUCZa0nsC2W2eMCwsPR1Heg/P6kT9dlXQgEQZ5r95HCMjhRGO3kz87bAHt GzR41keYlBkXr/G+peSjOqfkWzvVLuyezuOnuaN8BjpgW428AaS84T4EwaMozGWlkfl3 s50g== 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=pQ4Az4DdngOkS8RxtNjd5FQDmLqE2dQUudaXSSmj42E=; b=iifIGdRyS0SoDuwZribgpTtoazJqRRwZGKWkpvyrPiJXaDj3j1nnh5WmoUkgjT/2vP cpq5AoWvwu7OZaUptqnf5ieVEvAbP/OUtQiaJZpCAmCPXnMNqZ0FLBAAnXIXZkCnB1aD 8c7/mdLW8GK3X6D8e8QDJObzkm2pboaX9CaMlJUtknEP5vHLxsepzk/soYhacosH/ClM tKdcSinr/3f8Rwk502MMs8Qwc7yjzvWyaFw2oTvYIaXNtRljaSQbPqPkzWfvQHuV6Y79 TG9L2tlaOf5e1xVh7boGX6N9Xk1qGBI4HxIpynJuiOWD0bAjhSoIMMev8Y6c7r7EJzzc ZxVw== X-Gm-Message-State: AHYfb5iqGANj+cWkKgrRWez63bcvEIb/hWmHMe0VwaVYcpIFAeADQyEU ysJbDgXmxU2c//U0KeQ= X-Received: by 10.200.41.166 with SMTP id 35mr3623942qts.141.1501785317741; Thu, 03 Aug 2017 11:35:17 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Thu, 3 Aug 2017 14:35:06 -0400 Message-Id: <20170803183509.1250-9-dzrudy@gmail.com> In-Reply-To: <20170803183509.1250-1-dzrudy@gmail.com> References: <20170803183509.1250-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 03 Aug 2017 18:35:19 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 03 Aug 2017 18:35:19 +0000 (UTC) for IP:'209.85.216.195' DOMAIN:'mail-qt0-f195.google.com' HELO:'mail-qt0-f195.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.195 mail-qt0-f195.google.com 209.85.216.195 mail-qt0-f195.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH v2 08/11] 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 03 Aug 2017 18:44:48 +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 | 244 ++++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-snapshot.h | 58 +++++++++++ 5 files changed, 306 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 a4108a6..6481a4a 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -23,6 +23,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 @@ -39,7 +40,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) @@ -476,13 +476,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 @@ -502,7 +496,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) @@ -1212,33 +1205,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) { @@ -1605,19 +1571,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); = \ @@ -2605,212 +2558,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 6e1fd0c..e7a2bcf 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -109,7 +109,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 { @@ -139,16 +138,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; @@ -212,7 +205,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); @@ -223,13 +215,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); @@ -249,7 +234,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..eb2e6cd --- /dev/null +++ b/src/libvirt-snapshot.c @@ -0,0 +1,244 @@ +/* + * libvirt-snapshot.c: The PHP bindings to libvirt domain snapshot API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-php.h" +#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