basicfs_unix.go 3.2 KB

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