-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfile.go
102 lines (73 loc) · 1.45 KB
/
file.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
package tarfs
import (
"io"
"io/fs"
)
type file struct {
entry
r io.ReadSeeker
readDirPos int
closed bool
}
var _ fs.File = &file{}
func (f *file) Stat() (fs.FileInfo, error) {
const op = "stat"
if f.closed {
return nil, newErrClosed(op, f.Name())
}
return f.Info()
}
func (f *file) Read(b []byte) (int, error) {
const op = "read"
if f.closed {
return 0, newErrClosed(op, f.Name())
}
if f.IsDir() {
return 0, newErrDir(op, f.Name())
}
return f.r.Read(b)
}
func (f *file) Close() error {
const op = "close"
if f.closed {
return newErrClosed(op, f.Name())
}
f.r = nil
f.closed = true
return nil
}
var _ io.Seeker = &file{}
func (f *file) Seek(offset int64, whence int) (int64, error) {
const op = "seek"
if f.closed {
return 0, newErrClosed(op, f.Name())
}
if f.IsDir() {
return 0, newErrDir(op, f.Name())
}
return f.r.Seek(offset, whence)
}
var _ fs.ReadDirFile = &file{}
func (f *file) ReadDir(n int) ([]fs.DirEntry, error) {
const op = "readdir"
if f.closed {
return nil, newErrClosed(op, f.Name())
}
allEntries, err := f.entry.entries(op, f.Name())
if err != nil {
return nil, err
}
if f.readDirPos >= len(allEntries) {
if n <= 0 {
return nil, nil
}
return nil, io.EOF
}
if n <= 0 || f.readDirPos+n > len(allEntries) {
n = len(allEntries) - f.readDirPos
}
entries := make([]fs.DirEntry, n)
copy(entries, allEntries[f.readDirPos:])
f.readDirPos += n
return entries, nil
}