forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
zfs.c
96 lines (80 loc) · 2.1 KB
/
zfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* C wrappers around some zfs calls and C in general that should simplify
* using libzfs from go language, make go code shorter and more readable.
*/
#include <libzfs.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include "zpool.h"
#include "zfs.h"
dataset_list_t *create_dataset_list_item() {
dataset_list_t *zlist = malloc(sizeof(dataset_list_t));
memset(zlist, 0, sizeof(dataset_list_t));
return zlist;
}
void dataset_list_close(dataset_list_t *list) {
zfs_close(list->zh);
free(list);
}
int dataset_list_callb(zfs_handle_t *dataset, void *data) {
dataset_list_t **lroot = (dataset_list_t**)data;
dataset_list_t *nroot = create_dataset_list_item();
if ( !((*lroot)->zh) ) {
(*lroot)->zh = dataset;
} else {
nroot->zh = dataset;
nroot->pnext = (void*)*lroot;
*lroot = nroot;
}
return 0;
}
int dataset_list_root(libzfs_handle_t *libzfs, dataset_list_t **first) {
int err = 0;
dataset_list_t *zlist = create_dataset_list_item();
err = zfs_iter_root(libzfs, dataset_list_callb, &zlist);
if ( zlist->zh ) {
*first = zlist;
} else {
*first = 0;
free(zlist);
}
return err;
}
dataset_list_t *dataset_next(dataset_list_t *dataset) {
return dataset->pnext;
}
int dataset_list_children(zfs_handle_t *zfs, dataset_list_t **first) {
int err = 0;
dataset_list_t *zlist = create_dataset_list_item();
err = zfs_iter_children(zfs, dataset_list_callb, &zlist);
if ( zlist->zh ) {
*first = zlist;
} else {
*first = 0;
free(zlist);
}
return err;
}
int read_dataset_property(zfs_handle_t *zh, property_list_t *list, int prop) {
int r = 0;
zprop_source_t source;
char statbuf[INT_MAX_VALUE];
r = zfs_prop_get(zh, prop,
list->value, INT_MAX_VALUE, &source, statbuf, INT_MAX_VALUE, 1);
if (r == 0) {
// strcpy(list->name, zpool_prop_to_name(prop));
zprop_source_tostr(list->source, source);
}
list->property = (int)prop;
return r;
}
int clear_last_error(libzfs_handle_t *hdl) {
zfs_standard_error(hdl, EZFS_SUCCESS, "success");
return 0;
}
char** alloc_cstrings(int size) {
return malloc(size*sizeof(char*));
}
void strings_setat(char **a, int at, char *v) {
a[at] = v;
}