2015-05-10 00:41:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-03-08 19:12:16 +00:00
|
|
|
"context"
|
2015-05-10 00:41:16 +00:00
|
|
|
"fmt"
|
2015-06-14 13:19:11 +00:00
|
|
|
"io/ioutil"
|
2015-05-10 00:41:16 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-07-09 15:13:40 +00:00
|
|
|
"runtime"
|
2015-06-14 13:19:11 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/options"
|
|
|
|
"github.com/restic/restic/internal/repository"
|
|
|
|
. "github.com/restic/restic/internal/test"
|
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
|
|
|
|
_ = <-ch
|
|
|
|
|
|
|
|
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 {
|
|
|
|
case "darwin", "freebsd", "openbsd":
|
|
|
|
if isSymlink(fi1) && isSymlink(fi2) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi1.ModTime() == fi2.ModTime()
|
|
|
|
}
|
|
|
|
|
2015-06-14 13:19:11 +00:00
|
|
|
// directoriesEqualContents checks if both directories contain exactly the same
|
|
|
|
// contents.
|
2015-05-10 00:41:16 +00:00
|
|
|
func directoriesEqualContents(dir1, dir2 string) bool {
|
|
|
|
ch1 := walkDir(dir1)
|
|
|
|
ch2 := walkDir(dir2)
|
|
|
|
|
|
|
|
changes := false
|
|
|
|
|
|
|
|
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 {
|
|
|
|
fmt.Printf("+%v\n", b.path)
|
|
|
|
changes = true
|
|
|
|
} else if ch2 == nil {
|
|
|
|
fmt.Printf("-%v\n", a.path)
|
|
|
|
changes = true
|
|
|
|
} else if !a.equals(b) {
|
|
|
|
if a.path < b.path {
|
|
|
|
fmt.Printf("-%v\n", a.path)
|
|
|
|
changes = true
|
|
|
|
a = nil
|
|
|
|
continue
|
|
|
|
} else if a.path > b.path {
|
|
|
|
fmt.Printf("+%v\n", b.path)
|
|
|
|
changes = true
|
|
|
|
b = nil
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
fmt.Printf("%%%v\n", a.path)
|
|
|
|
changes = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a, b = nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if changes {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
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
|
2015-06-14 13:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// withTestEnvironment creates a test environment and calls f with it. After f has
|
|
|
|
// returned, the temporary directory is removed.
|
2015-06-21 11:27:56 +00:00
|
|
|
func withTestEnvironment(t testing.TB, f func(*testEnvironment, GlobalOptions)) {
|
2015-06-14 13:19:11 +00:00
|
|
|
if !RunIntegrationTest {
|
|
|
|
t.Skip("integration tests disabled")
|
|
|
|
}
|
|
|
|
|
2016-09-01 17:06:53 +00:00
|
|
|
repository.TestUseLowSecurityKDFParameters(t)
|
|
|
|
|
2015-06-14 13:19:11 +00:00
|
|
|
tempdir, err := ioutil.TempDir(TestTempDir, "restic-test-")
|
|
|
|
OK(t, err)
|
|
|
|
|
|
|
|
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-07-24 18:28:01 +00:00
|
|
|
OK(t, os.MkdirAll(env.mountpoint, 0700))
|
2015-06-14 13:19:11 +00:00
|
|
|
OK(t, os.MkdirAll(env.testdata, 0700))
|
2015-08-17 09:10:12 +00:00
|
|
|
OK(t, os.MkdirAll(env.cache, 0700))
|
|
|
|
OK(t, os.MkdirAll(env.repo, 0700))
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
gopts := GlobalOptions{
|
|
|
|
Repo: env.repo,
|
|
|
|
Quiet: true,
|
2017-03-08 19:12:16 +00:00
|
|
|
ctx: context.Background(),
|
2016-09-17 10:36:05 +00:00
|
|
|
password: TestPassword,
|
|
|
|
stdout: os.Stdout,
|
|
|
|
stderr: os.Stderr,
|
2017-04-02 18:33:24 +00:00
|
|
|
extended: make(options.Options),
|
2016-09-17 10:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// always overwrite global options
|
|
|
|
globalOptions = gopts
|
|
|
|
|
|
|
|
f(&env, gopts)
|
2015-06-14 13:19:11 +00:00
|
|
|
|
2016-01-23 18:12:02 +00:00
|
|
|
if !TestCleanupTempDirs {
|
2015-06-14 13:19:11 +00:00
|
|
|
t.Logf("leaving temporary directory %v used for test", tempdir)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-08-18 19:05:49 +00:00
|
|
|
RemoveAll(t, tempdir)
|
2015-06-14 13:19:11 +00:00
|
|
|
}
|