2015-05-10 00:41:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-22 18:39:03 +00:00
|
|
|
"bytes"
|
2023-05-06 08:37:17 +00:00
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
2015-05-10 00:41:16 +00:00
|
|
|
"fmt"
|
2023-05-06 08:37:17 +00:00
|
|
|
"io"
|
2015-05-10 00:41:16 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-07-09 15:13:40 +00:00
|
|
|
"runtime"
|
2023-05-01 10:51:37 +00:00
|
|
|
"sync"
|
2015-06-14 13:19:11 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2022-10-15 14:33:15 +00:00
|
|
|
"github.com/restic/restic/internal/backend/retry"
|
2023-05-06 08:37:17 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/options"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
2018-03-11 19:59:40 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest "github.com/restic/restic/internal/test"
|
2023-05-01 10:51:37 +00:00
|
|
|
"github.com/restic/restic/internal/ui/termstatus"
|
2015-05-10 00:41:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type dirEntry struct {
|
|
|
|
path string
|
|
|
|
fi os.FileInfo
|
2017-01-30 23:14:20 +00:00
|
|
|
link uint64
|
2015-05-10 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func walkDir(dir string) <-chan *dirEntry {
|
|
|
|
ch := make(chan *dirEntry, 100)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := filepath.Rel(dir, path)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ch <- &dirEntry{
|
|
|
|
path: name,
|
|
|
|
fi: info,
|
2017-01-30 23:14:20 +00:00
|
|
|
link: nlink(info),
|
2015-05-10 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Walk() error: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// first element is root
|
2020-03-06 22:32:12 +00:00
|
|
|
<-ch
|
2015-05-10 00:41:16 +00:00
|
|
|
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2015-07-20 19:29:21 +00:00
|
|
|
func isSymlink(fi os.FileInfo) bool {
|
|
|
|
mode := fi.Mode() & (os.ModeType | os.ModeCharDevice)
|
|
|
|
return mode == os.ModeSymlink
|
|
|
|
}
|
|
|
|
|
|
|
|
func sameModTime(fi1, fi2 os.FileInfo) bool {
|
|
|
|
switch runtime.GOOS {
|
2022-01-26 02:05:56 +00:00
|
|
|
case "darwin", "freebsd", "openbsd", "netbsd", "solaris":
|
2015-07-20 19:29:21 +00:00
|
|
|
if isSymlink(fi1) && isSymlink(fi2) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 15:11:30 +00:00
|
|
|
return fi1.ModTime().Equal(fi2.ModTime())
|
2015-07-20 19:29:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 18:39:03 +00:00
|
|
|
// directoriesContentsDiff returns a diff between both directories. If these
|
|
|
|
// contain exactly the same contents, then the diff is an empty string.
|
|
|
|
func directoriesContentsDiff(dir1, dir2 string) string {
|
|
|
|
var out bytes.Buffer
|
2015-05-10 00:41:16 +00:00
|
|
|
ch1 := walkDir(dir1)
|
|
|
|
ch2 := walkDir(dir2)
|
|
|
|
|
|
|
|
var a, b *dirEntry
|
|
|
|
for {
|
|
|
|
var ok bool
|
|
|
|
|
|
|
|
if ch1 != nil && a == nil {
|
|
|
|
a, ok = <-ch1
|
|
|
|
if !ok {
|
|
|
|
ch1 = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch2 != nil && b == nil {
|
|
|
|
b, ok = <-ch2
|
|
|
|
if !ok {
|
|
|
|
ch2 = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch1 == nil && ch2 == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if ch1 == nil {
|
2020-08-22 18:39:03 +00:00
|
|
|
fmt.Fprintf(&out, "+%v\n", b.path)
|
2015-05-10 00:41:16 +00:00
|
|
|
} else if ch2 == nil {
|
2020-08-22 18:39:03 +00:00
|
|
|
fmt.Fprintf(&out, "-%v\n", a.path)
|
|
|
|
} else if !a.equals(&out, b) {
|
2015-05-10 00:41:16 +00:00
|
|
|
if a.path < b.path {
|
2020-08-22 18:39:03 +00:00
|
|
|
fmt.Fprintf(&out, "-%v\n", a.path)
|
2015-05-10 00:41:16 +00:00
|
|
|
a = nil
|
|
|
|
continue
|
|
|
|
} else if a.path > b.path {
|
2020-08-22 18:39:03 +00:00
|
|
|
fmt.Fprintf(&out, "+%v\n", b.path)
|
2015-05-10 00:41:16 +00:00
|
|
|
b = nil
|
|
|
|
continue
|
|
|
|
}
|
2023-12-23 12:40:04 +00:00
|
|
|
fmt.Fprintf(&out, "%%%v\n", a.path)
|
2015-05-10 00:41:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a, b = nil, nil
|
|
|
|
}
|
|
|
|
|
2020-08-22 18:39:03 +00:00
|
|
|
return out.String()
|
2015-05-10 00:41:16 +00:00
|
|
|
}
|
2015-06-14 13:19:11 +00:00
|
|
|
|
|
|
|
type dirStat struct {
|
|
|
|
files, dirs, other uint
|
|
|
|
size uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func isFile(fi os.FileInfo) bool {
|
|
|
|
return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// dirStats walks dir and collects stats.
|
|
|
|
func dirStats(dir string) (stat dirStat) {
|
|
|
|
for entry := range walkDir(dir) {
|
|
|
|
if isFile(entry.fi) {
|
|
|
|
stat.files++
|
|
|
|
stat.size += uint64(entry.fi.Size())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry.fi.IsDir() {
|
|
|
|
stat.dirs++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
stat.other++
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat
|
|
|
|
}
|
|
|
|
|
|
|
|
type testEnvironment struct {
|
2017-07-24 18:28:01 +00:00
|
|
|
base, cache, repo, mountpoint, testdata string
|
2017-07-24 19:25:49 +00:00
|
|
|
gopts GlobalOptions
|
2015-06-14 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
// withTestEnvironment creates a test environment and returns a cleanup
|
|
|
|
// function which removes it.
|
|
|
|
func withTestEnvironment(t testing.TB) (env *testEnvironment, cleanup func()) {
|
2017-10-02 15:48:08 +00:00
|
|
|
if !rtest.RunIntegrationTest {
|
2015-06-14 13:19:11 +00:00
|
|
|
t.Skip("integration tests disabled")
|
|
|
|
}
|
|
|
|
|
2016-09-01 17:06:53 +00:00
|
|
|
repository.TestUseLowSecurityKDFParameters(t)
|
2018-03-11 19:59:40 +00:00
|
|
|
restic.TestDisableCheckPolynomial(t)
|
2022-10-15 14:33:15 +00:00
|
|
|
retry.TestFastRetries(t)
|
2016-09-01 17:06:53 +00:00
|
|
|
|
2022-12-02 18:36:43 +00:00
|
|
|
tempdir, err := os.MkdirTemp(rtest.TestTempDir, "restic-test-")
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, err)
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
env = &testEnvironment{
|
2017-07-24 18:28:01 +00:00
|
|
|
base: tempdir,
|
|
|
|
cache: filepath.Join(tempdir, "cache"),
|
|
|
|
repo: filepath.Join(tempdir, "repo"),
|
|
|
|
testdata: filepath.Join(tempdir, "testdata"),
|
|
|
|
mountpoint: filepath.Join(tempdir, "mount"),
|
2015-06-14 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.OK(t, os.MkdirAll(env.mountpoint, 0700))
|
|
|
|
rtest.OK(t, os.MkdirAll(env.testdata, 0700))
|
|
|
|
rtest.OK(t, os.MkdirAll(env.cache, 0700))
|
|
|
|
rtest.OK(t, os.MkdirAll(env.repo, 0700))
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
env.gopts = GlobalOptions{
|
2016-09-17 10:36:05 +00:00
|
|
|
Repo: env.repo,
|
|
|
|
Quiet: true,
|
2017-06-10 11:10:08 +00:00
|
|
|
CacheDir: env.cache,
|
2017-10-02 15:48:08 +00:00
|
|
|
password: rtest.TestPassword,
|
2016-09-17 10:36:05 +00:00
|
|
|
stdout: os.Stdout,
|
|
|
|
stderr: os.Stderr,
|
2017-04-02 18:33:24 +00:00
|
|
|
extended: make(options.Options),
|
2021-11-07 21:39:38 +00:00
|
|
|
|
|
|
|
// replace this hook with "nil" if listing a filetype more than once is necessary
|
2023-10-01 09:40:12 +00:00
|
|
|
backendTestHook: func(r backend.Backend) (backend.Backend, error) { return newOrderedListOnceBackend(r), nil },
|
2023-06-08 11:04:34 +00:00
|
|
|
// start with default set of backends
|
|
|
|
backends: globalOptions.backends,
|
2016-09-17 10:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// always overwrite global options
|
2017-07-24 19:25:49 +00:00
|
|
|
globalOptions = env.gopts
|
2016-09-17 10:36:05 +00:00
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
cleanup = func() {
|
2017-10-02 15:48:08 +00:00
|
|
|
if !rtest.TestCleanupTempDirs {
|
2017-07-24 19:25:49 +00:00
|
|
|
t.Logf("leaving temporary directory %v used for test", tempdir)
|
|
|
|
return
|
|
|
|
}
|
2017-10-02 15:48:08 +00:00
|
|
|
rtest.RemoveAll(t, tempdir)
|
2015-06-14 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 19:25:49 +00:00
|
|
|
return env, cleanup
|
2015-06-14 13:19:11 +00:00
|
|
|
}
|
2023-05-06 08:37:17 +00:00
|
|
|
|
|
|
|
func testSetupBackupData(t testing.TB, env *testEnvironment) string {
|
|
|
|
datafile := filepath.Join("testdata", "backup-data.tar.gz")
|
|
|
|
testRunInit(t, env.gopts)
|
|
|
|
rtest.SetupTarTestFixture(t, env.testdata, datafile)
|
|
|
|
return datafile
|
|
|
|
}
|
|
|
|
|
|
|
|
func listPacks(gopts GlobalOptions, t *testing.T) restic.IDSet {
|
|
|
|
r, err := OpenRepository(context.TODO(), gopts)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
packs := restic.NewIDSet()
|
|
|
|
|
|
|
|
rtest.OK(t, r.List(context.TODO(), restic.PackFile, func(id restic.ID, size int64) error {
|
|
|
|
packs.Insert(id)
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
return packs
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePacks(gopts GlobalOptions, t testing.TB, remove restic.IDSet) {
|
|
|
|
r, err := OpenRepository(context.TODO(), gopts)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
for id := range remove {
|
2023-10-01 09:40:12 +00:00
|
|
|
rtest.OK(t, r.Backend().Remove(context.TODO(), backend.Handle{Type: restic.PackFile, Name: id.String()}))
|
2023-05-06 08:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePacksExcept(gopts GlobalOptions, t testing.TB, keep restic.IDSet, removeTreePacks bool) {
|
|
|
|
r, err := OpenRepository(context.TODO(), gopts)
|
|
|
|
rtest.OK(t, err)
|
|
|
|
|
|
|
|
// Get all tree packs
|
2023-07-16 02:48:30 +00:00
|
|
|
rtest.OK(t, r.LoadIndex(context.TODO(), nil))
|
2023-05-06 08:37:17 +00:00
|
|
|
|
|
|
|
treePacks := restic.NewIDSet()
|
|
|
|
r.Index().Each(context.TODO(), func(pb restic.PackedBlob) {
|
|
|
|
if pb.Type == restic.TreeBlob {
|
|
|
|
treePacks.Insert(pb.PackID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// remove all packs containing data blobs
|
|
|
|
rtest.OK(t, r.List(context.TODO(), restic.PackFile, func(id restic.ID, size int64) error {
|
|
|
|
if treePacks.Has(id) != removeTreePacks || keep.Has(id) {
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-01 09:40:12 +00:00
|
|
|
return r.Backend().Remove(context.TODO(), backend.Handle{Type: restic.PackFile, Name: id.String()})
|
2023-05-06 08:37:17 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func includes(haystack []string, needle string) bool {
|
|
|
|
for _, s := range haystack {
|
|
|
|
if s == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadSnapshotMap(t testing.TB, gopts GlobalOptions) map[string]struct{} {
|
|
|
|
snapshotIDs := testRunList(t, "snapshots", gopts)
|
|
|
|
|
|
|
|
m := make(map[string]struct{})
|
|
|
|
for _, id := range snapshotIDs {
|
|
|
|
m[id.String()] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func lastSnapshot(old, new map[string]struct{}) (map[string]struct{}, string) {
|
|
|
|
for k := range new {
|
|
|
|
if _, ok := old[k]; !ok {
|
|
|
|
old[k] = struct{}{}
|
|
|
|
return old, k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return old, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendRandomData(filename string, bytes uint) error {
|
|
|
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0666)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.Seek(0, 2)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(f, io.LimitReader(rand.Reader, int64(bytes)))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprint(os.Stderr, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFileSize(filename string, size int64) error {
|
|
|
|
fi, err := os.Stat(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Size() != size {
|
|
|
|
return errors.Fatalf("wrong file size for %v: expected %v, got %v", filename, size, fi.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-07 20:06:39 +00:00
|
|
|
|
|
|
|
func withRestoreGlobalOptions(inner func() error) error {
|
|
|
|
gopts := globalOptions
|
|
|
|
defer func() {
|
|
|
|
globalOptions = gopts
|
|
|
|
}()
|
|
|
|
return inner()
|
|
|
|
}
|
|
|
|
|
|
|
|
func withCaptureStdout(inner func() error) (*bytes.Buffer, error) {
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
err := withRestoreGlobalOptions(func() error {
|
|
|
|
globalOptions.stdout = buf
|
|
|
|
return inner()
|
|
|
|
})
|
|
|
|
|
|
|
|
return buf, err
|
|
|
|
}
|
2023-05-01 10:51:37 +00:00
|
|
|
|
|
|
|
func withTermStatus(gopts GlobalOptions, callback func(ctx context.Context, term *termstatus.Terminal) error) error {
|
|
|
|
ctx, cancel := context.WithCancel(context.TODO())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
term := termstatus.New(gopts.stdout, gopts.stderr, gopts.Quiet)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
term.Run(ctx)
|
|
|
|
}()
|
|
|
|
|
|
|
|
defer wg.Wait()
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
return callback(ctx, term)
|
|
|
|
}
|