2016-11-24 12:07:14 +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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-11-24 12:07:14 +00:00
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2017-10-20 14:52:55 +00:00
|
|
|
"context"
|
2017-08-19 14:36:56 +00:00
|
|
|
"errors"
|
2016-11-24 12:07:14 +00:00
|
|
|
"io"
|
2017-04-01 09:04:11 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-09-02 05:52:38 +00:00
|
|
|
"strings"
|
2016-11-24 12:07:14 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The Filesystem interface abstracts access to the file system.
|
|
|
|
type Filesystem interface {
|
|
|
|
Chmod(name string, mode FileMode) error
|
|
|
|
Chtimes(name string, atime time.Time, mtime time.Time) error
|
|
|
|
Create(name string) (File, error)
|
2018-01-14 14:25:04 +00:00
|
|
|
CreateSymlink(target, name string) error
|
2016-11-24 12:07:14 +00:00
|
|
|
DirNames(name string) ([]string, error)
|
|
|
|
Lstat(name string) (FileInfo, error)
|
|
|
|
Mkdir(name string, perm FileMode) error
|
2017-08-19 14:36:56 +00:00
|
|
|
MkdirAll(name string, perm FileMode) error
|
2016-11-24 12:07:14 +00:00
|
|
|
Open(name string) (File, error)
|
2017-08-19 14:36:56 +00:00
|
|
|
OpenFile(name string, flags int, mode FileMode) (File, error)
|
2017-02-07 08:34:24 +00:00
|
|
|
ReadSymlink(name string) (string, error)
|
2016-11-24 12:07:14 +00:00
|
|
|
Remove(name string) error
|
2017-08-19 14:36:56 +00:00
|
|
|
RemoveAll(name string) error
|
2016-11-24 12:07:14 +00:00
|
|
|
Rename(oldname, newname string) error
|
|
|
|
Stat(name string) (FileInfo, error)
|
|
|
|
SymlinksSupported() bool
|
2017-10-20 14:52:55 +00:00
|
|
|
Walk(name string, walkFn WalkFunc) error
|
|
|
|
Watch(path string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, error)
|
2017-08-19 14:36:56 +00:00
|
|
|
Hide(name string) error
|
|
|
|
Unhide(name string) error
|
|
|
|
Glob(pattern string) ([]string, error)
|
|
|
|
Roots() ([]string, error)
|
|
|
|
Usage(name string) (Usage, error)
|
|
|
|
Type() FilesystemType
|
|
|
|
URI() string
|
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
|
|
|
SameFile(fi1, fi2 FileInfo) bool
|
2016-11-24 12:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The File interface abstracts access to a regular file, being a somewhat
|
|
|
|
// smaller interface than os.File
|
|
|
|
type File interface {
|
2017-08-19 14:36:56 +00:00
|
|
|
io.Closer
|
2016-11-24 12:07:14 +00:00
|
|
|
io.Reader
|
2017-08-19 14:36:56 +00:00
|
|
|
io.ReaderAt
|
|
|
|
io.Seeker
|
|
|
|
io.Writer
|
2016-11-24 12:07:14 +00:00
|
|
|
io.WriterAt
|
2017-08-19 14:36:56 +00:00
|
|
|
Name() string
|
2016-11-24 12:07:14 +00:00
|
|
|
Truncate(size int64) error
|
2017-04-01 09:04:11 +00:00
|
|
|
Stat() (FileInfo, error)
|
2017-08-19 14:36:56 +00:00
|
|
|
Sync() error
|
2016-11-24 12:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The FileInfo interface is almost the same as os.FileInfo, but with the
|
|
|
|
// Sys method removed (as we don't want to expose whatever is underlying)
|
|
|
|
// and with a couple of convenience methods added.
|
|
|
|
type FileInfo interface {
|
|
|
|
// Standard things present in os.FileInfo
|
|
|
|
Name() string
|
|
|
|
Mode() FileMode
|
|
|
|
Size() int64
|
|
|
|
ModTime() time.Time
|
|
|
|
IsDir() bool
|
|
|
|
// Extensions
|
|
|
|
IsRegular() bool
|
|
|
|
IsSymlink() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileMode is similar to os.FileMode
|
|
|
|
type FileMode uint32
|
|
|
|
|
2018-10-31 11:49:50 +00:00
|
|
|
func (fm FileMode) String() string {
|
|
|
|
return os.FileMode(fm).String()
|
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
// Usage represents filesystem space usage
|
|
|
|
type Usage struct {
|
|
|
|
Free int64
|
|
|
|
Total int64
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
type Matcher interface {
|
|
|
|
ShouldIgnore(name string) bool
|
2018-05-14 07:47:23 +00:00
|
|
|
SkipIgnoredDirs() bool
|
2017-10-20 14:52:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MatchResult interface {
|
|
|
|
IsIgnored() bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type Event struct {
|
|
|
|
Name string
|
|
|
|
Type EventType
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NonRemove EventType = 1 + iota
|
|
|
|
Remove
|
|
|
|
Mixed // Should probably not be necessary to be used in filesystem interface implementation
|
|
|
|
)
|
|
|
|
|
2018-05-26 09:08:23 +00:00
|
|
|
// Merge returns Mixed, except if evType and other are the same and not Mixed.
|
|
|
|
func (evType EventType) Merge(other EventType) EventType {
|
|
|
|
return evType | other
|
|
|
|
}
|
|
|
|
|
2017-10-20 14:52:55 +00:00
|
|
|
func (evType EventType) String() string {
|
|
|
|
switch {
|
|
|
|
case evType == NonRemove:
|
|
|
|
return "non-remove"
|
|
|
|
case evType == Remove:
|
|
|
|
return "remove"
|
|
|
|
case evType == Mixed:
|
|
|
|
return "mixed"
|
|
|
|
default:
|
|
|
|
panic("bug: Unknown event type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var ErrWatchNotSupported = errors.New("watching is not supported")
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
// Equivalents from os package.
|
2017-04-01 09:04:11 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
const ModePerm = FileMode(os.ModePerm)
|
|
|
|
const ModeSetgid = FileMode(os.ModeSetgid)
|
|
|
|
const ModeSetuid = FileMode(os.ModeSetuid)
|
|
|
|
const ModeSticky = FileMode(os.ModeSticky)
|
2017-12-29 21:23:06 +00:00
|
|
|
const ModeSymlink = FileMode(os.ModeSymlink)
|
|
|
|
const ModeType = FileMode(os.ModeType)
|
2017-08-19 14:36:56 +00:00
|
|
|
const PathSeparator = os.PathSeparator
|
|
|
|
const OptAppend = os.O_APPEND
|
|
|
|
const OptCreate = os.O_CREATE
|
|
|
|
const OptExclusive = os.O_EXCL
|
|
|
|
const OptReadOnly = os.O_RDONLY
|
|
|
|
const OptReadWrite = os.O_RDWR
|
|
|
|
const OptSync = os.O_SYNC
|
|
|
|
const OptTruncate = os.O_TRUNC
|
|
|
|
const OptWriteOnly = os.O_WRONLY
|
2016-11-24 12:07:14 +00:00
|
|
|
|
|
|
|
// SkipDir is used as a return value from WalkFuncs to indicate that
|
|
|
|
// the directory named in the call is to be skipped. It is not returned
|
|
|
|
// as an error by any function.
|
2017-04-01 09:04:11 +00:00
|
|
|
var SkipDir = filepath.SkipDir
|
|
|
|
|
|
|
|
// IsExist is the equivalent of os.IsExist
|
|
|
|
var IsExist = os.IsExist
|
|
|
|
|
|
|
|
// IsNotExist is the equivalent of os.IsNotExist
|
|
|
|
var IsNotExist = os.IsNotExist
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
// IsPermission is the equivalent of os.IsPermission
|
|
|
|
var IsPermission = os.IsPermission
|
|
|
|
|
|
|
|
// IsPathSeparator is the equivalent of os.IsPathSeparator
|
|
|
|
var IsPathSeparator = os.IsPathSeparator
|
|
|
|
|
|
|
|
func NewFilesystem(fsType FilesystemType, uri string) Filesystem {
|
|
|
|
var fs Filesystem
|
|
|
|
switch fsType {
|
|
|
|
case FilesystemTypeBasic:
|
2018-02-05 10:07:56 +00:00
|
|
|
fs = newBasicFilesystem(uri)
|
2018-10-02 18:29:06 +00:00
|
|
|
case FilesystemTypeFake:
|
|
|
|
fs = newFakeFilesystem(uri)
|
2017-08-19 14:36:56 +00:00
|
|
|
default:
|
|
|
|
l.Debugln("Unknown filesystem", fsType, uri)
|
|
|
|
fs = &errorFilesystem{
|
|
|
|
fsType: fsType,
|
|
|
|
uri: uri,
|
|
|
|
err: errors.New("filesystem with type " + fsType.String() + " does not exist."),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 10:07:56 +00:00
|
|
|
if l.ShouldDebug("walkfs") {
|
|
|
|
return NewWalkFilesystem(&logFilesystem{fs})
|
|
|
|
}
|
|
|
|
|
2018-01-27 12:52:48 +00:00
|
|
|
if l.ShouldDebug("fs") {
|
2018-02-05 10:07:56 +00:00
|
|
|
return &logFilesystem{NewWalkFilesystem(fs)}
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
2018-02-05 10:07:56 +00:00
|
|
|
|
|
|
|
return NewWalkFilesystem(fs)
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
2017-09-02 05:52:38 +00:00
|
|
|
|
|
|
|
// 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 {
|
2017-11-05 12:18:05 +00:00
|
|
|
// fs cannot import config, so we hard code .stfolder here (config.DefaultMarkerName)
|
2017-09-02 05:52:38 +00:00
|
|
|
internals := []string{".stfolder", ".stignore", ".stversions"}
|
|
|
|
pathSep := string(PathSeparator)
|
|
|
|
for _, internal := range internals {
|
|
|
|
if file == internal {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(file, internal+pathSep) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-03-12 12:18:59 +00:00
|
|
|
|
|
|
|
// Canonicalize checks that the file path is valid and returns it in the "canonical" form:
|
|
|
|
// - /foo/bar -> foo/bar
|
|
|
|
// - / -> "."
|
|
|
|
func Canonicalize(file string) (string, error) {
|
|
|
|
pathSep := string(PathSeparator)
|
|
|
|
|
|
|
|
if strings.HasPrefix(file, pathSep+pathSep) {
|
|
|
|
// The relative path may pretend to be an absolute path within
|
|
|
|
// the root, but the double path separator on Windows implies
|
|
|
|
// something else and is out of spec.
|
|
|
|
return "", ErrNotRelative
|
|
|
|
}
|
|
|
|
|
|
|
|
// The relative path should be clean from internal dotdots and similar
|
|
|
|
// funkyness.
|
|
|
|
file = filepath.Clean(file)
|
|
|
|
|
|
|
|
// It is not acceptable to attempt to traverse upwards.
|
|
|
|
switch file {
|
|
|
|
case "..":
|
|
|
|
return "", ErrNotRelative
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(file, ".."+pathSep) {
|
|
|
|
return "", ErrNotRelative
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(file, pathSep) {
|
|
|
|
if file == pathSep {
|
|
|
|
return ".", nil
|
|
|
|
}
|
|
|
|
return file[1:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|