2014-12-05 21:45:49 +01:00
|
|
|
package restic
|
2014-08-04 20:47:04 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-04 11:16:55 +02:00
|
|
|
"context"
|
2014-08-11 22:47:24 +02:00
|
|
|
"fmt"
|
2015-04-29 17:35:02 -04:00
|
|
|
"os/user"
|
2014-09-23 22:39:12 +02:00
|
|
|
"path/filepath"
|
2020-11-28 09:32:06 +01:00
|
|
|
"sync"
|
2014-08-04 20:47:04 +02:00
|
|
|
"time"
|
2017-07-23 14:21:03 +02:00
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
2014-08-04 20:47:04 +02:00
|
|
|
)
|
|
|
|
|
2016-05-08 22:38:38 +02:00
|
|
|
// Snapshot is the state of a resource at one point in time.
|
2014-08-04 20:47:04 +02:00
|
|
|
type Snapshot struct {
|
2016-08-31 20:29:54 +02:00
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Parent *ID `json:"parent,omitempty"`
|
|
|
|
Tree *ID `json:"tree"`
|
|
|
|
Paths []string `json:"paths"`
|
|
|
|
Hostname string `json:"hostname,omitempty"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
UID uint32 `json:"uid,omitempty"`
|
|
|
|
GID uint32 `json:"gid,omitempty"`
|
|
|
|
Excludes []string `json:"excludes,omitempty"`
|
2016-09-13 20:12:55 +02:00
|
|
|
Tags []string `json:"tags,omitempty"`
|
2017-03-05 17:51:57 +01:00
|
|
|
Original *ID `json:"original,omitempty"`
|
2016-08-31 20:29:54 +02:00
|
|
|
|
2023-06-19 19:30:41 +02:00
|
|
|
ProgramVersion string `json:"program_version,omitempty"`
|
|
|
|
|
2016-08-31 20:29:54 +02:00
|
|
|
id *ID // plaintext ID, used during restore
|
2014-08-04 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 22:38:38 +02:00
|
|
|
// NewSnapshot returns an initialized snapshot struct for the current user and
|
|
|
|
// time.
|
2017-08-29 18:29:46 +02:00
|
|
|
func NewSnapshot(paths []string, tags []string, hostname string, time time.Time) (*Snapshot, error) {
|
2018-01-03 22:10:20 +01:00
|
|
|
absPaths := make([]string, 0, len(paths))
|
|
|
|
for _, path := range paths {
|
|
|
|
p, err := filepath.Abs(path)
|
|
|
|
if err == nil {
|
|
|
|
absPaths = append(absPaths, p)
|
|
|
|
} else {
|
|
|
|
absPaths = append(absPaths, path)
|
2015-03-02 14:48:47 +01:00
|
|
|
}
|
2014-09-23 22:39:12 +02:00
|
|
|
}
|
|
|
|
|
2014-08-04 20:47:04 +02:00
|
|
|
sn := &Snapshot{
|
2018-01-03 22:10:20 +01:00
|
|
|
Paths: absPaths,
|
2017-08-29 18:29:46 +02:00
|
|
|
Time: time,
|
2017-02-10 19:37:33 +01:00
|
|
|
Tags: tags,
|
|
|
|
Hostname: hostname,
|
2014-08-04 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:37:33 +01:00
|
|
|
err := sn.fillUserInfo()
|
2015-02-03 22:04:09 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-08-04 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
2014-12-21 17:20:49 +01:00
|
|
|
return sn, nil
|
2014-08-04 20:47:04 +02:00
|
|
|
}
|
|
|
|
|
2016-05-08 22:38:38 +02:00
|
|
|
// LoadSnapshot loads the snapshot with the id and returns it.
|
2022-06-12 14:38:19 +02:00
|
|
|
func LoadSnapshot(ctx context.Context, loader LoaderUnpacked, id ID) (*Snapshot, error) {
|
2015-07-25 17:05:45 +02:00
|
|
|
sn := &Snapshot{id: &id}
|
2022-06-12 14:38:19 +02:00
|
|
|
err := LoadJSONUnpacked(ctx, loader, SnapshotFile, id, sn)
|
2014-08-04 22:46:14 +02:00
|
|
|
if err != nil {
|
2023-05-01 17:24:13 +02:00
|
|
|
return nil, fmt.Errorf("failed to load snapshot %v: %w", id.Str(), err)
|
2014-08-04 22:46:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sn, nil
|
2014-08-04 20:47:04 +02:00
|
|
|
}
|
2014-08-11 22:47:24 +02:00
|
|
|
|
2022-06-12 14:38:19 +02:00
|
|
|
// SaveSnapshot saves the snapshot sn and returns its ID.
|
|
|
|
func SaveSnapshot(ctx context.Context, repo SaverUnpacked, sn *Snapshot) (ID, error) {
|
|
|
|
return SaveJSONUnpacked(ctx, repo, SnapshotFile, sn)
|
|
|
|
}
|
|
|
|
|
2020-11-28 09:32:06 +01:00
|
|
|
// ForAllSnapshots reads all snapshots in parallel and calls the
|
|
|
|
// given function. It is guaranteed that the function is not run concurrently.
|
|
|
|
// If the called function returns an error, this function is cancelled and
|
|
|
|
// also returns this error.
|
2020-11-28 08:59:12 +01:00
|
|
|
// If a snapshot ID is in excludeIDs, it will be ignored.
|
2022-06-12 14:38:19 +02:00
|
|
|
func ForAllSnapshots(ctx context.Context, be Lister, loader LoaderUnpacked, excludeIDs IDSet, fn func(ID, *Snapshot, error) error) error {
|
2020-11-28 09:32:06 +01:00
|
|
|
var m sync.Mutex
|
|
|
|
|
2022-10-15 17:24:47 +02:00
|
|
|
// For most snapshots decoding is nearly for free, thus just assume were only limited by IO
|
|
|
|
return ParallelList(ctx, be, SnapshotFile, loader.Connections(), func(ctx context.Context, id ID, size int64) error {
|
|
|
|
if excludeIDs.Has(id) {
|
2020-07-19 07:13:41 +02:00
|
|
|
return nil
|
2020-11-28 09:32:06 +01:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:24:47 +02:00
|
|
|
sn, err := LoadSnapshot(ctx, loader, id)
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
return fn(id, sn, err)
|
|
|
|
})
|
2020-11-28 08:59:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-24 21:12:32 +01:00
|
|
|
func (sn Snapshot) String() string {
|
2016-08-19 20:50:52 +02:00
|
|
|
return fmt.Sprintf("<Snapshot %s of %v at %s by %s@%s>",
|
|
|
|
sn.id.Str(), sn.Paths, sn.Time, sn.Username, sn.Hostname)
|
2014-08-11 22:47:24 +02:00
|
|
|
}
|
2014-11-24 21:12:32 +01:00
|
|
|
|
2017-02-09 06:43:10 +07:00
|
|
|
// ID returns the snapshot's ID.
|
2016-08-31 20:29:54 +02:00
|
|
|
func (sn Snapshot) ID() *ID {
|
2014-11-24 21:12:32 +01:00
|
|
|
return sn.id
|
|
|
|
}
|
2015-04-29 17:35:02 -04:00
|
|
|
|
|
|
|
func (sn *Snapshot) fillUserInfo() error {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
2015-05-14 23:13:00 +02:00
|
|
|
return nil
|
2015-04-29 17:35:02 -04:00
|
|
|
}
|
|
|
|
sn.Username = usr.Username
|
2015-05-14 23:13:00 +02:00
|
|
|
|
2015-08-16 13:16:02 +02:00
|
|
|
// set userid and groupid
|
2022-10-14 17:40:49 +02:00
|
|
|
sn.UID, sn.GID, err = uidGidInt(usr)
|
2015-08-16 13:16:02 +02:00
|
|
|
return err
|
2015-04-29 17:35:02 -04:00
|
|
|
}
|
2015-05-17 20:48:59 +02:00
|
|
|
|
2017-03-05 17:43:18 +01:00
|
|
|
// AddTags adds the given tags to the snapshots tags, preventing duplicates.
|
|
|
|
// It returns true if any changes were made.
|
|
|
|
func (sn *Snapshot) AddTags(addTags []string) (changed bool) {
|
|
|
|
nextTag:
|
|
|
|
for _, add := range addTags {
|
|
|
|
for _, tag := range sn.Tags {
|
|
|
|
if tag == add {
|
|
|
|
continue nextTag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sn.Tags = append(sn.Tags, add)
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTags removes the given tags from the snapshots tags and
|
|
|
|
// returns true if any changes were made.
|
|
|
|
func (sn *Snapshot) RemoveTags(removeTags []string) (changed bool) {
|
|
|
|
for _, remove := range removeTags {
|
|
|
|
for i, tag := range sn.Tags {
|
|
|
|
if tag == remove {
|
|
|
|
// https://github.com/golang/go/wiki/SliceTricks
|
|
|
|
sn.Tags[i] = sn.Tags[len(sn.Tags)-1]
|
|
|
|
sn.Tags[len(sn.Tags)-1] = ""
|
|
|
|
sn.Tags = sn.Tags[:len(sn.Tags)-1]
|
|
|
|
|
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-07 03:19:06 +02:00
|
|
|
func (sn *Snapshot) hasTag(tag string) bool {
|
|
|
|
for _, snTag := range sn.Tags {
|
|
|
|
if tag == snTag {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-09 09:24:02 +02:00
|
|
|
// HasTags returns true if the snapshot has all the tags in l.
|
|
|
|
func (sn *Snapshot) HasTags(l []string) bool {
|
|
|
|
for _, tag := range l {
|
2021-07-15 11:38:15 +10:00
|
|
|
if tag == "" && len(sn.Tags) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2017-07-09 09:24:02 +02:00
|
|
|
if !sn.hasTag(tag) {
|
|
|
|
return false
|
2016-09-13 20:13:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 09:24:02 +02:00
|
|
|
return true
|
2016-09-13 20:13:04 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:17:59 +01:00
|
|
|
// HasTagList returns true if either
|
2022-08-19 19:12:26 +02:00
|
|
|
// - the snapshot satisfies at least one TagList, so there is a TagList in l
|
|
|
|
// for which all tags are included in sn, or
|
|
|
|
// - l is empty
|
2017-07-09 09:47:41 +02:00
|
|
|
func (sn *Snapshot) HasTagList(l []TagList) bool {
|
|
|
|
debug.Log("testing snapshot with tags %v against list: %v", sn.Tags, l)
|
|
|
|
|
|
|
|
if len(l) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range l {
|
|
|
|
if sn.HasTags(tags) {
|
2017-09-25 14:35:37 +02:00
|
|
|
debug.Log(" snapshot satisfies %v %v", tags, l)
|
2017-07-09 09:47:41 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-09 09:24:02 +02:00
|
|
|
// HasPaths returns true if the snapshot has all of the paths.
|
2017-03-06 02:21:58 +01:00
|
|
|
func (sn *Snapshot) HasPaths(paths []string) bool {
|
2022-08-30 04:38:17 +03:00
|
|
|
m := make(map[string]struct{}, len(sn.Paths))
|
|
|
|
for _, snPath := range sn.Paths {
|
|
|
|
m[snPath] = struct{}{}
|
|
|
|
}
|
2017-03-06 02:21:58 +01:00
|
|
|
for _, path := range paths {
|
2022-08-30 04:38:17 +03:00
|
|
|
if _, ok := m[path]; !ok {
|
2017-07-09 09:24:02 +02:00
|
|
|
return false
|
2016-05-10 21:51:56 +02:00
|
|
|
}
|
2016-05-10 21:23:18 +02:00
|
|
|
}
|
|
|
|
|
2017-07-09 09:24:02 +02:00
|
|
|
return true
|
2017-03-06 02:21:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-26 22:17:59 +01:00
|
|
|
// HasHostname returns true if either
|
|
|
|
// - the snapshot hostname is in the list of the given hostnames, or
|
|
|
|
// - the list of given hostnames is empty
|
|
|
|
func (sn *Snapshot) HasHostname(hostnames []string) bool {
|
|
|
|
if len(hostnames) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, hostname := range hostnames {
|
|
|
|
if sn.Hostname == hostname {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-06-18 13:05:47 +02:00
|
|
|
// Snapshots is a list of snapshots.
|
|
|
|
type Snapshots []*Snapshot
|
|
|
|
|
|
|
|
// Len returns the number of snapshots in sn.
|
|
|
|
func (sn Snapshots) Len() int {
|
|
|
|
return len(sn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less returns true iff the ith snapshot has been made after the jth.
|
|
|
|
func (sn Snapshots) Less(i, j int) bool {
|
|
|
|
return sn[i].Time.After(sn[j].Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap exchanges the two snapshots.
|
|
|
|
func (sn Snapshots) Swap(i, j int) {
|
|
|
|
sn[i], sn[j] = sn[j], sn[i]
|
|
|
|
}
|