2017-10-20 14:52:55 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// 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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2021-08-17 08:10:41 +00:00
|
|
|
//go:build (!solaris && !darwin) || (solaris && cgo) || (darwin && cgo)
|
2017-10-20 14:52:55 +00:00
|
|
|
// +build !solaris,!darwin solaris,cgo darwin,cgo
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-03-23 11:56:38 +00:00
|
|
|
"errors"
|
2017-10-20 14:52:55 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2018-04-16 18:07:00 +00:00
|
|
|
"strings"
|
2018-03-23 11:56:38 +00:00
|
|
|
"syscall"
|
2017-10-20 14:52:55 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-03-14 13:48:22 +00:00
|
|
|
"github.com/syncthing/notify"
|
2022-07-28 17:36:39 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2024-01-13 17:58:23 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/ignore/ignoreresult"
|
2017-10-20 14:52:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
if err := os.RemoveAll(testDir); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dir, err := filepath.Abs(".")
|
|
|
|
if err != nil {
|
|
|
|
panic("Cannot get absolute path to working dir")
|
|
|
|
}
|
2018-08-11 20:24:36 +00:00
|
|
|
|
2018-09-26 18:28:20 +00:00
|
|
|
dir, err = evalSymlinks(dir)
|
2017-10-20 14:52:55 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("Cannot get real path to working dir")
|
|
|
|
}
|
2018-08-11 20:24:36 +00:00
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
testDirAbs = filepath.Join(dir, testDir)
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsWindows {
|
2018-08-11 20:24:36 +00:00
|
|
|
testDirAbs = longFilenameSupport(testDirAbs)
|
|
|
|
}
|
|
|
|
|
2018-01-27 12:52:48 +00:00
|
|
|
testFs = NewFilesystem(FilesystemTypeBasic, testDirAbs)
|
2017-10-20 14:52:55 +00:00
|
|
|
|
|
|
|
backendBuffer = 10
|
2018-05-14 07:47:23 +00:00
|
|
|
|
|
|
|
exitCode := m.Run()
|
|
|
|
|
|
|
|
backendBuffer = 500
|
|
|
|
os.RemoveAll(testDir)
|
|
|
|
|
|
|
|
os.Exit(exitCode)
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2019-01-19 07:28:57 +00:00
|
|
|
testDir = "testdata"
|
|
|
|
failsOnOpenBSD = "Fails on OpenBSD. See https://github.com/rjeczalik/notify/issues/172"
|
2017-10-20 14:52:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testDirAbs string
|
|
|
|
testFs Filesystem
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWatchIgnore(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsOpenBSD {
|
2019-01-19 07:28:57 +00:00
|
|
|
t.Skip(failsOnOpenBSD)
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
name := "ignore"
|
|
|
|
|
|
|
|
file := "file"
|
|
|
|
ignored := "ignored"
|
|
|
|
|
|
|
|
testCase := func() {
|
|
|
|
createTestFile(name, file)
|
|
|
|
createTestFile(name, ignored)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEvents := []Event{
|
|
|
|
{file, NonRemove},
|
|
|
|
}
|
2018-02-04 21:25:59 +00:00
|
|
|
allowedEvents := []Event{
|
|
|
|
{name, NonRemove},
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2021-05-02 14:45:44 +00:00
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{ignore: filepath.Join(name, ignored), skipIgnoredDirs: true}, false)
|
2018-05-14 07:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchInclude(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsOpenBSD {
|
2019-01-19 07:28:57 +00:00
|
|
|
t.Skip(failsOnOpenBSD)
|
|
|
|
}
|
2018-05-14 07:47:23 +00:00
|
|
|
name := "include"
|
|
|
|
|
|
|
|
file := "file"
|
|
|
|
ignored := "ignored"
|
2024-01-13 17:58:23 +00:00
|
|
|
testFs.MkdirAll(filepath.Join(name, ignored), 0o777)
|
2018-05-14 07:47:23 +00:00
|
|
|
included := filepath.Join(ignored, "included")
|
|
|
|
|
|
|
|
testCase := func() {
|
|
|
|
createTestFile(name, file)
|
|
|
|
createTestFile(name, included)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEvents := []Event{
|
|
|
|
{file, NonRemove},
|
|
|
|
{included, NonRemove},
|
|
|
|
}
|
|
|
|
allowedEvents := []Event{
|
|
|
|
{name, NonRemove},
|
|
|
|
}
|
|
|
|
|
2021-05-02 14:45:44 +00:00
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{ignore: filepath.Join(name, ignored), include: filepath.Join(name, included)}, false)
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchRename(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsOpenBSD {
|
2019-01-19 07:28:57 +00:00
|
|
|
t.Skip(failsOnOpenBSD)
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
name := "rename"
|
|
|
|
|
|
|
|
old := createTestFile(name, "oldfile")
|
|
|
|
new := "newfile"
|
|
|
|
|
|
|
|
testCase := func() {
|
|
|
|
renameTestFile(name, old, new)
|
|
|
|
}
|
|
|
|
|
|
|
|
destEvent := Event{new, Remove}
|
|
|
|
// Only on these platforms the removed file can be differentiated from
|
|
|
|
// the created file during renaming
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsWindows || build.IsLinux || build.IsSolaris || build.IsIllumos || build.IsFreeBSD {
|
2017-10-20 14:52:55 +00:00
|
|
|
destEvent = Event{new, NonRemove}
|
|
|
|
}
|
|
|
|
expectedEvents := []Event{
|
|
|
|
{old, Remove},
|
|
|
|
destEvent,
|
|
|
|
}
|
2018-02-04 21:25:59 +00:00
|
|
|
allowedEvents := []Event{
|
|
|
|
{name, NonRemove},
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2018-01-28 10:44:43 +00:00
|
|
|
// set the "allow others" flag because we might get the create of
|
|
|
|
// "oldfile" initially
|
2021-05-02 14:45:44 +00:00
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{}, false)
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2019-05-10 07:09:58 +00:00
|
|
|
// TestWatchWinRoot checks that a watch at a drive letter does not panic due to
|
|
|
|
// out of root event on every event.
|
|
|
|
// https://github.com/syncthing/syncthing/issues/5695
|
|
|
|
func TestWatchWinRoot(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsWindows {
|
2019-05-10 07:09:58 +00:00
|
|
|
t.Skip("Windows specific test")
|
|
|
|
}
|
|
|
|
|
|
|
|
outChan := make(chan Event)
|
|
|
|
backendChan := make(chan notify.EventInfo, backendBuffer)
|
2019-05-25 19:08:26 +00:00
|
|
|
errChan := make(chan error)
|
2019-05-10 07:09:58 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// testFs is Filesystem, but we need BasicFilesystem here
|
|
|
|
root := `D:\`
|
|
|
|
fs := newBasicFilesystem(root)
|
2019-09-22 07:03:22 +00:00
|
|
|
watch, roots, err := fs.watchPaths(".")
|
2019-05-10 07:09:58 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-05-13 22:30:09 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
<-done
|
|
|
|
}()
|
2019-05-10 07:09:58 +00:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
t.Error(r)
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
}()
|
2019-11-21 07:41:15 +00:00
|
|
|
fs.watchLoop(ctx, ".", roots, backendChan, outChan, errChan, fakeMatcher{})
|
2020-05-13 22:30:09 +00:00
|
|
|
close(done)
|
2019-05-10 07:09:58 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
// filepath.Dir as watch has a /... suffix
|
|
|
|
name := "foo"
|
|
|
|
backendChan <- fakeEventInfo(filepath.Join(filepath.Dir(watch), name))
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
cancel()
|
|
|
|
t.Errorf("Timed out before receiving event")
|
|
|
|
case ev := <-outChan:
|
|
|
|
if ev.Name != name {
|
|
|
|
t.Errorf("Unexpected event %v, expected %v", ev.Name, name)
|
|
|
|
}
|
2019-05-25 19:08:26 +00:00
|
|
|
case err := <-errChan:
|
|
|
|
t.Error("Received fatal watch error:", err)
|
2019-05-10 07:09:58 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
// TestWatchOutside checks that no changes from outside the folder make it in
|
|
|
|
func TestWatchOutside(t *testing.T) {
|
2019-09-29 15:38:11 +00:00
|
|
|
expectErrorForPath(t, filepath.Join(filepath.Dir(testDirAbs), "outside"))
|
|
|
|
|
|
|
|
rootWithoutSlash := strings.TrimRight(filepath.ToSlash(testDirAbs), "/")
|
|
|
|
expectErrorForPath(t, rootWithoutSlash+"outside")
|
|
|
|
expectErrorForPath(t, rootWithoutSlash+"outside/thing")
|
|
|
|
}
|
|
|
|
|
|
|
|
func expectErrorForPath(t *testing.T, path string) {
|
2017-10-20 14:52:55 +00:00
|
|
|
outChan := make(chan Event)
|
|
|
|
backendChan := make(chan notify.EventInfo, backendBuffer)
|
2019-05-25 19:08:26 +00:00
|
|
|
errChan := make(chan error)
|
2017-10-20 14:52:55 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// testFs is Filesystem, but we need BasicFilesystem here
|
|
|
|
fs := newBasicFilesystem(testDirAbs)
|
|
|
|
|
2020-05-13 22:30:09 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
fs.watchLoop(ctx, ".", []string{testDirAbs}, backendChan, outChan, errChan, fakeMatcher{})
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
<-done
|
|
|
|
}()
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2019-09-29 15:38:11 +00:00
|
|
|
backendChan <- fakeEventInfo(path)
|
2018-05-14 07:47:23 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(10 * time.Second):
|
2019-05-25 19:08:26 +00:00
|
|
|
t.Errorf("Timed out before receiving error")
|
2019-09-29 15:38:11 +00:00
|
|
|
case e := <-outChan:
|
|
|
|
t.Errorf("Unexpected passed through event %v", e)
|
2019-05-25 19:08:26 +00:00
|
|
|
case <-errChan:
|
2018-05-14 07:47:23 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchSubpath(t *testing.T) {
|
|
|
|
outChan := make(chan Event)
|
|
|
|
backendChan := make(chan notify.EventInfo, backendBuffer)
|
2019-05-25 19:08:26 +00:00
|
|
|
errChan := make(chan error)
|
2017-10-20 14:52:55 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// testFs is Filesystem, but we need BasicFilesystem here
|
|
|
|
fs := newBasicFilesystem(testDirAbs)
|
|
|
|
|
|
|
|
abs, _ := fs.rooted("sub")
|
2020-05-13 22:30:09 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
fs.watchLoop(ctx, "sub", []string{testDirAbs}, backendChan, outChan, errChan, fakeMatcher{})
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
<-done
|
|
|
|
}()
|
2017-10-20 14:52:55 +00:00
|
|
|
|
|
|
|
backendChan <- fakeEventInfo(filepath.Join(abs, "file"))
|
|
|
|
|
|
|
|
timeout := time.NewTimer(2 * time.Second)
|
|
|
|
select {
|
|
|
|
case <-timeout.C:
|
|
|
|
t.Errorf("Timed out before receiving an event")
|
|
|
|
cancel()
|
|
|
|
case ev := <-outChan:
|
|
|
|
if ev.Name != filepath.Join("sub", "file") {
|
|
|
|
t.Errorf("While watching a subfolder, received an event with unexpected path %v", ev.Name)
|
|
|
|
}
|
2019-05-25 19:08:26 +00:00
|
|
|
case err := <-errChan:
|
|
|
|
t.Error("Received fatal watch error:", err)
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestWatchOverflow checks that an event at the root is sent when maxFiles is reached
|
|
|
|
func TestWatchOverflow(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsOpenBSD {
|
2019-01-19 07:28:57 +00:00
|
|
|
t.Skip(failsOnOpenBSD)
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
name := "overflow"
|
|
|
|
|
2018-02-04 21:25:59 +00:00
|
|
|
expectedEvents := []Event{
|
|
|
|
{".", NonRemove},
|
|
|
|
}
|
|
|
|
|
|
|
|
allowedEvents := []Event{
|
|
|
|
{name, NonRemove},
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
testCase := func() {
|
|
|
|
for i := 0; i < 5*backendBuffer; i++ {
|
2018-02-04 21:25:59 +00:00
|
|
|
file := "file" + strconv.Itoa(i)
|
|
|
|
createTestFile(name, file)
|
|
|
|
allowedEvents = append(allowedEvents, Event{file, NonRemove})
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-02 14:45:44 +00:00
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{}, false)
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 11:56:38 +00:00
|
|
|
func TestWatchErrorLinuxInterpretation(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsLinux {
|
2018-03-23 11:56:38 +00:00
|
|
|
t.Skip("testing of linux specific error codes")
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
errTooManyFiles := &os.PathError{
|
2018-03-23 11:56:38 +00:00
|
|
|
Op: "error while traversing",
|
|
|
|
Path: "foo",
|
|
|
|
Err: syscall.Errno(24),
|
|
|
|
}
|
2024-01-13 17:58:23 +00:00
|
|
|
errNoSpace := &os.PathError{
|
2018-03-23 11:56:38 +00:00
|
|
|
Op: "error while traversing",
|
|
|
|
Path: "bar",
|
|
|
|
Err: syscall.Errno(28),
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reachedMaxUserWatches(errTooManyFiles) {
|
|
|
|
t.Error("Underlying error syscall.Errno(24) should be recognised to be about inotify limits.")
|
|
|
|
}
|
|
|
|
if !reachedMaxUserWatches(errNoSpace) {
|
|
|
|
t.Error("Underlying error syscall.Errno(28) should be recognised to be about inotify limits.")
|
|
|
|
}
|
|
|
|
err := errors.New("Another error")
|
|
|
|
if reachedMaxUserWatches(err) {
|
|
|
|
t.Errorf("This error does not concern inotify limits: %#v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 21:01:25 +00:00
|
|
|
func TestWatchSymlinkedRoot(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsWindows {
|
2018-03-28 21:01:25 +00:00
|
|
|
t.Skip("Involves symlinks")
|
|
|
|
}
|
|
|
|
|
|
|
|
name := "symlinkedRoot"
|
2024-01-13 17:58:23 +00:00
|
|
|
if err := testFs.MkdirAll(name, 0o755); err != nil {
|
2018-03-28 21:01:25 +00:00
|
|
|
panic(fmt.Sprintf("Failed to create directory %s: %s", name, err))
|
|
|
|
}
|
2019-02-02 11:16:27 +00:00
|
|
|
defer testFs.RemoveAll(name)
|
2018-03-28 21:01:25 +00:00
|
|
|
|
|
|
|
root := filepath.Join(name, "root")
|
2024-01-13 17:58:23 +00:00
|
|
|
if err := testFs.MkdirAll(root, 0o777); err != nil {
|
2018-03-28 21:01:25 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
link := filepath.Join(name, "link")
|
|
|
|
|
|
|
|
if err := testFs.CreateSymlink(filepath.Join(testFs.URI(), root), link); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
linkedFs := NewFilesystem(FilesystemTypeBasic, filepath.Join(testFs.URI(), link))
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2019-05-25 19:08:26 +00:00
|
|
|
if _, _, err := linkedFs.Watch(".", fakeMatcher{}, ctx, false); err != nil {
|
2018-03-28 21:01:25 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
if err := linkedFs.MkdirAll("foo", 0o777); err != nil {
|
2018-03-28 21:01:25 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give the panic some time to happen
|
|
|
|
sleepMs(100)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnrootedChecked(t *testing.T) {
|
|
|
|
fs := newBasicFilesystem(testDirAbs)
|
2019-09-22 07:03:22 +00:00
|
|
|
if unrooted, err := fs.unrootedChecked("/random/other/path", []string{testDirAbs}); err == nil {
|
2019-05-25 19:08:26 +00:00
|
|
|
t.Error("unrootedChecked did not return an error on outside path, but returned", unrooted)
|
|
|
|
}
|
2018-03-28 21:01:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 18:07:00 +00:00
|
|
|
func TestWatchIssue4877(t *testing.T) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsWindows {
|
2018-04-16 18:07:00 +00:00
|
|
|
t.Skip("Windows specific test")
|
|
|
|
}
|
|
|
|
|
|
|
|
name := "Issue4877"
|
|
|
|
|
|
|
|
file := "file"
|
|
|
|
|
|
|
|
testCase := func() {
|
|
|
|
createTestFile(name, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEvents := []Event{
|
|
|
|
{file, NonRemove},
|
|
|
|
}
|
|
|
|
allowedEvents := []Event{
|
|
|
|
{name, NonRemove},
|
|
|
|
}
|
|
|
|
|
2018-09-11 20:30:32 +00:00
|
|
|
volName := filepath.VolumeName(testDirAbs)
|
|
|
|
if volName == "" {
|
|
|
|
t.Fatalf("Failed to get volume name for path %v", testDirAbs)
|
|
|
|
}
|
2018-04-16 18:07:00 +00:00
|
|
|
origTestFs := testFs
|
2018-09-11 20:30:32 +00:00
|
|
|
testFs = NewFilesystem(FilesystemTypeBasic, strings.ToLower(volName)+strings.ToUpper(testDirAbs[len(volName):]))
|
2018-04-16 18:07:00 +00:00
|
|
|
defer func() {
|
|
|
|
testFs = origTestFs
|
|
|
|
}()
|
|
|
|
|
2021-05-02 14:45:44 +00:00
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{}, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWatchModTime(t *testing.T) {
|
|
|
|
name := "modtime"
|
|
|
|
|
|
|
|
file := createTestFile(name, "foo")
|
|
|
|
path := filepath.Join(name, file)
|
|
|
|
now := time.Now()
|
|
|
|
before := now.Add(-10 * time.Second)
|
|
|
|
if err := testFs.Chtimes(path, before, before); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testCase := func() {
|
|
|
|
if err := testFs.Chtimes(path, now, now); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEvents := []Event{
|
|
|
|
{file, NonRemove},
|
|
|
|
}
|
|
|
|
|
|
|
|
var allowedEvents []Event
|
|
|
|
// Apparently an event for the parent is also sent on mac
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsDarwin {
|
2021-05-02 14:45:44 +00:00
|
|
|
allowedEvents = []Event{
|
|
|
|
{name, NonRemove},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 10:57:06 +00:00
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{}, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestModifyFile(t *testing.T) {
|
|
|
|
name := "modify"
|
|
|
|
|
|
|
|
old := createTestFile(name, "file")
|
|
|
|
modifyTestFile(name, old, "syncthing")
|
|
|
|
|
|
|
|
testCase := func() {
|
|
|
|
modifyTestFile(name, old, "modified")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEvents := []Event{
|
|
|
|
{old, NonRemove},
|
|
|
|
}
|
|
|
|
allowedEvents := []Event{
|
|
|
|
{name, NonRemove},
|
|
|
|
}
|
|
|
|
|
|
|
|
sleepMs(1000)
|
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{}, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTruncateFileOnly(t *testing.T) {
|
|
|
|
name := "truncate"
|
|
|
|
|
|
|
|
file := createTestFile(name, "file")
|
|
|
|
modifyTestFile(name, file, "syncthing")
|
|
|
|
|
|
|
|
// modified the content to empty use os.WriteFile will first truncate the file
|
|
|
|
// (/os/file.go:696) then write nothing. This logic is also used in many editors,
|
|
|
|
// such as when emptying a file in VSCode or JetBrain
|
|
|
|
//
|
|
|
|
// darwin will only modified the inode's metadata, such us mtime, file size, etc.
|
|
|
|
// but would not modified the file directly, so FSEvent 'FSEventsModified' will not
|
|
|
|
// be received
|
|
|
|
//
|
|
|
|
// we should watch the FSEvent 'FSEventsInodeMetaMod' to watch the Inode metadata,
|
|
|
|
// and that should be considered as an NonRemove Event
|
|
|
|
//
|
|
|
|
// notify also considered FSEventsInodeMetaMod as Write Event
|
|
|
|
// /watcher_fsevents.go:89
|
|
|
|
testCase := func() {
|
|
|
|
modifyTestFile(name, file, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedEvents := []Event{
|
|
|
|
{file, NonRemove},
|
|
|
|
}
|
|
|
|
allowedEvents := []Event{
|
|
|
|
{file, NonRemove},
|
|
|
|
}
|
|
|
|
|
|
|
|
sleepMs(1000)
|
2021-05-02 14:45:44 +00:00
|
|
|
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{}, true)
|
2018-04-16 18:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
// path relative to folder root, also creates parent dirs if necessary
|
|
|
|
func createTestFile(name string, file string) string {
|
|
|
|
joined := filepath.Join(name, file)
|
2024-01-13 17:58:23 +00:00
|
|
|
if err := testFs.MkdirAll(filepath.Dir(joined), 0o755); err != nil {
|
2017-10-20 14:52:55 +00:00
|
|
|
panic(fmt.Sprintf("Failed to create parent directory for %s: %s", joined, err))
|
|
|
|
}
|
|
|
|
handle, err := testFs.Create(joined)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to create test file %s: %s", joined, err))
|
|
|
|
}
|
|
|
|
handle.Close()
|
|
|
|
return file
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameTestFile(name string, old string, new string) {
|
|
|
|
old = filepath.Join(name, old)
|
|
|
|
new = filepath.Join(name, new)
|
|
|
|
if err := testFs.Rename(old, new); err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to rename %s to %s: %s", old, new, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 10:57:06 +00:00
|
|
|
func modifyTestFile(name string, file string, content string) {
|
|
|
|
joined := filepath.Join(testDirAbs, name, file)
|
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
err := os.WriteFile(joined, []byte(content), 0o755)
|
2021-06-09 10:57:06 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("Failed to modify test file %s: %s", joined, err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
func sleepMs(ms int) {
|
|
|
|
time.Sleep(time.Duration(ms) * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2021-05-02 14:45:44 +00:00
|
|
|
func testScenario(t *testing.T, name string, testCase func(), expectedEvents, allowedEvents []Event, fm fakeMatcher, ignorePerms bool) {
|
2024-01-13 17:58:23 +00:00
|
|
|
if err := testFs.MkdirAll(name, 0o755); err != nil {
|
2017-10-20 14:52:55 +00:00
|
|
|
panic(fmt.Sprintf("Failed to create directory %s: %s", name, err))
|
|
|
|
}
|
2019-02-02 11:16:27 +00:00
|
|
|
defer testFs.RemoveAll(name)
|
2017-10-20 14:52:55 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-01-28 10:44:43 +00:00
|
|
|
defer cancel()
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2021-05-02 14:45:44 +00:00
|
|
|
eventChan, errChan, err := testFs.Watch(name, fm, ctx, ignorePerms)
|
2017-10-20 14:52:55 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-02-04 21:25:59 +00:00
|
|
|
go testWatchOutput(t, name, eventChan, expectedEvents, allowedEvents, ctx, cancel)
|
2017-10-20 14:52:55 +00:00
|
|
|
|
|
|
|
testCase()
|
|
|
|
|
|
|
|
select {
|
2019-05-10 07:09:58 +00:00
|
|
|
case <-time.After(10 * time.Second):
|
2019-05-25 19:08:26 +00:00
|
|
|
t.Error("Timed out before receiving all expected events")
|
|
|
|
case err := <-errChan:
|
|
|
|
t.Error("Received fatal watch error:", err)
|
2018-01-28 10:44:43 +00:00
|
|
|
case <-ctx.Done():
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-04 21:25:59 +00:00
|
|
|
func testWatchOutput(t *testing.T, name string, in <-chan Event, expectedEvents, allowedEvents []Event, ctx context.Context, cancel context.CancelFunc) {
|
2024-01-13 17:58:23 +00:00
|
|
|
expected := make(map[Event]struct{})
|
2017-10-20 14:52:55 +00:00
|
|
|
for _, ev := range expectedEvents {
|
|
|
|
ev.Name = filepath.Join(name, ev.Name)
|
|
|
|
expected[ev] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var received Event
|
|
|
|
var last Event
|
|
|
|
for {
|
|
|
|
if len(expected) == 0 {
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case received = <-in:
|
|
|
|
}
|
|
|
|
|
|
|
|
// apparently the backend sometimes sends repeat events
|
|
|
|
if last == received {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := expected[received]; !ok {
|
2018-02-04 21:25:59 +00:00
|
|
|
if len(allowedEvents) > 0 {
|
2017-10-20 14:52:55 +00:00
|
|
|
sleepMs(100) // To facilitate overflow
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
t.Errorf("Received unexpected event %v expected one of %v", received, expected)
|
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
delete(expected, received)
|
|
|
|
last = received
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 07:47:23 +00:00
|
|
|
// Matches are done via direct comparison against both ignore and include
|
|
|
|
type fakeMatcher struct {
|
|
|
|
ignore string
|
|
|
|
include string
|
|
|
|
skipIgnoredDirs bool
|
|
|
|
}
|
2017-10-20 14:52:55 +00:00
|
|
|
|
2024-01-13 17:58:23 +00:00
|
|
|
func (fm fakeMatcher) Match(name string) ignoreresult.R {
|
|
|
|
if name != fm.include && name == fm.ignore {
|
|
|
|
return ignoreresult.Ignored
|
|
|
|
}
|
|
|
|
return ignoreresult.NotIgnored
|
2018-05-14 07:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fm fakeMatcher) SkipIgnoredDirs() bool {
|
|
|
|
return fm.skipIgnoredDirs
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type fakeEventInfo string
|
|
|
|
|
|
|
|
func (e fakeEventInfo) Path() string {
|
|
|
|
return string(e)
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (fakeEventInfo) Event() notify.Event {
|
2017-10-20 14:52:55 +00:00
|
|
|
return notify.Write
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (fakeEventInfo) Sys() interface{} {
|
2017-10-20 14:52:55 +00:00
|
|
|
return nil
|
|
|
|
}
|