-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs-relative.go
587 lines (490 loc) Β· 14.1 KB
/
fs-relative.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
package nef
import (
"fmt"
"io/fs"
"os"
)
// π₯ An important note about using standard golang file systems (io.fs/fs.FS)
// as opposed to using the native os calls directly (os.XXX).
// Note that (io.fs/fs.FS) represents a virtual file system where as os.XXX
// represent operations on the local file system. Working with either of
// these is fundamentally different to working with the other; bear this in
// mind to avoid confusion.
//
// virtual file system
// ===================
//
// The client is expected to create a file system rooted at a particular path.
// This path must be absolute. Any function call on the resulting file system
// that requires a path must be relative to this root and therefore must not
// begin or end with a slash.
//
// When composing paths to use with a file system, one might think that using
// filepath.Separator and building paths with filepath.Join is the most
// prudent thing to do to ensure correct functioning on different platforms. When
// it comes to file systems, this is most certainly not the case. The paths
// are virtual and they are mapped into an underlying file system, which typically
// is the local file system. This means that paths used only need to use '/'. And
// the silly thing is, characters like ':', or '\' for windows should not be
// treated as separators by the underlying file system. So really using
// filepath.Separator with a virtual file system is not valid. This is why
// there is a PathCalc.
//
func sanitise(root string) string {
return root
}
// 𧩠---> open
// π― openFS
type openFS struct {
fS fs.FS
root string
calc PathCalc
}
func (f *openFS) Open(name string) (fs.File, error) {
return f.fS.Open(name)
}
// 𧩠---> stat
// π― statFS
type statFS struct {
*openFS
}
func (f *openFS) Calc() PathCalc {
return f.calc
}
func NewStatFS(rel Rel) fs.StatFS {
ents := compose(sanitise(rel.Root))
return &ents.stat
}
func (f *statFS) Stat(name string) (fs.FileInfo, error) {
return os.Stat(f.calc.Join(f.root, name))
}
// 𧩠---> file system query
// π― readDirFS
type readDirFS struct {
*openFS
}
// NewReadDirFS creates a file system with read directory capability
func NewReadDirFS(rel Rel) fs.ReadDirFS {
ents := compose(sanitise(rel.Root))
return &ents.exists
}
// Open opens the named file.
//
// When Open returns an error, it should be of type *PathError
// with the Op field set to "open", the Path field set to name,
// and the Err field describing the problem.
//
// Open should reject attempts to open names that do not satisfy
// ValidPath(name), returning a *PathError with Err set to
// ErrInvalid or ErrNotExist.
func (n *readDirFS) Open(name string) (fs.File, error) {
return n.fS.Open(name)
}
// ReadDir reads the named directory
// and returns a list of directory entries sorted by filename.
//
// If fs implements [ReadDirFS], ReadDir calls fs.ReadDir.
// Otherwise ReadDir calls fs.Open and uses ReadDir and Close
// on the returned file.
func (n *readDirFS) ReadDir(name string) ([]fs.DirEntry, error) {
return fs.ReadDir(n.fS, name)
}
// π― queryStatusFS
type queryStatusFS struct {
*statFS
*readDirFS
}
func (q *queryStatusFS) Open(name string) (fs.File, error) {
return q.statFS.fS.Open(name)
}
// Stat returns a [FileInfo] describing the named file.
// If there is an error, it will be of type [*PathError].
func (q *queryStatusFS) Stat(name string) (fs.FileInfo, error) {
return q.statFS.Stat(name)
}
// π― existsInFS
type existsInFS struct {
*queryStatusFS
}
// ExistsInFS
func NewExistsInFS(rel Rel) ExistsInFS {
ents := compose(sanitise(rel.Root))
return &ents.exists
}
// disambiguators
func (f *existsInFS) Calc() PathCalc { return f.statFS.calc }
func (f *existsInFS) IsRelative() bool { return true }
// FileExists does file exist at the path specified
func (f *existsInFS) FileExists(name string) bool {
info, err := f.Stat(name)
if err != nil {
return false
}
if info.IsDir() {
return false
}
return true
}
// DirectoryExists does directory exist at the path specified
func (f *existsInFS) DirectoryExists(name string) bool {
info, err := f.Stat(name)
if err != nil {
return false
}
if !info.IsDir() {
return false
}
return true
}
// π― readFileFS
type readFileFS struct {
*queryStatusFS
}
func NewReadFileFS(rel Rel) ReadFileFS {
ents := compose(sanitise(rel.Root))
return &ents.reader
}
// ReadFile reads the named file from the file system fs and returns its contents.
// A successful call returns a nil error, not [io.EOF].
// (Because ReadFile reads the whole file, the expected EOF
// from the final Read is not treated as an error to be reported.)
//
// If fs implements [ReadFileFS], ReadFile calls fs.ReadFile.
// Otherwise ReadFile calls fs.Open and uses Read and Close
// on the returned [File].
func (f *readFileFS) ReadFile(name string) ([]byte, error) {
return fs.ReadFile(f.queryStatusFS.statFS.fS, name)
}
// 𧩠---> file system mutation
// π― copyFS
type copyFS struct {
*openFS
}
func (f *copyFS) Copy(from, to string) error {
return fmt.Errorf("copy not implemented yet (from: %q, to: %q)", from, to)
}
// CopyFS copies the file system fsys into the directory dir,
// creating dir if necessary.
func (f *copyFS) CopyFS(dir string, fsys fs.FS) error {
_ = fsys
return fmt.Errorf("copyFS not implemented yet (dir: %q)", dir)
}
// π― baseWriterFS
type baseWriterFS struct {
*openFS
*existsInFS
overwrite bool
}
// π― MakeDirFS
type makeDirAllFS struct {
*existsInFS
}
// NewMakeDirFS
func NewMakeDirFS(rel Rel) MakeDirFS {
ents := compose(sanitise(rel.Root)).mutate(rel.Overwrite)
return &ents.writer
}
// disambiguators
func (f *makeDirAllFS) Calc() PathCalc { return f.statFS.calc }
func (f *makeDirAllFS) IsRelative() bool { return true }
// Mkdir creates a new directory with the specified name and permission
// bits (before umask).
// If there is an error, it will be of type *PathError.
func (f *makeDirAllFS) MakeDir(name string, perm os.FileMode) error {
if !fs.ValidPath(name) {
return NewInvalidPathError("MakeDir", name)
}
if f.DirectoryExists(name) {
return nil
}
path := f.statFS.calc.Join(f.statFS.root, name)
return os.Mkdir(path, perm)
}
// MakeDirAll creates a directory named path,
// along with any necessary parents, and returns nil,
// or else returns an error.
// The permission bits perm (before umask) are used for all
// directories that MkdirAll creates.
// If path is already a directory, MakeDirAll does nothing
// and returns nil.
func (f *makeDirAllFS) MakeDirAll(name string, perm os.FileMode) error {
if !fs.ValidPath(name) {
return NewInvalidPathError("MakeDirAll", name)
}
if f.DirectoryExists(name) {
return nil
}
path := f.statFS.calc.Join(f.statFS.root, name)
return os.MkdirAll(path, perm)
}
// Ensure makes sure that a path exists at a particular location depending
// on the value of as.AsFile.
//
// When as.AsFile=false: the path denoted by Name is interpreted as being a directory.
// Name is created using os.MkdirAll. The returned value is Name joined with
// the default.
//
// When as.AsFile=true: the path denoted by Name is interpreted as being a file.
// The parent directory is created using os.MkdirAll. If the file denoted by
// Name exists, then Name is returned, otherwise default is returned. So
// the file denoted by Name is only returned if it already exists falling
// back to the default specified.
func (f *makeDirAllFS) Ensure(as PathAs,
) (at string, err error) {
if !fs.ValidPath(as.Name) {
return "", NewInvalidPathError("Ensure", as.Name)
}
var (
directory, file string
)
calc := f.statFS.calc
if as.AsFile {
directory, file = calc.Split(as.Name)
err = f.MakeDirAll(directory, as.Perm)
if f.FileExists(as.Name) {
return as.Name, nil
}
return calc.Clean(calc.Join(directory, file)), err
}
directory = as.Name
file = as.Default
err = f.MakeDirAll(directory, as.Perm)
return calc.Clean(calc.Join(directory, file)), err
}
// π― removeFS
type removeFS struct {
*openFS
}
func (f *removeFS) Remove(name string) error {
if !fs.ValidPath(name) {
return NewInvalidPathError("Remove", name)
}
path := f.calc.Join(f.root, f.calc.Clean(name))
return os.Remove(path)
}
func (f *removeFS) RemoveAll(path string) error {
if !fs.ValidPath(path) {
return NewInvalidPathError("RemoveAll", path)
}
return os.RemoveAll(f.calc.Join(f.root, f.calc.Clean(path)))
}
// π― renameFS
type renameFS struct {
*openFS
}
// Rename delegates to the Rename functionality implemented in the standard
// library.
func (f *renameFS) Rename(from, to string) error {
return os.Rename(
f.calc.Join(f.root, from),
f.calc.Join(f.root, to),
)
}
// π― writeFileFS
type writeFileFS struct {
*baseWriterFS
}
func NewWriteFileFS(rel Rel) WriteFileFS {
ents := compose(sanitise(rel.Root)).mutate(rel.Overwrite)
return &ents.writer
}
// Create creates or truncates the named file. If the file already exists,
// it is truncated. If the file does not exist, it is created with mode 0o666
// (before umask). If successful, methods on the returned File can
// be used for I/O; the associated file descriptor has mode O_RDWR.
// If there is an error, it will be of type *PathError.
//
// We need to maintain conformity with apis in the standard library. Ideally,
// this Create method would have the overwrite bool passed in as an argument,
// but doing so would break standard lib compatibility. Instead, the underlying
// implementation has to decide wether to Create on an override basis itself.
// The disadvantage of this approach is that the client can not decide on
// the fly wether a call to Create is on a override basis or not. This decision
// has to be made at the point of creating the file system. This is less
// flexible and just results in friction, but this is out of our power.
func (f *writeFileFS) Create(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, NewInvalidPathError("Create", name)
}
if !f.overwrite && f.FileExists(name) {
return nil, os.ErrExist
}
path := f.calc.Join(f.root, name)
return os.Create(path)
}
// WriteFile writes data to the named file, creating it if necessary.
// If the file does not exist, WriteFile creates it with permissions perm (before umask);
// otherwise WriteFile truncates it before writing, without changing permissions.
// Since WriteFile requires multiple system calls to complete, a failure mid-operation
// can leave the file in a partially written state.
func (f *writeFileFS) WriteFile(name string, data []byte, perm os.FileMode) error {
if !fs.ValidPath(name) {
return NewInvalidPathError("WriteFile", name)
}
path := f.calc.Join(f.root, name)
return os.WriteFile(path, data, perm)
}
// 𧩠---> file system aggregators
// π― readerFS
type readerFS struct {
*existsInFS
*readDirFS
*readFileFS
*statFS
}
// disambiguators
func (f *readerFS) Calc() PathCalc { return f.statFS.calc }
func (f *readerFS) IsRelative() bool { return true }
// NewReaderFS
func NewReaderFS(rel Rel) ReaderFS {
ents := compose(sanitise(rel.Root))
return &ents.reader
}
// π― aggregatorFS
type aggregatorFS struct {
*baseWriterFS
mover lazyMover
changer lazyChanger
}
// disambiguators
func (f *aggregatorFS) Calc() PathCalc { return f.statFS.calc }
func (f *aggregatorFS) IsRelative() bool { return true }
// Move is similar to rename but it has distinctly different semantics, which
// also varies depending on whether the file system was created with overwrite
// enabled or not.
// When overwrite is enabled, a move with overwrite the destination. If not
// enabled, Move will return as error (os.ErrExist).
// The paths denoted by from and to must be in different locations, otherwise
// the move amounts to a rename and the client should use Rename instead of
// move. When this scenario is detected, an error is returned.
func (f *aggregatorFS) Move(from, to string) error {
return f.mover.instance(
f.existsInFS.queryStatusFS.statFS.root,
f.overwrite,
f,
).move(from, to)
}
func (f *aggregatorFS) Change(from, to string) error {
return f.changer.instance(
f.existsInFS.queryStatusFS.statFS.root,
f.overwrite,
f,
).change(from, to)
}
// π― writerFS
type writerFS struct {
*copyFS
*makeDirAllFS
*aggregatorFS
*removeFS
*renameFS
*writeFileFS
}
func NewWriterFS(rel Rel) WriterFS {
ents := compose(sanitise(rel.Root)).mutate(rel.Overwrite)
return &ents.writer
}
// disambiguators
func (f *writerFS) Calc() PathCalc { return f.statFS.calc }
func (f *writerFS) IsRelative() bool { return true }
// π― mutatorFS
type mutatorFS struct {
*readerFS
*writerFS
}
// disambiguators
func (f *mutatorFS) Calc() PathCalc { return f.statFS.calc }
func (f *mutatorFS) IsRelative() bool { return true }
func newMutatorFS(rel *Rel) *mutatorFS {
ents := compose(sanitise(rel.Root)).mutate(rel.Overwrite)
return &mutatorFS{
readerFS: &ents.reader,
writerFS: &ents.writer,
}
}
func NewUniversalFS(rel Rel) UniversalFS {
return newMutatorFS(&rel)
}
// 𧩠---> construction
type (
entities struct {
open openFS
read readDirFS
stat statFS
query queryStatusFS
exists existsInFS
reader readerFS
copy copyFS
remove removeFS
rename renameFS
writer writerFS
}
)
func (e *entities) mutate(overwrite bool) *entities {
writer := &baseWriterFS{
existsInFS: &e.exists,
openFS: &e.open,
overwrite: overwrite,
}
e.writer = writerFS{
copyFS: ©FS{
openFS: &e.open,
},
makeDirAllFS: &makeDirAllFS{
existsInFS: &e.exists,
},
aggregatorFS: &aggregatorFS{
baseWriterFS: writer,
},
removeFS: &removeFS{
openFS: &e.open,
},
renameFS: &renameFS{
openFS: &e.open,
},
writeFileFS: &writeFileFS{
baseWriterFS: writer,
},
}
return e
}
func compose(root string) *entities {
open := openFS{
fS: os.DirFS(root),
root: root,
calc: &RelativeCalc{
Root: root,
},
}
read := readDirFS{
openFS: &open,
}
stat := statFS{
openFS: &open,
}
query := queryStatusFS{
statFS: &statFS{
openFS: &open,
},
readDirFS: &read,
}
exists := existsInFS{
queryStatusFS: &query,
}
reader := readerFS{
readDirFS: &read,
readFileFS: &readFileFS{
queryStatusFS: &query,
},
existsInFS: &exists,
statFS: &stat,
}
return &entities{
open: open,
read: read,
stat: stat,
query: query,
exists: exists,
reader: reader,
}
}