basicfs_unix.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. // +build !windows
  7. package fs
  8. import (
  9. "fmt"
  10. "os"
  11. "path/filepath"
  12. "strings"
  13. )
  14. func (BasicFilesystem) SymlinksSupported() bool {
  15. return true
  16. }
  17. func (f *BasicFilesystem) CreateSymlink(target, name string) error {
  18. name, err := f.rooted(name)
  19. if err != nil {
  20. return err
  21. }
  22. return os.Symlink(target, name)
  23. }
  24. func (f *BasicFilesystem) ReadSymlink(name string) (string, error) {
  25. name, err := f.rooted(name)
  26. if err != nil {
  27. return "", err
  28. }
  29. return os.Readlink(name)
  30. }
  31. func (f *BasicFilesystem) mkdirAll(path string, perm os.FileMode) error {
  32. return os.MkdirAll(path, perm)
  33. }
  34. // Unhide is a noop on unix, as unhiding files requires renaming them.
  35. // We still check that the relative path does not try to escape the root
  36. func (f *BasicFilesystem) Unhide(name string) error {
  37. _, err := f.rooted(name)
  38. return err
  39. }
  40. // Hide is a noop on unix, as hiding files requires renaming them.
  41. // We still check that the relative path does not try to escape the root
  42. func (f *BasicFilesystem) Hide(name string) error {
  43. _, err := f.rooted(name)
  44. return err
  45. }
  46. func (f *BasicFilesystem) Roots() ([]string, error) {
  47. return []string{"/"}, nil
  48. }
  49. // unrootedChecked returns the path relative to the folder root (same as
  50. // unrooted). It panics if the given path is not a subpath and handles the
  51. // special case when the given path is the folder root without a trailing
  52. // pathseparator.
  53. func (f *BasicFilesystem) unrootedChecked(absPath, root string) string {
  54. if absPath+string(PathSeparator) == root {
  55. return "."
  56. }
  57. if !strings.HasPrefix(absPath, root) {
  58. panic(fmt.Sprintf("bug: Notify backend is processing a change outside of the filesystem root: f.root==%v, root==%v, path==%v", f.root, root, absPath))
  59. }
  60. return rel(absPath, root)
  61. }
  62. func rel(path, prefix string) string {
  63. return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator))
  64. }
  65. var evalSymlinks = filepath.EvalSymlinks