2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-29 16:23:59 +00:00

Merge pull request #857 from middelink/fix-856

Fix SamePaths() and make it into a receiver function
This commit is contained in:
Alexander Neumann 2017-03-06 09:10:33 +01:00
commit cc140744d6
3 changed files with 21 additions and 19 deletions

View File

@ -68,7 +68,7 @@ func runSnapshots(opts SnapshotOptions, gopts GlobalOptions, args []string) erro
continue 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 { pos := sort.Search(len(list), func(i int) bool {
return list[i].Time.After(sn.Time) return list[i].Time.After(sn.Time)
}) })

View File

@ -57,7 +57,7 @@ func changeTags(repo *repository.Repository, snapshotID restic.ID, setTags, addT
if err != nil { if err != nil {
return false, err 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 return false, nil
} }

View File

@ -134,7 +134,7 @@ func (sn *Snapshot) RemoveTags(removeTags []string) (changed bool) {
return 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 { func (sn *Snapshot) HasTags(tags []string) bool {
nextTag: nextTag:
for _, tag := range tags { for _, tag := range tags {
@ -150,28 +150,30 @@ nextTag:
return true return true
} }
// SamePaths compares the Snapshot's paths and provided paths are exactly the same // HasPaths returns true if the snapshot has at least all of paths.
func SamePaths(expected, actual []string) bool { func (sn *Snapshot) HasPaths(paths []string) bool {
if len(expected) == 0 || len(actual) == 0 { nextPath:
return true for _, path := range paths {
for _, snPath := range sn.Paths {
if path == snPath {
continue nextPath
}
} }
for i := range expected {
found := false
for j := range actual {
if expected[i] == actual[j] {
found = true
break
}
}
if !found {
return false return false
} }
}
return true 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. // ErrNoSnapshotFound is returned when no snapshot for the given criteria could be found.
var ErrNoSnapshotFound = errors.New("no snapshot found") var ErrNoSnapshotFound = errors.New("no snapshot found")
@ -188,7 +190,7 @@ func FindLatestSnapshot(repo Repository, targets []string, hostname string) (ID,
if err != nil { if err != nil {
return ID{}, errors.Errorf("Error listing snapshot: %v", err) 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 latest = snapshot.Time
latestID = snapshotID latestID = snapshotID
found = true found = true