2017-08-19 14:36:56 +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,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
2021-08-17 08:10:41 +00:00
|
|
|
//go:build !windows
|
2017-08-19 14:36:56 +00:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
2018-09-11 20:30:32 +00:00
|
|
|
import (
|
|
|
|
"os"
|
2018-09-26 18:28:20 +00:00
|
|
|
"path/filepath"
|
2022-07-26 06:24:58 +00:00
|
|
|
"strconv"
|
2018-09-11 20:30:32 +00:00
|
|
|
"strings"
|
|
|
|
)
|
2017-08-19 14:36:56 +00:00
|
|
|
|
2022-08-12 05:48:00 +00:00
|
|
|
func (*BasicFilesystem) SymlinksSupported() bool {
|
2017-08-19 14:36:56 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) CreateSymlink(target, name string) error {
|
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Symlink(target, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *BasicFilesystem) ReadSymlink(name string) (string, error) {
|
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return os.Readlink(name)
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*BasicFilesystem) mkdirAll(path string, perm os.FileMode) error {
|
2018-02-16 14:19:20 +00:00
|
|
|
return os.MkdirAll(path, perm)
|
2017-08-19 14:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unhide is a noop on unix, as unhiding files requires renaming them.
|
|
|
|
// We still check that the relative path does not try to escape the root
|
|
|
|
func (f *BasicFilesystem) Unhide(name string) error {
|
|
|
|
_, err := f.rooted(name)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hide is a noop on unix, as hiding files requires renaming them.
|
|
|
|
// We still check that the relative path does not try to escape the root
|
|
|
|
func (f *BasicFilesystem) Hide(name string) error {
|
|
|
|
_, err := f.rooted(name)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*BasicFilesystem) Roots() ([]string, error) {
|
2017-08-19 14:36:56 +00:00
|
|
|
return []string{"/"}, nil
|
|
|
|
}
|
2018-06-04 11:41:03 +00:00
|
|
|
|
2022-07-26 06:24:58 +00:00
|
|
|
func (f *BasicFilesystem) Lchown(name, uid, gid string) error {
|
|
|
|
name, err := f.rooted(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nuid, err := strconv.Atoi(uid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ngid, err := strconv.Atoi(gid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Lchown(name, nuid, ngid)
|
|
|
|
}
|
|
|
|
|
2018-09-11 20:30:32 +00:00
|
|
|
// unrootedChecked returns the path relative to the folder root (same as
|
2019-05-25 19:08:26 +00:00
|
|
|
// unrooted) or an error if the given path is not a subpath and handles the
|
2018-09-11 20:30:32 +00:00
|
|
|
// special case when the given path is the folder root without a trailing
|
|
|
|
// pathseparator.
|
2019-09-22 07:03:22 +00:00
|
|
|
func (f *BasicFilesystem) unrootedChecked(absPath string, roots []string) (string, *ErrWatchEventOutsideRoot) {
|
|
|
|
for _, root := range roots {
|
2019-09-29 15:38:11 +00:00
|
|
|
// Make sure the root ends with precisely one path separator, to
|
|
|
|
// ease prefix comparisons.
|
|
|
|
root := strings.TrimRight(root, string(PathSeparator)) + string(PathSeparator)
|
|
|
|
|
2019-09-22 07:03:22 +00:00
|
|
|
if absPath+string(PathSeparator) == root {
|
|
|
|
return ".", nil
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(absPath, root) {
|
|
|
|
return rel(absPath, root), nil
|
|
|
|
}
|
2018-09-11 20:30:32 +00:00
|
|
|
}
|
2019-09-22 07:03:22 +00:00
|
|
|
return "", f.newErrWatchEventOutsideRoot(absPath, roots)
|
2018-09-11 20:30:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func rel(path, prefix string) string {
|
|
|
|
return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator))
|
2018-06-04 11:41:03 +00:00
|
|
|
}
|
2018-09-26 18:28:20 +00:00
|
|
|
|
|
|
|
var evalSymlinks = filepath.EvalSymlinks
|
2019-05-11 08:06:04 +00:00
|
|
|
|
|
|
|
// watchPaths adjust the folder root for use with the notify backend and the
|
|
|
|
// corresponding absolute path to be passed to notify to watch name.
|
2019-09-22 07:03:22 +00:00
|
|
|
func (f *BasicFilesystem) watchPaths(name string) (string, []string, error) {
|
2019-05-11 08:06:04 +00:00
|
|
|
root, err := evalSymlinks(f.root)
|
|
|
|
if err != nil {
|
2019-09-22 07:03:22 +00:00
|
|
|
return "", nil, err
|
2019-05-11 08:06:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
absName, err := rooted(name, root)
|
|
|
|
if err != nil {
|
2019-09-22 07:03:22 +00:00
|
|
|
return "", nil, err
|
2019-05-11 08:06:04 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 07:03:22 +00:00
|
|
|
return filepath.Join(absName, "..."), []string{root}, nil
|
2019-05-11 08:06:04 +00:00
|
|
|
}
|