-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper.go
410 lines (360 loc) · 10.8 KB
/
super.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
package squashfs
import (
"encoding/binary"
"io"
"io/fs"
"os"
"path"
"runtime"
"strings"
"sync"
)
const SuperblockSize = 96
// Superblock is the main object representing a squashfs image, and exposes various information about
// the file. You can ignore most of these and use the object directly to access files/etc, or inspect
// various elements of the squashfs image.
type Superblock struct {
fs io.ReaderAt
order binary.ByteOrder
clos io.Closer
rootIno *Inode
rootInoN uint64
inoIdx map[uint32]inodeRef // inode refs cache (see export table)
inoIdxL sync.RWMutex
inoOfft uint64
idTable []uint32
Magic uint32 // magic identifier
InodeCnt uint32 // number of inodes in filesystem
ModTime int32 // creation unix time as int32 (will stop working in 2038)
BlockSize uint32 // size of a single data block, must match 1<<BlockLog
FragCount uint32
Comp Compression // Compression used, usually GZip
BlockLog uint16
Flags Flags // squashfs flags
IdCount uint16
VMajor uint16
VMinor uint16
RootInode inodeRef // inode number/reference of root
BytesUsed uint64
IdTableStart uint64
XattrIdTableStart uint64
InodeTableStart uint64
DirTableStart uint64
FragTableStart uint64
ExportTableStart uint64
}
var _ fs.FS = (*Superblock)(nil)
var _ fs.ReadDirFS = (*Superblock)(nil)
var _ fs.StatFS = (*Superblock)(nil)
// New returns a new instance of superblock for a given io.ReaderAt that can
// be used to access files inside squashfs.
func New(fs io.ReaderAt, options ...Option) (*Superblock, error) {
sb := &Superblock{fs: fs,
inoIdx: make(map[uint32]inodeRef),
}
head := make([]byte, SuperblockSize)
_, err := fs.ReadAt(head, 0)
if err != nil {
return nil, err
}
err = sb.UnmarshalBinary(head)
if err != nil {
return nil, err
}
if sb.VMajor != 4 || sb.VMinor != 0 {
return nil, ErrInvalidVersion
}
// apply options
for _, opt := range options {
err = opt(sb)
if err != nil {
return nil, err
}
}
// get root inode
sb.rootIno, err = sb.GetInodeRef(sb.RootInode)
if err != nil {
return nil, err
}
sb.rootInoN = uint64(sb.rootIno.Ino)
sb.readIdTable()
return sb, nil
}
// Open returns a new instance of superblock for a given file that can
// be used to access files inside squashfs. The file will be closed by
// the garbage collector or when Close() is called on the superblock.
func Open(file string, options ...Option) (*Superblock, error) {
f, err := os.Open(file)
if err != nil {
return nil, err
}
sb, err := New(f, options...)
if err != nil {
f.Close()
return nil, err
}
sb.clos = f
clean := func(sb *Superblock) {
sb.Close()
}
runtime.SetFinalizer(sb, clean)
return sb, nil
}
func (sb *Superblock) readIdTable() error {
// read id table
idtable, err := sb.newIndirectTableReader(int64(sb.IdTableStart), 0)
if err != nil {
return err
}
var id uint32
sb.idTable = make([]uint32, sb.IdCount)
for i := range sb.idTable {
err := binary.Read(idtable, sb.order, &id)
if err != nil {
return err
}
sb.idTable[i] = id
}
//log.Printf("sqashfs: id table = %+v", sb.idTable)
return nil
}
// UnmarshalBinary reads a binary header values into Superblock
func (s *Superblock) UnmarshalBinary(data []byte) error {
if len(data) != SuperblockSize {
return ErrInvalidSuper
}
switch string(data[:4]) {
case "hsqs":
s.order = binary.LittleEndian
case "sqsh":
s.order = binary.BigEndian
default:
return ErrInvalidFile
}
s.Magic = s.order.Uint32(data[0:4])
s.InodeCnt = s.order.Uint32(data[4:8])
s.ModTime = int32(s.order.Uint32(data[8:12]))
s.BlockSize = s.order.Uint32(data[12:16])
s.FragCount = s.order.Uint32(data[16:20])
s.Comp = Compression(s.order.Uint16(data[20:22]))
s.BlockLog = s.order.Uint16(data[22:24])
s.Flags = Flags(s.order.Uint16(data[24:26]))
s.IdCount = s.order.Uint16(data[26:28])
s.VMajor = s.order.Uint16(data[28:30])
s.VMinor = s.order.Uint16(data[30:32])
s.RootInode = inodeRef(s.order.Uint64(data[32:40]))
s.BytesUsed = s.order.Uint64(data[40:48])
s.IdTableStart = s.order.Uint64(data[48:56])
s.XattrIdTableStart = s.order.Uint64(data[56:64])
s.InodeTableStart = s.order.Uint64(data[64:72])
s.DirTableStart = s.order.Uint64(data[72:80])
s.FragTableStart = s.order.Uint64(data[80:88])
s.ExportTableStart = s.order.Uint64(data[88:96])
if s.Magic != 0x73717368 {
// shouldn't happen
return ErrInvalidFile
}
if uint32(1)<<s.BlockLog != s.BlockSize {
return ErrInvalidSuper
}
//log.Printf("parsed SquashFS %d.%d blocksize=%d bytes=%d comp=%s flags=%s", s.VMajor, s.VMinor, s.BlockSize, s.BytesUsed, s.Comp, s.Flags)
//log.Printf("inode table at 0x%x, export at 0x%x, count=%d, root=%s", s.InodeTableStart, s.ExportTableStart, s.InodeCnt, s.RootInode)
return nil
}
// SetInodeOffset allows setting the inode offset used for interacting with fuse. This can be safely ignored if not using fuse
// or when mounting only a single squashfs via fuse.
func (s *Superblock) SetInodeOffset(offt uint64) {
s.inoOfft = offt
}
// FindInode returns the inode for a given path. If followSymlink is false and
// a symlink is found in the path, it will be followed anyway. If however the
// target file is a symlink, then its inode will be returned.
func (s *Superblock) FindInode(name string, followSymlinks bool) (*Inode, error) {
return s.FindInodeUnder(s.rootIno, name, followSymlinks)
}
// FindInodeUnder returns an inode for a path starting at a given different inode
//
// Note that it is not possible to access directories outside the given path,
// including using symlinks, as this effectively acts as a chroot. This can be
// useful to implement fs.Sub
func (s *Superblock) FindInodeUnder(cur *Inode, name string, followSymlinks bool) (*Inode, error) {
// similar to lookup, but handles slashes in name and returns an inode
parents := make(map[uint32]*Inode)
parents[cur.Ino] = cur
symlinkRedirects := 40 // maximum number of redirects before giving up
for {
if len(name) == 0 {
// trailing slash?
return cur, nil
}
pos := strings.IndexByte(name, '/')
if pos == -1 {
// no / - perform final lookup
if !followSymlinks {
return cur.lookupRelativeInode(name)
}
res, err := cur.lookupRelativeInode(name)
if err != nil {
return nil, err
}
if !res.Type.IsSymlink() {
return res, nil
}
// need to perform symlink lookup, we are not done here
if symlinkRedirects == 0 {
return nil, ErrTooManySymlinks
}
symlinkRedirects -= 1
sym, err := res.Readlink()
if err != nil {
return nil, err
}
// ensure symlink isn't empty and isn't absolute either
if len(sym) == 0 || sym[0] == '/' {
return nil, fs.ErrInvalid
}
// continue lookup from that point
name = string(sym)
continue
}
if pos == 0 {
// skip initial or subsequent /
name = name[1:]
continue
}
if !cur.IsDir() {
return nil, ErrNotDirectory
}
if name[:pos] == "." {
// special case: keep cur as is
name = name[pos+1:]
continue
}
if name[:pos] == ".." {
// special case: move to parent dir
name = name[pos+1:]
cur = parents[cur.Ino]
continue
}
t, err := cur.lookupRelativeInode(name[:pos])
if err != nil {
return nil, err
}
if t.Type.IsSymlink() {
if symlinkRedirects == 0 {
return nil, ErrTooManySymlinks
}
symlinkRedirects -= 1
sym, err := t.Readlink()
if err != nil {
return nil, err
}
// ensure symlink isn't empty and isn't absolute either
if len(sym) == 0 || sym[0] == '/' {
return nil, fs.ErrInvalid
}
// prepend symlink to name & remove symlink
// if symlink a=b and name=a/c it becomes b/c
name = string(sym) + name[pos:] // no +1 to pos means we keep the / we had in name
// do not update cur since lookup resumes from that point
continue
}
// there still are further lookups, so this must be a directory
if !t.IsDir() {
return nil, ErrNotDirectory
}
// move forward
parents[t.Ino] = cur
cur = t
name = name[pos+1:]
}
}
// Open returns a fs.File for a given path, which can be a different object depending
// if the file is a regular file or a directory.
func (sb *Superblock) Open(name string) (fs.File, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid}
}
ino, err := sb.FindInode(name, true)
if err != nil {
return nil, &fs.PathError{Op: "open", Path: name, Err: err}
}
return ino.OpenFile(path.Base(name)), nil
}
// Readlink allows reading the value of a symbolic link inside the archive.
func (sb *Superblock) Readlink(name string) (string, error) {
if !fs.ValidPath(name) {
return "", &fs.PathError{Op: "readlink", Path: name, Err: fs.ErrInvalid}
}
ino, err := sb.FindInode(name, true)
if err != nil {
return "", &fs.PathError{Op: "readlink", Path: name, Err: err}
}
res, err := ino.Readlink()
if err != nil {
return "", &fs.PathError{Op: "readlink", Path: name, Err: err}
}
return string(res), nil
}
// ReadDir implements fs.ReadDirFS and allows listing any directory inside the archive
func (sb *Superblock) ReadDir(name string) ([]fs.DirEntry, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "readdir", Path: name, Err: fs.ErrInvalid}
}
ino, err := sb.FindInode(name, true)
if err != nil {
return nil, &fs.PathError{Op: "readdir", Path: name, Err: err}
}
switch ino.Type {
case 1, 8:
// basic dir, we need to iterate (cache data?)
dr, err := sb.dirReader(ino, nil)
if err != nil {
return nil, err
}
return dr.ReadDir(0)
default:
return nil, fs.ErrInvalid
}
}
// Stat will return stats for a given path inside the squashfs archive
func (sb *Superblock) Stat(name string) (fs.FileInfo, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "stat", Path: name, Err: fs.ErrInvalid}
}
ino, err := sb.FindInode(name, true)
if err != nil {
return nil, err
}
return &fileinfo{name: path.Base(name), ino: ino}, nil
}
// Lstat will return stats for a given path inside the sqhashfs archive. If
// the target is a symbolic link, data on the link itself will be returned.
func (sb *Superblock) Lstat(name string) (fs.FileInfo, error) {
if !fs.ValidPath(name) {
return nil, &fs.PathError{Op: "lstat", Path: name, Err: fs.ErrInvalid}
}
ino, err := sb.FindInode(name, false)
if err != nil {
return nil, err
}
return &fileinfo{name: path.Base(name), ino: ino}, nil
}
// Close will close the underlying file when a filesystem was open with Open()
func (sb *Superblock) Close() error {
if sb.clos != nil {
return sb.clos.Close()
}
return nil
}
func (sb *Superblock) getInodeRefCache(ino uint32) (inodeRef, bool) {
sb.inoIdxL.RLock()
defer sb.inoIdxL.RUnlock()
res, ok := sb.inoIdx[ino]
return res, ok
}
func (sb *Superblock) setInodeRefCache(ino uint32, inoR inodeRef) {
sb.inoIdxL.Lock()
defer sb.inoIdxL.Unlock()
sb.inoIdx[ino] = inoR
}