Move pylibbe to openindiana category
[unleashed-userland.git] / components / openindiana / pylibbe / src / libbe_py.c
blob75641d2426d114742c4c603403af40099de54a6f
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2012 OmniTI Computer Consulting, Inc. All rights reserved.
27 #include <Python.h>
28 #include <sys/varargs.h>
29 #include <stdio.h>
30 #include <libnvpair.h>
32 #include <libbe.h>
33 //#include <libbe_priv.h>
34 boolean_t be_valid_be_name(const char *);
36 enum {
37 BE_PY_SUCCESS = 0,
38 BE_PY_ERR_APPEND = 6000,
39 BE_PY_ERR_DICT,
40 BE_PY_ERR_LIST,
41 BE_PY_ERR_NVLIST,
42 BE_PY_ERR_PARSETUPLE,
43 BE_PY_ERR_PRINT_ERR,
44 BE_PY_ERR_VAR_CONV,
45 } bePyErr;
48 * public libbe functions
51 PyObject *beCreateSnapshot(PyObject *, PyObject *);
52 PyObject *beCopy(PyObject *, PyObject *);
53 PyObject *beList(PyObject *, PyObject *);
54 PyObject *beActivate(PyObject *, PyObject *);
55 PyObject *beDestroy(PyObject *, PyObject *);
56 PyObject *beDestroySnapshot(PyObject *, PyObject *);
57 PyObject *beRename(PyObject *, PyObject *);
58 PyObject *beMount(PyObject *, PyObject *);
59 PyObject *beUnmount(PyObject *, PyObject *);
60 PyObject *bePrintErrors(PyObject *, PyObject *);
61 PyObject *beGetErrDesc(PyObject *, PyObject *);
62 char *beMapLibbePyErrorToString(int);
63 void initlibbe_py();
65 static boolean_t convertBEInfoToDictionary(be_node_list_t *be,
66 PyObject **listDict);
67 static boolean_t convertDatasetInfoToDictionary(be_dataset_list_t *ds,
68 PyObject **listDict);
69 static boolean_t convertSnapshotInfoToDictionary(be_snapshot_list_t *ss,
70 PyObject **listDict);
71 static boolean_t convertPyArgsToNvlist(nvlist_t **nvList, int numArgs, ...);
74 /* ~~~~~~~~~~~~~~~ */
75 /* Public Funtions */
76 /* ~~~~~~~~~~~~~~~ */
79 * Function: beCreateSnapshot
80 * Description: Convert Python args to nvlist pairs and
81 * call libbe:be_create_snapshot to create a
82 * snapshot of all the datasets within a BE
83 * Parameters:
84 * args - pointer to a python object containing:
85 * beName - The name of the BE to create a snapshot of
86 * snapName - The name of the snapshot to create (optional)
88 * The following public attribute values. defined by libbe.h,
89 * are used by this function:
91 * Returns a pointer to a python object and an optional snapshot name:
92 * 0, [snapName] - Success
93 * 1, [snapName] - Failure
94 * Scope:
95 * Public
97 /* ARGSUSED */
98 PyObject *
99 beCreateSnapshot(PyObject *self, PyObject *args)
101 char *beName = NULL;
102 char *snapName = NULL;
103 int ret = BE_PY_SUCCESS;
104 nvlist_t *beAttrs = NULL;
105 PyObject *retVals = NULL;
107 if (!PyArg_ParseTuple(args, "z|z", &beName, &snapName)) {
108 return (Py_BuildValue("[is]", BE_PY_ERR_PARSETUPLE, NULL));
111 if (!convertPyArgsToNvlist(&beAttrs, 4,
112 BE_ATTR_ORIG_BE_NAME, beName,
113 BE_ATTR_SNAP_NAME, snapName)) {
114 nvlist_free(beAttrs);
115 return (Py_BuildValue("[is]", BE_PY_ERR_NVLIST, NULL));
118 if (beAttrs == NULL) {
119 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
122 if ((ret = be_create_snapshot(beAttrs)) != 0) {
123 nvlist_free(beAttrs);
124 return (Py_BuildValue("[is]", ret, NULL));
126 if (snapName == NULL) {
127 if (nvlist_lookup_pairs(beAttrs, NV_FLAG_NOENTOK,
128 BE_ATTR_SNAP_NAME, DATA_TYPE_STRING, &snapName,
129 NULL) != 0) {
130 nvlist_free(beAttrs);
131 return (Py_BuildValue("[is]",
132 BE_PY_ERR_NVLIST, NULL));
134 retVals = Py_BuildValue("[is]", ret, snapName);
135 nvlist_free(beAttrs);
136 return (retVals);
138 nvlist_free(beAttrs);
140 return (Py_BuildValue("[is]", ret, NULL));
144 * Function: beCopy
145 * Description: Convert Python args to nvlist pairs and call libbe:be_copy
146 * to create a Boot Environment
147 * Parameters:
148 * args - pointer to a python object containing:
149 * trgtBeName - The name of the BE to create
150 * srcBeName - The name of the BE used to create trgtBeName (optional)
151 * rpool - The pool to create the new BE in (optional)
152 * srcSnapName - The snapshot name (optional)
153 * beNameProperties - The properties to use when creating
154 * the BE (optional)
156 * Returns a pointer to a python object. That Python object will consist of
157 * the return code and optional attributes, trgtBeName and snapshotName
158 * BE_SUCCESS, [trgtBeName], [trgtSnapName] - Success
159 * 1, [trgtBeName], [trgtSnapName] - Failure
160 * Scope:
161 * Public
163 /* ARGSUSED */
164 PyObject *
165 beCopy(PyObject *self, PyObject *args)
167 char *trgtBeName = NULL;
168 char *srcBeName = NULL;
169 char *srcSnapName = NULL;
170 char *trgtSnapName = NULL;
171 char *rpool = NULL;
172 char *beDescription = NULL;
173 Py_ssize_t pos = 0;
174 int ret = BE_PY_SUCCESS;
175 nvlist_t *beAttrs = NULL;
176 nvlist_t *beProps = NULL;
177 PyObject *beNameProperties = NULL;
178 PyObject *pkey = NULL;
179 PyObject *pvalue = NULL;
180 PyObject *retVals = NULL;
182 if (!PyArg_ParseTuple(args, "|zzzzOz", &trgtBeName, &srcBeName,
183 &srcSnapName, &rpool, &beNameProperties, &beDescription)) {
184 return (Py_BuildValue("[iss]", BE_PY_ERR_PARSETUPLE,
185 NULL, NULL));
188 if (!convertPyArgsToNvlist(&beAttrs, 10,
189 BE_ATTR_NEW_BE_NAME, trgtBeName,
190 BE_ATTR_ORIG_BE_NAME, srcBeName,
191 BE_ATTR_SNAP_NAME, srcSnapName,
192 BE_ATTR_NEW_BE_POOL, rpool,
193 BE_ATTR_NEW_BE_DESC, beDescription)) {
194 nvlist_free(beAttrs);
195 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST, NULL, NULL));
198 if (beNameProperties != NULL) {
199 if (nvlist_alloc(&beProps, NV_UNIQUE_NAME, 0) != 0) {
200 (void) printf("nvlist_alloc failed.\n");
201 nvlist_free(beAttrs);
202 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
203 NULL, NULL));
205 while (PyDict_Next(beNameProperties, &pos, &pkey, &pvalue)) {
206 if (!convertPyArgsToNvlist(&beProps, 2,
207 PyString_AsString(pkey),
208 PyString_AsString(pvalue))) {
209 nvlist_free(beProps);
210 nvlist_free(beAttrs);
211 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
212 NULL, NULL));
217 if (beProps != NULL && beAttrs != NULL &&
218 nvlist_add_nvlist(beAttrs, BE_ATTR_ZFS_PROPERTIES,
219 beProps) != 0) {
220 nvlist_free(beProps);
221 nvlist_free(beAttrs);
222 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
223 NULL, NULL));
226 if (beProps != NULL) nvlist_free(beProps);
228 if (trgtBeName == NULL) {
230 * Caller wants to get back the BE_ATTR_NEW_BE_NAME and
231 * BE_ATTR_SNAP_NAME
233 if ((ret = be_copy(beAttrs)) != BE_SUCCESS) {
234 nvlist_free(beAttrs);
235 return (Py_BuildValue("[iss]", ret, NULL, NULL));
239 * When no trgtBeName is passed to be_copy, be_copy
240 * returns an auto generated beName and snapshot name.
242 if (nvlist_lookup_string(beAttrs, BE_ATTR_NEW_BE_NAME,
243 &trgtBeName) != 0) {
244 nvlist_free(beAttrs);
245 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
246 NULL, NULL));
248 if (nvlist_lookup_string(beAttrs, BE_ATTR_SNAP_NAME,
249 &trgtSnapName) != 0) {
250 nvlist_free(beAttrs);
251 return (Py_BuildValue("[iss]", BE_PY_ERR_NVLIST,
252 NULL, NULL));
255 retVals = Py_BuildValue("[iss]", BE_PY_SUCCESS,
256 trgtBeName, trgtSnapName);
257 nvlist_free(beAttrs);
258 return (retVals);
260 } else {
261 ret = be_copy(beAttrs);
262 nvlist_free(beAttrs);
263 return (Py_BuildValue("[iss]", ret, NULL, NULL));
268 * Function: beList
269 * Description: Convert Python args to nvlist pairs and call libbe:be_list
270 * to gather information about Boot Environments
271 * Parameters:
272 * args - pointer to a python object containing:
273 * beName - The name of the BE to list (optional)
275 * Returns a pointer to a python object. That Python object will consist of
276 * the return code and a list of Dicts or NULL.
277 * BE_PY_SUCCESS, listOfDicts - Success
278 * bePyErr or be_errno_t, NULL - Failure
279 * Scope:
280 * Public
282 /* ARGSUSED */
283 PyObject *
284 beList(PyObject *self, PyObject *args)
286 char *beName = NULL;
287 int ret = BE_PY_SUCCESS;
288 be_node_list_t *list = NULL;
289 be_node_list_t *be = NULL;
290 PyObject *dict = NULL;
291 PyObject *listOfDicts = NULL;
293 if ((listOfDicts = PyList_New(0)) == NULL) {
294 ret = BE_PY_ERR_DICT;
295 listOfDicts = Py_None;
296 goto done;
299 if (!PyArg_ParseTuple(args, "|z", &beName)) {
300 ret = BE_PY_ERR_PARSETUPLE;
301 goto done;
304 if ((ret = be_list(beName, &list)) != BE_SUCCESS) {
305 goto done;
308 for (be = list; be != NULL; be = be->be_next_node) {
309 be_dataset_list_t *ds = be->be_node_datasets;
310 be_snapshot_list_t *ss = be->be_node_snapshots;
312 if ((dict = PyDict_New()) == NULL) {
313 ret = BE_PY_ERR_DICT;
314 goto done;
317 if (!convertBEInfoToDictionary(be, &dict)) {
318 /* LINTED */
319 Py_DECREF(dict);
320 ret = BE_PY_ERR_VAR_CONV;
321 goto done;
324 if (PyList_Append(listOfDicts, dict) != 0) {
325 /* LINTED */
326 Py_DECREF(dict);
327 ret = BE_PY_ERR_APPEND;
328 goto done;
331 /* LINTED */
332 Py_DECREF(dict);
334 while (ds != NULL) {
335 if ((dict = PyDict_New()) == NULL) {
336 ret = BE_PY_ERR_DICT;
337 goto done;
340 if (!convertDatasetInfoToDictionary(ds, &dict)) {
341 /* LINTED */
342 Py_DECREF(dict);
343 ret = BE_PY_ERR_VAR_CONV;
344 goto done;
347 if (PyList_Append(listOfDicts, dict) != 0) {
348 /* LINTED */
349 Py_DECREF(dict);
350 ret = BE_PY_ERR_APPEND;
351 goto done;
354 ds = ds->be_next_dataset;
356 /* LINTED */
357 Py_DECREF(dict);
361 while (ss != NULL) {
362 if ((dict = PyDict_New()) == NULL) {
363 /* LINTED */
364 Py_DECREF(dict);
365 ret = BE_PY_ERR_DICT;
366 goto done;
369 if (!convertSnapshotInfoToDictionary(ss, &dict)) {
370 /* LINTED */
371 Py_DECREF(dict);
372 ret = BE_PY_ERR_VAR_CONV;
373 goto done;
376 if (PyList_Append(listOfDicts, dict) != 0) {
377 /* LINTED */
378 Py_DECREF(dict);
379 ret = BE_PY_ERR_APPEND;
380 goto done;
383 ss = ss->be_next_snapshot;
385 /* LINTED */
386 Py_DECREF(dict);
390 done:
391 if (list != NULL)
392 be_free_list(list);
393 return (Py_BuildValue("[iO]", ret, listOfDicts));
397 * Function: beActivate
398 * Description: Convert Python args to nvlist pairs and call libbe:be_activate
399 * to activate a Boot Environment
400 * Parameters:
401 * args - pointer to a python object containing:
402 * beName - The name of the BE to activate
404 * Returns a pointer to a python object:
405 * BE_SUCCESS - Success
406 * bePyErr or be_errno_t - Failure
407 * Scope:
408 * Public
410 /* ARGSUSED */
411 PyObject *
412 beActivate(PyObject *self, PyObject *args)
414 char *beName = NULL;
415 int ret = BE_PY_SUCCESS;
416 nvlist_t *beAttrs = NULL;
418 if (!PyArg_ParseTuple(args, "z", &beName)) {
419 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
422 if (!convertPyArgsToNvlist(&beAttrs, 2, BE_ATTR_ORIG_BE_NAME, beName)) {
423 nvlist_free(beAttrs);
424 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
427 if (beAttrs == NULL) {
428 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
431 ret = be_activate(beAttrs);
432 nvlist_free(beAttrs);
433 return (Py_BuildValue("i", ret));
437 * Function: beDestroy
438 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
439 * to destroy a Boot Environment
440 * Parameters:
441 * args - pointer to a python object containing:
442 * beName - The name of the BE to destroy
444 * Returns a pointer to a python object:
445 * BE_SUCCESS - Success
446 * bePyErr or be_errno_t - Failure
447 * Scope:
448 * Public
450 /* ARGSUSED */
451 PyObject *
452 beDestroy(PyObject *self, PyObject *args)
454 char *beName = NULL;
455 int destroy_snaps = 0;
456 int force_unmount = 0;
457 int destroy_flags = 0;
458 int ret = BE_PY_SUCCESS;
459 nvlist_t *beAttrs = NULL;
461 if (!PyArg_ParseTuple(args, "z|ii", &beName, &destroy_snaps,
462 &force_unmount)) {
463 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
466 if (destroy_snaps == 1)
467 destroy_flags |= BE_DESTROY_FLAG_SNAPSHOTS;
469 if (force_unmount == 1)
470 destroy_flags |= BE_DESTROY_FLAG_FORCE_UNMOUNT;
472 if (!convertPyArgsToNvlist(&beAttrs, 2, BE_ATTR_ORIG_BE_NAME, beName)) {
473 nvlist_free(beAttrs);
474 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
477 if (nvlist_add_uint16(beAttrs, BE_ATTR_DESTROY_FLAGS, destroy_flags)
478 != 0) {
479 (void) printf("nvlist_add_uint16 failed for "
480 "BE_ATTR_DESTROY_FLAGS (%d).\n", destroy_flags);
481 nvlist_free(beAttrs);
482 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
485 if (beAttrs == NULL) {
486 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
489 ret = be_destroy(beAttrs);
490 nvlist_free(beAttrs);
491 return (Py_BuildValue("i", ret));
495 * Function: beDestroySnapshot
496 * Description: Convert Python args to nvlist pairs and call libbe:be_destroy
497 * to destroy a snapshot of a Boot Environment
498 * Parameters:
499 * args - pointer to a python object containing:
500 * beName - The name of the BE to destroy
501 * snapName - The name of the snapshot to destroy
503 * Returns a pointer to a python object:
504 * BE_SUCCESS - Success
505 * bePyErr or be_errno_t - Failure
506 * Scope:
507 * Public
509 /* ARGSUSED */
510 PyObject *
511 beDestroySnapshot(PyObject *self, PyObject *args)
513 char *beName = NULL;
514 char *snapName = NULL;
515 int ret = BE_PY_SUCCESS;
516 nvlist_t *beAttrs = NULL;
518 if (!PyArg_ParseTuple(args, "zz", &beName, &snapName)) {
519 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
522 if (!convertPyArgsToNvlist(&beAttrs, 4,
523 BE_ATTR_ORIG_BE_NAME, beName,
524 BE_ATTR_SNAP_NAME, snapName)) {
525 nvlist_free(beAttrs);
526 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
529 if (beAttrs == NULL) {
530 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
533 ret = be_destroy_snapshot(beAttrs);
534 nvlist_free(beAttrs);
535 return (Py_BuildValue("i", ret));
539 * Function: beRename
540 * Description: Convert Python args to nvlist pairs and call libbe:be_rename
541 * to rename a Boot Environment
542 * Parameters:
543 * args - pointer to a python object containing:
544 * oldBeName - The name of the old Boot Environment
545 * newBeName - The name of the new Boot Environment
547 * Returns a pointer to a python object:
548 * BE_SUCCESS - Success
549 * bePyErr or be_errno_t - Failure
550 * Scope:
551 * Public
553 /* ARGSUSED */
554 PyObject *
555 beRename(PyObject *self, PyObject *args)
557 char *oldBeName = NULL;
558 char *newBeName = NULL;
559 int ret = BE_PY_SUCCESS;
560 nvlist_t *beAttrs = NULL;
562 if (!PyArg_ParseTuple(args, "zz", &oldBeName, &newBeName)) {
563 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
566 if (!convertPyArgsToNvlist(&beAttrs, 4,
567 BE_ATTR_ORIG_BE_NAME, oldBeName,
568 BE_ATTR_NEW_BE_NAME, newBeName)) {
569 nvlist_free(beAttrs);
570 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
573 if (beAttrs == NULL) {
574 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
577 ret = be_rename(beAttrs);
578 nvlist_free(beAttrs);
579 return (Py_BuildValue("i", ret));
583 * Function: beMount
584 * Description: Convert Python args to nvlist pairs and call libbe:be_mount
585 * to mount a Boot Environment
586 * Parameters:
587 * args - pointer to a python object containing:
588 * beName - The name of the Boot Environment to mount
589 * mountpoint - The path of the mountpoint to mount the
590 * Boot Environment on (optional)
592 * Returns a pointer to a python object:
593 * BE_SUCCESS - Success
594 * bePyErr or be_errno_t - Failure
595 * Scope:
596 * Public
598 /* ARGSUSED */
599 PyObject *
600 beMount(PyObject *self, PyObject *args)
602 char *beName = NULL;
603 char *mountpoint = NULL;
604 int ret = BE_PY_SUCCESS;
605 nvlist_t *beAttrs = NULL;
607 if (!PyArg_ParseTuple(args, "zz", &beName, &mountpoint)) {
608 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
611 if (!convertPyArgsToNvlist(&beAttrs, 4,
612 BE_ATTR_ORIG_BE_NAME, beName,
613 BE_ATTR_MOUNTPOINT, mountpoint)) {
614 nvlist_free(beAttrs);
615 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
618 if (beAttrs == NULL) {
619 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
622 ret = be_mount(beAttrs);
623 nvlist_free(beAttrs);
624 return (Py_BuildValue("i", ret));
628 * Function: beUnmount
629 * Description: Convert Python args to nvlist pairs and call libbe:be_unmount
630 * to unmount a Boot Environment
631 * Parameters:
632 * args - pointer to a python object containing:
633 * beName - The name of the Boot Environment to unmount
635 * Returns a pointer to a python object:
636 * BE_SUCCESS - Success
637 * bePyErr or be_errno_t - Failure
638 * Scope:
639 * Public
641 /* ARGSUSED */
642 PyObject *
643 beUnmount(PyObject *self, PyObject *args)
645 char *beName = NULL;
646 int force_unmount = 0;
647 int unmount_flags = 0;
648 int ret = BE_PY_SUCCESS;
649 nvlist_t *beAttrs = NULL;
651 if (!PyArg_ParseTuple(args, "z|i", &beName, &force_unmount)) {
652 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
655 if (force_unmount == 1)
656 unmount_flags |= BE_UNMOUNT_FLAG_FORCE;
658 if (!convertPyArgsToNvlist(&beAttrs, 2,
659 BE_ATTR_ORIG_BE_NAME, beName)) {
660 nvlist_free(beAttrs);
661 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
664 if (nvlist_add_uint16(beAttrs, BE_ATTR_UNMOUNT_FLAGS, unmount_flags)
665 != 0) {
666 (void) printf("nvlist_add_uint16 failed for "
667 "BE_ATTR_UNMOUNT_FLAGS (%d).\n", unmount_flags);
668 nvlist_free(beAttrs);
669 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
672 if (beAttrs == NULL) {
673 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
676 ret = be_unmount(beAttrs);
677 nvlist_free(beAttrs);
678 return (Py_BuildValue("i", ret));
682 * Function: beRollback
683 * Description: Convert Python args to nvlist pairs and call libbe:be_rollback
684 * to rollback a Boot Environment to a previously taken
685 * snapshot.
686 * Parameters:
687 * args - pointer to a python object containing:
688 * beName - The name of the Boot Environment to unmount
690 * Returns a pointer to a python object:
691 * BE_SUCCESS - Success
692 * bePyErr or be_errno_t - Failure
693 * Scope:
694 * Public
696 /* ARGSUSED */
697 PyObject *
698 beRollback(PyObject *self, PyObject *args)
700 char *beName = NULL;
701 char *snapName = NULL;
702 int ret = BE_PY_SUCCESS;
703 nvlist_t *beAttrs = NULL;
705 if (!PyArg_ParseTuple(args, "zz", &beName, &snapName)) {
706 return (Py_BuildValue("i", BE_PY_ERR_PARSETUPLE));
709 if (!convertPyArgsToNvlist(&beAttrs, 4,
710 BE_ATTR_ORIG_BE_NAME, beName,
711 BE_ATTR_SNAP_NAME, snapName)) {
712 nvlist_free(beAttrs);
713 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
716 if (beAttrs == NULL) {
717 return (Py_BuildValue("i", BE_PY_ERR_NVLIST));
720 ret = be_rollback(beAttrs);
721 nvlist_free(beAttrs);
722 return (Py_BuildValue("i", ret));
726 * Function: bePrintErrors
727 * Description: Convert Python args to boolean and call libbe_print_errors to
728 * turn on/off error output for the library.
729 * Parameter:
730 * args - pointer to a python object containing:
731 * print_errors - Boolean that turns library error
732 * printing on or off.
733 * Parameters:
734 * args - pointer to a python object containing:
735 * 0 - do not print errors - Python boolean "False"
736 * 1 - print errors - Python boolean "True"
738 * Returns 1 on missing or invalid argument, 0 otherwise
739 * Scope:
740 * Public
742 /* ARGSUSED */
743 PyObject *
744 bePrintErrors(PyObject *self, PyObject *args)
746 int print_errors;
748 if (!PyArg_ParseTuple(args, "i", &print_errors) ||
749 (print_errors != 1 && print_errors != 0))
750 return (Py_BuildValue("i", BE_PY_ERR_PRINT_ERR));
751 libbe_print_errors(print_errors == 1);
752 return (Py_BuildValue("i", BE_PY_SUCCESS));
756 * Function: beGetErrDesc
757 * Description: Convert Python args to an int and call be_err_to_str to
758 * map an error code to an error string.
759 * Parameter:
760 * args - pointer to a python object containing:
761 * errCode - value to map to an error string.
763 * Returns: error string or NULL
764 * Scope:
765 * Public
767 /* ARGSUSED */
768 PyObject *
769 beGetErrDesc(PyObject *self, PyObject *args)
771 int errCode = 0;
772 char *beErrStr = NULL;
774 if (!PyArg_ParseTuple(args, "i", &errCode)) {
775 return (Py_BuildValue("s", NULL));
779 * First check libbe_py errors. If NULL is returned check error codes
780 * in libbe.
783 if ((beErrStr = beMapLibbePyErrorToString(errCode)) == NULL) {
784 beErrStr = be_err_to_str(errCode);
787 return (Py_BuildValue("s", beErrStr));
791 * Function: beVerifyBEName
792 * Description: Call be_valid_be_name() to verify the BE name.
793 * Parameter:
794 * args - pointer to a python object containing:
795 * string - value to map to a string.
797 * Returns: 0 for success or 1 for failure
798 * Scope:
799 * Public
801 /* ARGSUSED */
802 PyObject *
803 beVerifyBEName(PyObject *self, PyObject *args)
805 char *string = NULL;
807 if (!PyArg_ParseTuple(args, "s", &string)) {
808 return (Py_BuildValue("i", 1));
811 if (be_valid_be_name(string)) {
812 return (Py_BuildValue("i", 0));
813 } else {
814 return (Py_BuildValue("i", 1));
818 /* ~~~~~~~~~~~~~~~~~ */
819 /* Private Functions */
820 /* ~~~~~~~~~~~~~~~~~ */
822 static boolean_t
823 convertBEInfoToDictionary(be_node_list_t *be, PyObject **listDict)
825 if (be->be_node_name != NULL) {
826 if (PyDict_SetItemString(*listDict, BE_ATTR_ORIG_BE_NAME,
827 PyString_FromString(be->be_node_name)) != 0) {
828 return (B_FALSE);
832 if (be->be_rpool != NULL) {
833 if (PyDict_SetItemString(*listDict, BE_ATTR_ORIG_BE_POOL,
834 PyString_FromString(be->be_rpool)) != 0) {
835 return (B_FALSE);
839 if (be->be_mntpt != NULL) {
840 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTPOINT,
841 PyString_FromString(be->be_mntpt)) != 0) {
842 return (B_FALSE);
846 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTED,
847 (be->be_mounted ? Py_True : Py_False)) != 0) {
848 return (B_FALSE);
851 if (PyDict_SetItemString(*listDict, BE_ATTR_ACTIVE,
852 (be->be_active ? Py_True : Py_False)) != 0) {
853 return (B_FALSE);
856 if (PyDict_SetItemString(*listDict, BE_ATTR_ACTIVE_ON_BOOT,
857 (be->be_active_on_boot ? Py_True : Py_False)) != 0) {
858 return (B_FALSE);
861 if (PyDict_SetItemString(*listDict, BE_ATTR_GLOBAL_ACTIVE,
862 (be->be_global_active ? Py_True : Py_False)) != 0) {
863 return (B_FALSE);
866 if (be->be_space_used != 0) {
867 if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
868 PyLong_FromUnsignedLongLong(be->be_space_used)) != 0) {
869 return (B_FALSE);
873 if (be->be_root_ds != NULL) {
874 if (PyDict_SetItemString(*listDict, BE_ATTR_ROOT_DS,
875 PyString_FromString(be->be_root_ds)) != 0) {
876 return (B_FALSE);
880 if (be->be_node_creation != NULL) {
881 if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
882 PyLong_FromLong(be->be_node_creation)) != 0) {
883 return (B_FALSE);
887 if (be->be_policy_type != NULL) {
888 if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
889 PyString_FromString(be->be_policy_type)) != 0) {
890 return (B_FALSE);
894 if (be->be_uuid_str != NULL) {
895 if (PyDict_SetItemString(*listDict, BE_ATTR_UUID_STR,
896 PyString_FromString(be->be_uuid_str)) != 0) {
897 return (B_FALSE);
901 return (B_TRUE);
904 static boolean_t
905 convertDatasetInfoToDictionary(be_dataset_list_t *ds, PyObject **listDict)
907 if (ds->be_dataset_name != NULL) {
908 if (PyDict_SetItemString(*listDict, BE_ATTR_DATASET,
909 PyString_FromString(ds->be_dataset_name)) != 0) {
910 return (B_FALSE);
914 if (PyDict_SetItemString(*listDict, BE_ATTR_STATUS,
915 (ds->be_ds_mounted ? Py_True : Py_False)) != 0) {
916 return (B_FALSE);
919 if (ds->be_ds_mntpt != NULL) {
920 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTPOINT,
921 PyString_FromString(ds->be_ds_mntpt)) != 0) {
922 return (B_FALSE);
926 if (PyDict_SetItemString(*listDict, BE_ATTR_MOUNTED,
927 (ds->be_ds_mounted ? Py_True : Py_False)) != 0) {
928 return (B_FALSE);
931 if (ds->be_ds_space_used != 0) {
932 if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
933 PyLong_FromUnsignedLongLong(ds->be_ds_space_used))
934 != 0) {
935 return (B_FALSE);
939 if (ds->be_dataset_name != 0) {
940 if (PyDict_SetItemString(*listDict, BE_ATTR_DATASET,
941 PyString_FromString(ds->be_dataset_name)) != 0) {
942 return (B_FALSE);
946 if (ds->be_ds_plcy_type != NULL) {
947 if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
948 PyString_FromString(ds->be_ds_plcy_type)) != 0) {
949 return (B_FALSE);
953 if (ds->be_ds_creation != NULL) {
954 if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
955 PyLong_FromLong(ds->be_ds_creation)) != 0) {
956 return (B_FALSE);
960 return (B_TRUE);
963 static boolean_t
964 convertSnapshotInfoToDictionary(be_snapshot_list_t *ss, PyObject **listDict)
966 if (ss->be_snapshot_name != NULL) {
967 if (PyDict_SetItemString(*listDict, BE_ATTR_SNAP_NAME,
968 PyString_FromString(ss->be_snapshot_name)) != 0) {
969 return (B_FALSE);
973 if (ss->be_snapshot_creation != NULL) {
974 if (PyDict_SetItemString(*listDict, BE_ATTR_DATE,
975 PyLong_FromLong(ss->be_snapshot_creation)) != 0) {
976 return (B_FALSE);
980 if (ss->be_snapshot_type != NULL) {
981 if (PyDict_SetItemString(*listDict, BE_ATTR_POLICY,
982 PyString_FromString(ss->be_snapshot_type)) != 0) {
983 return (B_FALSE);
987 if (ss->be_snapshot_space_used != 0) {
988 if (PyDict_SetItemString(*listDict, BE_ATTR_SPACE,
989 PyLong_FromUnsignedLongLong(ss->be_snapshot_space_used))
990 != 0) {
991 return (B_FALSE);
995 return (B_TRUE);
999 * Convert string arguments to nvlist attributes
1002 static boolean_t
1003 convertPyArgsToNvlist(nvlist_t **nvList, int numArgs, ...)
1005 char *pt, *pt2;
1006 va_list ap;
1007 int i;
1009 if (*nvList == NULL) {
1010 if (nvlist_alloc(nvList, NV_UNIQUE_NAME, 0) != 0) {
1011 (void) printf("nvlist_alloc failed.\n");
1012 return (B_FALSE);
1016 va_start(ap, numArgs);
1018 for (i = 0; i < numArgs; i += 2) {
1019 if ((pt = va_arg(ap, char *)) == NULL ||
1020 (pt2 = va_arg(ap, char *)) == NULL) {
1021 continue;
1023 if (nvlist_add_string(*nvList, pt, pt2) != 0) {
1024 (void) printf("nvlist_add_string failed for %s (%s).\n",
1025 pt, pt2);
1026 nvlist_free(*nvList);
1027 return (B_FALSE);
1031 va_end(ap);
1033 return (B_TRUE);
1037 * Function: beMapLibbePyErrorToString
1038 * Description: Convert Python args to an int and map an error code to an
1039 * error string.
1040 * Parameter:
1041 * errCode - value to map to an error string.
1043 * Returns error string or NULL
1044 * Scope:
1045 * Public
1048 char *
1049 beMapLibbePyErrorToString(int errCode)
1051 switch (errCode) {
1052 case BE_PY_ERR_APPEND:
1053 return ("Unable to append a dictionary to a list "
1054 "of dictinaries.");
1055 case BE_PY_ERR_DICT:
1056 return ("Creation of a Python dictionary failed.");
1057 case BE_PY_ERR_LIST:
1058 return ("beList() failed.");
1059 case BE_PY_ERR_NVLIST:
1060 return ("An nvlist operation failed.");
1061 case BE_PY_ERR_PARSETUPLE:
1062 return ("PyArg_ParseTuple() failed to convert variable to C.");
1063 case BE_PY_ERR_PRINT_ERR:
1064 return ("bePrintErrors() failed.");
1065 case BE_PY_ERR_VAR_CONV:
1066 return ("Unable to add variables to a Python dictionary.");
1067 default:
1068 return (NULL);
1072 /* Private python initialization structure */
1074 static struct PyMethodDef libbeMethods[] = {
1075 {"beCopy", (PyCFunction)beCopy, METH_VARARGS, "Create/Copy a BE."},
1076 {"beCreateSnapshot", (PyCFunction)beCreateSnapshot, METH_VARARGS,
1077 "Create a snapshot."},
1078 {"beDestroy", (PyCFunction)beDestroy, METH_VARARGS, "Destroy a BE."},
1079 {"beDestroySnapshot", (PyCFunction)beDestroySnapshot, METH_VARARGS,
1080 "Destroy a snapshot."},
1081 {"beMount", (PyCFunction)beMount, METH_VARARGS, "Mount a BE."},
1082 {"beUnmount", (PyCFunction)beUnmount, METH_VARARGS, "Unmount a BE."},
1083 {"beList", (PyCFunction)beList, METH_VARARGS, "List BE info."},
1084 {"beRename", (PyCFunction)beRename, METH_VARARGS, "Rename a BE."},
1085 {"beActivate", (PyCFunction)beActivate, METH_VARARGS, "Activate a BE."},
1086 {"beRollback", (PyCFunction)beRollback, METH_VARARGS, "Rollback a BE."},
1087 {"bePrintErrors", (PyCFunction)bePrintErrors, METH_VARARGS,
1088 "Enable/disable error printing."},
1089 {"beGetErrDesc", (PyCFunction)beGetErrDesc, METH_VARARGS,
1090 "Map Error codes to strings."},
1091 {"beVerifyBEName", (PyCFunction)beVerifyBEName, METH_VARARGS,
1092 "Verify BE name."},
1093 {NULL, NULL, 0, NULL}
1096 void
1097 initlibbe_py()
1099 /* PyMODINIT_FUNC; */
1100 (void) Py_InitModule("libbe_py", libbeMethods);