basicfs_unix.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. //go:build !windows
  7. // +build !windows
  8. package fs
  9. import (
  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) or an error 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 string, roots []string) (string, *ErrWatchEventOutsideRoot) {
  54. for _, root := range roots {
  55. // Make sure the root ends with precisely one path separator, to
  56. // ease prefix comparisons.
  57. root := strings.TrimRight(root, string(PathSeparator)) + string(PathSeparator)
  58. if absPath+string(PathSeparator) == root {
  59. return ".", nil
  60. }
  61. if strings.HasPrefix(absPath, root) {
  62. return rel(absPath, root), nil
  63. }
  64. }
  65. return "", f.newErrWatchEventOutsideRoot(absPath, roots)
  66. }
  67. func rel(path, prefix string) string {
  68. return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator))
  69. }
  70. var evalSymlinks = filepath.EvalSymlinks
  71. // watchPaths adjust the folder root for use with the notify backend and the
  72. // corresponding absolute path to be passed to notify to watch name.
  73. func (f *BasicFilesystem) watchPaths(name string) (string, []string, error) {
  74. root, err := evalSymlinks(f.root)
  75. if err != nil {
  76. return "", nil, err
  77. }
  78. absName, err := rooted(name, root)
  79. if err != nil {
  80. return "", nil, err
  81. }
  82. return filepath.Join(absName, "..."), []string{root}, nil
  83. }