2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-08-05 09:12:20 +00:00
|
|
|
|
2021-08-17 08:10:41 +00:00
|
|
|
//go:build integration
|
2014-08-05 13:50:05 +00:00
|
|
|
// +build integration
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
package integration
|
2014-08-05 09:12:20 +00:00
|
|
|
|
|
|
|
import (
|
2014-12-17 11:31:03 +00:00
|
|
|
cr "crypto/rand"
|
2014-08-05 09:12:20 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2014-09-27 18:09:36 +00:00
|
|
|
"math/rand"
|
2014-08-05 09:12:20 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-01-21 23:59:08 +00:00
|
|
|
"runtime"
|
2014-12-17 11:31:03 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
2015-06-18 13:22:45 +00:00
|
|
|
"testing"
|
2014-08-05 09:12:20 +00:00
|
|
|
"time"
|
2015-05-01 10:11:57 +00:00
|
|
|
"unicode"
|
2014-11-19 04:18:19 +00:00
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/rc"
|
2021-03-17 21:22:49 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2014-08-05 09:12:20 +00:00
|
|
|
)
|
|
|
|
|
2014-09-27 18:09:36 +00:00
|
|
|
func init() {
|
|
|
|
rand.Seed(42)
|
|
|
|
}
|
|
|
|
|
2014-09-16 21:14:19 +00:00
|
|
|
const (
|
|
|
|
id1 = "I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU"
|
2015-11-27 08:09:39 +00:00
|
|
|
id2 = "MRIW7OK-NETT3M4-N6SBWME-N25O76W-YJKVXPH-FUMQJ3S-P57B74J-GBITBAC"
|
2014-12-17 11:31:03 +00:00
|
|
|
id3 = "373HSRP-QLPNLIE-JYKZVQF-P4PKZ63-R2ZE6K3-YD442U2-JHBGBQG-WWXAHAU"
|
2014-09-16 21:14:19 +00:00
|
|
|
apiKey = "abc123"
|
|
|
|
)
|
|
|
|
|
2014-08-05 09:12:20 +00:00
|
|
|
func generateFiles(dir string, files, maxexp int, srcname string) error {
|
2020-07-28 09:13:15 +00:00
|
|
|
return generateFilesWithTime(dir, files, maxexp, srcname, time.Now())
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateFilesWithTime(dir string, files, maxexp int, srcname string, t0 time.Time) error {
|
2014-08-05 09:12:20 +00:00
|
|
|
fd, err := os.Open(srcname)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < files; i++ {
|
|
|
|
n := randomName()
|
2014-12-17 11:31:03 +00:00
|
|
|
|
|
|
|
if rand.Float64() < 0.05 {
|
|
|
|
// Some files and directories are dotfiles
|
|
|
|
n = "." + n
|
|
|
|
}
|
|
|
|
|
2014-08-05 09:12:20 +00:00
|
|
|
p0 := filepath.Join(dir, string(n[0]), n[0:2])
|
|
|
|
err = os.MkdirAll(p0, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-05-25 09:05:12 +00:00
|
|
|
p1 := filepath.Join(p0, n)
|
|
|
|
|
|
|
|
s := int64(1 << uint(rand.Intn(maxexp)))
|
|
|
|
a := int64(128 * 1024)
|
2014-08-05 09:12:20 +00:00
|
|
|
if a > s {
|
|
|
|
a = s
|
|
|
|
}
|
2015-05-25 09:05:12 +00:00
|
|
|
s += rand.Int63n(a)
|
2014-08-05 09:12:20 +00:00
|
|
|
|
2020-07-28 09:13:15 +00:00
|
|
|
if err := generateOneFile(fd, p1, s, t0); err != nil {
|
2014-08-05 09:12:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-25 09:05:12 +00:00
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
|
2015-05-25 09:05:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
|
2020-07-28 09:13:15 +00:00
|
|
|
func generateOneFile(fd io.ReadSeeker, p1 string, s int64, t0 time.Time) error {
|
2023-05-09 06:54:02 +00:00
|
|
|
src := io.LimitReader(&infiniteReader{fd}, int64(s))
|
2015-05-25 09:05:12 +00:00
|
|
|
dst, err := os.Create(p1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
|
2015-05-25 09:05:12 +00:00
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
|
2015-05-25 09:05:12 +00:00
|
|
|
err = dst.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-02 11:16:27 +00:00
|
|
|
os.Chmod(p1, os.FileMode(rand.Intn(0777)|0400))
|
2015-05-25 09:05:12 +00:00
|
|
|
|
2020-07-28 09:13:15 +00:00
|
|
|
t := t0.Add(-time.Duration(rand.Intn(30*86400)) * time.Second)
|
2015-05-25 09:05:12 +00:00
|
|
|
err = os.Chtimes(p1, t, t)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
func alterFiles(dir string) error {
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Something we deleted. Never mind.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:31:13 +00:00
|
|
|
info, err = os.Stat(path)
|
2014-12-17 11:31:03 +00:00
|
|
|
if err != nil {
|
2015-02-21 23:35:19 +00:00
|
|
|
// Something we deleted while walking. Ignore.
|
|
|
|
return nil
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
|
2014-12-20 23:12:12 +00:00
|
|
|
if strings.HasPrefix(filepath.Base(path), "test-") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
switch filepath.Base(path) {
|
|
|
|
case ".stfolder":
|
|
|
|
return nil
|
|
|
|
case ".stversions":
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:31:13 +00:00
|
|
|
// File structure is base/x/xy/xyz12345...
|
|
|
|
// comps == 1: base (don't touch)
|
|
|
|
// comps == 2: base/x (must be dir)
|
|
|
|
// comps == 3: base/x/xy (must be dir)
|
|
|
|
// comps > 3: base/x/xy/xyz12345... (can be dir or file)
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
comps := len(strings.Split(path, string(os.PathSeparator)))
|
2015-03-01 11:31:13 +00:00
|
|
|
|
|
|
|
r := rand.Intn(10)
|
2014-12-17 11:31:03 +00:00
|
|
|
switch {
|
2015-03-01 11:31:13 +00:00
|
|
|
case r == 0 && comps > 2:
|
2014-12-17 11:31:03 +00:00
|
|
|
// Delete every tenth file or directory, except top levels
|
2015-05-08 07:31:29 +00:00
|
|
|
return removeAll(path)
|
2014-12-17 11:31:03 +00:00
|
|
|
|
2015-03-01 11:31:13 +00:00
|
|
|
case r == 1 && info.Mode().IsRegular():
|
2014-12-17 11:31:03 +00:00
|
|
|
if info.Mode()&0200 != 0200 {
|
|
|
|
// Not owner writable. Fix.
|
2015-03-08 07:41:43 +00:00
|
|
|
if err = os.Chmod(path, 0644); err != nil {
|
2014-12-17 11:31:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Overwrite a random kilobyte of every tenth file
|
|
|
|
fd, err := os.OpenFile(path, os.O_RDWR, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Size() > 1024 {
|
|
|
|
_, err = fd.Seek(rand.Int63n(info.Size()), os.SEEK_SET)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = io.Copy(fd, io.LimitReader(cr.Reader, 1024))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-05-08 07:31:29 +00:00
|
|
|
return fd.Close()
|
2015-03-01 11:31:13 +00:00
|
|
|
|
2015-05-01 10:11:57 +00:00
|
|
|
// Change capitalization
|
|
|
|
case r == 2 && comps > 3 && rand.Float64() < 0.2:
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsDarwin || build.IsWindows {
|
2015-06-18 13:22:45 +00:00
|
|
|
// Syncthing is currently broken for case-only renames on case-
|
|
|
|
// insensitive platforms.
|
|
|
|
// https://github.com/syncthing/syncthing/issues/1787
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-01 10:11:57 +00:00
|
|
|
base := []rune(filepath.Base(path))
|
|
|
|
for i, r := range base {
|
|
|
|
if rand.Float64() < 0.5 {
|
|
|
|
base[i] = unicode.ToLower(r)
|
|
|
|
} else {
|
|
|
|
base[i] = unicode.ToUpper(r)
|
|
|
|
}
|
|
|
|
}
|
2015-05-08 07:31:29 +00:00
|
|
|
newPath := filepath.Join(filepath.Dir(path), string(base))
|
|
|
|
if newPath != path {
|
2017-11-05 09:05:18 +00:00
|
|
|
return os.Rename(path, newPath)
|
2015-05-08 07:31:29 +00:00
|
|
|
}
|
2015-05-01 10:11:57 +00:00
|
|
|
|
2016-01-03 18:08:19 +00:00
|
|
|
/*
|
|
|
|
This doesn't in fact work. Sometimes it appears to. We need to get this sorted...
|
|
|
|
|
|
|
|
// Switch between files and directories
|
|
|
|
case r == 3 && comps > 3 && rand.Float64() < 0.2:
|
|
|
|
if !info.Mode().IsRegular() {
|
|
|
|
err = removeAll(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d1 := []byte("I used to be a dir: " + path)
|
2021-11-22 07:59:47 +00:00
|
|
|
err := os.WriteFile(path, d1, 0644)
|
2016-01-03 18:08:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2016-08-16 10:01:58 +00:00
|
|
|
err := os.Remove(path)
|
2016-01-03 18:08:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(path, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
generateFiles(path, 10, 20, "../LICENSE")
|
|
|
|
}
|
2015-02-21 23:35:19 +00:00
|
|
|
return err
|
2016-01-03 18:08:19 +00:00
|
|
|
*/
|
2015-03-01 11:31:13 +00:00
|
|
|
|
2015-06-18 13:22:45 +00:00
|
|
|
/*
|
|
|
|
This fails. Bug?
|
|
|
|
|
2015-11-12 02:20:34 +00:00
|
|
|
// Rename the file, while potentially moving it up in the directory hierarchy
|
2015-06-18 13:22:45 +00:00
|
|
|
case r == 4 && comps > 2 && (info.Mode().IsRegular() || rand.Float64() < 0.2):
|
|
|
|
rpath := filepath.Dir(path)
|
|
|
|
if rand.Float64() < 0.2 {
|
|
|
|
for move := rand.Intn(comps - 1); move > 0; move-- {
|
|
|
|
rpath = filepath.Join(rpath, "..")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return osutil.TryRename(path, filepath.Join(rpath, randomName()))
|
|
|
|
*/
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-01 11:31:13 +00:00
|
|
|
return generateFiles(dir, 25, 20, "../LICENSE")
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 18:09:36 +00:00
|
|
|
func ReadRand(bs []byte) (int, error) {
|
|
|
|
var r uint32
|
|
|
|
for i := range bs {
|
|
|
|
if i%4 == 0 {
|
|
|
|
r = uint32(rand.Int63())
|
|
|
|
}
|
|
|
|
bs[i] = byte(r >> uint((i%4)*8))
|
|
|
|
}
|
|
|
|
return len(bs), nil
|
|
|
|
}
|
|
|
|
|
2014-08-05 09:12:20 +00:00
|
|
|
func randomName() string {
|
|
|
|
var b [16]byte
|
2014-09-27 18:09:36 +00:00
|
|
|
ReadRand(b[:])
|
2014-08-05 09:12:20 +00:00
|
|
|
return fmt.Sprintf("%x", b[:])
|
|
|
|
}
|
|
|
|
|
2023-05-09 06:54:02 +00:00
|
|
|
type infiniteReader struct {
|
2014-08-05 09:12:20 +00:00
|
|
|
rd io.ReadSeeker
|
|
|
|
}
|
|
|
|
|
2023-05-09 06:54:02 +00:00
|
|
|
func (i *infiniteReader) Read(bs []byte) (int, error) {
|
2014-08-05 09:12:20 +00:00
|
|
|
n, err := i.rd.Read(bs)
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
i.rd.Seek(0, 0)
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// rm -rf
|
|
|
|
func removeAll(dirs ...string) error {
|
|
|
|
for _, dir := range dirs {
|
2017-11-05 09:05:18 +00:00
|
|
|
files, err := filepath.Glob(dir)
|
2015-03-29 10:55:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, file := range files {
|
|
|
|
// Set any non-writeable files and dirs to writeable. This is necessary for os.RemoveAll to work on Windows.
|
|
|
|
filepath.Walk(file, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if info.Mode()&0700 != 0700 {
|
|
|
|
os.Chmod(path, 0777)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
os.RemoveAll(file)
|
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare a number of directories. Returns nil if the contents are identical,
|
|
|
|
// otherwise an error describing the first found difference.
|
|
|
|
func compareDirectories(dirs ...string) error {
|
|
|
|
chans := make([]chan fileInfo, len(dirs))
|
|
|
|
for i := range chans {
|
|
|
|
chans[i] = make(chan fileInfo)
|
|
|
|
}
|
2014-12-30 14:13:58 +00:00
|
|
|
errcs := make([]chan error, len(dirs))
|
2014-08-05 09:12:20 +00:00
|
|
|
abort := make(chan struct{})
|
|
|
|
|
|
|
|
for i := range dirs {
|
2014-12-30 14:13:58 +00:00
|
|
|
errcs[i] = startWalker(dirs[i], chans[i], abort)
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]fileInfo, len(dirs))
|
|
|
|
for {
|
|
|
|
numDone := 0
|
|
|
|
for i := range chans {
|
|
|
|
fi, ok := <-chans[i]
|
|
|
|
if !ok {
|
2014-12-30 14:13:58 +00:00
|
|
|
err, hasError := <-errcs[i]
|
|
|
|
if hasError {
|
|
|
|
close(abort)
|
|
|
|
return err
|
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
numDone++
|
|
|
|
}
|
|
|
|
res[i] = fi
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < len(res); i++ {
|
|
|
|
if res[i] != res[0] {
|
|
|
|
close(abort)
|
2020-03-03 21:40:00 +00:00
|
|
|
return fmt.Errorf("mismatch; %#v (%s) != %#v (%s)", res[i], dirs[i], res[0], dirs[0])
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if numDone == len(dirs) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-30 14:13:58 +00:00
|
|
|
func directoryContents(dir string) ([]fileInfo, error) {
|
2014-12-17 11:31:03 +00:00
|
|
|
res := make(chan fileInfo)
|
2014-12-30 14:13:58 +00:00
|
|
|
errc := startWalker(dir, res, nil)
|
2014-12-17 11:31:03 +00:00
|
|
|
|
|
|
|
var files []fileInfo
|
|
|
|
for f := range res {
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
|
|
|
|
2014-12-30 14:13:58 +00:00
|
|
|
return files, <-errc
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mergeDirectoryContents(c ...[]fileInfo) []fileInfo {
|
|
|
|
m := make(map[string]fileInfo)
|
|
|
|
|
|
|
|
for _, l := range c {
|
|
|
|
for _, f := range l {
|
|
|
|
if cur, ok := m[f.name]; !ok || cur.mod < f.mod {
|
|
|
|
m[f.name] = f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]fileInfo, len(m))
|
|
|
|
i := 0
|
|
|
|
for _, f := range m {
|
|
|
|
res[i] = f
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(fileInfoList(res))
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareDirectoryContents(actual, expected []fileInfo) error {
|
|
|
|
if len(actual) != len(expected) {
|
|
|
|
return fmt.Errorf("len(actual) = %d; len(expected) = %d", len(actual), len(expected))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range actual {
|
|
|
|
if actual[i] != expected[i] {
|
2020-03-03 21:40:00 +00:00
|
|
|
return fmt.Errorf("mismatch; actual %#v != expected %#v", actual[i], expected[i])
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-05 09:12:20 +00:00
|
|
|
type fileInfo struct {
|
|
|
|
name string
|
|
|
|
mode os.FileMode
|
2014-09-16 21:14:19 +00:00
|
|
|
mod int64
|
2021-03-17 21:22:49 +00:00
|
|
|
hash [sha256.Size]byte
|
2015-05-25 09:05:12 +00:00
|
|
|
size int64
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
|
2014-12-30 14:44:05 +00:00
|
|
|
func (f fileInfo) String() string {
|
|
|
|
return fmt.Sprintf("%s %04o %d %x", f.name, f.mode, f.mod, f.hash)
|
|
|
|
}
|
|
|
|
|
2014-12-17 11:31:03 +00:00
|
|
|
type fileInfoList []fileInfo
|
|
|
|
|
|
|
|
func (l fileInfoList) Len() int {
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l fileInfoList) Less(a, b int) bool {
|
|
|
|
return l[a].name < l[b].name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l fileInfoList) Swap(a, b int) {
|
|
|
|
l[a], l[b] = l[b], l[a]
|
|
|
|
}
|
|
|
|
|
2014-12-30 14:13:58 +00:00
|
|
|
func startWalker(dir string, res chan<- fileInfo, abort <-chan struct{}) chan error {
|
2014-08-05 09:12:20 +00:00
|
|
|
walker := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rn, _ := filepath.Rel(dir, path)
|
2014-10-15 07:53:49 +00:00
|
|
|
if rn == "." || rn == ".stfolder" {
|
2014-08-05 09:12:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-03 10:03:44 +00:00
|
|
|
if rn == ".stversions" {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
2014-08-05 09:12:20 +00:00
|
|
|
|
|
|
|
var f fileInfo
|
2014-12-03 10:03:44 +00:00
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
2014-11-19 04:18:19 +00:00
|
|
|
f = fileInfo{
|
|
|
|
name: rn,
|
|
|
|
mode: os.ModeSymlink,
|
|
|
|
}
|
|
|
|
|
2017-02-07 08:34:24 +00:00
|
|
|
tgt, err := os.Readlink(path)
|
2014-11-19 04:18:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-17 21:22:49 +00:00
|
|
|
f.hash = sha256.Sum256([]byte(tgt))
|
2014-11-19 04:18:19 +00:00
|
|
|
} else if info.IsDir() {
|
2014-08-05 09:12:20 +00:00
|
|
|
f = fileInfo{
|
|
|
|
name: rn,
|
|
|
|
mode: info.Mode(),
|
|
|
|
// hash and modtime zero for directories
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f = fileInfo{
|
|
|
|
name: rn,
|
|
|
|
mode: info.Mode(),
|
2018-02-03 09:28:37 +00:00
|
|
|
// comparing timestamps with better precision than a second
|
|
|
|
// is problematic as there is rounding and truncatign going
|
|
|
|
// on at every level
|
|
|
|
mod: info.ModTime().Unix(),
|
2015-05-25 09:05:12 +00:00
|
|
|
size: info.Size(),
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
2021-03-17 21:22:49 +00:00
|
|
|
sum, err := sha256file(path)
|
2014-08-05 09:12:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.hash = sum
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case res <- f:
|
|
|
|
return nil
|
|
|
|
case <-abort:
|
|
|
|
return errors.New("abort")
|
|
|
|
}
|
|
|
|
}
|
2014-12-30 14:13:58 +00:00
|
|
|
|
|
|
|
errc := make(chan error)
|
2014-08-05 09:12:20 +00:00
|
|
|
go func() {
|
2014-12-30 14:13:58 +00:00
|
|
|
err := filepath.Walk(dir, walker)
|
2014-08-05 09:12:20 +00:00
|
|
|
close(res)
|
2014-12-30 14:13:58 +00:00
|
|
|
if err != nil {
|
|
|
|
errc <- err
|
|
|
|
}
|
|
|
|
close(errc)
|
2014-08-05 09:12:20 +00:00
|
|
|
}()
|
2014-12-30 14:13:58 +00:00
|
|
|
return errc
|
2014-08-05 09:12:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 21:22:49 +00:00
|
|
|
func sha256file(fname string) (hash [sha256.Size]byte, err error) {
|
2014-08-05 09:12:20 +00:00
|
|
|
f, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
2021-03-17 21:22:49 +00:00
|
|
|
h := sha256.New()
|
2014-08-05 09:12:20 +00:00
|
|
|
io.Copy(h, f)
|
|
|
|
hb := h.Sum(nil)
|
|
|
|
copy(hash[:], hb)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2014-12-17 11:31:03 +00:00
|
|
|
|
|
|
|
func isTimeout(err error) bool {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.Contains(err.Error(), "use of closed network connection") ||
|
2018-05-30 11:23:52 +00:00
|
|
|
strings.Contains(err.Error(), "request canceled while waiting") ||
|
|
|
|
strings.Contains(err.Error(), "operation timed out")
|
2014-12-17 11:31:03 +00:00
|
|
|
}
|
2015-01-21 23:59:08 +00:00
|
|
|
|
|
|
|
func getTestName() string {
|
|
|
|
callers := make([]uintptr, 100)
|
|
|
|
runtime.Callers(1, callers)
|
|
|
|
for i, caller := range callers {
|
|
|
|
f := runtime.FuncForPC(caller)
|
|
|
|
if f != nil {
|
|
|
|
if f.Name() == "testing.tRunner" {
|
|
|
|
testf := runtime.FuncForPC(callers[i-1])
|
|
|
|
if testf != nil {
|
|
|
|
path := strings.Split(testf.Name(), ".")
|
|
|
|
return path[len(path)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return time.Now().String()
|
|
|
|
}
|
2015-06-18 13:22:45 +00:00
|
|
|
|
|
|
|
func checkedStop(t *testing.T, p *rc.Process) {
|
|
|
|
if _, err := p.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startInstance(t *testing.T, i int) *rc.Process {
|
|
|
|
log.Printf("Starting instance %d...", i)
|
|
|
|
addr := fmt.Sprintf("127.0.0.1:%d", 8080+i)
|
|
|
|
log := fmt.Sprintf("logs/%s-%d-%d.out", getTestName(), i, time.Now().Unix()%86400)
|
|
|
|
|
|
|
|
p := rc.NewProcess(addr)
|
|
|
|
p.LogTo(log)
|
2021-11-04 07:42:55 +00:00
|
|
|
if err := p.Start("../bin/syncthing", "--home", fmt.Sprintf("h%d", i), "--no-browser"); err != nil {
|
2015-06-18 13:22:45 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
p.AwaitStartup()
|
2017-11-12 22:28:45 +00:00
|
|
|
p.PauseAll()
|
2015-06-18 13:22:45 +00:00
|
|
|
return p
|
|
|
|
}
|
2016-03-30 06:53:47 +00:00
|
|
|
|
|
|
|
func symlinksSupported() bool {
|
2021-11-22 07:59:47 +00:00
|
|
|
tmp, err := os.MkdirTemp("", "symlink-test")
|
2016-03-30 06:53:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
err = os.Symlink("tmp", filepath.Join(tmp, "link"))
|
|
|
|
return err == nil
|
|
|
|
}
|
2020-09-07 07:35:37 +00:00
|
|
|
|
|
|
|
// checkRemoteInSync checks if the devices associated twith the given processes
|
|
|
|
// are in sync according to the remote status on both sides.
|
|
|
|
func checkRemoteInSync(folder string, p1, p2 *rc.Process) error {
|
|
|
|
if inSync, err := p1.RemoteInSync(folder, p2.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !inSync {
|
|
|
|
return fmt.Errorf(`from device %v folder "%v" is not in sync on device %v`, p1.ID(), folder, p2.ID())
|
|
|
|
}
|
|
|
|
if inSync, err := p2.RemoteInSync(folder, p1.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !inSync {
|
|
|
|
return fmt.Errorf(`from device %v folder "%v" is not in sync on device %v`, p2.ID(), folder, p1.ID())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|