forked from syncthing/syncthing
/
basicfs.go
384 lines (329 loc) · 9.54 KB
/
basicfs.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright (C) 2016 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package fs
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/shirou/gopsutil/v3/disk"
"github.com/syncthing/syncthing/lib/build"
)
var (
errInvalidFilenameEmpty = errors.New("name is invalid, must not be empty")
errInvalidFilenameWindowsSpacePeriod = errors.New("name is invalid, must not end in space or period on Windows")
errInvalidFilenameWindowsReservedName = errors.New("name is invalid, contains Windows reserved name (NUL, COM1, etc.)")
errInvalidFilenameWindowsReservedChar = errors.New("name is invalid, contains Windows reserved character (?, *, etc.)")
)
type OptionJunctionsAsDirs struct{}
func (o *OptionJunctionsAsDirs) apply(fs Filesystem) Filesystem {
if basic, ok := fs.(*BasicFilesystem); !ok {
l.Warnln("WithJunctionsAsDirs must only be used with FilesystemTypeBasic")
} else {
basic.junctionsAsDirs = true
}
return fs
}
func (o *OptionJunctionsAsDirs) String() string {
return "junctionsAsDirs"
}
// The BasicFilesystem implements all aspects by delegating to package os.
// All paths are relative to the root and cannot (should not) escape the root directory.
type BasicFilesystem struct {
root string
junctionsAsDirs bool
options []Option
}
func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem {
if root == "" {
root = "." // Otherwise "" becomes "/" below
}
// The reason it's done like this:
// C: -> C:\ -> C:\ (issue that this is trying to fix)
// C:\somedir -> C:\somedir\ -> C:\somedir
// C:\somedir\ -> C:\somedir\\ -> C:\somedir
// This way in the tests, we get away without OS specific separators
// in the test configs.
sep := string(filepath.Separator)
root = filepath.Dir(root + sep)
// Attempt tilde expansion; leave unchanged in case of error
if path, err := ExpandTilde(root); err == nil {
root = path
}
// Attempt absolutification; leave unchanged in case of error
if !filepath.IsAbs(root) {
// Abs() looks like a fairly expensive syscall on Windows, while
// IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
// somewhat faster in the general case, hence the outer if...
if path, err := filepath.Abs(root); err == nil {
root = path
}
}
// Attempt to enable long filename support on Windows. We may still not
// have an absolute path here if the previous steps failed.
if build.IsWindows {
root = longFilenameSupport(root)
}
fs := &BasicFilesystem{
root: root,
options: opts,
}
for _, opt := range opts {
opt.apply(fs)
}
return fs
}
// rooted expands the relative path to the full path that is then used with os
// package. If the relative path somehow causes the final path to escape the root
// directory, this returns an error, to prevent accessing files that are not in the
// shared directory.
func (f *BasicFilesystem) rooted(rel string) (string, error) {
return rooted(rel, f.root)
}
func rooted(rel, root string) (string, error) {
// The root must not be empty.
if root == "" {
return "", errInvalidFilenameEmpty
}
var err error
// Takes care that rel does not try to escape
rel, err = Canonicalize(rel)
if err != nil {
return "", err
}
return filepath.Join(root, rel), nil
}
func (f *BasicFilesystem) unrooted(path string) string {
return rel(path, f.root)
}
func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
name, err := f.rooted(name)
if err != nil {
return err
}
return os.Chmod(name, os.FileMode(mode))
}
func (f *BasicFilesystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
name, err := f.rooted(name)
if err != nil {
return err
}
return os.Chtimes(name, atime, mtime)
}
func (f *BasicFilesystem) Mkdir(name string, perm FileMode) error {
name, err := f.rooted(name)
if err != nil {
return err
}
return os.Mkdir(name, os.FileMode(perm))
}
// MkdirAll creates a directory named path, along with any necessary parents,
// and returns nil, or else returns an error.
// The permission bits perm are used for all directories that MkdirAll creates.
// If path is already a directory, MkdirAll does nothing and returns nil.
func (f *BasicFilesystem) MkdirAll(path string, perm FileMode) error {
path, err := f.rooted(path)
if err != nil {
return err
}
return f.mkdirAll(path, os.FileMode(perm))
}
func (f *BasicFilesystem) Lstat(name string) (FileInfo, error) {
name, err := f.rooted(name)
if err != nil {
return nil, err
}
fi, err := f.underlyingLstat(name)
if err != nil {
return nil, err
}
return basicFileInfo{fi}, err
}
func (f *BasicFilesystem) Remove(name string) error {
name, err := f.rooted(name)
if err != nil {
return err
}
return os.Remove(name)
}
func (f *BasicFilesystem) RemoveAll(name string) error {
name, err := f.rooted(name)
if err != nil {
return err
}
return os.RemoveAll(name)
}
func (f *BasicFilesystem) Rename(oldpath, newpath string) error {
oldpath, err := f.rooted(oldpath)
if err != nil {
return err
}
newpath, err = f.rooted(newpath)
if err != nil {
return err
}
return os.Rename(oldpath, newpath)
}
func (f *BasicFilesystem) Stat(name string) (FileInfo, error) {
name, err := f.rooted(name)
if err != nil {
return nil, err
}
fi, err := os.Stat(name)
if err != nil {
return nil, err
}
return basicFileInfo{fi}, err
}
func (f *BasicFilesystem) DirNames(name string) ([]string, error) {
name, err := f.rooted(name)
if err != nil {
return nil, err
}
fd, err := os.OpenFile(name, OptReadOnly, 0777)
if err != nil {
return nil, err
}
defer fd.Close()
names, err := fd.Readdirnames(-1)
if err != nil {
return nil, err
}
return names, nil
}
func (f *BasicFilesystem) Open(name string) (File, error) {
rootedName, err := f.rooted(name)
if err != nil {
return nil, err
}
fd, err := os.Open(rootedName)
if err != nil {
return nil, err
}
return basicFile{fd, name}, err
}
func (f *BasicFilesystem) OpenFile(name string, flags int, mode FileMode) (File, error) {
rootedName, err := f.rooted(name)
if err != nil {
return nil, err
}
fd, err := os.OpenFile(rootedName, flags, os.FileMode(mode))
if err != nil {
return nil, err
}
return basicFile{fd, name}, err
}
func (f *BasicFilesystem) Create(name string) (File, error) {
rootedName, err := f.rooted(name)
if err != nil {
return nil, err
}
fd, err := os.Create(rootedName)
if err != nil {
return nil, err
}
return basicFile{fd, name}, err
}
func (f *BasicFilesystem) Walk(root string, walkFn WalkFunc) error {
// implemented in WalkFilesystem
return errors.New("not implemented")
}
func (f *BasicFilesystem) Glob(pattern string) ([]string, error) {
pattern, err := f.rooted(pattern)
if err != nil {
return nil, err
}
files, err := filepath.Glob(pattern)
unrooted := make([]string, len(files))
for i := range files {
unrooted[i] = f.unrooted(files[i])
}
return unrooted, err
}
func (f *BasicFilesystem) Usage(name string) (Usage, error) {
name, err := f.rooted(name)
if err != nil {
return Usage{}, err
}
u, err := disk.Usage(name)
if err != nil {
return Usage{}, err
}
return Usage{
Free: u.Free,
Total: u.Total,
}, nil
}
func (f *BasicFilesystem) Type() FilesystemType {
return FilesystemTypeBasic
}
func (f *BasicFilesystem) URI() string {
return strings.TrimPrefix(f.root, `\\?\`)
}
func (f *BasicFilesystem) Options() []Option {
return f.options
}
func (f *BasicFilesystem) SameFile(fi1, fi2 FileInfo) bool {
// Like os.SameFile, we always return false unless fi1 and fi2 were created
// by this package's Stat/Lstat method.
f1, ok1 := fi1.(basicFileInfo)
f2, ok2 := fi2.(basicFileInfo)
if !ok1 || !ok2 {
return false
}
return os.SameFile(f1.osFileInfo(), f2.osFileInfo())
}
func (f *BasicFilesystem) underlying() (Filesystem, bool) {
return nil, false
}
func (f *BasicFilesystem) wrapperType() filesystemWrapperType {
return filesystemWrapperTypeNone
}
// basicFile implements the fs.File interface on top of an os.File
type basicFile struct {
*os.File
name string
}
func (f basicFile) Name() string {
return f.name
}
func (f basicFile) Stat() (FileInfo, error) {
info, err := f.File.Stat()
if err != nil {
return nil, err
}
return basicFileInfo{info}, nil
}
// basicFileInfo implements the fs.FileInfo interface on top of an os.FileInfo.
type basicFileInfo struct {
os.FileInfo
}
func (e basicFileInfo) IsSymlink() bool {
// Must use basicFileInfo.Mode() because it may apply magic.
return e.Mode()&ModeSymlink != 0
}
func (e basicFileInfo) IsRegular() bool {
// Must use basicFileInfo.Mode() because it may apply magic.
return e.Mode()&ModeType == 0
}
// longFilenameSupport adds the necessary prefix to the path to enable long
// filename support on windows if necessary.
// This does NOT check the current system, i.e. will also take effect on unix paths.
func longFilenameSupport(path string) string {
if filepath.IsAbs(path) && !strings.HasPrefix(path, `\\`) {
return `\\?\` + path
}
return path
}
type ErrWatchEventOutsideRoot struct{ msg string }
func (e *ErrWatchEventOutsideRoot) Error() string {
return e.msg
}
func (f *BasicFilesystem) newErrWatchEventOutsideRoot(absPath string, roots []string) *ErrWatchEventOutsideRoot {
return &ErrWatchEventOutsideRoot{fmt.Sprintf("Watching for changes encountered an event outside of the filesystem root: f.root==%v, roots==%v, path==%v. This should never happen, please report this message to forum.syncthing.net.", f.root, roots, absPath)}
}