forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zfs.c
executable file
·285 lines (242 loc) · 7.11 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* 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 "common.h"
#include "zpool.h"
#include "zfs.h"
zfs_type_t dataset_type(dataset_list_ptr dataset) {
if (dataset == NULL || dataset->zh == NULL) {
return 0;
}
return zfs_get_type(dataset->zh);
}
dataset_list_ptr dataset_open(const char *path) {
dataset_list_ptr list = create_dataset_list_item();
zfs_type_t types = ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT | ZFS_TYPE_VOLUME | ZFS_TYPE_POOL | ZFS_TYPE_BOOKMARK;
list->zh = zfs_open(libzfs_get_handle(), path, types);
if (list->zh == NULL) {
dataset_list_free(list);
list = NULL;
}
return list;
}
int dataset_create(const char *path, zfs_type_t type, nvlist_ptr props) {
return zfs_create(libzfs_get_handle(), path, type, props);
}
int dataset_destroy(zfs_handle_t *zh, boolean_t defer) {
return zfs_destroy(zh, defer);
}
dataset_list_t *dataset_list_children(zfs_handle_t *zh) {
int err = 0;
if (zh == NULL) return NULL;
dataset_list_t *zlist = create_dataset_list_item();
if (!zlist) return NULL;
err = zfs_iter_children(zh, dataset_list_callb, &zlist);
if ( err != 0 || zlist->zh == NULL) {
dataset_list_free(zlist);
return NULL;
}
(void) zfs_iter_bookmarks(zh, dataset_list_callb, &zlist);
return zlist;
}
zpool_list_ptr dataset_get_pool(zfs_handle_t *zh) {
zpool_list_ptr pool = create_zpool_list_item();
if(pool != NULL) {
pool->zph = zfs_get_pool_handle(zh);
}
return pool;
}
int dataset_prop_set(zfs_handle_t *zh, zfs_prop_t prop, const char *value) {
return zfs_prop_set(zh, zfs_prop_to_name(prop), value);
}
int dataset_user_prop_set(zfs_handle_t *zh, const char *prop, const char *value) {
return zfs_prop_set(zh, prop, value);
}
int dataset_clone(zfs_handle_t *zh, const char *target, nvlist_ptr props) {
return zfs_clone(zh, target, props);
}
int dataset_snapshot(const char *path, boolean_t recur, nvlist_ptr props) {
return zfs_snapshot(libzfs_get_handle(), path, recur, props);
}
int dataset_rollback(zfs_handle_t *zh, zfs_handle_t *snap_zh, boolean_t force) {
return zfs_rollback(zh, snap_zh, force);
}
int dataset_promote(zfs_handle_t *zh) {
return zfs_promote(zh);
}
int dataset_rename(zfs_handle_t *zh, const char* new_name, boolean_t recur, boolean_t force_unm) {
return zfs_rename(zh, new_name, recur, force_unm);
}
const char *dataset_is_mounted(zfs_handle_t *zh){
char *mp = NULL;
// zfs_is_mounted returns B_TRUE or B_FALSE
if (0 != zfs_is_mounted(zh, &mp)) {
return mp;
}
return NULL;
}
int dataset_mount(zfs_handle_t *zh, const char *options, int flags) {
if ( 0 < strlen(options)) {
return zfs_mount(zh, options, flags);
} else {
return zfs_mount(zh, NULL, flags);
}
}
int dataset_unmount(zfs_handle_t *zh, int flags) {
return zfs_unmount(zh, NULL, flags);
}
int dataset_unmountall(zfs_handle_t *zh, int flags) {
return zfs_unmountall(zh, flags);
}
const char *dataset_get_name(zfs_handle_t *zh) {
return zfs_get_name(zh);
}
//int read_dataset_property(zfs_handle_t *zh, property_list_t *list, int prop) {
property_list_t *read_dataset_property(zfs_handle_t *zh, int prop) {
int r = 0;
zprop_source_t source;
char statbuf[ZFS_MAX_DATASET_NAME_LEN];
property_list_ptr list = NULL;
list = new_property_list();
if (list == NULL) {
return NULL;
}
r = zfs_prop_get(
zh, prop,
list->value, sizeof(list->value), &source, statbuf, sizeof(statbuf), B_TRUE
);
if (r == 0) {
zprop_source_tostr(list->source, source);
list->property = (int)prop;
return list;
} else {
//printf("prop_get %d: %d(%s)\n", prop, libzfs_last_error(), libzfs_last_error_str());
free_properties(list);
return NULL;
}
}
// int read_user_property(zfs_handle_t *zh, property_list_t *list, const char *prop) {
property_list_t *read_user_property(zfs_handle_t *zh, const char* prop) {
nvlist_t *user_props = zfs_get_user_props(zh);
nvlist_t *propval;
zprop_source_t sourcetype;
char *strval;
char *sourceval;
char buf[ZFS_MAXPROPLEN];
// char source[ZFS_MAX_DATASET_NAME_LEN];
property_list_ptr list = new_property_list();
if (zfs_prop_userquota(prop)) {
sourcetype = ZPROP_SRC_LOCAL;
(void) strncpy(list->source,
"local", sizeof (list->source));
if (zfs_prop_get_userquota(zh, prop,
buf, sizeof (buf), B_TRUE) != 0) {
sourcetype = ZPROP_SRC_NONE;
strval = "-";
} else {
strval = buf;
}
}
else if (zfs_prop_written(prop)) {
sourcetype = ZPROP_SRC_LOCAL;
(void) strncpy(list->source,
"local", sizeof (list->source));
if (zfs_prop_get_written(zh, prop,
buf, sizeof (buf), B_TRUE) != 0) {
sourcetype = ZPROP_SRC_NONE;
strval = "-";
} else {
strval = buf;
}
} else if (nvlist_lookup_nvlist(user_props,
prop, &propval) != 0) {
sourcetype = ZPROP_SRC_NONE;
(void) strncpy(list->source,
"none", sizeof (list->source));
strval = "-";
} else {
verify(nvlist_lookup_string(propval,
ZPROP_VALUE, &strval) == 0);
verify(nvlist_lookup_string(propval,
ZPROP_SOURCE, &sourceval) == 0);
if (strcmp(sourceval,
zfs_get_name(zh)) == 0) {
sourcetype = ZPROP_SRC_LOCAL;
(void) strncpy(list->source,
"local", sizeof (list->source));
} else if (strcmp(sourceval,
ZPROP_SOURCE_VAL_RECVD) == 0) {
sourcetype = ZPROP_SRC_RECEIVED;
(void) strncpy(list->source,
"received", sizeof (list->source));
} else {
sourcetype = ZPROP_SRC_INHERITED;
(void) strncpy(list->source,
sourceval, sizeof (list->source));
}
}
(void) strncpy(list->value,
strval, sizeof (list->value));
return list;
}
char** alloc_cstrings(int size) {
return malloc(size*sizeof(char*));
}
void strings_setat(char **a, int at, char *v) {
a[at] = v;
}
sendflags_t *alloc_sendflags() {
sendflags_t *r = malloc(sizeof(sendflags_t));
memset(r, 0, sizeof(sendflags_t));
return r;
}
void sendflags_set_raw(sendflags_t * flags) {
#if LIBZFS_VERSION_MINOR == 7
#else
flags->raw = B_TRUE;
#endif
}
recvflags_t *alloc_recvflags() {
recvflags_t *r = malloc(sizeof(recvflags_t));
memset(r, 0, sizeof(recvflags_t));
return r;
}
struct zfs_cmd *new_zfs_cmd(){
struct zfs_cmd *cmd = malloc(sizeof(struct zfs_cmd));
memset(cmd, 0, sizeof(struct zfs_cmd));
return cmd;
}
int estimate_send_size(struct zfs_cmd *zc) {
int rc = zfs_ioctl(libzfs_get_handle(), ZFS_IOC_SEND, zc);
if (rc != 0) {
rc = errno;
}
return rc;
}
#if LIBZFS_VERSION_MINOR == 7
int gozfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags, const char *redactbook) {
uint32_t lzc_send_flags = 0;
if (flags->largeblock) {
lzc_send_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
}
if (flags->embed_data) {
lzc_send_flags |= LZC_SEND_FLAG_EMBED_DATA;
}
if (flags->compress) {
lzc_send_flags |= LZC_SEND_FLAG_COMPRESS;
}
return zfs_send_one(zhp, from, fd, lzc_send_flags);
}
#else
int gozfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags, const char *redactbook) {
#if LIBZFS_VERSION_PATCH != 1
return zfs_send_one(zhp, from, fd, flags, redactbook);
#else
return zfs_send_one(zhp, from, fd, *flags);
#endif
}
#endif