debug_symlink_unix.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (C) 2017 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. )
  12. // DebugSymlinkForTestsOnly is not and should not be used in Syncthing code,
  13. // hence the cumbersome name to make it obvious if this ever leaks. Its
  14. // reason for existence is the Windows version, which allows creating
  15. // symlinks when non-elevated.
  16. func DebugSymlinkForTestsOnly(oldFs, newFs Filesystem, oldname, newname string) error {
  17. if caseFs, ok := unwrapFilesystem(newFs).(*caseFilesystem); ok {
  18. if err := caseFs.checkCase(newname); err != nil {
  19. return err
  20. }
  21. caseFs.dropCache()
  22. }
  23. if err := os.Symlink(filepath.Join(oldFs.URI(), oldname), filepath.Join(newFs.URI(), newname)); err != nil {
  24. return err
  25. }
  26. return nil
  27. }
  28. // unwrapFilesystem removes "wrapping" filesystems to expose the underlying filesystem.
  29. func unwrapFilesystem(fs Filesystem) Filesystem {
  30. for {
  31. switch sfs := fs.(type) {
  32. case *logFilesystem:
  33. fs = sfs.Filesystem
  34. case *walkFilesystem:
  35. fs = sfs.Filesystem
  36. case *MtimeFS:
  37. fs = sfs.Filesystem
  38. default:
  39. return sfs
  40. }
  41. }
  42. }