From 51af8807016cbdfd33fc62d83c6ee1f1a6100a00 Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 12 Nov 2017 23:02:23 +0100 Subject: [PATCH] refactor: parametrize PrefixFilter VersionType check refs #34 --- cmd/autosnap.go | 2 +- cmd/config_fsvfilter.go | 29 ++++++++++++++++------------- cmd/config_job_local.go | 2 +- cmd/config_job_source.go | 2 +- cmd/prune.go | 3 ++- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/cmd/autosnap.go b/cmd/autosnap.go index 449a007..8018089 100644 --- a/cmd/autosnap.go +++ b/cmd/autosnap.go @@ -47,7 +47,7 @@ func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) { l := a.log.WithField(logFSField, d.ToString()) - fsvs, err := zfs.ZFSListFilesystemVersions(d, &PrefixSnapshotFilter{a.Prefix}) + fsvs, err := zfs.ZFSListFilesystemVersions(d, NewTypedPrefixFilter(a.Prefix, zfs.Snapshot)) if err != nil { l.WithError(err).Error("cannot list filesystem versions") continue diff --git a/cmd/config_fsvfilter.go b/cmd/config_fsvfilter.go index c1c3754..8e13dd5 100644 --- a/cmd/config_fsvfilter.go +++ b/cmd/config_fsvfilter.go @@ -6,8 +6,18 @@ import ( "strings" ) -type PrefixSnapshotFilter struct { - Prefix string +type PrefixFilter struct { + prefix string + fstype zfs.VersionType + fstypeSet bool // optionals anyone? +} + +func NewPrefixFilter(prefix string) *PrefixFilter { + return &PrefixFilter{prefix: prefix} +} + +func NewTypedPrefixFilter(prefix string, versionType zfs.VersionType) *PrefixFilter { + return &PrefixFilter{prefix, versionType, true} } func parseSnapshotPrefix(i string) (p string, err error) { @@ -19,15 +29,8 @@ func parseSnapshotPrefix(i string) (p string, err error) { return } -func parsePrefixSnapshotFilter(i string) (f *PrefixSnapshotFilter, err error) { - if !(len(i) > 0) { - err = errors.Errorf("snapshot prefix must be longer than 0 characters") - return - } - f = &PrefixSnapshotFilter{i} - return -} - -func (f *PrefixSnapshotFilter) Filter(fsv zfs.FilesystemVersion) (accept bool, err error) { - return fsv.Type == zfs.Snapshot && strings.HasPrefix(fsv.Name, f.Prefix), nil +func (f *PrefixFilter) Filter(fsv zfs.FilesystemVersion) (accept bool, err error) { + fstypeMatches := (!f.fstypeSet || fsv.Type == f.fstype) + prefixMatches := strings.HasPrefix(fsv.Name, f.prefix) + return fstypeMatches && prefixMatches, nil } diff --git a/cmd/config_job_local.go b/cmd/config_job_local.go index c95e924..c6a7f6d 100644 --- a/cmd/config_job_local.go +++ b/cmd/config_job_local.go @@ -90,7 +90,7 @@ func (j *LocalJob) JobStart(ctx context.Context) { // All local datasets will be passed to its Map() function, // but only those for which a mapping exists will actually be pulled. // We can pay this small performance penalty for now. - handler := NewHandler(log.WithField(logTaskField, "handler"), localPullACL{}, &PrefixSnapshotFilter{j.SnapshotPrefix}) + handler := NewHandler(log.WithField(logTaskField, "handler"), localPullACL{}, NewPrefixFilter(j.SnapshotPrefix)) registerEndpoints(local, handler) diff --git a/cmd/config_job_source.go b/cmd/config_job_source.go index 69ddecd..a96b3ae 100644 --- a/cmd/config_job_source.go +++ b/cmd/config_job_source.go @@ -164,7 +164,7 @@ outer: } // construct connection handler - handler := NewHandler(log, j.Filesystems, &PrefixSnapshotFilter{j.SnapshotPrefix}) + handler := NewHandler(log, j.Filesystems, NewPrefixFilter(j.SnapshotPrefix)) // handle connection rpcServer := rpc.NewServer(rwc) diff --git a/cmd/prune.go b/cmd/prune.go index 419223c..a59cd00 100644 --- a/cmd/prune.go +++ b/cmd/prune.go @@ -46,7 +46,8 @@ func (p *Pruner) Run(ctx context.Context) (r []PruneResult, err error) { log := log.WithField(logFSField, fs.ToString()) - fsversions, err := zfs.ZFSListFilesystemVersions(fs, &PrefixSnapshotFilter{p.SnapshotPrefix}) + snapshotFilter := NewTypedPrefixFilter(p.SnapshotPrefix, zfs.Snapshot) + fsversions, err := zfs.ZFSListFilesystemVersions(fs, snapshotFilter) if err != nil { log.WithError(err).Error("error listing filesytem versions") continue