sftp.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright © 2015 Jerry Jacobs <jerry.jacobs@xor-gate.org>.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package afero
  14. import (
  15. "os"
  16. "time"
  17. "github.com/spf13/afero/sftp"
  18. "github.com/pkg/sftp"
  19. )
  20. // SftpFs is a Fs implementation that uses functions provided by the sftp package.
  21. //
  22. // For details in any method, check the documentation of the sftp package
  23. // (github.com/pkg/sftp).
  24. type SftpFs struct{
  25. SftpClient *sftp.Client
  26. }
  27. func (s SftpFs) Name() string { return "SftpFs" }
  28. func (s SftpFs) Create(name string) (File, error) {
  29. f, err := sftpfs.FileCreate(s.SftpClient, name)
  30. return f, err
  31. }
  32. func (s SftpFs) Mkdir(name string, perm os.FileMode) error {
  33. err := s.SftpClient.Mkdir(name)
  34. if err != nil {
  35. return err
  36. }
  37. return s.SftpClient.Chmod(name, perm)
  38. }
  39. func (s SftpFs) MkdirAll(path string, perm os.FileMode) error {
  40. // Fast path: if we can tell whether path is a directory or file, stop with success or error.
  41. dir, err := s.Stat(path)
  42. if err == nil {
  43. if dir.IsDir() {
  44. return nil
  45. }
  46. return err
  47. }
  48. // Slow path: make sure parent exists and then call Mkdir for path.
  49. i := len(path)
  50. for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
  51. i--
  52. }
  53. j := i
  54. for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
  55. j--
  56. }
  57. if j > 1 {
  58. // Create parent
  59. err = s.MkdirAll(path[0:j-1], perm)
  60. if err != nil {
  61. return err
  62. }
  63. }
  64. // Parent now exists; invoke Mkdir and use its result.
  65. err = s.Mkdir(path, perm)
  66. if err != nil {
  67. // Handle arguments like "foo/." by
  68. // double-checking that directory doesn't exist.
  69. dir, err1 := s.Lstat(path)
  70. if err1 == nil && dir.IsDir() {
  71. return nil
  72. }
  73. return err
  74. }
  75. return nil
  76. }
  77. func (s SftpFs) Open(name string) (File, error) {
  78. f, err := sftpfs.FileOpen(s.SftpClient, name)
  79. return f, err
  80. }
  81. func (s SftpFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
  82. return nil,nil
  83. }
  84. func (s SftpFs) Remove(name string) error {
  85. return s.SftpClient.Remove(name)
  86. }
  87. func (s SftpFs) RemoveAll(path string) error {
  88. // TODO have a look at os.RemoveAll
  89. // https://github.com/golang/go/blob/master/src/os/path.go#L66
  90. return nil
  91. }
  92. func (s SftpFs) Rename(oldname, newname string) error {
  93. return s.SftpClient.Rename(oldname, newname)
  94. }
  95. func (s SftpFs) Stat(name string) (os.FileInfo, error) {
  96. return s.SftpClient.Stat(name)
  97. }
  98. func (s SftpFs) Lstat(p string) (os.FileInfo, error) {
  99. return s.SftpClient.Lstat(p)
  100. }
  101. func (s SftpFs) Chmod(name string, mode os.FileMode) error {
  102. return s.SftpClient.Chmod(name, mode)
  103. }
  104. func (s SftpFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  105. return s.SftpClient.Chtimes(name, atime, mtime)
  106. }