lib: Introduce fs.IsParent (fixes #5324) (#5326)

This commit is contained in:
Simon Frei 2018-11-22 11:16:45 +01:00 committed by Jakob Borg
parent 513d3bc374
commit 2f9840ddae
6 changed files with 19 additions and 11 deletions

View File

@ -199,12 +199,11 @@ func NewFilesystem(fsType FilesystemType, uri string) Filesystem {
func IsInternal(file string) bool { func IsInternal(file string) bool {
// fs cannot import config, so we hard code .stfolder here (config.DefaultMarkerName) // fs cannot import config, so we hard code .stfolder here (config.DefaultMarkerName)
internals := []string{".stfolder", ".stignore", ".stversions"} internals := []string{".stfolder", ".stignore", ".stversions"}
pathSep := string(PathSeparator)
for _, internal := range internals { for _, internal := range internals {
if file == internal { if file == internal {
return true return true
} }
if strings.HasPrefix(file, internal+pathSep) { if IsParent(file, internal) {
return true return true
} }
} }

View File

@ -77,3 +77,15 @@ func WindowsInvalidFilename(name string) bool {
// The path must not contain any disallowed characters // The path must not contain any disallowed characters
return strings.ContainsAny(name, windowsDisallowedCharacters) return strings.ContainsAny(name, windowsDisallowedCharacters)
} }
func IsParent(path, parent string) bool {
if len(parent) == 0 {
// The empty string is the parent of everything except the empty
// string. (Avoids panic in the next step.)
return len(path) > 0
}
if parent[len(parent)-1] != PathSeparator {
parent += string(PathSeparator)
}
return strings.HasPrefix(path, parent)
}

View File

@ -336,7 +336,7 @@ func loadParseIncludeFile(filesystem fs.Filesystem, file string, cd ChangeDetect
if filesystem.Type() == fs.FilesystemTypeBasic { if filesystem.Type() == fs.FilesystemTypeBasic {
uri := filesystem.URI() uri := filesystem.URI()
joined := filepath.Join(uri, file) joined := filepath.Join(uri, file)
if !strings.HasPrefix(joined, uri) { if !fs.IsParent(joined, uri) {
filesystem = fs.NewFilesystem(filesystem.Type(), filepath.Dir(joined)) filesystem = fs.NewFilesystem(filesystem.Type(), filepath.Dir(joined))
file = filepath.Base(joined) file = filepath.Base(joined)
} }

View File

@ -13,7 +13,6 @@ import (
"math/rand" "math/rand"
"path/filepath" "path/filepath"
"sort" "sort"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
@ -411,7 +410,6 @@ func (f *folder) scanSubdirs(subDirs []string) error {
// ignored files. // ignored files.
var toIgnore []db.FileInfoTruncated var toIgnore []db.FileInfoTruncated
ignoredParent := "" ignoredParent := ""
pathSep := string(fs.PathSeparator)
for _, sub := range subDirs { for _, sub := range subDirs {
var iterError error var iterError error
@ -423,7 +421,7 @@ func (f *folder) scanSubdirs(subDirs []string) error {
return false return false
} }
if ignoredParent != "" && !strings.HasPrefix(file.Name, ignoredParent+pathSep) { if ignoredParent != "" && !fs.IsParent(file.Name, ignoredParent) {
for _, file := range toIgnore { for _, file := range toIgnore {
l.Debugln("marking file as ignored", file) l.Debugln("marking file as ignored", file)
nf := file.ConvertToIgnoredFileInfo(f.model.id.Short()) nf := file.ConvertToIgnoredFileInfo(f.model.id.Short())
@ -698,11 +696,10 @@ func (f *folder) clearScanErrors(subDirs []string) {
return return
} }
filtered := f.scanErrors[:0] filtered := f.scanErrors[:0]
pathSep := string(fs.PathSeparator)
outer: outer:
for _, fe := range f.scanErrors { for _, fe := range f.scanErrors {
for _, sub := range subDirs { for _, sub := range subDirs {
if strings.HasPrefix(fe.Path, sub+pathSep) { if fe.Path == sub || fs.IsParent(fe.Path, sub) {
continue outer continue outer
} }
} }
@ -735,7 +732,7 @@ func unifySubs(dirs []string, exists func(dir string) bool) []string {
dirs = append(dirs[:i], dirs[i+1:]...) dirs = append(dirs[:i], dirs[i+1:]...)
continue continue
} }
if dir == prev || strings.HasPrefix(dir, prev+string(fs.PathSeparator)) { if dir == prev || fs.IsParent(dir, prev) {
dirs = append(dirs[:i], dirs[i+1:]...) dirs = append(dirs[:i], dirs[i+1:]...)
continue continue
} }

View File

@ -288,7 +288,7 @@ func (m *Model) warnAboutOverwritingProtectedFiles(folder string) {
var filesAtRisk []string var filesAtRisk []string
for _, protectedFilePath := range m.protectedFiles { for _, protectedFilePath := range m.protectedFiles {
// check if file is synced in this folder // check if file is synced in this folder
if !strings.HasPrefix(protectedFilePath, folderLocation) { if protectedFilePath != folderLocation && !fs.IsParent(protectedFilePath, folderLocation) {
continue continue
} }

View File

@ -250,7 +250,7 @@ func (w *walker) walkAndHashFiles(ctx context.Context, toHashChan chan<- protoco
return skip return skip
} }
// If the parent wasn't ignored already, set this path as the "highest" ignored parent // If the parent wasn't ignored already, set this path as the "highest" ignored parent
if info.IsDir() && (ignoredParent == "" || !strings.HasPrefix(path, ignoredParent+string(fs.PathSeparator))) { if info.IsDir() && (ignoredParent == "" || !fs.IsParent(path, ignoredParent)) {
ignoredParent = path ignoredParent = path
} }
return nil return nil