server_windows.go 786 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package sftp
  2. import (
  3. "path"
  4. "path/filepath"
  5. )
  6. func (s *Server) toLocalPath(p string) string {
  7. if s.workDir != "" && !path.IsAbs(p) {
  8. p = path.Join(s.workDir, p)
  9. }
  10. lp := filepath.FromSlash(p)
  11. if path.IsAbs(p) {
  12. tmp := lp
  13. for len(tmp) > 0 && tmp[0] == '\\' {
  14. tmp = tmp[1:]
  15. }
  16. if filepath.IsAbs(tmp) {
  17. // If the FromSlash without any starting slashes is absolute,
  18. // then we have a filepath encoded with a prefix '/'.
  19. // e.g. "/C:/Windows" to "C:\\Windows"
  20. return tmp
  21. }
  22. tmp += "\\"
  23. if filepath.IsAbs(tmp) {
  24. // If the FromSlash without any starting slashes but with extra end slash is absolute,
  25. // then we have a filepath encoded with a prefix '/' and a dropped '/' at the end.
  26. // e.g. "/C:" to "C:\\"
  27. return tmp
  28. }
  29. }
  30. return lp
  31. }