2016-11-24 05:07:14 -07: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-08 23:52:18 -07:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-11-24 05:07:14 -07:00
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2017-04-25 17:15:23 -07:00
|
|
|
"errors"
|
2016-11-24 05:07:14 -07:00
|
|
|
"os"
|
2017-08-19 07:36:56 -07:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
2016-11-24 05:07:14 -07:00
|
|
|
"time"
|
2017-08-19 07:36:56 -07:00
|
|
|
|
|
|
|
"github.com/calmh/du"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidFilename = errors.New("filename is invalid")
|
|
|
|
ErrNotRelative = errors.New("not a relative path")
|
2016-11-24 05:07:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// The BasicFilesystem implements all aspects by delegating to package os.
|
2017-08-19 07:36:56 -07:00
|
|
|
// All paths are relative to the root and cannot (should not) escape the root directory.
|
2016-11-24 05:07:14 -07:00
|
|
|
type BasicFilesystem struct {
|
2018-03-28 14:01:25 -07:00
|
|
|
root string
|
|
|
|
rootSymlinkEvaluated string
|
2017-08-19 07:36:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newBasicFilesystem(root string) *BasicFilesystem {
|
|
|
|
// The reason it's done like this:
|
|
|
|
// C: -> C:\ -> C:\ (issue that this is trying to fix)
|
|
|
|
// C:\somedir -> C:\somedir\ -> C:\somedir
|
|
|
|
// C:\somedir\ -> C:\somedir\\ -> C:\somedir
|
|
|
|
// This way in the tests, we get away without OS specific separators
|
|
|
|
// in the test configs.
|
|
|
|
root = filepath.Dir(root + string(filepath.Separator))
|
|
|
|
|
|
|
|
// Attempt tilde expansion; leave unchanged in case of error
|
|
|
|
if path, err := ExpandTilde(root); err == nil {
|
|
|
|
root = path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt absolutification; leave unchanged in case of error
|
|
|
|
if !filepath.IsAbs(root) {
|
|
|
|
// Abs() looks like a fairly expensive syscall on Windows, while
|
|
|
|
// IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
|
|
|
|
// somewhat faster in the general case, hence the outer if...
|
|
|
|
if path, err := filepath.Abs(root); err == nil {
|
|
|
|
root = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:01:25 -07:00
|
|
|
rootSymlinkEvaluated, err := filepath.EvalSymlinks(root)
|
|
|
|
if err != nil {
|
|
|
|
rootSymlinkEvaluated = root
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BasicFilesystem{
|
|
|
|
root: adjustRoot(root),
|
|
|
|
rootSymlinkEvaluated: adjustRoot(rootSymlinkEvaluated),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func adjustRoot(root string) string {
|
2017-08-19 07:36:56 -07:00
|
|
|
// Attempt to enable long filename support on Windows. We may still not
|
|
|
|
// have an absolute path here if the previous steps failed.
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if filepath.IsAbs(root) && !strings.HasPrefix(root, `\\`) {
|
|
|
|
root = `\\?\` + root
|
|
|
|
}
|
2018-03-28 14:01:25 -07:00
|
|
|
return root
|
2017-08-19 07:36:56 -07:00
|
|
|
}
|
|
|
|
|
2018-03-28 14:01:25 -07:00
|
|
|
// If we're not on Windows, we want the path to end with a slash to
|
|
|
|
// penetrate symlinks. On Windows, paths must not end with a slash.
|
|
|
|
if root[len(root)-1] != filepath.Separator {
|
|
|
|
root = root + string(filepath.Separator)
|
2017-08-19 07:36:56 -07:00
|
|
|
}
|
2018-03-28 14:01:25 -07:00
|
|
|
|
|
|
|
return root
|
2017-08-19 07:36:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// rooted expands the relative path to the full path that is then used with os
|
|
|
|
// package. If the relative path somehow causes the final path to escape the root
|
2017-09-07 02:40:46 -07:00
|
|
|
// directory, this returns an error, to prevent accessing files that are not in the
|
2017-08-19 07:36:56 -07:00
|
|
|
// shared directory.
|
|
|
|
func (f *BasicFilesystem) rooted(rel string) (string, error) {
|
2018-04-16 11:07:00 -07:00
|
|
|
return rooted(rel, f.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rootedSymlinkEvaluated does the same as rooted, but the returned path will not
|
|
|
|
// contain any symlinks. package. If the relative path somehow causes the final
|
|
|
|
// path to escape the root directory, this returns an error, to prevent accessing
|
|
|
|
// files that are not in the shared directory.
|
|
|
|
func (f *BasicFilesystem) rootedSymlinkEvaluated(rel string) (string, error) {
|
|
|
|
return rooted(rel, f.rootSymlinkEvaluated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rooted(rel, root string) (string, error) {
|
2017-08-19 07:36:56 -07:00
|
|
|
// The root must not be empty.
|
2018-04-16 11:07:00 -07:00
|
|
|
if root == "" {
|
2017-08-19 07:36:56 -07:00
|
|
|
return "", ErrInvalidFilename
|
|
|
|
}
|
|
|
|
|
|
|
|
pathSep := string(PathSeparator)
|
|
|
|
|
|
|
|
// The expected prefix for the resulting path is the root, with a path
|
|
|
|
// separator at the end.
|
2018-04-16 11:07:00 -07:00
|
|
|
expectedPrefix := filepath.FromSlash(root)
|
2017-08-19 07:36:56 -07:00
|
|
|
if !strings.HasSuffix(expectedPrefix, pathSep) {
|
|
|
|
expectedPrefix += pathSep
|
|
|
|
}
|
|
|
|
|
2018-03-12 05:18:59 -07:00
|
|
|
var err error
|
|
|
|
rel, err = Canonicalize(rel)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2017-08-19 07:36:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The supposedly correct path is the one filepath.Join will return, as
|
|
|
|
// it does cleaning and so on. Check that one first to make sure no
|
|
|
|
// obvious escape attempts have been made.
|
2018-04-16 11:07:00 -07:00
|
|
|
joined := filepath.Join(root, rel)
|
2017-08-19 07:36:56 -07:00
|
|
|
if rel == "." && !strings.HasSuffix(joined, pathSep) {
|
|
|
|
joined += pathSep
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(joined, expectedPrefix) {
|
|
|
|
return "", ErrNotRelative
|
|
|
|
}
|
|
|
|
|
|
|
|
return joined, nil
|
2016-11-24 05:07:14 -07:00
|
|
|
}
|
|
|
|
|
2017-08-19 07:36:56 -07:00
|
|
|
func (f *BasicFilesystem) unrooted(path string) string {
|
2018-03-28 14:01:25 -07:00
|
|
|
return rel(path, f.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) unrootedSymlinkEvaluated(path string) string {
|
|
|
|
return rel(path, f.rootSymlinkEvaluated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rel(path, prefix string) string {
|
|
|
|
return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator))
|
2016-11-24 05:07:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
|
2017-08-19 07:36:56 -07:00
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-24 05:07:14 -07:00
|
|
|
return os.Chmod(name, os.FileMode(mode))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
2017-08-19 07:36:56 -07:00
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-24 05:07:14 -07:00
|
|
|
return os.Chtimes(name, atime, mtime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Mkdir(name string, perm FileMode) error {
|
2017-08-19 07:36:56 -07:00
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-24 05:07:14 -07:00
|
|
|
return os.Mkdir(name, os.FileMode(perm))
|
|
|
|
}
|
|
|
|
|
2018-02-16 07:19:20 -07:00
|
|
|
// MkdirAll creates a directory named path, along with any necessary parents,
|
|
|
|
// and returns nil, or else returns an error.
|
|
|
|
// The permission bits perm are used for all directories that MkdirAll creates.
|
|
|
|
// If path is already a directory, MkdirAll does nothing and returns nil.
|
|
|
|
func (f *BasicFilesystem) MkdirAll(path string, perm FileMode) error {
|
|
|
|
path, err := f.rooted(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.mkdirAll(path, os.FileMode(perm))
|
|
|
|
}
|
|
|
|
|
2016-11-24 05:07:14 -07:00
|
|
|
func (f *BasicFilesystem) Lstat(name string) (FileInfo, error) {
|
2017-08-19 07:36:56 -07:00
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-01 02:04:11 -07:00
|
|
|
fi, err := underlyingLstat(name)
|
2016-11-24 05:07:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fsFileInfo{fi}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Remove(name string) error {
|
2017-08-19 07:36:56 -07:00
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-24 05:07:14 -07:00
|
|
|
return os.Remove(name)
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:36:56 -07:00
|
|
|
func (f *BasicFilesystem) RemoveAll(name string) error {
|
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(name)
|
|
|
|
}
|
|
|
|
|
2016-11-24 05:07:14 -07:00
|
|
|
func (f *BasicFilesystem) Rename(oldpath, newpath string) error {
|
2017-08-19 07:36:56 -07:00
|
|
|
oldpath, err := f.rooted(oldpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
newpath, err = f.rooted(newpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-24 05:07:14 -07:00
|
|
|
return os.Rename(oldpath, newpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Stat(name string) (FileInfo, error) {
|
2017-08-19 07:36:56 -07:00
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-11-24 05:07:14 -07:00
|
|
|
fi, err := os.Stat(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fsFileInfo{fi}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) DirNames(name string) ([]string, error) {
|
2017-08-19 07:36:56 -07:00
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fd, err := os.OpenFile(name, OptReadOnly, 0777)
|
2016-11-24 05:07:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
names, err := fd.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Open(name string) (File, error) {
|
2017-08-19 07:36:56 -07:00
|
|
|
rootedName, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fd, err := os.Open(rootedName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fsFile{fd, name}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) OpenFile(name string, flags int, mode FileMode) (File, error) {
|
|
|
|
rootedName, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fd, err := os.OpenFile(rootedName, flags, os.FileMode(mode))
|
2017-04-01 02:04:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-19 07:36:56 -07:00
|
|
|
return fsFile{fd, name}, err
|
2016-11-24 05:07:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Create(name string) (File, error) {
|
2017-08-19 07:36:56 -07:00
|
|
|
rootedName, err := f.rooted(name)
|
2017-04-01 02:04:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-19 07:36:56 -07:00
|
|
|
fd, err := os.Create(rootedName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fsFile{fd, name}, err
|
2017-04-01 02:04:11 -07:00
|
|
|
}
|
|
|
|
|
2017-04-25 17:15:23 -07:00
|
|
|
func (f *BasicFilesystem) Walk(root string, walkFn WalkFunc) error {
|
|
|
|
// implemented in WalkFilesystem
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2017-08-19 07:36:56 -07:00
|
|
|
func (f *BasicFilesystem) Glob(pattern string) ([]string, error) {
|
|
|
|
pattern, err := f.rooted(pattern)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
files, err := filepath.Glob(pattern)
|
|
|
|
unrooted := make([]string, len(files))
|
|
|
|
for i := range files {
|
|
|
|
unrooted[i] = f.unrooted(files[i])
|
|
|
|
}
|
|
|
|
return unrooted, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Usage(name string) (Usage, error) {
|
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return Usage{}, err
|
|
|
|
}
|
|
|
|
u, err := du.Get(name)
|
|
|
|
return Usage{
|
|
|
|
Free: u.FreeBytes,
|
|
|
|
Total: u.TotalBytes,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) Type() FilesystemType {
|
|
|
|
return FilesystemTypeBasic
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) URI() string {
|
|
|
|
return strings.TrimPrefix(f.root, `\\?\`)
|
|
|
|
}
|
|
|
|
|
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 11:11:09 -07:00
|
|
|
func (f *BasicFilesystem) SameFile(fi1, fi2 FileInfo) bool {
|
|
|
|
// Like os.SameFile, we always return false unless fi1 and fi2 were created
|
|
|
|
// by this package's Stat/Lstat method.
|
|
|
|
f1, ok1 := fi1.(fsFileInfo)
|
|
|
|
f2, ok2 := fi2.(fsFileInfo)
|
|
|
|
if !ok1 || !ok2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.SameFile(f1.FileInfo, f2.FileInfo)
|
|
|
|
}
|
|
|
|
|
2017-04-01 02:04:11 -07:00
|
|
|
// fsFile implements the fs.File interface on top of an os.File
|
|
|
|
type fsFile struct {
|
|
|
|
*os.File
|
2017-08-19 07:36:56 -07:00
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fsFile) Name() string {
|
|
|
|
return f.name
|
2017-04-01 02:04:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f fsFile) Stat() (FileInfo, error) {
|
|
|
|
info, err := f.File.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return fsFileInfo{info}, nil
|
2016-11-24 05:07:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// fsFileInfo implements the fs.FileInfo interface on top of an os.FileInfo.
|
|
|
|
type fsFileInfo struct {
|
|
|
|
os.FileInfo
|
|
|
|
}
|
|
|
|
|
2017-12-29 14:23:06 -07:00
|
|
|
func (e fsFileInfo) IsSymlink() bool {
|
|
|
|
// Must use fsFileInfo.Mode() because it may apply magic.
|
|
|
|
return e.Mode()&ModeSymlink != 0
|
2016-11-24 05:07:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e fsFileInfo) IsRegular() bool {
|
2017-12-29 14:23:06 -07:00
|
|
|
// Must use fsFileInfo.Mode() because it may apply magic.
|
|
|
|
return e.Mode()&ModeType == 0
|
2016-11-24 05:07:14 -07:00
|
|
|
}
|