lib: Folder marker is now a folder
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4341 LGTM: calmh
This commit is contained in:
committed by
Jakob Borg
parent
19e52a10df
commit
ab132ff6fe
@@ -278,10 +278,10 @@ func (m *Matcher) clean(d time.Duration) {
|
||||
// ShouldIgnore returns true when a file is temporary, internal or ignored
|
||||
func (m *Matcher) ShouldIgnore(filename string) bool {
|
||||
switch {
|
||||
case IsTemporary(filename):
|
||||
case fs.IsTemporary(filename):
|
||||
return true
|
||||
|
||||
case IsInternal(filename):
|
||||
case fs.IsInternal(filename):
|
||||
return true
|
||||
|
||||
case m.Match(filename).IsIgnored():
|
||||
@@ -458,23 +458,6 @@ func parseIgnoreFile(fs fs.Filesystem, fd io.Reader, currentFile string, cd Chan
|
||||
return lines, patterns, nil
|
||||
}
|
||||
|
||||
// IsInternal returns true if the file, as a path relative to the folder
|
||||
// root, represents an internal file that should always be ignored. The file
|
||||
// path must be clean (i.e., in canonical shortest form).
|
||||
func IsInternal(file string) bool {
|
||||
internals := []string{".stfolder", ".stignore", ".stversions"}
|
||||
pathSep := string(fs.PathSeparator)
|
||||
for _, internal := range internals {
|
||||
if file == internal {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(file, internal+pathSep) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// WriteIgnores is a convenience function to avoid code duplication
|
||||
func WriteIgnores(filesystem fs.Filesystem, path string, content []string) error {
|
||||
fd, err := osutil.CreateAtomicFilesystem(filesystem, path)
|
||||
|
||||
@@ -837,37 +837,6 @@ func TestGobwasGlobIssue18(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsInternal(t *testing.T) {
|
||||
cases := []struct {
|
||||
file string
|
||||
internal bool
|
||||
}{
|
||||
{".stfolder", true},
|
||||
{".stignore", true},
|
||||
{".stversions", true},
|
||||
{".stfolder/foo", true},
|
||||
{".stignore/foo", true},
|
||||
{".stversions/foo", true},
|
||||
|
||||
{".stfolderfoo", false},
|
||||
{".stignorefoo", false},
|
||||
{".stversionsfoo", false},
|
||||
{"foo.stfolder", false},
|
||||
{"foo.stignore", false},
|
||||
{"foo.stversions", false},
|
||||
{"foo/.stfolder", false},
|
||||
{"foo/.stignore", false},
|
||||
{"foo/.stversions", false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
res := IsInternal(filepath.FromSlash(tc.file))
|
||||
if res != tc.internal {
|
||||
t.Errorf("Unexpected result: IsInteral(%q): %v should be %v", tc.file, res, tc.internal)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoot(t *testing.T) {
|
||||
stignore := `
|
||||
!/a
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
// Copyright (C) 2015 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 ignore
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
WindowsTempPrefix = "~syncthing~"
|
||||
UnixTempPrefix = ".syncthing."
|
||||
)
|
||||
|
||||
var TempPrefix string
|
||||
|
||||
// Real filesystems usually handle 255 bytes. encfs has varying and
|
||||
// confusing file name limits. We take a safe way out and switch to hashing
|
||||
// quite early.
|
||||
const maxFilenameLength = 160 - len(UnixTempPrefix) - len(".tmp")
|
||||
|
||||
func init() {
|
||||
if runtime.GOOS == "windows" {
|
||||
TempPrefix = WindowsTempPrefix
|
||||
} else {
|
||||
TempPrefix = UnixTempPrefix
|
||||
}
|
||||
}
|
||||
|
||||
// IsTemporary is true if the file name has the temporary prefix. Regardless
|
||||
// of the normally used prefix, the standard Windows and Unix temp prefixes
|
||||
// are always recognized as temp files.
|
||||
func IsTemporary(name string) bool {
|
||||
name = filepath.Base(name)
|
||||
if strings.HasPrefix(name, WindowsTempPrefix) ||
|
||||
strings.HasPrefix(name, UnixTempPrefix) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TempName(name string) string {
|
||||
tdir := filepath.Dir(name)
|
||||
tbase := filepath.Base(name)
|
||||
if len(tbase) > maxFilenameLength {
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(name))
|
||||
tbase = fmt.Sprintf("%x", hash.Sum(nil))
|
||||
}
|
||||
tname := fmt.Sprintf("%s%s.tmp", TempPrefix, tbase)
|
||||
return filepath.Join(tdir, tname)
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
// Copyright (C) 2015 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 ignore
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLongTempFilename(t *testing.T) {
|
||||
filename := ""
|
||||
for i := 0; i < 300; i++ {
|
||||
filename += "l"
|
||||
}
|
||||
tFile := TempName(filename)
|
||||
if len(tFile) < 10 || len(tFile) > 200 {
|
||||
t.Fatal("Invalid long filename")
|
||||
}
|
||||
if !strings.HasSuffix(TempName("short"), "short.tmp") {
|
||||
t.Fatal("Invalid short filename", TempName("short"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user