-
Notifications
You must be signed in to change notification settings - Fork 74
/
fs_local.go
301 lines (260 loc) · 7.14 KB
/
fs_local.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package webdav
import (
"context"
"fmt"
"io"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/emersion/go-webdav/internal"
)
// LocalFileSystem implements FileSystem for a local directory.
type LocalFileSystem string
var _ FileSystem = LocalFileSystem("")
func (fs LocalFileSystem) localPath(name string) (string, error) {
if (filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0) || strings.Contains(name, "\x00") {
return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: invalid character in path")
}
name = path.Clean(name)
if !path.IsAbs(name) {
return "", internal.HTTPErrorf(http.StatusBadRequest, "webdav: expected absolute path, got %q", name)
}
return filepath.Join(string(fs), filepath.FromSlash(name)), nil
}
func (fs LocalFileSystem) externalPath(name string) (string, error) {
rel, err := filepath.Rel(string(fs), name)
if err != nil {
return "", err
}
return "/" + filepath.ToSlash(rel), nil
}
func (fs LocalFileSystem) Open(ctx context.Context, name string) (io.ReadCloser, error) {
p, err := fs.localPath(name)
if err != nil {
return nil, err
}
return os.Open(p)
}
func fileInfoFromOS(p string, fi os.FileInfo) *FileInfo {
return &FileInfo{
Path: p,
Size: fi.Size(),
ModTime: fi.ModTime(),
IsDir: fi.IsDir(),
// TODO: fallback to http.DetectContentType?
MIMEType: mime.TypeByExtension(path.Ext(p)),
// RFC 2616 section 13.3.3 describes strong ETags. Ideally these would
// be checksums or sequence numbers, however these are expensive to
// compute. The modification time with nanosecond granularity is good
// enough, as it's very unlikely for the same file to be modified twice
// during a single nanosecond.
ETag: fmt.Sprintf("%x%x", fi.ModTime().UnixNano(), fi.Size()),
}
}
func errFromOS(err error) error {
if os.IsNotExist(err) {
return NewHTTPError(http.StatusNotFound, err)
} else if os.IsPermission(err) {
return NewHTTPError(http.StatusForbidden, err)
} else if os.IsTimeout(err) {
return NewHTTPError(http.StatusServiceUnavailable, err)
} else {
return err
}
}
func (fs LocalFileSystem) Stat(ctx context.Context, name string) (*FileInfo, error) {
p, err := fs.localPath(name)
if err != nil {
return nil, err
}
fi, err := os.Stat(p)
if err != nil {
return nil, errFromOS(err)
}
return fileInfoFromOS(name, fi), nil
}
func (fs LocalFileSystem) ReadDir(ctx context.Context, name string, recursive bool) ([]FileInfo, error) {
path, err := fs.localPath(name)
if err != nil {
return nil, err
}
var l []FileInfo
err = filepath.Walk(path, func(p string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
href, err := fs.externalPath(p)
if err != nil {
return err
}
l = append(l, *fileInfoFromOS(href, fi))
if !recursive && fi.IsDir() && path != p {
return filepath.SkipDir
}
return nil
})
return l, errFromOS(err)
}
func (fs LocalFileSystem) Create(ctx context.Context, name string, body io.ReadCloser, opts *CreateOptions) (fi *FileInfo, created bool, err error) {
p, err := fs.localPath(name)
if err != nil {
return nil, false, err
}
fi, _ = fs.Stat(ctx, name)
created = fi == nil
etag := ""
if fi != nil {
etag = fi.ETag
}
if opts.IfMatch.IsSet() {
if ok, err := opts.IfMatch.MatchETag(etag); err != nil {
return nil, false, NewHTTPError(http.StatusBadRequest, err)
} else if !ok {
return nil, false, NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-Match condition failed"))
}
}
if opts.IfNoneMatch.IsSet() {
if ok, err := opts.IfNoneMatch.MatchETag(etag); err != nil {
return nil, false, NewHTTPError(http.StatusBadRequest, err)
} else if ok {
return nil, false, NewHTTPError(http.StatusPreconditionFailed, fmt.Errorf("If-None-Match condition failed"))
}
}
wc, err := os.Create(p)
if err != nil {
return nil, false, errFromOS(err)
}
defer wc.Close()
if _, err := io.Copy(wc, body); err != nil {
os.Remove(p)
return nil, false, err
}
if err := wc.Close(); err != nil {
os.Remove(p)
return nil, false, err
}
fi, err = fs.Stat(ctx, name)
if err != nil {
return nil, false, err
}
return fi, created, err
}
func (fs LocalFileSystem) RemoveAll(ctx context.Context, name string) error {
p, err := fs.localPath(name)
if err != nil {
return err
}
// WebDAV semantics are that it should return a "404 Not Found" error in
// case the resource doesn't exist. We need to Stat before RemoveAll.
if _, err = os.Stat(p); err != nil {
return errFromOS(err)
}
return errFromOS(os.RemoveAll(p))
}
func (fs LocalFileSystem) Mkdir(ctx context.Context, name string) error {
p, err := fs.localPath(name)
if err != nil {
return err
}
return errFromOS(os.Mkdir(p, 0755))
}
func copyRegularFile(src, dst string, perm os.FileMode) error {
srcFile, err := os.Open(src)
if err != nil {
return errFromOS(err)
}
defer srcFile.Close()
dstFile, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm)
if os.IsNotExist(err) {
return NewHTTPError(http.StatusConflict, err)
} else if err != nil {
return errFromOS(err)
}
defer dstFile.Close()
if _, err := io.Copy(dstFile, srcFile); err != nil {
return err
}
return dstFile.Close()
}
func (fs LocalFileSystem) Copy(ctx context.Context, src, dst string, options *CopyOptions) (created bool, err error) {
srcPath, err := fs.localPath(src)
if err != nil {
return false, err
}
dstPath, err := fs.localPath(dst)
if err != nil {
return false, err
}
// TODO: "Note that an infinite-depth COPY of /A/ into /A/B/ could lead to
// infinite recursion if not handled correctly"
srcInfo, err := os.Stat(srcPath)
if err != nil {
return false, errFromOS(err)
}
srcPerm := srcInfo.Mode() & os.ModePerm
if _, err := os.Stat(dstPath); err != nil {
if !os.IsNotExist(err) {
return false, errFromOS(err)
}
created = true
} else {
if options.NoOverwrite {
return false, NewHTTPError(http.StatusPreconditionFailed, os.ErrExist)
}
if err := os.RemoveAll(dstPath); err != nil {
return false, errFromOS(err)
}
}
err = filepath.Walk(srcPath, func(p string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
if err := os.Mkdir(dstPath, srcPerm); err != nil {
return errFromOS(err)
}
} else {
if err := copyRegularFile(srcPath, dstPath, srcPerm); err != nil {
return err
}
}
if fi.IsDir() && options.NoRecursive {
return filepath.SkipDir
}
return nil
})
if err != nil {
return false, errFromOS(err)
}
return created, nil
}
func (fs LocalFileSystem) Move(ctx context.Context, src, dst string, options *MoveOptions) (created bool, err error) {
srcPath, err := fs.localPath(src)
if err != nil {
return false, err
}
dstPath, err := fs.localPath(dst)
if err != nil {
return false, err
}
if _, err := os.Stat(dstPath); err != nil {
if !os.IsNotExist(err) {
return false, errFromOS(err)
}
created = true
} else {
if options.NoOverwrite {
return false, NewHTTPError(http.StatusPreconditionFailed, os.ErrExist)
}
if err := os.RemoveAll(dstPath); err != nil {
return false, errFromOS(err)
}
}
if err := os.Rename(srcPath, dstPath); err != nil {
return false, errFromOS(err)
}
return created, nil
}