From e1c828be3e00cc2daf7d0dc42eb2f910ece45e4f Mon Sep 17 00:00:00 2001 From: Pauline Middelink Date: Mon, 6 Mar 2017 02:21:58 +0100 Subject: [PATCH] Fix SamePaths() and make it into a receiver function Add `HasPath(paths []string) bool` to Snapshot for testing if the snapshot has at least the paths given to the function. Reimplemented SamePaths(paths []string) so it does what the name implies, compare if all given paths are in the snapshot. --- src/cmds/restic/cmd_snapshots.go | 2 +- src/cmds/restic/cmd_tag.go | 2 +- src/restic/snapshot.go | 36 +++++++++++++++++--------------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/cmds/restic/cmd_snapshots.go b/src/cmds/restic/cmd_snapshots.go index 5e3db0c5c..a78342994 100644 --- a/src/cmds/restic/cmd_snapshots.go +++ b/src/cmds/restic/cmd_snapshots.go @@ -68,7 +68,7 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro continue } - if restic.SamePaths(sn.Paths, opts.Paths) && (opts.Host == "" || opts.Host == sn.Hostname) { + if (opts.Host == "" || opts.Host == sn.Hostname) && sn.HasPaths(opts.Paths) { pos := sort.Search(len(list), func(i int) bool { return list[i].Time.After(sn.Time) }) diff --git a/src/cmds/restic/cmd_tag.go b/src/cmds/restic/cmd_tag.go index 257320352..1d8d5ebd2 100644 --- a/src/cmds/restic/cmd_tag.go +++ b/src/cmds/restic/cmd_tag.go @@ -57,7 +57,7 @@ func changeTags(repo *repository.Repository, snapshotID restic.ID, setTags, addT if err != nil { return false, err } - if (host != "" && host != sn.Hostname) || !sn.HasTags(tags) || !restic.SamePaths(sn.Paths, paths) { + if (host != "" && host != sn.Hostname) || !sn.HasTags(tags) || !sn.HasPaths(paths) { return false, nil } diff --git a/src/restic/snapshot.go b/src/restic/snapshot.go index 68f5e6878..ed89a60ca 100644 --- a/src/restic/snapshot.go +++ b/src/restic/snapshot.go @@ -134,7 +134,7 @@ func (sn *Snapshot) RemoveTags(removeTags []string) (changed bool) { return } -// HasTags returns true if the snapshot has all the tags. +// HasTags returns true if the snapshot has at least all of tags. func (sn *Snapshot) HasTags(tags []string) bool { nextTag: for _, tag := range tags { @@ -150,28 +150,30 @@ nextTag: return true } -// SamePaths compares the Snapshot's paths and provided paths are exactly the same -func SamePaths(expected, actual []string) bool { - if len(expected) == 0 || len(actual) == 0 { - return true - } - - for i := range expected { - found := false - for j := range actual { - if expected[i] == actual[j] { - found = true - break +// HasPaths returns true if the snapshot has at least all of paths. +func (sn *Snapshot) HasPaths(paths []string) bool { +nextPath: + for _, path := range paths { + for _, snPath := range sn.Paths { + if path == snPath { + continue nextPath } } - if !found { - return false - } + + return false } return true } +// SamePaths returns true if the snapshot matches the entire paths set +func (sn *Snapshot) SamePaths(paths []string) bool { + if len(sn.Paths) != len(paths) { + return false + } + return sn.HasPaths(paths) +} + // ErrNoSnapshotFound is returned when no snapshot for the given criteria could be found. var ErrNoSnapshotFound = errors.New("no snapshot found") @@ -188,7 +190,7 @@ func FindLatestSnapshot(repo Repository, targets []string, hostname string) (ID, if err != nil { return ID{}, errors.Errorf("Error listing snapshot: %v", err) } - if snapshot.Time.After(latest) && SamePaths(snapshot.Paths, targets) && (hostname == "" || hostname == snapshot.Hostname) { + if snapshot.Time.After(latest) && snapshot.HasPaths(targets) && (hostname == "" || hostname == snapshot.Hostname) { latest = snapshot.Time latestID = snapshotID found = true