From 6e99fc78cc1b22f4e878fd49b6fb6a014961342e Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 28 Sep 2015 12:33:09 +0200 Subject: [PATCH 1/3] Add depth to a few methods --- utils.go | 4 ++-- zfs.go | 18 +++++++++--------- zpool.go | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/utils.go b/utils.go index 404ab2b..31d06fb 100644 --- a/utils.go +++ b/utils.go @@ -266,8 +266,8 @@ func parseInodeChanges(lines [][]string) ([]*InodeChange, error) { return changes, nil } -func listByType(t, filter string) ([]*Dataset, error) { - args := []string{"get", "-rHp", "-t", t, "all"} +func listByType(t, filter string, depth int) ([]*Dataset, error) { + args := []string{"get", fmt.Sprintf("-d%d", depth), "-rHp", "-t", t, "all"} if filter != "" { args = append(args, filter) } diff --git a/zfs.go b/zfs.go index daad94e..edfee50 100644 --- a/zfs.go +++ b/zfs.go @@ -117,29 +117,29 @@ func zfs(arg ...string) ([][]string, error) { // Datasets returns a slice of ZFS datasets, regardless of type. // A filter argument may be passed to select a dataset with the matching name, // or empty string ("") may be used to select all datasets. -func Datasets(filter string) ([]*Dataset, error) { - return listByType("all", filter) +func Datasets(filter string, depth int) ([]*Dataset, error) { + return listByType("all", filter, depth) } // Snapshots returns a slice of ZFS snapshots. // A filter argument may be passed to select a snapshot with the matching name, // or empty string ("") may be used to select all snapshots. -func Snapshots(filter string) ([]*Dataset, error) { - return listByType(DatasetSnapshot, filter) +func Snapshots(filter string, depth int) ([]*Dataset, error) { + return listByType(DatasetSnapshot, filter, depth) } // Filesystems returns a slice of ZFS filesystems. // A filter argument may be passed to select a filesystem with the matching name, // or empty string ("") may be used to select all filesystems. -func Filesystems(filter string) ([]*Dataset, error) { - return listByType(DatasetFilesystem, filter) +func Filesystems(filter string, depth int) ([]*Dataset, error) { + return listByType(DatasetFilesystem, filter, depth) } // Volumes returns a slice of ZFS volumes. // A filter argument may be passed to select a volume with the matching name, // or empty string ("") may be used to select all volumes. -func Volumes(filter string) ([]*Dataset, error) { - return listByType(DatasetVolume, filter) +func Volumes(filter string, depth int) ([]*Dataset, error) { + return listByType(DatasetVolume, filter, depth) } // GetDataset retrieves a single ZFS dataset by name. This dataset could be @@ -337,7 +337,7 @@ func (d *Dataset) Rename(name string, createParent bool, recursiveRenameSnapshot // Snapshots returns a slice of all ZFS snapshots of a given dataset. func (d *Dataset) Snapshots() ([]*Dataset, error) { - return Snapshots(d.Name) + return Snapshots(d.Name, 1) } // CreateFilesystem creates a new ZFS filesystem with the specified name and diff --git a/zpool.go b/zpool.go index 6ba52d3..5c42c99 100644 --- a/zpool.go +++ b/zpool.go @@ -49,13 +49,13 @@ func GetZpool(name string) (*Zpool, error) { } // Datasets returns a slice of all ZFS datasets in a zpool. -func (z *Zpool) Datasets() ([]*Dataset, error) { - return Datasets(z.Name) +func (z *Zpool) Datasets(depth int) ([]*Dataset, error) { + return Datasets(z.Name, depth) } // Snapshots returns a slice of all ZFS snapshots in a zpool. -func (z *Zpool) Snapshots() ([]*Dataset, error) { - return Snapshots(z.Name) +func (z *Zpool) Snapshots(depth int) ([]*Dataset, error) { + return Snapshots(z.Name, depth) } // CreateZpool creates a new ZFS zpool with the specified name, properties, From d8ca902743c33023641172733fcc4a0541effb6b Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 28 Sep 2015 13:38:42 +0200 Subject: [PATCH 2/3] Handle depth better --- utils.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/utils.go b/utils.go index 31d06fb..38c3850 100644 --- a/utils.go +++ b/utils.go @@ -267,7 +267,12 @@ func parseInodeChanges(lines [][]string) ([]*InodeChange, error) { } func listByType(t, filter string, depth int) ([]*Dataset, error) { - args := []string{"get", fmt.Sprintf("-d%d", depth), "-rHp", "-t", t, "all"} + var args []string + if depth > 0 { + args = []string{"get", fmt.Sprintf("-d%d", depth), "-rHp", "-t", t, "all"} + } else { + args = []string{"get", "-rHp", "-t", t, "all"} + } if filter != "" { args = append(args, filter) } From 1d98308527a0762b50d87e9ead58d6a53e4c5263 Mon Sep 17 00:00:00 2001 From: Eric Dillmann Date: Sat, 20 Feb 2016 10:41:14 +0100 Subject: [PATCH 3/3] Adapt tests to use depth arg --- zfs_test.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/zfs_test.go b/zfs_test.go index 41b4fb1..6ebb15b 100644 --- a/zfs_test.go +++ b/zfs_test.go @@ -71,7 +71,7 @@ func zpoolTest(t *testing.T, fn func()) { func TestDatasets(t *testing.T) { zpoolTest(t, func() { - _, err := zfs.Datasets("") + _, err := zfs.Datasets("", 1) ok(t, err) ds, err := zfs.GetDataset("test") @@ -85,7 +85,7 @@ func TestDatasets(t *testing.T) { func TestSnapshots(t *testing.T) { zpoolTest(t, func() { - snapshots, err := zfs.Snapshots("") + snapshots, err := zfs.Snapshots("", 1) ok(t, err) for _, snapshot := range snapshots { @@ -99,7 +99,7 @@ func TestFilesystems(t *testing.T) { f, err := zfs.CreateFilesystem("test/filesystem-test", nil) ok(t, err) - filesystems, err := zfs.Filesystems("") + filesystems, err := zfs.Filesystems("", 1) ok(t, err) for _, filesystem := range filesystems { @@ -121,7 +121,7 @@ func TestCreateFilesystemWithProperties(t *testing.T) { equals(t, "lz4", f.Compression) - filesystems, err := zfs.Filesystems("") + filesystems, err := zfs.Filesystems("", 1) ok(t, err) for _, filesystem := range filesystems { @@ -141,7 +141,7 @@ func TestVolumes(t *testing.T) { sleep(1) equals(t, zfs.DatasetVolume, v.Type) - volumes, err := zfs.Volumes("") + volumes, err := zfs.Volumes("", 1) ok(t, err) for _, volume := range volumes { @@ -157,7 +157,7 @@ func TestSnapshot(t *testing.T) { f, err := zfs.CreateFilesystem("test/snapshot-test", nil) ok(t, err) - filesystems, err := zfs.Filesystems("") + filesystems, err := zfs.Filesystems("", 1) ok(t, err) for _, filesystem := range filesystems { @@ -182,7 +182,7 @@ func TestClone(t *testing.T) { f, err := zfs.CreateFilesystem("test/snapshot-test", nil) ok(t, err) - filesystems, err := zfs.Filesystems("") + filesystems, err := zfs.Filesystems("", 1) ok(t, err) for _, filesystem := range filesystems { @@ -213,7 +213,7 @@ func TestSendSnapshot(t *testing.T) { f, err := zfs.CreateFilesystem("test/snapshot-test", nil) ok(t, err) - filesystems, err := zfs.Filesystems("") + filesystems, err := zfs.Filesystems("", 1) ok(t, err) for _, filesystem := range filesystems { @@ -264,8 +264,7 @@ func TestListZpool(t *testing.T) { zpoolTest(t, func() { pools, err := zfs.ListZpools() ok(t, err) - equals(t, "test", pools[0].Name) - + equals(t, true, len(pools)>0) }) } @@ -274,7 +273,7 @@ func TestRollback(t *testing.T) { f, err := zfs.CreateFilesystem("test/snapshot-test", nil) ok(t, err) - filesystems, err := zfs.Filesystems("") + filesystems, err := zfs.Filesystems("", 1) ok(t, err) for _, filesystem := range filesystems {