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-06-01 20:50:14 +00:00
|
|
|
|
2014-03-08 22:02:01 +00:00
|
|
|
package scanner
|
2014-03-02 22:58:14 +00:00
|
|
|
|
|
|
|
import (
|
2014-08-26 08:11:25 +00:00
|
|
|
"bytes"
|
2017-04-26 00:15:23 +00:00
|
|
|
"context"
|
2015-10-27 08:26:08 +00:00
|
|
|
"crypto/rand"
|
2014-03-02 22:58:14 +00:00
|
|
|
"fmt"
|
2015-10-27 08:26:08 +00:00
|
|
|
"io"
|
2018-01-24 00:05:47 +00:00
|
|
|
"io/ioutil"
|
2015-03-18 22:54:50 +00:00
|
|
|
"os"
|
2014-08-16 16:33:01 +00:00
|
|
|
"path/filepath"
|
2015-03-18 22:54:50 +00:00
|
|
|
"runtime"
|
2014-08-30 07:55:01 +00:00
|
|
|
rdebug "runtime/debug"
|
2014-07-30 18:10:46 +00:00
|
|
|
"sort"
|
2015-10-27 08:26:08 +00:00
|
|
|
"sync"
|
2014-03-02 22:58:14 +00:00
|
|
|
"testing"
|
2014-07-17 12:48:02 +00:00
|
|
|
|
2016-03-06 20:32:10 +00:00
|
|
|
"github.com/d4l3k/messagediff"
|
2017-04-01 09:04:11 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore"
|
2017-11-25 21:49:53 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2018-01-14 14:30:11 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
2015-03-18 22:54:50 +00:00
|
|
|
"golang.org/x/text/unicode/norm"
|
2014-03-02 22:58:14 +00:00
|
|
|
)
|
|
|
|
|
2014-08-30 07:22:23 +00:00
|
|
|
type testfile struct {
|
2016-07-04 10:40:29 +00:00
|
|
|
name string
|
|
|
|
length int64
|
|
|
|
hash string
|
2014-08-30 07:22:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testfileList []testfile
|
|
|
|
|
|
|
|
var testdata = testfileList{
|
|
|
|
{"afile", 4, "b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c"},
|
|
|
|
{"dir1", 128, ""},
|
2014-08-31 21:32:27 +00:00
|
|
|
{filepath.Join("dir1", "dfile"), 5, "49ae93732fcf8d63fe1cce759664982dbd5b23161f007dba8561862adc96d063"},
|
2014-08-30 07:22:23 +00:00
|
|
|
{"dir2", 128, ""},
|
2014-08-31 21:32:27 +00:00
|
|
|
{filepath.Join("dir2", "cfile"), 4, "bf07a7fbb825fc0aae7bf4a1177b2b31fcf8a3feeaf7092761e18c859ee52a9c"},
|
2014-09-04 20:29:53 +00:00
|
|
|
{"excludes", 37, "df90b52f0c55dba7a7a940affe482571563b1ac57bd5be4d8a0291e7de928e06"},
|
2014-08-30 07:22:23 +00:00
|
|
|
{"further-excludes", 5, "7eb0a548094fa6295f7fd9200d69973e5f5ec5c04f2a86d998080ac43ecf89f1"},
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2014-08-30 07:55:01 +00:00
|
|
|
func init() {
|
|
|
|
// This test runs the risk of entering infinite recursion if it fails.
|
2015-04-28 15:34:55 +00:00
|
|
|
// Limit the stack size to 10 megs to crash early in that case instead of
|
2014-08-30 07:55:01 +00:00
|
|
|
// potentially taking down the box...
|
|
|
|
rdebug.SetMaxStack(10 * 1 << 20)
|
|
|
|
}
|
|
|
|
|
2014-08-11 18:20:01 +00:00
|
|
|
func TestWalkSub(t *testing.T) {
|
2017-08-19 14:36:56 +00:00
|
|
|
ignores := ignore.New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."))
|
2014-12-23 09:05:08 +00:00
|
|
|
err := ignores.Load("testdata/.stignore")
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
fchan := Walk(context.TODO(), Config{
|
2017-08-19 14:36:56 +00:00
|
|
|
Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"),
|
|
|
|
Subs: []string{"dir2"},
|
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
Matcher: ignores,
|
|
|
|
Hashers: 2,
|
2016-05-09 18:25:39 +00:00
|
|
|
})
|
2018-02-14 07:59:46 +00:00
|
|
|
var files []protocol.FileInfo
|
2014-08-11 18:20:01 +00:00
|
|
|
for f := range fchan {
|
|
|
|
files = append(files, f)
|
|
|
|
}
|
|
|
|
|
2014-08-30 07:22:23 +00:00
|
|
|
// The directory contains two files, where one is ignored from a higher
|
|
|
|
// level. We should see only the directory and one of the files.
|
|
|
|
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Fatalf("Incorrect length %d != 2", len(files))
|
2014-08-11 18:20:01 +00:00
|
|
|
}
|
2018-02-14 07:59:46 +00:00
|
|
|
if files[0].Name != "dir2" {
|
2014-08-30 07:22:23 +00:00
|
|
|
t.Errorf("Incorrect file %v != dir2", files[0])
|
|
|
|
}
|
2018-02-14 07:59:46 +00:00
|
|
|
if files[1].Name != filepath.Join("dir2", "cfile") {
|
2014-08-30 07:22:23 +00:00
|
|
|
t.Errorf("Incorrect file %v != dir2/cfile", files[1])
|
2014-08-11 18:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
func TestWalk(t *testing.T) {
|
2017-08-19 14:36:56 +00:00
|
|
|
ignores := ignore.New(fs.NewFilesystem(fs.FilesystemTypeBasic, "."))
|
2014-12-23 09:05:08 +00:00
|
|
|
err := ignores.Load("testdata/.stignore")
|
2014-09-04 20:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Log(ignores)
|
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
fchan := Walk(context.TODO(), Config{
|
2017-08-19 14:36:56 +00:00
|
|
|
Filesystem: fs.NewFilesystem(fs.FilesystemTypeBasic, "testdata"),
|
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
Matcher: ignores,
|
|
|
|
Hashers: 2,
|
2016-05-09 18:25:39 +00:00
|
|
|
})
|
2014-05-04 16:20:25 +00:00
|
|
|
|
2018-02-14 07:59:46 +00:00
|
|
|
var tmp []protocol.FileInfo
|
2014-08-30 07:22:23 +00:00
|
|
|
for f := range fchan {
|
|
|
|
tmp = append(tmp, f)
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
2014-08-30 07:22:23 +00:00
|
|
|
sort.Sort(fileList(tmp))
|
|
|
|
files := fileList(tmp).testfiles()
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(testdata, files); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("Walk returned unexpected data. Diff:\n%s", diff)
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 12:44:15 +00:00
|
|
|
func TestVerify(t *testing.T) {
|
|
|
|
blocksize := 16
|
|
|
|
// data should be an even multiple of blocksize long
|
|
|
|
data := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut e")
|
|
|
|
buf := bytes.NewBuffer(data)
|
2015-11-17 20:20:46 +00:00
|
|
|
progress := newByteCounter()
|
|
|
|
defer progress.Close()
|
2014-09-27 12:44:15 +00:00
|
|
|
|
2017-04-26 00:15:23 +00:00
|
|
|
blocks, err := Blocks(context.TODO(), buf, blocksize, -1, progress, false)
|
2014-09-27 12:44:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if exp := len(data) / blocksize; len(blocks) != exp {
|
|
|
|
t.Fatalf("Incorrect number of blocks %d != %d", len(blocks), exp)
|
|
|
|
}
|
|
|
|
|
2015-11-17 20:20:46 +00:00
|
|
|
if int64(len(data)) != progress.Total() {
|
|
|
|
t.Fatalf("Incorrect counter value %d != %d", len(data), progress.Total())
|
2015-08-26 22:49:06 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 12:44:15 +00:00
|
|
|
buf = bytes.NewBuffer(data)
|
2018-01-14 14:30:11 +00:00
|
|
|
err = verify(buf, blocksize, blocks)
|
2014-09-27 12:44:15 +00:00
|
|
|
t.Log(err)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Unexpected verify failure", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bytes.NewBuffer(append(data, '\n'))
|
2018-01-14 14:30:11 +00:00
|
|
|
err = verify(buf, blocksize, blocks)
|
2014-09-27 12:44:15 +00:00
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected verify success")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = bytes.NewBuffer(data[:len(data)-1])
|
2018-01-14 14:30:11 +00:00
|
|
|
err = verify(buf, blocksize, blocks)
|
2014-09-27 12:44:15 +00:00
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected verify success")
|
|
|
|
}
|
|
|
|
|
|
|
|
data[42] = 42
|
|
|
|
buf = bytes.NewBuffer(data)
|
2018-01-14 14:30:11 +00:00
|
|
|
err = verify(buf, blocksize, blocks)
|
2014-09-27 12:44:15 +00:00
|
|
|
t.Log(err)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Unexpected verify success")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 22:54:50 +00:00
|
|
|
func TestNormalization(t *testing.T) {
|
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
t.Skip("Normalization test not possible on darwin")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll("testdata/normalization")
|
|
|
|
defer os.RemoveAll("testdata/normalization")
|
|
|
|
|
|
|
|
tests := []string{
|
|
|
|
"0-A", // ASCII A -- accepted
|
|
|
|
"1-\xC3\x84", // NFC 'Ä' -- conflicts with the entry below, accepted
|
|
|
|
"1-\x41\xCC\x88", // NFD 'Ä' -- conflicts with the entry above, ignored
|
|
|
|
"2-\xC3\x85", // NFC 'Å' -- accepted
|
|
|
|
"3-\x41\xCC\x83", // NFD 'Ã' -- converted to NFC
|
|
|
|
"4-\xE2\x98\x95", // U+2615 HOT BEVERAGE (☕) -- accepted
|
|
|
|
"5-\xCD\xE2", // EUC-CN "wài" (外) -- ignored (not UTF8)
|
|
|
|
}
|
|
|
|
numInvalid := 2
|
2015-04-16 20:18:17 +00:00
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// On Windows, in case 5 the character gets replaced with a
|
|
|
|
// replacement character \xEF\xBF\xBD at the point it's written to disk,
|
|
|
|
// which means it suddenly becomes valid (sort of).
|
|
|
|
numInvalid--
|
|
|
|
}
|
|
|
|
|
2015-03-18 22:54:50 +00:00
|
|
|
numValid := len(tests) - numInvalid
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, ".")
|
|
|
|
|
2015-03-18 22:54:50 +00:00
|
|
|
for _, s1 := range tests {
|
|
|
|
// Create a directory for each of the interesting strings above
|
2017-08-19 14:36:56 +00:00
|
|
|
if err := fs.MkdirAll(filepath.Join("testdata/normalization", s1), 0755); err != nil {
|
2015-03-18 22:54:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s2 := range tests {
|
|
|
|
// Within each dir, create a file with each of the interesting
|
|
|
|
// file names. Ensure that the file doesn't exist when it's
|
|
|
|
// created. This detects and fails if there's file name
|
|
|
|
// normalization stuff at the filesystem level.
|
2017-08-19 14:36:56 +00:00
|
|
|
if fd, err := fs.OpenFile(filepath.Join("testdata/normalization", s1, s2), os.O_CREATE|os.O_EXCL, 0644); err != nil {
|
2015-03-18 22:54:50 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
} else {
|
2017-08-19 14:36:56 +00:00
|
|
|
fd.Write([]byte("test"))
|
2015-03-18 22:54:50 +00:00
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can normalize a directory name, but we can't descend into it in the
|
|
|
|
// same pass due to how filepath.Walk works. So we run the scan twice to
|
|
|
|
// make sure it all gets done. In production, things will be correct
|
|
|
|
// eventually...
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
_, err := walkDir(fs, "testdata/normalization")
|
2015-03-18 22:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
tmp, err := walkDir(fs, "testdata/normalization")
|
2015-03-18 22:54:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
files := fileList(tmp).testfiles()
|
|
|
|
|
|
|
|
// We should have one file per combination, plus the directories
|
lib/scanner: Fix UTF-8 normalization on ZFS (fixes #4649)
It turns out that ZFS doesn't do any normalization when storing files,
but does do normalization "as part of any comparison process".
In practice, this seems to mean that if you LStat a normalized filename,
ZFS will return the FileInfo for the un-normalized version of that
filename.
This meant that our test to see whether a separate file with a
normalized version of the filename already exists was failing, as we
were detecting the same file.
The fix is to use os.SameFile, to see whether we're getting the same
FileInfo from the normalized and un-normalized versions of the same
filename.
One complication is that ZFS also seems to apply its magic to os.Rename,
meaning that we can't use it to rename an un-normalized file to its
normalized filename. Instead we have to move via a temporary object. If
the move to the temporary object fails, that's OK, we can skip it and
move on. If the move from the temporary object fails however, I'm not
sure of the best approach: the current one is to leave the temporary
file name as-is, and get Syncthing to syncronize it, so at least we
don't lose the file. I'm not sure if there are any implications of this
however.
As part of reworking normalizePath, I spotted that it appeared to be
returning the wrong thing: the doc and the surrounding code expecting it
to return the normalized filename, but it was returning the
un-normalized one. I fixed this, but it seems suspicious that, if the
previous behaviour was incorrect, noone ever ran afoul of it. Maybe all
filesystems will do some searching and give you a normalized filename if
you request an unnormalized one.
As part of this, I found that TestNormalization was broken: it was
passing, when in fact one of the files it should have verified was
present was missing. Maybe this was related to the above issue with
normalizePath's return value, I'm not sure. Fixed en route.
Kindly tested by @khinsen on the forum, and it appears to work.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4646
2018-01-05 18:11:09 +00:00
|
|
|
// themselves, plus the "testdata/normalization" directory
|
2015-03-18 22:54:50 +00:00
|
|
|
|
lib/scanner: Fix UTF-8 normalization on ZFS (fixes #4649)
It turns out that ZFS doesn't do any normalization when storing files,
but does do normalization "as part of any comparison process".
In practice, this seems to mean that if you LStat a normalized filename,
ZFS will return the FileInfo for the un-normalized version of that
filename.
This meant that our test to see whether a separate file with a
normalized version of the filename already exists was failing, as we
were detecting the same file.
The fix is to use os.SameFile, to see whether we're getting the same
FileInfo from the normalized and un-normalized versions of the same
filename.
One complication is that ZFS also seems to apply its magic to os.Rename,
meaning that we can't use it to rename an un-normalized file to its
normalized filename. Instead we have to move via a temporary object. If
the move to the temporary object fails, that's OK, we can skip it and
move on. If the move from the temporary object fails however, I'm not
sure of the best approach: the current one is to leave the temporary
file name as-is, and get Syncthing to syncronize it, so at least we
don't lose the file. I'm not sure if there are any implications of this
however.
As part of reworking normalizePath, I spotted that it appeared to be
returning the wrong thing: the doc and the surrounding code expecting it
to return the normalized filename, but it was returning the
un-normalized one. I fixed this, but it seems suspicious that, if the
previous behaviour was incorrect, noone ever ran afoul of it. Maybe all
filesystems will do some searching and give you a normalized filename if
you request an unnormalized one.
As part of this, I found that TestNormalization was broken: it was
passing, when in fact one of the files it should have verified was
present was missing. Maybe this was related to the above issue with
normalizePath's return value, I'm not sure. Fixed en route.
Kindly tested by @khinsen on the forum, and it appears to work.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4646
2018-01-05 18:11:09 +00:00
|
|
|
expectedNum := numValid*numValid + numValid + 1
|
2015-03-18 22:54:50 +00:00
|
|
|
if len(files) != expectedNum {
|
|
|
|
t.Errorf("Expected %d files, got %d", expectedNum, len(files))
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file names should all be in NFC form.
|
|
|
|
|
|
|
|
for _, f := range files {
|
|
|
|
t.Logf("%q (% x) %v", f.name, f.name, norm.NFC.IsNormalString(f.name))
|
|
|
|
if !norm.NFC.IsNormalString(f.name) {
|
|
|
|
t.Errorf("File name %q is not NFC normalized", f.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 14:06:29 +00:00
|
|
|
func TestIssue1507(t *testing.T) {
|
2018-02-14 07:59:46 +00:00
|
|
|
w := &walker{}
|
|
|
|
c := make(chan protocol.FileInfo, 100)
|
|
|
|
fn := w.walkAndHashFiles(context.TODO(), c, c)
|
2015-03-22 14:06:29 +00:00
|
|
|
|
|
|
|
fn("", nil, protocol.ErrClosed)
|
|
|
|
}
|
|
|
|
|
2017-03-18 00:25:47 +00:00
|
|
|
func TestWalkSymlinkUnix(t *testing.T) {
|
2017-02-07 08:34:24 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2016-07-26 11:55:25 +00:00
|
|
|
t.Skip("skipping unsupported symlink test")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a folder with a symlink in it
|
2016-08-16 10:01:58 +00:00
|
|
|
os.RemoveAll("_symlinks")
|
2016-07-26 11:55:25 +00:00
|
|
|
os.Mkdir("_symlinks", 0755)
|
2018-01-24 00:05:47 +00:00
|
|
|
defer os.RemoveAll("_symlinks")
|
|
|
|
os.Symlink("../testdata", "_symlinks/link")
|
2016-07-26 11:55:25 +00:00
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
for _, path := range []string{".", "link"} {
|
|
|
|
// Scan it
|
|
|
|
files, _ := walkDir(fs.NewFilesystem(fs.FilesystemTypeBasic, "_symlinks"), path)
|
2016-07-26 11:55:25 +00:00
|
|
|
|
2018-02-14 07:59:46 +00:00
|
|
|
// Verify that we got one symlink and with the correct attributes
|
2018-01-24 00:05:47 +00:00
|
|
|
if len(files) != 1 {
|
|
|
|
t.Errorf("expected 1 symlink, not %d", len(files))
|
|
|
|
}
|
2018-02-14 07:59:46 +00:00
|
|
|
if len(files[0].Blocks) != 0 {
|
|
|
|
t.Errorf("expected zero blocks for symlink, not %d", len(files[0].Blocks))
|
2018-01-24 00:05:47 +00:00
|
|
|
}
|
2018-02-14 07:59:46 +00:00
|
|
|
if files[0].SymlinkTarget != "../testdata" {
|
|
|
|
t.Errorf("expected symlink to have target destination, not %q", files[0].SymlinkTarget)
|
2018-01-24 00:05:47 +00:00
|
|
|
}
|
2016-07-26 11:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 00:25:47 +00:00
|
|
|
func TestWalkSymlinkWindows(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skip("skipping unsupported symlink test")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a folder with a symlink in it
|
|
|
|
os.RemoveAll("_symlinks")
|
|
|
|
os.Mkdir("_symlinks", 0755)
|
2018-01-24 00:05:47 +00:00
|
|
|
defer os.RemoveAll("_symlinks")
|
|
|
|
if err := osutil.DebugSymlinkForTestsOnly("../testdata", "_symlinks/link"); err != nil {
|
2017-03-18 00:25:47 +00:00
|
|
|
// Probably we require permissions we don't have.
|
|
|
|
t.Skip(err)
|
|
|
|
}
|
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
for _, path := range []string{".", "link"} {
|
|
|
|
// Scan it
|
|
|
|
files, _ := walkDir(fs.NewFilesystem(fs.FilesystemTypeBasic, "_symlinks"), path)
|
2017-03-18 00:25:47 +00:00
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
// Verify that we got zero symlinks
|
|
|
|
if len(files) != 0 {
|
|
|
|
t.Errorf("expected zero symlinks, not %d", len(files))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-18 00:25:47 +00:00
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
func TestWalkRootSymlink(t *testing.T) {
|
|
|
|
// Create a folder with a symlink in it
|
|
|
|
tmp, err := ioutil.TempDir("", "")
|
2017-03-18 00:25:47 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-01-24 00:05:47 +00:00
|
|
|
defer os.RemoveAll(tmp)
|
2017-03-18 00:25:47 +00:00
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
link := tmp + "/link"
|
|
|
|
dest, _ := filepath.Abs("testdata/dir1")
|
|
|
|
if err := osutil.DebugSymlinkForTestsOnly(dest, link); err != nil {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Probably we require permissions we don't have.
|
|
|
|
t.Skip("Need admin permissions or developer mode to run symlink test on Windows: " + err.Error())
|
|
|
|
} else {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-18 00:25:47 +00:00
|
|
|
}
|
|
|
|
|
2018-01-24 00:05:47 +00:00
|
|
|
// Scan it
|
|
|
|
files, err := walkDir(fs.NewFilesystem(fs.FilesystemTypeBasic, link), ".")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Expected no error when root folder path is provided via a symlink: " + err.Error())
|
|
|
|
}
|
|
|
|
// Verify that we got two files
|
|
|
|
if len(files) != 2 {
|
|
|
|
t.Errorf("expected two files, not %d", len(files))
|
2017-03-18 00:25:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-14 07:59:46 +00:00
|
|
|
func walkDir(fs fs.Filesystem, dir string) ([]protocol.FileInfo, error) {
|
2018-01-24 00:05:47 +00:00
|
|
|
fchan := Walk(context.TODO(), Config{
|
2017-08-19 14:36:56 +00:00
|
|
|
Filesystem: fs,
|
|
|
|
Subs: []string{dir},
|
2015-03-18 22:54:50 +00:00
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
AutoNormalize: true,
|
2015-04-29 18:46:32 +00:00
|
|
|
Hashers: 2,
|
2016-05-09 18:25:39 +00:00
|
|
|
})
|
2015-03-18 22:54:50 +00:00
|
|
|
|
2018-02-14 07:59:46 +00:00
|
|
|
var tmp []protocol.FileInfo
|
2015-03-18 22:54:50 +00:00
|
|
|
for f := range fchan {
|
|
|
|
tmp = append(tmp, f)
|
|
|
|
}
|
|
|
|
sort.Sort(fileList(tmp))
|
|
|
|
|
|
|
|
return tmp, nil
|
|
|
|
}
|
|
|
|
|
2018-02-14 07:59:46 +00:00
|
|
|
type fileList []protocol.FileInfo
|
2014-07-30 18:10:46 +00:00
|
|
|
|
2014-12-08 15:36:15 +00:00
|
|
|
func (l fileList) Len() int {
|
|
|
|
return len(l)
|
2014-07-30 18:10:46 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 15:36:15 +00:00
|
|
|
func (l fileList) Less(a, b int) bool {
|
2018-02-14 07:59:46 +00:00
|
|
|
return l[a].Name < l[b].Name
|
2014-07-30 18:10:46 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 15:36:15 +00:00
|
|
|
func (l fileList) Swap(a, b int) {
|
|
|
|
l[a], l[b] = l[b], l[a]
|
2014-07-30 18:10:46 +00:00
|
|
|
}
|
2014-08-30 07:22:23 +00:00
|
|
|
|
|
|
|
func (l fileList) testfiles() testfileList {
|
|
|
|
testfiles := make(testfileList, len(l))
|
|
|
|
for i, f := range l {
|
2018-02-14 07:59:46 +00:00
|
|
|
if len(f.Blocks) > 1 {
|
2014-08-30 07:22:23 +00:00
|
|
|
panic("simple test case stuff only supports a single block per file")
|
|
|
|
}
|
2018-02-14 07:59:46 +00:00
|
|
|
testfiles[i] = testfile{name: f.Name, length: f.FileSize()}
|
|
|
|
if len(f.Blocks) == 1 {
|
|
|
|
testfiles[i].hash = fmt.Sprintf("%x", f.Blocks[0].Hash)
|
2014-08-30 07:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return testfiles
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l testfileList) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("{\n")
|
|
|
|
for _, f := range l {
|
2016-07-04 10:40:29 +00:00
|
|
|
fmt.Fprintf(&b, " %s (%d bytes): %s\n", f.name, f.length, f.hash)
|
2014-08-30 07:22:23 +00:00
|
|
|
}
|
|
|
|
b.WriteString("}")
|
|
|
|
return b.String()
|
|
|
|
}
|
2015-09-04 10:54:01 +00:00
|
|
|
|
2015-10-27 08:26:08 +00:00
|
|
|
var initOnce sync.Once
|
|
|
|
|
|
|
|
const (
|
|
|
|
testdataSize = 17 << 20
|
|
|
|
testdataName = "_random.data"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkHashFile(b *testing.B) {
|
|
|
|
initOnce.Do(initTestFile)
|
|
|
|
b.ResetTimer()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-08-19 14:36:56 +00:00
|
|
|
if _, err := HashFile(context.TODO(), fs.NewFilesystem(fs.FilesystemTypeBasic, ""), testdataName, protocol.BlockSize, nil, true); err != nil {
|
2015-10-27 08:26:08 +00:00
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 09:33:17 +00:00
|
|
|
b.SetBytes(testdataSize)
|
2015-10-27 08:26:08 +00:00
|
|
|
b.ReportAllocs()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestFile() {
|
|
|
|
fd, err := os.Create(testdataName)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lr := io.LimitReader(rand.Reader, testdataSize)
|
|
|
|
if _, err := io.Copy(fd, lr); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fd.Close(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2017-04-26 00:15:23 +00:00
|
|
|
|
|
|
|
func TestStopWalk(t *testing.T) {
|
|
|
|
// Create tree that is 100 levels deep, with each level containing 100
|
|
|
|
// files (each 1 MB) and 100 directories (in turn containing 100 files
|
|
|
|
// and 100 directories, etc). That is, in total > 100^100 files and as
|
|
|
|
// many directories. It'll take a while to scan, giving us time to
|
|
|
|
// cancel it and make sure the scan stops.
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
// Use an errorFs as the backing fs for the rest of the interface
|
|
|
|
// The way we get it is a bit hacky tho.
|
|
|
|
errorFs := fs.NewFilesystem(fs.FilesystemType(-1), ".")
|
|
|
|
fs := fs.NewWalkFilesystem(&infiniteFS{errorFs, 100, 100, 1e6})
|
2017-04-26 00:15:23 +00:00
|
|
|
|
|
|
|
const numHashers = 4
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-01-24 00:05:47 +00:00
|
|
|
fchan := Walk(ctx, Config{
|
2017-08-19 14:36:56 +00:00
|
|
|
Filesystem: fs,
|
2017-04-26 00:15:23 +00:00
|
|
|
BlockSize: 128 * 1024,
|
|
|
|
Hashers: numHashers,
|
|
|
|
ProgressTickIntervalS: -1, // Don't attempt to build the full list of files before starting to scan...
|
|
|
|
})
|
|
|
|
|
|
|
|
// Receive a few entries to make sure the walker is up and running,
|
|
|
|
// scanning both files and dirs. Do some quick sanity tests on the
|
|
|
|
// returned file entries to make sure we are not just reading crap from
|
|
|
|
// a closed channel or something.
|
|
|
|
dirs := 0
|
|
|
|
files := 0
|
|
|
|
for {
|
|
|
|
f := <-fchan
|
|
|
|
t.Log("Scanned", f)
|
2018-02-14 07:59:46 +00:00
|
|
|
if f.IsDirectory() {
|
|
|
|
if len(f.Name) == 0 || f.Permissions == 0 {
|
2017-04-26 00:15:23 +00:00
|
|
|
t.Error("Bad directory entry", f)
|
|
|
|
}
|
|
|
|
dirs++
|
|
|
|
} else {
|
2018-02-14 07:59:46 +00:00
|
|
|
if len(f.Name) == 0 || len(f.Blocks) == 0 || f.Permissions == 0 {
|
2017-04-26 00:15:23 +00:00
|
|
|
t.Error("Bad file entry", f)
|
|
|
|
}
|
|
|
|
files++
|
|
|
|
}
|
|
|
|
if dirs > 5 && files > 5 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel the walker.
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
// Empty out any waiting entries and wait for the channel to close.
|
|
|
|
// Count them, they should be zero or very few - essentially, each
|
|
|
|
// hasher has the choice of returning a fully handled entry or
|
|
|
|
// cancelling, but they should not start on another item.
|
|
|
|
extra := 0
|
|
|
|
for range fchan {
|
|
|
|
extra++
|
|
|
|
}
|
|
|
|
t.Log("Extra entries:", extra)
|
|
|
|
if extra > numHashers {
|
|
|
|
t.Error("unexpected extra entries received after cancel")
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 14:30:11 +00:00
|
|
|
|
2018-03-12 12:18:59 +00:00
|
|
|
func TestIssue4799(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
|
|
|
fs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmp)
|
|
|
|
|
|
|
|
fd, err := fs.Create("foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
files, err := walkDir(fs, "/foo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(files) != 1 || files[0].Name != "foo" {
|
|
|
|
t.Error(`Received unexpected file infos when walking "/foo"`, files)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-14 14:30:11 +00:00
|
|
|
// Verify returns nil or an error describing the mismatch between the block
|
|
|
|
// list and actual reader contents
|
|
|
|
func verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
|
|
|
|
hf := sha256.New()
|
|
|
|
// A 32k buffer is used for copying into the hash function.
|
|
|
|
buf := make([]byte, 32<<10)
|
|
|
|
|
|
|
|
for i, block := range blocks {
|
|
|
|
lr := &io.LimitedReader{R: r, N: int64(blocksize)}
|
|
|
|
_, err := io.CopyBuffer(hf, lr, buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := hf.Sum(nil)
|
|
|
|
hf.Reset()
|
|
|
|
|
|
|
|
if !bytes.Equal(hash, block.Hash) {
|
|
|
|
return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should have reached the end now
|
|
|
|
bs := make([]byte, 1)
|
|
|
|
n, err := r.Read(bs)
|
|
|
|
if n != 0 || err != io.EOF {
|
|
|
|
return fmt.Errorf("file continues past end of blocks")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|