-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
entry.go
167 lines (125 loc) · 2.97 KB
/
entry.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
package tarfs
import (
"archive/tar"
"bytes"
"io"
"io/fs"
"sort"
"time"
)
type entry interface {
fs.DirEntry
size() int64
readdir(path string) ([]fs.DirEntry, error)
readfile(path string) ([]byte, error)
entries(op, path string) ([]fs.DirEntry, error)
open() (fs.File, error)
}
type regEntry struct {
fs.DirEntry
name string
ra io.ReaderAt
offset int64
}
var _ entry = ®Entry{}
func (e *regEntry) size() int64 {
info, _ := e.Info() // err is necessarily nil
return info.Size()
}
func (e *regEntry) readdir(path string) ([]fs.DirEntry, error) {
return nil, newErrNotDir("readdir", path)
}
func (e *regEntry) readfile(path string) ([]byte, error) {
r, err := e.reader()
if err != nil {
return nil, err
}
b := bytes.NewBuffer(make([]byte, 0, e.size()))
if _, err := io.Copy(b, r); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (e *regEntry) entries(op, path string) ([]fs.DirEntry, error) {
return nil, newErrNotDir(op, path)
}
func (e *regEntry) open() (fs.File, error) {
r, err := e.reader()
if err != nil {
return nil, err
}
return &file{e, &readSeeker{&readCounter{r, 0}, e}, -1, false}, nil
}
func (e *regEntry) reader() (io.Reader, error) {
tr := tar.NewReader(io.NewSectionReader(e.ra, e.offset, 1<<63-1-e.offset))
if _, err := tr.Next(); err != nil {
return nil, err
}
return tr, nil
}
type dirEntry struct {
fs.DirEntry
_entries []fs.DirEntry
sorted bool
}
func newDirEntry(e fs.DirEntry) *dirEntry {
return &dirEntry{e, make([]fs.DirEntry, 0, 10), false}
}
func (e *dirEntry) append(c fs.DirEntry) {
e._entries = append(e._entries, c)
}
var _ entry = &dirEntry{}
func (e *dirEntry) size() int64 {
return 0
}
func (e *dirEntry) readdir(path string) ([]fs.DirEntry, error) {
if !e.sorted {
sort.Sort(entriesByName(e._entries))
}
entries := make([]fs.DirEntry, len(e._entries))
copy(entries, e._entries)
return entries, nil
}
func (e *dirEntry) readfile(path string) ([]byte, error) {
return nil, newErrDir("readfile", path)
}
func (e *dirEntry) entries(op, path string) ([]fs.DirEntry, error) {
if !e.sorted {
sort.Sort(entriesByName(e._entries))
}
return e._entries, nil
}
func (e *dirEntry) open() (fs.File, error) {
return &file{e, nil, 0, false}, nil
}
type fakeDirFileInfo string
var _ fs.FileInfo = fakeDirFileInfo("")
func (e fakeDirFileInfo) Name() string {
return string(e)
}
func (fakeDirFileInfo) Size() int64 {
return 0
}
func (fakeDirFileInfo) Mode() fs.FileMode {
return fs.ModeDir
}
func (fakeDirFileInfo) ModTime() time.Time {
return time.Time{}
}
func (fakeDirFileInfo) IsDir() bool {
return true
}
func (fakeDirFileInfo) Sys() interface{} {
return nil
}
type entriesByName []fs.DirEntry
var _ sort.Interface = entriesByName{}
func (entries entriesByName) Less(i, j int) bool {
return entries[i].Name() < entries[j].Name()
}
func (entries entriesByName) Len() int {
return len(entries)
}
func (entries entriesByName) Swap(i, j int) {
entries[i], entries[j] = entries[j], entries[i]
}