...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package sftpfs
15
16 import (
17 "os"
18 "time"
19
20 "github.com/pkg/sftp"
21 "github.com/spf13/afero"
22 )
23
24
25
26
27
28 type Fs struct {
29 client *sftp.Client
30 }
31
32 func New(client *sftp.Client) afero.Fs {
33 return &Fs{client: client}
34 }
35
36 func (s Fs) Name() string { return "sftpfs" }
37
38 func (s Fs) Create(name string) (afero.File, error) {
39 return FileCreate(s.client, name)
40 }
41
42 func (s Fs) Mkdir(name string, perm os.FileMode) error {
43 err := s.client.Mkdir(name)
44 if err != nil {
45 return err
46 }
47 return s.client.Chmod(name, perm)
48 }
49
50 func (s Fs) MkdirAll(path string, perm os.FileMode) error {
51
52 dir, err := s.Stat(path)
53 if err == nil {
54 if dir.IsDir() {
55 return nil
56 }
57 return err
58 }
59
60
61 i := len(path)
62 for i > 0 && os.IsPathSeparator(path[i-1]) {
63 i--
64 }
65
66 j := i
67 for j > 0 && !os.IsPathSeparator(path[j-1]) {
68 j--
69 }
70
71 if j > 1 {
72
73 err = s.MkdirAll(path[0:j-1], perm)
74 if err != nil {
75 return err
76 }
77 }
78
79
80 err = s.Mkdir(path, perm)
81 if err != nil {
82
83
84 dir, err1 := s.Lstat(path)
85 if err1 == nil && dir.IsDir() {
86 return nil
87 }
88 return err
89 }
90 return nil
91 }
92
93 func (s Fs) Open(name string) (afero.File, error) {
94 return FileOpen(s.client, name)
95 }
96
97
98
99 func (s Fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
100 sshfsFile, err := s.client.OpenFile(name, flag)
101 if err != nil {
102 return nil, err
103 }
104 err = sshfsFile.Chmod(perm)
105 return &File{fd: sshfsFile}, err
106 }
107
108 func (s Fs) Remove(name string) error {
109 return s.client.Remove(name)
110 }
111
112 func (s Fs) RemoveAll(path string) error {
113
114
115 return nil
116 }
117
118 func (s Fs) Rename(oldname, newname string) error {
119 return s.client.Rename(oldname, newname)
120 }
121
122 func (s Fs) Stat(name string) (os.FileInfo, error) {
123 return s.client.Stat(name)
124 }
125
126 func (s Fs) Lstat(p string) (os.FileInfo, error) {
127 return s.client.Lstat(p)
128 }
129
130 func (s Fs) Chmod(name string, mode os.FileMode) error {
131 return s.client.Chmod(name, mode)
132 }
133
134 func (s Fs) Chown(name string, uid, gid int) error {
135 return s.client.Chown(name, uid, gid)
136 }
137
138 func (s Fs) Chtimes(name string, atime time.Time, mtime time.Time) error {
139 return s.client.Chtimes(name, atime, mtime)
140 }
141
View as plain text