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