vfs.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. // Package vfs provides local and remote filesystems support
  2. package vfs
  3. import (
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/url"
  8. "os"
  9. "path"
  10. "path/filepath"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "github.com/eikenb/pipeat"
  15. "github.com/pkg/sftp"
  16. "github.com/drakkan/sftpgo/kms"
  17. "github.com/drakkan/sftpgo/logger"
  18. "github.com/drakkan/sftpgo/utils"
  19. )
  20. const dirMimeType = "inode/directory"
  21. var (
  22. validAzAccessTier = []string{"", "Archive", "Hot", "Cool"}
  23. // ErrStorageSizeUnavailable is returned if the storage backend does not support getting the size
  24. ErrStorageSizeUnavailable = errors.New("unable to get available size for this storage backend")
  25. // ErrVfsUnsupported defines the error for an unsupported VFS operation
  26. ErrVfsUnsupported = errors.New("not supported")
  27. credentialsDirPath string
  28. tempPath string
  29. sftpFingerprints []string
  30. )
  31. // SetCredentialsDirPath sets the credentials dir path
  32. func SetCredentialsDirPath(credentialsPath string) {
  33. credentialsDirPath = credentialsPath
  34. }
  35. // GetCredentialsDirPath returns the credentials dir path
  36. func GetCredentialsDirPath() string {
  37. return credentialsDirPath
  38. }
  39. // SetTempPath sets the path for temporary files
  40. func SetTempPath(fsPath string) {
  41. tempPath = fsPath
  42. }
  43. // GetTempPath returns the path for temporary files
  44. func GetTempPath() string {
  45. return tempPath
  46. }
  47. // SetSFTPFingerprints sets the SFTP host key fingerprints
  48. func SetSFTPFingerprints(fp []string) {
  49. sftpFingerprints = fp
  50. }
  51. // Fs defines the interface for filesystem backends
  52. type Fs interface {
  53. Name() string
  54. ConnectionID() string
  55. Stat(name string) (os.FileInfo, error)
  56. Lstat(name string) (os.FileInfo, error)
  57. Open(name string, offset int64) (File, *pipeat.PipeReaderAt, func(), error)
  58. Create(name string, flag int) (File, *PipeWriter, func(), error)
  59. Rename(source, target string) error
  60. Remove(name string, isDir bool) error
  61. Mkdir(name string) error
  62. MkdirAll(name string, uid int, gid int) error
  63. Symlink(source, target string) error
  64. Chown(name string, uid int, gid int) error
  65. Chmod(name string, mode os.FileMode) error
  66. Chtimes(name string, atime, mtime time.Time) error
  67. Truncate(name string, size int64) error
  68. ReadDir(dirname string) ([]os.FileInfo, error)
  69. Readlink(name string) (string, error)
  70. IsUploadResumeSupported() bool
  71. IsAtomicUploadSupported() bool
  72. CheckRootPath(username string, uid int, gid int) bool
  73. ResolvePath(sftpPath string) (string, error)
  74. IsNotExist(err error) bool
  75. IsPermission(err error) bool
  76. IsNotSupported(err error) bool
  77. ScanRootDirContents() (int, int64, error)
  78. GetDirSize(dirname string) (int, int64, error)
  79. GetAtomicUploadPath(name string) string
  80. GetRelativePath(name string) string
  81. Walk(root string, walkFn filepath.WalkFunc) error
  82. Join(elem ...string) string
  83. HasVirtualFolders() bool
  84. GetMimeType(name string) (string, error)
  85. GetAvailableDiskSize(dirName string) (*sftp.StatVFS, error)
  86. Close() error
  87. }
  88. // File defines an interface representing a SFTPGo file
  89. type File interface {
  90. io.Reader
  91. io.Writer
  92. io.Closer
  93. io.ReaderAt
  94. io.WriterAt
  95. io.Seeker
  96. Stat() (os.FileInfo, error)
  97. Name() string
  98. Truncate(size int64) error
  99. }
  100. // QuotaCheckResult defines the result for a quota check
  101. type QuotaCheckResult struct {
  102. HasSpace bool
  103. AllowedSize int64
  104. AllowedFiles int
  105. UsedSize int64
  106. UsedFiles int
  107. QuotaSize int64
  108. QuotaFiles int
  109. }
  110. // GetRemainingSize returns the remaining allowed size
  111. func (q *QuotaCheckResult) GetRemainingSize() int64 {
  112. if q.QuotaSize > 0 {
  113. return q.QuotaSize - q.UsedSize
  114. }
  115. return 0
  116. }
  117. // GetRemainingFiles returns the remaining allowed files
  118. func (q *QuotaCheckResult) GetRemainingFiles() int {
  119. if q.QuotaFiles > 0 {
  120. return q.QuotaFiles - q.UsedFiles
  121. }
  122. return 0
  123. }
  124. // S3FsConfig defines the configuration for S3 based filesystem
  125. type S3FsConfig struct {
  126. Bucket string `json:"bucket,omitempty"`
  127. // KeyPrefix is similar to a chroot directory for local filesystem.
  128. // If specified then the SFTP user will only see objects that starts
  129. // with this prefix and so you can restrict access to a specific
  130. // folder. The prefix, if not empty, must not start with "/" and must
  131. // end with "/".
  132. // If empty the whole bucket contents will be available
  133. KeyPrefix string `json:"key_prefix,omitempty"`
  134. Region string `json:"region,omitempty"`
  135. AccessKey string `json:"access_key,omitempty"`
  136. AccessSecret *kms.Secret `json:"access_secret,omitempty"`
  137. Endpoint string `json:"endpoint,omitempty"`
  138. StorageClass string `json:"storage_class,omitempty"`
  139. // The buffer size (in MB) to use for multipart uploads. The minimum allowed part size is 5MB,
  140. // and if this value is set to zero, the default value (5MB) for the AWS SDK will be used.
  141. // The minimum allowed value is 5.
  142. // Please note that if the upload bandwidth between the SFTP client and SFTPGo is greater than
  143. // the upload bandwidth between SFTPGo and S3 then the SFTP client have to wait for the upload
  144. // of the last parts to S3 after it ends the file upload to SFTPGo, and it may time out.
  145. // Keep this in mind if you customize these parameters.
  146. UploadPartSize int64 `json:"upload_part_size,omitempty"`
  147. // How many parts are uploaded in parallel
  148. UploadConcurrency int `json:"upload_concurrency,omitempty"`
  149. }
  150. func (c *S3FsConfig) isEqual(other *S3FsConfig) bool {
  151. if c.Bucket != other.Bucket {
  152. return false
  153. }
  154. if c.KeyPrefix != other.KeyPrefix {
  155. return false
  156. }
  157. if c.Region != other.Region {
  158. return false
  159. }
  160. if c.AccessKey != other.AccessKey {
  161. return false
  162. }
  163. if c.Endpoint != other.Endpoint {
  164. return false
  165. }
  166. if c.StorageClass != other.StorageClass {
  167. return false
  168. }
  169. if c.UploadPartSize != other.UploadPartSize {
  170. return false
  171. }
  172. if c.UploadConcurrency != other.UploadConcurrency {
  173. return false
  174. }
  175. if c.AccessSecret == nil {
  176. c.AccessSecret = kms.NewEmptySecret()
  177. }
  178. if other.AccessSecret == nil {
  179. other.AccessSecret = kms.NewEmptySecret()
  180. }
  181. return c.AccessSecret.IsEqual(other.AccessSecret)
  182. }
  183. func (c *S3FsConfig) checkCredentials() error {
  184. if c.AccessKey == "" && !c.AccessSecret.IsEmpty() {
  185. return errors.New("access_key cannot be empty with access_secret not empty")
  186. }
  187. if c.AccessSecret.IsEmpty() && c.AccessKey != "" {
  188. return errors.New("access_secret cannot be empty with access_key not empty")
  189. }
  190. if c.AccessSecret.IsEncrypted() && !c.AccessSecret.IsValid() {
  191. return errors.New("invalid encrypted access_secret")
  192. }
  193. if !c.AccessSecret.IsEmpty() && !c.AccessSecret.IsValidInput() {
  194. return errors.New("invalid access_secret")
  195. }
  196. return nil
  197. }
  198. // EncryptCredentials encrypts access secret if it is in plain text
  199. func (c *S3FsConfig) EncryptCredentials(additionalData string) error {
  200. if c.AccessSecret.IsPlain() {
  201. c.AccessSecret.SetAdditionalData(additionalData)
  202. err := c.AccessSecret.Encrypt()
  203. if err != nil {
  204. return err
  205. }
  206. }
  207. return nil
  208. }
  209. // Validate returns an error if the configuration is not valid
  210. func (c *S3FsConfig) Validate() error {
  211. if c.AccessSecret == nil {
  212. c.AccessSecret = kms.NewEmptySecret()
  213. }
  214. if c.Bucket == "" {
  215. return errors.New("bucket cannot be empty")
  216. }
  217. if c.Region == "" {
  218. return errors.New("region cannot be empty")
  219. }
  220. if err := c.checkCredentials(); err != nil {
  221. return err
  222. }
  223. if c.KeyPrefix != "" {
  224. if strings.HasPrefix(c.KeyPrefix, "/") {
  225. return errors.New("key_prefix cannot start with /")
  226. }
  227. c.KeyPrefix = path.Clean(c.KeyPrefix)
  228. if !strings.HasSuffix(c.KeyPrefix, "/") {
  229. c.KeyPrefix += "/"
  230. }
  231. }
  232. if c.UploadPartSize != 0 && (c.UploadPartSize < 5 || c.UploadPartSize > 5000) {
  233. return errors.New("upload_part_size cannot be != 0, lower than 5 (MB) or greater than 5000 (MB)")
  234. }
  235. if c.UploadConcurrency < 0 || c.UploadConcurrency > 64 {
  236. return fmt.Errorf("invalid upload concurrency: %v", c.UploadConcurrency)
  237. }
  238. return nil
  239. }
  240. // GCSFsConfig defines the configuration for Google Cloud Storage based filesystem
  241. type GCSFsConfig struct {
  242. Bucket string `json:"bucket,omitempty"`
  243. // KeyPrefix is similar to a chroot directory for local filesystem.
  244. // If specified then the SFTP user will only see objects that starts
  245. // with this prefix and so you can restrict access to a specific
  246. // folder. The prefix, if not empty, must not start with "/" and must
  247. // end with "/".
  248. // If empty the whole bucket contents will be available
  249. KeyPrefix string `json:"key_prefix,omitempty"`
  250. CredentialFile string `json:"-"`
  251. Credentials *kms.Secret `json:"credentials,omitempty"`
  252. // 0 explicit, 1 automatic
  253. AutomaticCredentials int `json:"automatic_credentials,omitempty"`
  254. StorageClass string `json:"storage_class,omitempty"`
  255. }
  256. func (c *GCSFsConfig) isEqual(other *GCSFsConfig) bool {
  257. if c.Bucket != other.Bucket {
  258. return false
  259. }
  260. if c.KeyPrefix != other.KeyPrefix {
  261. return false
  262. }
  263. if c.AutomaticCredentials != other.AutomaticCredentials {
  264. return false
  265. }
  266. if c.StorageClass != other.StorageClass {
  267. return false
  268. }
  269. if c.Credentials == nil {
  270. c.Credentials = kms.NewEmptySecret()
  271. }
  272. if other.Credentials == nil {
  273. other.Credentials = kms.NewEmptySecret()
  274. }
  275. return c.Credentials.IsEqual(other.Credentials)
  276. }
  277. // Validate returns an error if the configuration is not valid
  278. func (c *GCSFsConfig) Validate(credentialsFilePath string) error {
  279. if c.Credentials == nil {
  280. c.Credentials = kms.NewEmptySecret()
  281. }
  282. if c.Bucket == "" {
  283. return errors.New("bucket cannot be empty")
  284. }
  285. if c.KeyPrefix != "" {
  286. if strings.HasPrefix(c.KeyPrefix, "/") {
  287. return errors.New("key_prefix cannot start with /")
  288. }
  289. c.KeyPrefix = path.Clean(c.KeyPrefix)
  290. if !strings.HasSuffix(c.KeyPrefix, "/") {
  291. c.KeyPrefix += "/"
  292. }
  293. }
  294. if c.Credentials.IsEncrypted() && !c.Credentials.IsValid() {
  295. return errors.New("invalid encrypted credentials")
  296. }
  297. if !c.Credentials.IsValidInput() && c.AutomaticCredentials == 0 {
  298. fi, err := os.Stat(credentialsFilePath)
  299. if err != nil {
  300. return fmt.Errorf("invalid credentials %v", err)
  301. }
  302. if fi.Size() == 0 {
  303. return errors.New("credentials cannot be empty")
  304. }
  305. }
  306. return nil
  307. }
  308. // AzBlobFsConfig defines the configuration for Azure Blob Storage based filesystem
  309. type AzBlobFsConfig struct {
  310. Container string `json:"container,omitempty"`
  311. // Storage Account Name, leave blank to use SAS URL
  312. AccountName string `json:"account_name,omitempty"`
  313. // Storage Account Key leave blank to use SAS URL.
  314. // The access key is stored encrypted based on the kms configuration
  315. AccountKey *kms.Secret `json:"account_key,omitempty"`
  316. // Optional endpoint. Default is "blob.core.windows.net".
  317. // If you use the emulator the endpoint must include the protocol,
  318. // for example "http://127.0.0.1:10000"
  319. Endpoint string `json:"endpoint,omitempty"`
  320. // Shared access signature URL, leave blank if using account/key
  321. SASURL string `json:"sas_url,omitempty"`
  322. // KeyPrefix is similar to a chroot directory for local filesystem.
  323. // If specified then the SFTPGo userd will only see objects that starts
  324. // with this prefix and so you can restrict access to a specific
  325. // folder. The prefix, if not empty, must not start with "/" and must
  326. // end with "/".
  327. // If empty the whole bucket contents will be available
  328. KeyPrefix string `json:"key_prefix,omitempty"`
  329. // The buffer size (in MB) to use for multipart uploads.
  330. // If this value is set to zero, the default value (1MB) for the Azure SDK will be used.
  331. // Please note that if the upload bandwidth between the SFTPGo client and SFTPGo server is
  332. // greater than the upload bandwidth between SFTPGo and Azure then the SFTP client have
  333. // to wait for the upload of the last parts to Azure after it ends the file upload to SFTPGo,
  334. // and it may time out.
  335. // Keep this in mind if you customize these parameters.
  336. UploadPartSize int64 `json:"upload_part_size,omitempty"`
  337. // How many parts are uploaded in parallel
  338. UploadConcurrency int `json:"upload_concurrency,omitempty"`
  339. // Set to true if you use an Azure emulator such as Azurite
  340. UseEmulator bool `json:"use_emulator,omitempty"`
  341. // Blob Access Tier
  342. AccessTier string `json:"access_tier,omitempty"`
  343. }
  344. func (c *AzBlobFsConfig) isEqual(other *AzBlobFsConfig) bool {
  345. if c.Container != other.Container {
  346. return false
  347. }
  348. if c.AccountName != other.AccountName {
  349. return false
  350. }
  351. if c.Endpoint != other.Endpoint {
  352. return false
  353. }
  354. if c.SASURL != other.SASURL {
  355. return false
  356. }
  357. if c.KeyPrefix != other.KeyPrefix {
  358. return false
  359. }
  360. if c.UploadPartSize != other.UploadPartSize {
  361. return false
  362. }
  363. if c.UploadConcurrency != other.UploadConcurrency {
  364. return false
  365. }
  366. if c.UseEmulator != other.UseEmulator {
  367. return false
  368. }
  369. if c.AccessTier != other.AccessTier {
  370. return false
  371. }
  372. if c.AccountKey == nil {
  373. c.AccountKey = kms.NewEmptySecret()
  374. }
  375. if other.AccountKey == nil {
  376. other.AccountKey = kms.NewEmptySecret()
  377. }
  378. return c.AccountKey.IsEqual(other.AccountKey)
  379. }
  380. // EncryptCredentials encrypts access secret if it is in plain text
  381. func (c *AzBlobFsConfig) EncryptCredentials(additionalData string) error {
  382. if c.AccountKey.IsPlain() {
  383. c.AccountKey.SetAdditionalData(additionalData)
  384. if err := c.AccountKey.Encrypt(); err != nil {
  385. return err
  386. }
  387. }
  388. return nil
  389. }
  390. func (c *AzBlobFsConfig) checkCredentials() error {
  391. if c.AccountName == "" || !c.AccountKey.IsValidInput() {
  392. return errors.New("credentials cannot be empty or invalid")
  393. }
  394. if c.AccountKey.IsEncrypted() && !c.AccountKey.IsValid() {
  395. return errors.New("invalid encrypted account_key")
  396. }
  397. return nil
  398. }
  399. // Validate returns an error if the configuration is not valid
  400. func (c *AzBlobFsConfig) Validate() error {
  401. if c.AccountKey == nil {
  402. c.AccountKey = kms.NewEmptySecret()
  403. }
  404. if c.SASURL != "" {
  405. _, err := url.Parse(c.SASURL)
  406. return err
  407. }
  408. if c.Container == "" {
  409. return errors.New("container cannot be empty")
  410. }
  411. if err := c.checkCredentials(); err != nil {
  412. return err
  413. }
  414. if c.KeyPrefix != "" {
  415. if strings.HasPrefix(c.KeyPrefix, "/") {
  416. return errors.New("key_prefix cannot start with /")
  417. }
  418. c.KeyPrefix = path.Clean(c.KeyPrefix)
  419. if !strings.HasSuffix(c.KeyPrefix, "/") {
  420. c.KeyPrefix += "/"
  421. }
  422. }
  423. if c.UploadPartSize < 0 || c.UploadPartSize > 100 {
  424. return fmt.Errorf("invalid upload part size: %v", c.UploadPartSize)
  425. }
  426. if c.UploadConcurrency < 0 || c.UploadConcurrency > 64 {
  427. return fmt.Errorf("invalid upload concurrency: %v", c.UploadConcurrency)
  428. }
  429. if !utils.IsStringInSlice(c.AccessTier, validAzAccessTier) {
  430. return fmt.Errorf("invalid access tier %#v, valid values: \"''%v\"", c.AccessTier, strings.Join(validAzAccessTier, ", "))
  431. }
  432. return nil
  433. }
  434. // CryptFsConfig defines the configuration to store local files as encrypted
  435. type CryptFsConfig struct {
  436. Passphrase *kms.Secret `json:"passphrase,omitempty"`
  437. }
  438. func (c *CryptFsConfig) isEqual(other *CryptFsConfig) bool {
  439. if c.Passphrase == nil {
  440. c.Passphrase = kms.NewEmptySecret()
  441. }
  442. if other.Passphrase == nil {
  443. other.Passphrase = kms.NewEmptySecret()
  444. }
  445. return c.Passphrase.IsEqual(other.Passphrase)
  446. }
  447. // EncryptCredentials encrypts access secret if it is in plain text
  448. func (c *CryptFsConfig) EncryptCredentials(additionalData string) error {
  449. if c.Passphrase.IsPlain() {
  450. c.Passphrase.SetAdditionalData(additionalData)
  451. if err := c.Passphrase.Encrypt(); err != nil {
  452. return err
  453. }
  454. }
  455. return nil
  456. }
  457. // Validate returns an error if the configuration is not valid
  458. func (c *CryptFsConfig) Validate() error {
  459. if c.Passphrase == nil || c.Passphrase.IsEmpty() {
  460. return errors.New("invalid passphrase")
  461. }
  462. if !c.Passphrase.IsValidInput() {
  463. return errors.New("passphrase cannot be empty or invalid")
  464. }
  465. if c.Passphrase.IsEncrypted() && !c.Passphrase.IsValid() {
  466. return errors.New("invalid encrypted passphrase")
  467. }
  468. return nil
  469. }
  470. // PipeWriter defines a wrapper for pipeat.PipeWriterAt.
  471. type PipeWriter struct {
  472. writer *pipeat.PipeWriterAt
  473. err error
  474. done chan bool
  475. }
  476. // NewPipeWriter initializes a new PipeWriter
  477. func NewPipeWriter(w *pipeat.PipeWriterAt) *PipeWriter {
  478. return &PipeWriter{
  479. writer: w,
  480. err: nil,
  481. done: make(chan bool),
  482. }
  483. }
  484. // Close waits for the upload to end, closes the pipeat.PipeWriterAt and returns an error if any.
  485. func (p *PipeWriter) Close() error {
  486. p.writer.Close() //nolint:errcheck // the returned error is always null
  487. <-p.done
  488. return p.err
  489. }
  490. // Done unlocks other goroutines waiting on Close().
  491. // It must be called when the upload ends
  492. func (p *PipeWriter) Done(err error) {
  493. p.err = err
  494. p.done <- true
  495. }
  496. // WriteAt is a wrapper for pipeat WriteAt
  497. func (p *PipeWriter) WriteAt(data []byte, off int64) (int, error) {
  498. return p.writer.WriteAt(data, off)
  499. }
  500. // Write is a wrapper for pipeat Write
  501. func (p *PipeWriter) Write(data []byte) (int, error) {
  502. return p.writer.Write(data)
  503. }
  504. // IsDirectory checks if a path exists and is a directory
  505. func IsDirectory(fs Fs, path string) (bool, error) {
  506. fileInfo, err := fs.Stat(path)
  507. if err != nil {
  508. return false, err
  509. }
  510. return fileInfo.IsDir(), err
  511. }
  512. // IsLocalOsFs returns true if fs is a local filesystem implementation
  513. func IsLocalOsFs(fs Fs) bool {
  514. return fs.Name() == osFsName
  515. }
  516. // IsCryptOsFs returns true if fs is an encrypted local filesystem implementation
  517. func IsCryptOsFs(fs Fs) bool {
  518. return fs.Name() == cryptFsName
  519. }
  520. // IsSFTPFs returns true if fs is an SFTP filesystem
  521. func IsSFTPFs(fs Fs) bool {
  522. return strings.HasPrefix(fs.Name(), sftpFsName)
  523. }
  524. // IsBufferedSFTPFs returns true if this is a buffered SFTP filesystem
  525. func IsBufferedSFTPFs(fs Fs) bool {
  526. if !IsSFTPFs(fs) {
  527. return false
  528. }
  529. return !fs.IsUploadResumeSupported()
  530. }
  531. // IsLocalOrUnbufferedSFTPFs returns true if fs is local or SFTP with no buffer
  532. func IsLocalOrUnbufferedSFTPFs(fs Fs) bool {
  533. if IsLocalOsFs(fs) {
  534. return true
  535. }
  536. if IsSFTPFs(fs) {
  537. return fs.IsUploadResumeSupported()
  538. }
  539. return false
  540. }
  541. // IsLocalOrSFTPFs returns true if fs is local or SFTP
  542. func IsLocalOrSFTPFs(fs Fs) bool {
  543. return IsLocalOsFs(fs) || IsSFTPFs(fs)
  544. }
  545. // HasOpenRWSupport returns true if the fs can open a file
  546. // for reading and writing at the same time
  547. func HasOpenRWSupport(fs Fs) bool {
  548. if IsLocalOsFs(fs) {
  549. return true
  550. }
  551. if IsSFTPFs(fs) && fs.IsUploadResumeSupported() {
  552. return true
  553. }
  554. return false
  555. }
  556. // IsLocalOrCryptoFs returns true if fs is local or local encrypted
  557. func IsLocalOrCryptoFs(fs Fs) bool {
  558. return IsLocalOsFs(fs) || IsCryptOsFs(fs)
  559. }
  560. // SetPathPermissions calls fs.Chown.
  561. // It does nothing for local filesystem on windows
  562. func SetPathPermissions(fs Fs, path string, uid int, gid int) {
  563. if uid == -1 && gid == -1 {
  564. return
  565. }
  566. if IsLocalOsFs(fs) {
  567. if runtime.GOOS == "windows" {
  568. return
  569. }
  570. }
  571. if err := fs.Chown(path, uid, gid); err != nil {
  572. fsLog(fs, logger.LevelWarn, "error chowning path %v: %v", path, err)
  573. }
  574. }
  575. func fsLog(fs Fs, level logger.LogLevel, format string, v ...interface{}) {
  576. logger.Log(level, fs.Name(), fs.ConnectionID(), format, v...)
  577. }