basicfs_unix.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "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). It panics 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, root string) string {
  53. if absPath+string(PathSeparator) == root {
  54. return "."
  55. }
  56. if !strings.HasPrefix(absPath, root) {
  57. 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))
  58. }
  59. return rel(absPath, root)
  60. }
  61. func rel(path, prefix string) string {
  62. return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator))
  63. }