From 8f48d3ebf52a35f3ddc7dd223bd08ac30955d6c1 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Sat, 1 Oct 2016 15:10:12 +0300 Subject: [PATCH] refactor ListSubvolumes --- btrfs.go | 19 +++++++++++++++++++ cmd/gbtrfs.go | 7 ++++++- subvolume.go | 21 --------------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/btrfs.go b/btrfs.go index 3fe2100..aef7e10 100644 --- a/btrfs.go +++ b/btrfs.go @@ -201,6 +201,25 @@ func (f *FS) ReceiveTo(r io.Reader, mount string) error { return Receive(r, filepath.Join(f.f.Name(), mount)) } +func (f *FS) ListSubvolumes(filter func(Subvolume) bool) ([]Subvolume, error) { + //root, err := getPathRootID(f) + //if err != nil { + // return nil, fmt.Errorf("can't get rootid for '%s': %v", path, err) + //} + m, err := listSubVolumes(f.f) + if err != nil { + return nil, err + } + out := make([]Subvolume, 0, len(m)) + for _, v := range m { + if filter != nil && !filter(v) { + continue + } + out = append(out, v) + } + return out, nil +} + type Compression string const ( diff --git a/cmd/gbtrfs.go b/cmd/gbtrfs.go index b499077..03c3c6f 100644 --- a/cmd/gbtrfs.go +++ b/cmd/gbtrfs.go @@ -74,7 +74,12 @@ var SubvolumeListCmd = &cobra.Command{ if len(args) != 1 { return fmt.Errorf("expected one destination argument") } - list, err := btrfs.ListSubVolumes(args[0]) + fs, err := btrfs.Open(args[0], true) + if err != nil { + return err + } + defer fs.Close() + list, err := fs.ListSubvolumes(nil) if err == nil { for _, v := range list { fmt.Printf("%+v\n", v) diff --git a/subvolume.go b/subvolume.go index 1cfe913..f8aee57 100644 --- a/subvolume.go +++ b/subvolume.go @@ -146,27 +146,6 @@ func SnapshotSubVolume(subvol, dst string, ro bool) error { return nil } -func ListSubVolumes(path string) ([]Subvolume, error) { - f, err := openDir(path) - if err != nil { - return nil, err - } - defer f.Close() - //root, err := getPathRootID(f) - //if err != nil { - // return nil, fmt.Errorf("can't get rootid for '%s': %v", path, err) - //} - m, err := listSubVolumes(f) - if err != nil { - return nil, err - } - out := make([]Subvolume, 0, len(m)) - for _, v := range m { - out = append(out, v) - } - return out, nil -} - type Subvolume struct { ObjectID uint64 TransID uint64