2019-04-10 20:36:37 +00:00
|
|
|
// Copyright (C) 2019 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 https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2020-07-22 20:10:24 +00:00
|
|
|
"errors"
|
2020-05-13 19:46:24 +00:00
|
|
|
"fmt"
|
|
|
|
osexec "os/exec"
|
|
|
|
"path/filepath"
|
2019-04-10 20:36:37 +00:00
|
|
|
"testing"
|
2022-07-28 17:36:39 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2019-04-10 20:36:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func testWalkSkipSymlink(t *testing.T, fsType FilesystemType, uri string) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsWindows {
|
2020-05-13 19:46:24 +00:00
|
|
|
t.Skip("Symlinks skipping is not tested on windows")
|
2019-04-10 20:36:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fs := NewFilesystem(fsType, uri)
|
|
|
|
|
2024-11-19 10:32:56 +00:00
|
|
|
if err := fs.MkdirAll("target/foo", 0o755); err != nil {
|
2019-04-10 20:36:37 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-11-19 10:32:56 +00:00
|
|
|
if err := fs.Mkdir("towalk", 0o755); err != nil {
|
2019-04-10 20:36:37 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := fs.CreateSymlink("target", "towalk/symlink"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := fs.Walk("towalk", func(path string, info FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if info.Name() != "symlink" && info.Name() != "towalk" {
|
|
|
|
t.Fatal("Walk unexpected file", info.Name())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 19:46:24 +00:00
|
|
|
|
|
|
|
func createDirJunct(target string, name string) error {
|
|
|
|
output, err := osexec.Command("cmd", "/c", "mklink", "/J", name, target).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to run mklink %v %v: %v %q", name, target, err, output)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWalkTraverseDirJunct(t *testing.T, fsType FilesystemType, uri string) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsWindows {
|
2020-05-13 19:46:24 +00:00
|
|
|
t.Skip("Directory junctions are available and tested on windows only")
|
|
|
|
}
|
|
|
|
|
2021-03-11 14:23:56 +00:00
|
|
|
fs := NewFilesystem(fsType, uri, new(OptionJunctionsAsDirs))
|
2020-05-13 19:46:24 +00:00
|
|
|
|
|
|
|
if err := fs.MkdirAll("target/foo", 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := fs.Mkdir("towalk", 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := createDirJunct(filepath.Join(uri, "target"), filepath.Join(uri, "towalk/dirjunct")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
traversed := false
|
|
|
|
if err := fs.Walk("towalk", func(path string, info FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if info.Name() == "foo" {
|
|
|
|
traversed = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !traversed {
|
|
|
|
t.Fatal("Directory junction was not traversed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWalkInfiniteRecursion(t *testing.T, fsType FilesystemType, uri string) {
|
2022-07-28 17:36:39 +00:00
|
|
|
if !build.IsWindows {
|
2020-05-13 19:46:24 +00:00
|
|
|
t.Skip("Infinite recursion detection is tested on windows only")
|
|
|
|
}
|
|
|
|
|
2021-03-11 14:23:56 +00:00
|
|
|
fs := NewFilesystem(fsType, uri, new(OptionJunctionsAsDirs))
|
2020-05-13 19:46:24 +00:00
|
|
|
|
|
|
|
if err := fs.MkdirAll("target/foo", 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := fs.Mkdir("towalk", 0); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := createDirJunct(filepath.Join(uri, "target"), filepath.Join(uri, "towalk/dirjunct")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := createDirJunct(filepath.Join(uri, "towalk"), filepath.Join(uri, "target/foo/recurse")); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dirjunctCnt := 0
|
|
|
|
fooCnt := 0
|
2020-07-22 20:10:24 +00:00
|
|
|
found := false
|
2020-05-13 19:46:24 +00:00
|
|
|
if err := fs.Walk("towalk", func(path string, info FileInfo, err error) error {
|
|
|
|
if err != nil {
|
2020-07-22 20:10:24 +00:00
|
|
|
if errors.Is(err, ErrInfiniteRecursion) {
|
|
|
|
if found {
|
|
|
|
t.Fatal("second infinite recursion detected at", path)
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-13 19:46:24 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if info.Name() == "dirjunct" {
|
|
|
|
dirjunctCnt++
|
|
|
|
} else if info.Name() == "foo" {
|
|
|
|
fooCnt++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-22 20:10:24 +00:00
|
|
|
if dirjunctCnt != 2 || fooCnt != 1 || !found {
|
2020-05-13 19:46:24 +00:00
|
|
|
t.Fatal("Infinite recursion not detected correctly")
|
|
|
|
}
|
|
|
|
}
|