2017-01-10 07:09:31 +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/.
|
2017-01-10 07:09:31 +00:00
|
|
|
|
|
|
|
package osutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2017-08-19 14:36:56 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2017-01-10 07:09:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TraversesSymlinkError is an error indicating symlink traversal
|
|
|
|
type TraversesSymlinkError struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:27:34 +00:00
|
|
|
func (e *TraversesSymlinkError) Error() string {
|
2017-01-10 07:09:31 +00:00
|
|
|
return fmt.Sprintf("traverses symlink: %s", e.path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotADirectoryError is an error indicating an expected path is not a directory
|
|
|
|
type NotADirectoryError struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:27:34 +00:00
|
|
|
func (e *NotADirectoryError) Error() string {
|
2017-01-10 07:09:31 +00:00
|
|
|
return fmt.Sprintf("not a directory: %s", e.path)
|
|
|
|
}
|
|
|
|
|
2018-04-17 20:53:06 +00:00
|
|
|
// TraversesSymlink returns an error if any path component of name (including name
|
|
|
|
// itself) traverses a symlink.
|
2017-08-19 14:36:56 +00:00
|
|
|
func TraversesSymlink(filesystem fs.Filesystem, name string) error {
|
2018-04-17 20:53:06 +00:00
|
|
|
var err error
|
|
|
|
name, err = fs.Canonicalize(name)
|
2017-01-10 07:09:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "." {
|
2018-04-17 20:53:06 +00:00
|
|
|
// The result of calling TraversesSymlink(filesystem, filepath.Dir("foo"))
|
2017-01-10 07:09:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-17 20:53:06 +00:00
|
|
|
var path string
|
2021-04-11 13:29:43 +00:00
|
|
|
for _, part := range fs.PathComponents(name) {
|
2017-01-10 07:09:31 +00:00
|
|
|
path = filepath.Join(path, part)
|
2017-08-19 14:36:56 +00:00
|
|
|
info, err := filesystem.Lstat(path)
|
2017-01-10 07:09:31 +00:00
|
|
|
if err != nil {
|
2017-08-19 14:36:56 +00:00
|
|
|
if fs.IsNotExist(err) {
|
2017-01-10 07:09:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
if info.IsSymlink() {
|
2017-01-10 07:09:31 +00:00
|
|
|
return &TraversesSymlinkError{
|
2018-04-17 20:53:06 +00:00
|
|
|
path: path,
|
2017-01-10 07:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
|
|
return &NotADirectoryError{
|
2018-04-17 20:53:06 +00:00
|
|
|
path: path,
|
2017-01-10 07:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|