restic/internal/restic/snapshot_find.go

162 lines
4.3 KiB
Go
Raw Normal View History

package restic
import (
"context"
"fmt"
2018-03-03 17:02:47 +00:00
"path/filepath"
"time"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
)
// ErrNoSnapshotFound is returned when no snapshot for the given criteria could be found.
var ErrNoSnapshotFound = errors.New("no snapshot found")
2022-10-03 12:30:48 +00:00
// findLatestSnapshot finds latest snapshot with optional target/directory, tags, hostname, and timestamp filters.
func findLatestSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string,
tags []TagList, paths []string, timeStampLimit *time.Time) (*Snapshot, error) {
2018-03-03 17:02:47 +00:00
var err error
absTargets := make([]string, 0, len(paths))
for _, target := range paths {
2018-03-03 17:02:47 +00:00
if !filepath.IsAbs(target) {
target, err = filepath.Abs(target)
if err != nil {
return nil, errors.Wrap(err, "Abs")
2018-03-03 17:02:47 +00:00
}
}
absTargets = append(absTargets, filepath.Clean(target))
2018-03-03 17:02:47 +00:00
}
var latest *Snapshot
err = ForAllSnapshots(ctx, be, loader, nil, func(id ID, snapshot *Snapshot, err error) error {
if err != nil {
2020-11-28 07:59:12 +00:00
return errors.Errorf("Error loading snapshot %v: %v", id.Str(), err)
}
if timeStampLimit != nil && snapshot.Time.After(*timeStampLimit) {
return nil
}
if latest != nil && snapshot.Time.Before(latest.Time) {
return nil
}
if !snapshot.HasHostname(hosts) {
return nil
2017-07-09 07:47:41 +00:00
}
if !snapshot.HasTagList(tags) {
return nil
}
2017-07-09 07:47:41 +00:00
2018-03-03 17:02:47 +00:00
if !snapshot.HasPaths(absTargets) {
return nil
2017-07-09 07:47:41 +00:00
}
latest = snapshot
return nil
})
if err != nil {
return nil, err
}
if latest == nil {
return nil, ErrNoSnapshotFound
}
return latest, nil
}
// FindSnapshot takes a string and tries to find a snapshot whose ID matches
// the string as closely as possible.
func FindSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, s string) (*Snapshot, error) {
// no need to list snapshots if `s` is already a full id
id, err := ParseID(s)
if err != nil {
// find snapshot id with prefix
id, err = Find(ctx, be, SnapshotFile, s)
if err != nil {
return nil, err
}
}
return LoadSnapshot(ctx, loader, id)
}
// FindFilteredSnapshot returns either the latests from a filtered list of all snapshots or a snapshot specified by `snapshotID`.
func FindFilteredSnapshot(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string, tags []TagList, paths []string, timeStampLimit *time.Time, snapshotID string) (*Snapshot, error) {
if snapshotID == "latest" {
sn, err := findLatestSnapshot(ctx, be, loader, hosts, tags, paths, timeStampLimit)
if err == ErrNoSnapshotFound {
err = fmt.Errorf("snapshot filter (Paths:%v Tags:%v Hosts:%v): %w", paths, tags, hosts, err)
}
return sn, err
}
return FindSnapshot(ctx, be, loader, snapshotID)
}
type SnapshotFindCb func(string, *Snapshot, error) error
// FindFilteredSnapshots yields Snapshots, either given explicitly by `snapshotIDs` or filtered from the list of all snapshots.
func FindFilteredSnapshots(ctx context.Context, be Lister, loader LoaderUnpacked, hosts []string, tags []TagList, paths []string, snapshotIDs []string, fn SnapshotFindCb) error {
if len(snapshotIDs) != 0 {
var err error
usedFilter := false
ids := NewIDSet()
// Process all snapshot IDs given as arguments.
for _, s := range snapshotIDs {
var sn *Snapshot
if s == "latest" {
if usedFilter {
continue
}
usedFilter = true
sn, err = findLatestSnapshot(ctx, be, loader, hosts, tags, paths, nil)
if err == ErrNoSnapshotFound {
err = errors.Errorf("no snapshot matched given filter (Paths:%v Tags:%v Hosts:%v)", paths, tags, hosts)
}
if sn != nil {
ids.Insert(*sn.ID())
}
} else {
sn, err = FindSnapshot(ctx, be, loader, s)
if err == nil {
if ids.Has(*sn.ID()) {
continue
} else {
ids.Insert(*sn.ID())
s = sn.ID().String()
}
}
}
err = fn(s, sn, err)
if err != nil {
return err
}
}
// Give the user some indication their filters are not used.
if !usedFilter && (len(hosts) != 0 || len(tags) != 0 || len(paths) != 0) {
return fn("filters", nil, errors.Errorf("explicit snapshot ids are given"))
}
return nil
}
return ForAllSnapshots(ctx, be, loader, nil, func(id ID, sn *Snapshot, err error) error {
if err != nil {
return fn(id.String(), sn, err)
}
if !sn.HasHostname(hosts) || !sn.HasTagList(tags) || !sn.HasPaths(paths) {
return nil
}
return fn(id.String(), sn, err)
})
}