From ac5a911c64a30d5d29c9f9d9cd2930dca30270d3 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 30 Apr 2017 16:13:05 +0200 Subject: [PATCH] zfs: glob mapping for complete wildcard ("*") --- zfs/mapping_test.go | 14 ++++++++++++++ zfs/zfs.go | 23 +++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/zfs/mapping_test.go b/zfs/mapping_test.go index a1a741d..038fbb0 100644 --- a/zfs/mapping_test.go +++ b/zfs/mapping_test.go @@ -27,6 +27,20 @@ func TestGlobMapping(t *testing.T) { } +func TestGlobMappingWildcard(t *testing.T) { + m := GlobMapping{ + PrefixPath: EmptyDatasetPath, + TargetRoot: toDatasetPath("backups/share1"), + } + + var r DatasetPath + var err error + r, err = m.Map(toDatasetPath("tank/usr/home")) + + assert.Equal(t, toDatasetPath("backups/share1/tank/usr/home"), r) + assert.NoError(t, err) +} + func TestComboMapping(t *testing.T) { m1 := GlobMapping{ diff --git a/zfs/zfs.go b/zfs/zfs.go index ad2764e..1d38643 100644 --- a/zfs/zfs.go +++ b/zfs/zfs.go @@ -35,7 +35,16 @@ func (p DatasetPath) ToString() string { return strings.Join(p, "/") } +func (p DatasetPath) Empty() bool { + return len(p) == 0 +} + +var EmptyDatasetPath DatasetPath = []string{} + func NewDatasetPath(s string) (p DatasetPath, err error) { + if s == "" { + return EmptyDatasetPath, nil // the empty dataset path + } // TODO validation return toDatasetPath(s), nil } @@ -60,11 +69,17 @@ var ZFS_BINARY string = "zfs" func zfsList(root string, filter DatasetFilter) (datasets []DatasetPath, err error) { const ZFS_LIST_FIELD_COUNT = 1 - - cmd := exec.Command(ZFS_BINARY, "list", "-H", "-r", + args := make([]string, 0, 10) + args = append(args, + "list", "-H", "-r", "-t", "filesystem,volume", - "-o", "name", - root) + "-o", "name") + + if len(root) > 0 { + args = append(args, root) + } + + cmd := exec.Command(ZFS_BINARY, args...) var stdout io.Reader var stderr io.Reader