From 65eb260be4a64ffe5c2e84f0196f65179b6e33fa Mon Sep 17 00:00:00 2001 From: Faruk Kasumovic Date: Thu, 7 May 2015 22:59:44 +0200 Subject: [PATCH] - Make dataset destroy returns meaningful error if dataset contains children - Implementation of recursive dataset destroy DestroyRecursive() --- .gitignore | 1 + zfs.go | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5ef4730..cf468c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .gitconfig +*.sublime-* diff --git a/zfs.go b/zfs.go index e19c67b..6b07fba 100644 --- a/zfs.go +++ b/zfs.go @@ -156,7 +156,20 @@ func (d *Dataset) Close() { } } +// Destroys the dataset. The caller must make sure that the filesystem +// isn't mounted, and that there are no active dependents. Set Defer argument +// to true to defer destruction for when dataset is not in use. func (d *Dataset) Destroy(Defer bool) (err error) { + if len(d.Children) > 0 { + path, e := d.Path() + if e != nil { + return + } + dsType, e := d.GetProperty(ZFSPropType) + err = errors.New("Cannot destroy dataset " + path + + ": " + dsType.Value + " has children") + return + } if d.list != nil { if ec := C.zfs_destroy(d.list.zh, boolean_t(Defer)); ec != 0 { err = LastError() @@ -167,6 +180,23 @@ func (d *Dataset) Destroy(Defer bool) (err error) { return } +// Recursively destroy children of dataset and dataset. +func (d *Dataset) DestroyRecursive() (err error) { + if len(d.Children) > 0 { + for _, c := range d.Children { + if err = c.DestroyRecursive(); err != nil { + return + } + // close handle to destroyed child dataset + c.Close() + } + // clear closed children array + d.Children = make([]Dataset, 0) + } + err = d.Destroy(false) + return +} + func (d *Dataset) Pool() (p Pool, err error) { if d.list == nil { err = errors.New(msgDatasetIsNil) @@ -259,7 +289,7 @@ func (d *Dataset) Clone(target string, props map[ZFSProp]Property) (rd Dataset, return } -// Create dataset snapshot +// Create dataset snapshot. Set recur to true to snapshot child datasets. func DatasetSnapshot(path string, recur bool, props map[ZFSProp]Property) (rd Dataset, err error) { var cprops *C.nvlist_t if cprops, err = datasetPropertiesTo_nvlist(props); err != nil { @@ -285,6 +315,7 @@ func (d *Dataset) Path() (path string, err error) { return } +// Rollabck dataset snapshot func (d *Dataset) Rollback(snap *Dataset, force bool) (err error) { if d.list == nil { err = errors.New(msgDatasetIsNil) @@ -297,6 +328,7 @@ func (d *Dataset) Rollback(snap *Dataset, force bool) (err error) { return } +// Rename dataset func (d *Dataset) Rename(newname string, recur, force_umount bool) (err error) { if d.list == nil {