-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
fs.go
94 lines (69 loc) · 2.08 KB
/
fs.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
package dcraw
import (
"io"
"io/fs"
"time"
)
// These implement an [fs.FS] with a single root directory,
// and a single file in that directory, named [readerFSname],
// that reads from the [io.ReadSeeker].
type readerFS struct{ r io.ReadSeeker }
type readerDir struct{ r io.ReadSeeker }
type readerFile struct{ io.ReadSeeker }
const readerFSname = "input"
func (f readerFS) Open(name string) (fs.File, error) {
if name == "." {
return &readerDir{f.r}, nil
}
if name == readerFSname {
_, err := f.r.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
return readerFile{f.r}, nil
}
if fs.ValidPath(name) {
return nil, fs.ErrNotExist
}
return nil, fs.ErrInvalid
}
func (f readerFile) Close() error { return nil }
func (f readerFile) Stat() (fs.FileInfo, error) { return f, nil }
func (f readerFile) Size() int64 {
current, err := f.Seek(0, io.SeekCurrent)
if err != nil {
return 0
}
end, _ := f.Seek(0, io.SeekEnd)
f.Seek(current, io.SeekStart)
return end
}
func (f readerFile) IsDir() bool { return false }
func (f readerFile) ModTime() time.Time { return time.Time{} }
func (f readerFile) Mode() fs.FileMode { return 0400 }
func (f readerFile) Name() string { return readerFSname }
func (f readerFile) Sys() any { return nil }
func (f readerFile) Info() (fs.FileInfo, error) { return f, nil }
func (f readerFile) Type() fs.FileMode { return f.Mode().Type() }
func (d *readerDir) Close() error {
d.r = nil
return nil
}
func (d *readerDir) ReadDir(n int) (entries []fs.DirEntry, err error) {
switch {
case d.r != nil:
entries = []fs.DirEntry{readerFile{d.r}}
d.r = nil
case n > 0:
err = io.EOF
}
return
}
func (d *readerDir) Read([]byte) (int, error) { return 0, nil }
func (d *readerDir) Stat() (fs.FileInfo, error) { return d, nil }
func (d *readerDir) IsDir() bool { return true }
func (d *readerDir) ModTime() time.Time { return time.Time{} }
func (d *readerDir) Mode() fs.FileMode { return fs.ModeDir | 0700 }
func (d *readerDir) Name() string { return "." }
func (d *readerDir) Size() int64 { return 0 }
func (d *readerDir) Sys() any { return nil }