-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
237 lines (208 loc) · 4.45 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"fmt"
"github.com/alexneamtu/goftp-server"
"github.com/pkg/errors"
"io"
"os"
"path/filepath"
"strings"
)
type FileDriver struct {
RootPath string
server.Perm
}
type FileInfo struct {
os.FileInfo
mode os.FileMode
owner string
group string
}
func (f *FileInfo) Mode() os.FileMode {
return f.mode
}
func (f *FileInfo) Owner() string {
return f.owner
}
func (f *FileInfo) Group() string {
return f.group
}
func (driver *FileDriver) realPath(path string) string {
paths := strings.Split(path, "/")
return filepath.Join(append([]string{driver.RootPath}, paths...)...)
}
func (driver *FileDriver) Init(conn *server.Conn) {
//driver.conn = conn
}
func (driver *FileDriver) ChangeDir(path string) error {
rPath := driver.realPath(path)
f, err := os.Lstat(rPath)
if err != nil {
return err
}
if f.IsDir() {
return nil
}
return errors.New("Not a directory")
}
func (driver *FileDriver) Stat(path string) (server.FileInfo, error) {
basepath := driver.realPath(path)
rPath, err := filepath.Abs(basepath)
if err != nil {
return nil, err
}
f, err := os.Lstat(rPath)
if err != nil {
return nil, err
}
mode, err := driver.Perm.GetMode(path)
if err != nil {
return nil, err
}
if f.IsDir() {
mode |= os.ModeDir
}
owner, err := driver.Perm.GetOwner(path)
if err != nil {
return nil, err
}
group, err := driver.Perm.GetGroup(path)
if err != nil {
return nil, err
}
return &FileInfo{f, mode, owner, group}, nil
}
func (driver *FileDriver) ListDir(path string, callback func(server.FileInfo) error) error {
basepath := driver.realPath(path)
return filepath.Walk(basepath, func(f string, info os.FileInfo, err error) error {
if err != nil {
return err
}
rPath, _ := filepath.Rel(basepath, f)
if rPath == info.Name() {
mode, err := driver.Perm.GetMode(rPath)
if err != nil {
return err
}
if info.IsDir() {
mode |= os.ModeDir
}
owner, err := driver.Perm.GetOwner(rPath)
if err != nil {
return err
}
group, err := driver.Perm.GetGroup(rPath)
if err != nil {
return err
}
err = callback(&FileInfo{info, mode, owner, group})
if err != nil {
return err
}
}
return nil
})
}
func (driver *FileDriver) DeleteDir(path string) error {
rPath := driver.realPath(path)
f, err := os.Lstat(rPath)
if err != nil {
return err
}
if f.IsDir() {
return os.Remove(rPath)
}
return errors.New("Not a directory")
}
func (driver *FileDriver) DeleteFile(path string) error {
rPath := driver.realPath(path)
f, err := os.Lstat(rPath)
if err != nil {
return err
}
if !f.IsDir() {
return os.Remove(rPath)
}
return errors.New("Not a file")
}
func (driver *FileDriver) Rename(fromPath string, toPath string) error {
oldPath := driver.realPath(fromPath)
newPath := driver.realPath(toPath)
return os.Rename(oldPath, newPath)
}
func (driver *FileDriver) MakeDir(path string) error {
rPath := driver.realPath(path)
return os.Mkdir(rPath, os.ModePerm)
}
func (driver *FileDriver) GetFile(path string, offset int64) (int64, io.ReadCloser, error) {
rPath := driver.realPath(path)
f, err := os.Open(rPath)
if err != nil {
return 0, nil, err
}
info, err := f.Stat()
if err != nil {
return 0, nil, err
}
f.Seek(offset, os.SEEK_SET)
return info.Size(), f, nil
}
func (driver *FileDriver) PutFile(destPath string, data io.Reader, appendData bool) (int64, error) {
rPath := driver.realPath(destPath)
var isExist bool
f, err := os.Lstat(rPath)
if err == nil {
isExist = true
if f.IsDir() {
return 0, errors.New("A dir has the same name")
}
} else {
if os.IsNotExist(err) {
isExist = false
} else {
return 0, errors.New(fmt.Sprintln("Put File error:", err))
}
}
if appendData && !isExist {
appendData = false
}
if !appendData {
if isExist {
err = os.Remove(rPath)
if err != nil {
return 0, err
}
}
f, err := os.Create(rPath)
if err != nil {
return 0, err
}
defer f.Close()
bytes, err := io.Copy(f, data)
if err != nil {
return 0, err
}
return bytes, nil
}
of, err := os.OpenFile(rPath, os.O_APPEND|os.O_RDWR, 0660)
if err != nil {
return 0, err
}
defer of.Close()
_, err = of.Seek(0, os.SEEK_END)
if err != nil {
return 0, err
}
bytes, err := io.Copy(of, data)
if err != nil {
return 0, err
}
return bytes, nil
}
type FileDriverFactory struct {
RootPath string
server.Perm
}
func (factory *FileDriverFactory) NewDriver() (server.Driver, error) {
return &FileDriver{factory.RootPath, factory.Perm}, nil
}