-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathentry.go
227 lines (196 loc) · 4.45 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
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
package asar // import "layeh.com/asar"
import (
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
)
// Flag is a bit field of Entry flags.
type Flag uint32
const (
// FlagNone denotes an entry with no flags.
FlagNone Flag = 0
// FlagDir denotes a directory entry.
FlagDir Flag = 1 << iota
// FlagExecutable denotes a file with the executable bit set.
FlagExecutable
// FlagUnpacked denotes that the entry's contents are not included in
// the archive.
FlagUnpacked
)
// Entry is a file or a folder in an ASAR archive.
type Entry struct {
Name string
Size int64
Offset int64
Flags Flag
Parent *Entry
Children []*Entry
r io.ReaderAt
baseOffset int64
}
// New creates a new Entry.
func New(name string, ra io.ReaderAt, size, offset int64, flags Flag) *Entry {
return &Entry{
Name: name,
Size: size,
Offset: offset,
Flags: flags,
r: ra,
}
}
// FileInfo returns the os.FileInfo information about the entry.
func (e *Entry) FileInfo() os.FileInfo {
return fileInfo{e}
}
type fileInfo struct {
e *Entry
}
func (f fileInfo) Name() string {
return f.e.Name
}
func (f fileInfo) Size() int64 {
return f.e.Size
}
func (f fileInfo) Mode() os.FileMode {
if f.e.Flags&FlagDir != 0 {
return 0555 | os.ModeDir
}
if f.e.Flags&FlagExecutable != 0 {
return 0555
}
return 0444
}
func (f fileInfo) ModTime() time.Time {
return time.Time{}
}
func (f fileInfo) IsDir() bool {
return f.e.Flags&FlagDir != 0
}
func (f fileInfo) Sys() interface{} {
return f.e
}
// Path returns the file path to the entry.
//
// For example, given the following tree structure:
// root
// - sub1
// - sub2
// - file2.jpg
//
// file2.jpg's path would be:
// sub2/file2.jpg
func (e *Entry) Path() string {
if e.Parent == nil {
return ""
}
var p []string
for e != nil && e.Parent != nil {
p = append(p, e.Name)
e = e.Parent
}
l := len(p) / 2
for i := 0; i < l; i++ {
j := len(p) - i - 1
p[i], p[j] = p[j], p[i]
}
return strings.Join(p, "/")
}
// Open returns an *io.SectionReader of the entry's contents. nil is returned if
// the entry cannot be opened (e.g. because it is a directory).
func (e *Entry) Open() *io.SectionReader {
if e.Flags&FlagDir != 0 || e.Flags&FlagUnpacked != 0 {
return nil
}
return io.NewSectionReader(e.r, e.baseOffset+e.Offset, e.Size)
}
// WriteTo writes the entry's contents to the given writer. nil is returned if the entry cannot
// be opened (e.g. if the entry is a directory).
func (e *Entry) WriteTo(w io.Writer) (n int64, err error) {
r := e.Open()
if r == nil {
return 0, errors.New("asar: entry cannot be opened")
}
return io.Copy(w, r)
}
// Bytes returns the entry's contents as a byte slice. nil is returned if the
// entry cannot be read.
func (e *Entry) Bytes() []byte {
body := e.Open()
if body == nil {
return nil
}
b, err := ioutil.ReadAll(body)
if err != nil {
return nil
}
return b
}
// Bytes returns the entry's contents as a string. nil is returned if the entry
// cannot be read.
func (e *Entry) String() string {
body := e.Bytes()
if body == nil {
return ""
}
return string(body)
}
// Find searches for a sub-entry of the current entry. nil is returned if the
// requested sub-entry cannot be found.
//
// For example, given the following tree structure:
// root
// - sub1
// - sub2
// - sub2.1
// - file2.jpg
//
// The following expression would return the .jpg *Entry:
// root.Find("sub2", "sub2.1", "file2.jpg")
func (e *Entry) Find(path ...string) *Entry {
pathLoop:
for _, name := range path {
for _, child := range e.Children {
if child.Name == name {
e = child
continue pathLoop
}
}
return nil
}
return e
}
// Walk recursively walks over the entry's children. See filepath.Walk and
// filepath.WalkFunc for more information.
func (e *Entry) Walk(walkFn filepath.WalkFunc) error {
return walk(e, "", walkFn)
}
func walk(e *Entry, parentPath string, walkFn filepath.WalkFunc) error {
for i := 0; i < len(e.Children); i++ {
child := e.Children[i]
childPath := parentPath + child.Name
err := walkFn(childPath, child.FileInfo(), nil)
if err == filepath.SkipDir {
continue
}
if err != nil {
return err
}
if child.Flags&FlagDir == 0 {
continue
}
if err := walk(child, childPath+"/", walkFn); err != nil {
return err
}
}
return nil
}
func validFilename(filename string) bool {
if filename == "." || filename == ".." {
return false
}
return strings.IndexAny(filename, "\x00\\/") == -1
}