forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset_list.c
62 lines (51 loc) · 1.31 KB
/
dataset_list.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
#include <libzfs.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
#include "zpool.h"
#include "zfs.h"
dataset_list_t *create_dataset_list_item() {
dataset_list_t *zlist = (dataset_list_t *)malloc(sizeof(dataset_list_t));
if (zlist == NULL) return NULL;
memset(zlist, 0, sizeof(dataset_list_t));
return zlist;
}
void dataset_list_free(dataset_list_t *list) {
dataset_list_t *next = list;
while(list != NULL) {
next = list->pnext;
if (list->zh) {
zfs_close(list->zh);
}
free(list);
list = next;
}
}
int dataset_list_callb(zfs_handle_t *dataset, void *data) {
dataset_list_t **lroot = (dataset_list_t**)data;
if ( (*lroot)->zh == NULL ) {
(*lroot)->zh = dataset;
} else {
dataset_list_t *nroot = create_dataset_list_item();
if (!nroot) return -1;
nroot->zh = dataset;
nroot->pnext = (void*)*lroot;
*lroot = nroot;
}
return 0;
}
dataset_list_ptr dataset_list_root() {
int err = 0;
dataset_list_t *zlist = create_dataset_list_item();
if (zlist == NULL) return NULL;
err = zfs_iter_root(libzfs_get_handle(), dataset_list_callb, &zlist);
if ( err != 0 || zlist->zh == NULL) {
dataset_list_free(zlist);
return NULL;
}
return zlist;
}
dataset_list_ptr dataset_list_next(dataset_list_t *dataset) {
return dataset->pnext;
}