basicfs_unix.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "os"
  10. "path/filepath"
  11. "strings"
  12. )
  13. func (BasicFilesystem) SymlinksSupported() bool {
  14. return true
  15. }
  16. func (f *BasicFilesystem) CreateSymlink(target, name string) error {
  17. name, err := f.rooted(name)
  18. if err != nil {
  19. return err
  20. }
  21. return os.Symlink(target, name)
  22. }
  23. func (f *BasicFilesystem) ReadSymlink(name string) (string, error) {
  24. name, err := f.rooted(name)
  25. if err != nil {
  26. return "", err
  27. }
  28. return os.Readlink(name)
  29. }
  30. func (f *BasicFilesystem) mkdirAll(path string, perm os.FileMode) error {
  31. return os.MkdirAll(path, perm)
  32. }
  33. // Unhide is a noop on unix, as unhiding files requires renaming them.
  34. // We still check that the relative path does not try to escape the root
  35. func (f *BasicFilesystem) Unhide(name string) error {
  36. _, err := f.rooted(name)
  37. return err
  38. }
  39. // Hide is a noop on unix, as hiding files requires renaming them.
  40. // We still check that the relative path does not try to escape the root
  41. func (f *BasicFilesystem) Hide(name string) error {
  42. _, err := f.rooted(name)
  43. return err
  44. }
  45. func (f *BasicFilesystem) Roots() ([]string, error) {
  46. return []string{"/"}, nil
  47. }
  48. // unrootedChecked returns the path relative to the folder root (same as
  49. // unrooted) or an error if the given path is not a subpath and handles the
  50. // special case when the given path is the folder root without a trailing
  51. // pathseparator.
  52. func (f *BasicFilesystem) unrootedChecked(absPath string, roots []string) (string, *ErrWatchEventOutsideRoot) {
  53. for _, root := range roots {
  54. // Make sure the root ends with precisely one path separator, to
  55. // ease prefix comparisons.
  56. root := strings.TrimRight(root, string(PathSeparator)) + string(PathSeparator)
  57. if absPath+string(PathSeparator) == root {
  58. return ".", nil
  59. }
  60. if strings.HasPrefix(absPath, root) {
  61. return rel(absPath, root), nil
  62. }
  63. }
  64. return "", f.newErrWatchEventOutsideRoot(absPath, roots)
  65. }
  66. func rel(path, prefix string) string {
  67. return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator))
  68. }
  69. var evalSymlinks = filepath.EvalSymlinks
  70. // watchPaths adjust the folder root for use with the notify backend and the
  71. // corresponding absolute path to be passed to notify to watch name.
  72. func (f *BasicFilesystem) watchPaths(name string) (string, []string, error) {
  73. root, err := evalSymlinks(f.root)
  74. if err != nil {
  75. return "", nil, err
  76. }
  77. absName, err := rooted(name, root)
  78. if err != nil {
  79. return "", nil, err
  80. }
  81. return filepath.Join(absName, "..."), []string{root}, nil
  82. }