-
Notifications
You must be signed in to change notification settings - Fork 8
/
LibZFS.java
299 lines (272 loc) · 9.25 KB
/
LibZFS.java
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
package org.jvnet.solaris.libzfs;
import com.sun.jna.Pointer;
import org.jvnet.solaris.libzfs.jna.libzfs;
import static org.jvnet.solaris.libzfs.jna.libzfs.LIBZFS;
import org.jvnet.solaris.libzfs.jna.libzfs.zpool_iter_f;
import org.jvnet.solaris.libzfs.jna.libzfs_handle_t;
import org.jvnet.solaris.libzfs.jna.zfs_handle_t;
import org.jvnet.solaris.libzfs.jna.zfs_type_t;
import org.jvnet.solaris.libzfs.jna.zpool_handle_t;
import org.jvnet.solaris.nvlist.jna.nvlist_t;
import static org.jvnet.solaris.nvlist.jna.libnvpair.NV_UNIQUE_NAME;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Collections;
import java.io.File;
/**
* Entry point to ZFS functionality in Java.
*
* @author Kohsuke Kawaguchi
*/
public class LibZFS implements ZFSContainer {
private libzfs_handle_t handle;
public LibZFS() {
handle = LIBZFS.libzfs_init();
}
/**
* List up all the root file systems and return them.
*
* <p>
* In ZFS, each zpool gets a top-level zfs file system automatically.
* This method returns those top-level file systems.
*
* @return can be empty but never null.
*/
public List<ZFSFileSystem> roots() {
final List<ZFSFileSystem> r = new ArrayList<ZFSFileSystem>();
LIBZFS.zfs_iter_root(handle, new libzfs.zfs_iter_f() {
public int callback(zfs_handle_t handle, Pointer arg) {
r.add(new ZFSFileSystem(LibZFS.this, handle));
return 0;
}
}, null);
return r;
}
/**
* Lists up all the ZFS pools.
*
* @return can be empty but never null.
*/
public List<ZFSPool> pools() {
final List<ZFSPool> r = new ArrayList<ZFSPool>();
LIBZFS.zpool_iter(handle, new zpool_iter_f() {
public int callback(zpool_handle_t handle, Pointer arg) {
r.add(new ZFSPool(LibZFS.this, handle));
return 0;
}
}, null);
return r;
}
/**
* Gets the pool of the given name.
*/
public ZFSPool getPool(String name) {
zpool_handle_t h = LIBZFS.zpool_open(handle, name);
if(h==null) return null; // not found
return new ZFSPool(this,h);
}
/**
* Does a zfs dataset of the given name exist?
*
* @param dataSetName
* the dataset name of check for.
* @return does the dataset exist?
*/
public boolean exists(final String dataSetName) {
final boolean exists = exists(dataSetName, EnumSet.allOf(ZFSType.class));
return exists;
}
/**
* Does a zfs dataset of the given name and the given types exist?
*
* @param name
* the dataset name of check for.
* @param typeMask
* the specific zfs types to check for.
* @return does the dataset exist?
*/
public boolean exists(final String name, final Set<ZFSType> typeMask) {
int mask = 0;
for (ZFSType t : typeMask) {
mask |= t.code;
}
final boolean exists = LIBZFS.zfs_dataset_exists(handle, name, mask);
return exists;
}
/**
* Does a zfs dataset of the given name and the given type exist?
*
* @param dataSetName
* the dataset name of check for.
* @param type
* the specific zfs type to check for.
* @return does the dataset exist?
*/
public boolean exists(final String dataSetName, final ZFSType type) {
final boolean exists = exists(dataSetName, EnumSet.of(type));
return exists;
}
/**
* Create a ZFS Data Set of a given name and zfs type.
*
* @param dataSetName
* name of the dataset to create.
* @param type
* the zfs type of dataset to create.
* @return
* Never null. Created dataset.
*/
public <T extends ZFSObject> T create(String dataSetName, Class<T> type) {
return type.cast(create(dataSetName, ZFSType.fromType(type), null));
}
/**
* Create a ZFS Data Set of a given name, zfs type and properties.
*
* @param dataSetName
* Full name of the dataset to create, like "rpool/abc/def".
* @param type
* the zfs type of dataset to create. Either {@link ZFSType#FILESYSTEM} or {@link ZFSType#VOLUME}.
* @param props
* zfs dataset properties. Can be null.
* @return created dataset.
*/
public ZFSObject create(final String dataSetName, final ZFSType type,
final Map<String, String> props) {
final nvlist_t nvl = nvlist_t.alloc(NV_UNIQUE_NAME);
if(props!=null) {
for (Map.Entry<String, String> e : props.entrySet()) {
nvl.put(e.getKey(), e.getValue());
}
}
/* create intermediate directories */
final String[] dirs = dataSetName.split("/");
final StringBuilder sb = new StringBuilder(dirs[0]);
for (int i = 1; i < dirs.length; i++) {
sb.append('/').append(dirs[i]);
if (!exists(sb.toString())) {
if (LIBZFS.zfs_create(handle, sb.toString(), type.code, nvl) != 0) {
throw new ZFSException(this,"Failed to create "+dataSetName);
}
}
}
final ZFSObject dataSet = open(dataSetName);
return dataSet;
}
/**
* Open a ZFS Data Set of a given name.
*
* @param dataSetName
* name of the dataset to open.
* @return opened dataset, or null if no such dataset exists.
*/
public ZFSObject open(final String dataSetName) {
final ZFSObject dataSet = open(dataSetName, zfs_type_t.DATASET);
return dataSet;
}
/**
* Open a ZFS Data Set of a given name and type.
*
* @param dataSetName
* name of the dataset to open.
* @param mask
* the zfs type mask of dataset to open.
* @return opened dataset, or null if no such dataset exists.
*/
public ZFSObject open(final String dataSetName, final int /* zfs_type_t */mask) {
zfs_handle_t h = LIBZFS.zfs_open(handle, dataSetName, mask);
if(h==null) {
int err = LIBZFS.libzfs_errno(handle);
if(err==0) return null;
throw new ZFSException(this);
}
return ZFSObject.create(this,h);
}
/**
* Opens a ZFS dataset of the given name and type.
*/
public <T extends ZFSObject> T open(String dataSetName, Class<T> type) {
return type.cast(open(dataSetName,ZFSType.fromType(type).code));
}
/**
* Gets a {@link ZFSFileSystem} mounted at the given directory.
*
* @return
* null if no such file system exists.
*/
public ZFSFileSystem getFileSystemByMountPoint(File dir) {
dir = dir.getAbsoluteFile();
for (ZFSFileSystem f : descendants(ZFSFileSystem.class)) {
File mp = f.getMountPoint();
if(mp!=null && mp.equals(dir))
return f;
}
return null;
}
public List<ZFSFileSystem> children() {
return roots();
}
public <T extends ZFSObject> List<T> children(Class<T> type) {
if(type.isAssignableFrom(ZFSFileSystem.class))
return (List)roots();
else
return Collections.emptyList();
}
public List<ZFSObject> descendants() {
return children(ZFSObject.class);
}
public <T extends ZFSObject> List<T> descendants(Class<T> type) {
ArrayList<T> r = new ArrayList<T>();
r.addAll(children(type));
for (ZFSFileSystem p : roots())
r.addAll(p.descendants(type));
return r;
}
/**
* Returns {@link libzfs_handle_t} that this object wraps.
* <p>
* If the caller wants to use methods that don't yet have a high-level
* binding, the returned {@link libzfs_handle_t} can be used directly in
* conjunction with {@link libzfs#LIBZFS}.
*/
public libzfs_handle_t getHandle() {
return handle;
}
@Override
protected void finalize() throws Throwable {
super.finalize();
dispose();
}
/**
* Eagerly releases the native resource associated with this wrapper,
* instead of waiting for GC to take care of it.
*/
public synchronized void dispose() {
if (handle != null) {
LIBZFS.libzfs_fini(handle);
handle = null;
}
}
}