vfs.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Package vfs provides local and remote filesystems support
  2. package vfs
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "path"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "github.com/drakkan/sftpgo/logger"
  12. "github.com/eikenb/pipeat"
  13. "github.com/pkg/sftp"
  14. )
  15. // Fs defines the interface for filesystem backends
  16. type Fs interface {
  17. Name() string
  18. ConnectionID() string
  19. Stat(name string) (os.FileInfo, error)
  20. Lstat(name string) (os.FileInfo, error)
  21. Open(name string) (*os.File, *pipeat.PipeReaderAt, func(), error)
  22. Create(name string, flag int) (*os.File, *pipeat.PipeWriterAt, func(), error)
  23. Rename(source, target string) error
  24. Remove(name string, isDir bool) error
  25. Mkdir(name string) error
  26. Symlink(source, target string) error
  27. Chown(name string, uid int, gid int) error
  28. Chmod(name string, mode os.FileMode) error
  29. Chtimes(name string, atime, mtime time.Time) error
  30. ReadDir(dirname string) ([]os.FileInfo, error)
  31. IsUploadResumeSupported() bool
  32. IsAtomicUploadSupported() bool
  33. CheckRootPath(username string, uid int, gid int) bool
  34. ResolvePath(sftpPath string) (string, error)
  35. IsNotExist(err error) bool
  36. IsPermission(err error) bool
  37. ScanRootDirContents() (int, int64, error)
  38. GetAtomicUploadPath(name string) string
  39. GetRelativePath(name string) string
  40. Join(elem ...string) string
  41. }
  42. // IsDirectory checks if a path exists and is a directory
  43. func IsDirectory(fs Fs, path string) (bool, error) {
  44. fileInfo, err := fs.Stat(path)
  45. if err != nil {
  46. return false, err
  47. }
  48. return fileInfo.IsDir(), err
  49. }
  50. // GetSFTPError returns an sftp error from a filesystem error
  51. func GetSFTPError(fs Fs, err error) error {
  52. if fs.IsNotExist(err) {
  53. return sftp.ErrSSHFxNoSuchFile
  54. } else if fs.IsPermission(err) {
  55. return sftp.ErrSSHFxPermissionDenied
  56. } else if err != nil {
  57. return sftp.ErrSSHFxFailure
  58. }
  59. return nil
  60. }
  61. // IsLocalOsFs returns true if fs is the local filesystem implementation
  62. func IsLocalOsFs(fs Fs) bool {
  63. return fs.Name() == osFsName
  64. }
  65. // ValidateS3FsConfig returns nil if the specified s3 config is valid, otherwise an error
  66. func ValidateS3FsConfig(config *S3FsConfig) error {
  67. if len(config.Bucket) == 0 {
  68. return errors.New("bucket cannot be empty")
  69. }
  70. if len(config.Region) == 0 {
  71. return errors.New("region cannot be empty")
  72. }
  73. if len(config.AccessKey) == 0 && len(config.AccessSecret) > 0 {
  74. return errors.New("access_key cannot be empty with access_secret not empty")
  75. }
  76. if len(config.AccessSecret) == 0 && len(config.AccessKey) > 0 {
  77. return errors.New("access_secret cannot be empty with access_key not empty")
  78. }
  79. if len(config.KeyPrefix) > 0 {
  80. if strings.HasPrefix(config.KeyPrefix, "/") {
  81. return errors.New("key_prefix cannot start with /")
  82. }
  83. config.KeyPrefix = path.Clean(config.KeyPrefix)
  84. if !strings.HasSuffix(config.KeyPrefix, "/") {
  85. config.KeyPrefix += "/"
  86. }
  87. }
  88. return nil
  89. }
  90. // ValidateGCSFsConfig returns nil if the specified GCS config is valid, otherwise an error
  91. func ValidateGCSFsConfig(config *GCSFsConfig, credentialsFilePath string) error {
  92. if len(config.Bucket) == 0 {
  93. return errors.New("bucket cannot be empty")
  94. }
  95. if len(config.KeyPrefix) > 0 {
  96. if strings.HasPrefix(config.KeyPrefix, "/") {
  97. return errors.New("key_prefix cannot start with /")
  98. }
  99. config.KeyPrefix = path.Clean(config.KeyPrefix)
  100. if !strings.HasSuffix(config.KeyPrefix, "/") {
  101. config.KeyPrefix += "/"
  102. }
  103. }
  104. if len(config.Credentials) == 0 {
  105. fi, err := os.Stat(credentialsFilePath)
  106. if err != nil {
  107. return fmt.Errorf("invalid credentials %v", err)
  108. }
  109. if fi.Size() == 0 {
  110. return errors.New("credentials cannot be empty")
  111. }
  112. }
  113. return nil
  114. }
  115. // SetPathPermissions calls fs.Chown.
  116. // It does nothing for local filesystem on windows
  117. func SetPathPermissions(fs Fs, path string, uid int, gid int) {
  118. if IsLocalOsFs(fs) {
  119. if runtime.GOOS == "windows" {
  120. return
  121. }
  122. }
  123. if err := fs.Chown(path, uid, gid); err != nil {
  124. fsLog(fs, logger.LevelWarn, "error chowning path %v: %v", path, err)
  125. }
  126. }
  127. func fsLog(fs Fs, level logger.LogLevel, format string, v ...interface{}) {
  128. logger.Log(level, fs.Name(), fs.ConnectionID(), format, v...)
  129. }