Skip to content

Commit f4a1382

Browse files
committed
Allow validating zfs resource names
1 parent e174c11 commit f4a1382

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

libzfs.pyx

+20
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,26 @@ cdef struct prop_iter_state:
301301
void *props
302302

303303

304+
def validate_dataset_name(name):
305+
return validate_zfs_resource_name(name, zfs.ZFS_TYPE_FILESYSTEM | zfs.ZFS_TYPE_VOLUME)
306+
307+
308+
def validate_snapshot_name(name):
309+
return validate_zfs_resource_name(name, zfs.ZFS_TYPE_SNAPSHOT)
310+
311+
312+
def validate_pool_name(name):
313+
return validate_zfs_resource_name(name, zfs.ZFS_TYPE_POOL)
314+
315+
316+
cdef validate_zfs_resource_name(str name, int r_type):
317+
cdef const char *c_name = name
318+
cdef int ret
319+
with nogil:
320+
ret = libzfs.zfs_name_valid(c_name, <zfs.zfs_type_t>r_type)
321+
return bool(ret)
322+
323+
304324
class DiffRecord(object):
305325
def __init__(self, raw):
306326
timestamp, cmd, typ, rest = raw.split(maxsplit=3)

pxd/libzfs.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ cdef extern from "libzfs.h" nogil:
537537

538538
extern const char *zfs_type_to_name(zfs_type_t)
539539
extern void zfs_refresh_properties(zfs_handle_t *)
540-
extern int zfs_name_valid(const char *, zfs_type_t)
540+
extern int zfs_name_valid(const char *, zfs.zfs_type_t)
541541
extern zfs_handle_t *zfs_path_to_zhandle(libzfs_handle_t *, char *, zfs.zfs_type_t)
542542
extern int zfs_dataset_exists(libzfs_handle_t *, const char *,
543543
zfs_type_t)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
config = namedtuple('config', ['CFLAGS', 'CPPFLAGS', 'LDFLAGS'])([], [], [])
4444

4545

46-
libraries=['nvpair', 'zfs', 'zfs_core', 'uutil']
46+
libraries = ['nvpair', 'zfs', 'zfs_core', 'uutil']
4747
if platform.system().lower() == 'freebsd':
4848
libraries.append('geom')
4949

0 commit comments

Comments
 (0)