FindLatestSnapshot: Use absolute paths

This commit is contained in:
Alexander Neumann 2018-03-03 18:02:47 +01:00
parent fa4f438bc1
commit baebf45e2e
1 changed files with 15 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"os" "os"
"path/filepath"
"time" "time"
"github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/errors"
@ -14,13 +15,25 @@ var ErrNoSnapshotFound = errors.New("no snapshot found")
// FindLatestSnapshot finds latest snapshot with optional target/directory, tags and hostname filters. // FindLatestSnapshot finds latest snapshot with optional target/directory, tags and hostname filters.
func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string, tagLists []TagList, hostname string) (ID, error) { func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string, tagLists []TagList, hostname string) (ID, error) {
var err error
absTargets := make([]string, 0, len(targets))
for _, target := range targets {
if !filepath.IsAbs(target) {
target, err = filepath.Abs(target)
if err != nil {
return ID{}, errors.Wrap(err, "Abs")
}
}
absTargets = append(absTargets, target)
}
var ( var (
latest time.Time latest time.Time
latestID ID latestID ID
found bool found bool
) )
err := repo.List(ctx, SnapshotFile, func(snapshotID ID, size int64) error { err = repo.List(ctx, SnapshotFile, func(snapshotID ID, size int64) error {
snapshot, err := LoadSnapshot(ctx, repo, snapshotID) snapshot, err := LoadSnapshot(ctx, repo, snapshotID)
if err != nil { if err != nil {
return errors.Errorf("Error loading snapshot %v: %v", snapshotID.Str(), err) return errors.Errorf("Error loading snapshot %v: %v", snapshotID.Str(), err)
@ -33,7 +46,7 @@ func FindLatestSnapshot(ctx context.Context, repo Repository, targets []string,
return nil return nil
} }
if !snapshot.HasPaths(targets) { if !snapshot.HasPaths(absTargets) {
return nil return nil
} }