-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinode.go
601 lines (518 loc) · 13.2 KB
/
inode.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package squashfs
import (
"encoding/binary"
"errors"
"io"
"io/fs"
"log"
"strings"
"sync/atomic"
)
type Inode struct {
// refcnt is first value to get guaranteed 64bits alignment, if not sync/atomic will panic
refcnt uint64 // for fuse
sb *Superblock
Type Type
Perm uint16
UidIdx uint16
GidIdx uint16
ModTime int32
Ino uint32 // inode number
StartBlock uint64
NLink uint32
Size uint64 // Careful, actual on disk size varies depending on type
Offset uint32 // uint16 for directories
ParentIno uint32 // for directories
SymTarget []byte // The target path this symlink points to
IdxCount uint16 // index count for advanced directories
XattrIdx uint32 // xattr table index (if relevant)
Sparse uint64
// fragment
FragBlock uint32
FragOfft uint32
// file blocks (some have value 0x1001000)
Blocks []uint32
BlocksOfft []uint64
DirIndex []*DirIndexEntry
}
func (sb *Superblock) GetInode(ino uint64) (*Inode, error) {
if ino == 1 {
// get root inode
return sb.rootIno, nil
}
if ino == sb.rootInoN {
// we reverse
ino = 1
}
// check index cache
inoR, ok := sb.getInodeRefCache(uint32(ino))
if ok {
return sb.GetInodeRef(inoR)
}
// we do not use the flags here, but only see if the table is present. If absent it will be all f's
//if !sb.Flags.Has(EXPORTABLE) {
if sb.ExportTableStart == ^uint64(0) {
return nil, ErrInodeNotExported
}
// load the export table
tr, err := sb.newTableReader(int64(sb.ExportTableStart), int(8*(ino-1)))
if err != nil {
return nil, err
}
err = binary.Read(tr, sb.order, &inoR)
if err != nil {
return nil, err
}
// cache value
sb.setInodeRefCache(uint32(ino), inoR)
return sb.GetInodeRef(inoR)
}
func (sb *Superblock) GetInodeRef(inor inodeRef) (*Inode, error) {
r, err := sb.newInodeReader(inor)
if err != nil {
return nil, err
}
ino := &Inode{sb: sb}
// read inode info
err = binary.Read(r, sb.order, &ino.Type)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.Perm)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.UidIdx)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.GidIdx)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.ModTime)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.Ino)
if err != nil {
return nil, err
}
//log.Printf("read inode #%d type=%d", ino.Ino, ino.Type)
switch ino.Type {
case 1: // Basic Directory
var u32 uint32
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
ino.StartBlock = uint64(u32)
err = binary.Read(r, sb.order, &ino.NLink)
if err != nil {
return nil, err
}
var u16 uint16
err = binary.Read(r, sb.order, &u16)
if err != nil {
return nil, err
}
ino.Size = uint64(u16)
err = binary.Read(r, sb.order, &u16)
if err != nil {
return nil, err
}
ino.Offset = uint32(u16)
err = binary.Read(r, sb.order, &ino.ParentIno)
if err != nil {
return nil, err
}
//log.Printf("squashfs: read basic directory success, parent=%d", ino.ParentIno)
case 8: // Extended dir
var u32 uint32
var u16 uint16
err = binary.Read(r, sb.order, &ino.NLink)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
ino.Size = uint64(u32)
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
ino.StartBlock = uint64(u32)
err = binary.Read(r, sb.order, &ino.ParentIno)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.IdxCount)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &u16)
if err != nil {
return nil, err
}
ino.Offset = uint32(u16)
err = binary.Read(r, sb.order, &ino.XattrIdx)
if err != nil {
return nil, err
}
ino.DirIndex = make([]*DirIndexEntry, ino.IdxCount) // max 65536 as this is 16bits
buf := make([]byte, 4*3) // read 4 u32 values
for n := range ino.DirIndex {
_, err = io.ReadFull(r, buf)
if err != nil {
return nil, err
}
di := &DirIndexEntry{
Index: sb.order.Uint32(buf[:4]),
Start: sb.order.Uint32(buf[4:8]),
}
nameLen := sb.order.Uint32(buf[8:12]) + 1
if nameLen > 256 {
// MAX_PATH is actually lower than that on most platforms
return nil, errors.New("directory index contains name length >256")
}
name := make([]byte, sb.order.Uint32(buf[8:12])+1)
_, err = io.ReadFull(r, name)
if err != nil {
return nil, err
}
di.Name = string(name)
ino.DirIndex[n] = di
}
//log.Printf("squashfs: read extended directory success, parent=%d indexes=%d size=%d", ino.ParentIno, ino.IdxCount, ino.Size)
case 2: // Basic file
var u32 uint32
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
ino.StartBlock = uint64(u32)
// fragment_block_index
err = binary.Read(r, sb.order, &ino.FragBlock)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.FragOfft)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
ino.Size = uint64(u32)
// try to find out how many block_sizes entries
blocks := int(ino.Size / uint64(sb.BlockSize))
if ino.FragBlock == 0xffffffff {
// file does not end in a fragment
if ino.Size%uint64(sb.BlockSize) != 0 {
blocks += 1
}
}
//log.Printf("estimated %d blocks", blocks)
ino.Blocks = make([]uint32, blocks)
ino.BlocksOfft = make([]uint64, blocks)
offt := uint64(0)
// read blocks
for i := 0; i < blocks; i += 1 {
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
ino.Blocks[i] = u32
ino.BlocksOfft[i] = offt
offt += uint64(u32) & 0xfffff // 1MB-1, since max block size is 1MB
}
if ino.FragBlock != 0xffffffff {
// this has a fragment instead of last block
ino.Blocks = append(ino.Blocks, 0xffffffff) // special code
}
case 9: // extended file
err = binary.Read(r, sb.order, &ino.StartBlock)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.Size)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.Sparse) // TODO how to handle this?
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.NLink)
if err != nil {
return nil, err
}
// fragment_block_index
err = binary.Read(r, sb.order, &ino.FragBlock)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.FragOfft)
if err != nil {
return nil, err
}
err = binary.Read(r, sb.order, &ino.XattrIdx)
if err != nil {
return nil, err
}
// try to find out how many block_sizes entries
blocks := int(ino.Size / uint64(sb.BlockSize))
if ino.FragBlock == 0xffffffff {
// file does not end in a fragment
if ino.Size%uint64(sb.BlockSize) != 0 {
blocks += 1
}
}
//log.Printf("estimated %d blocks", blocks)
ino.Blocks = make([]uint32, blocks)
ino.BlocksOfft = make([]uint64, blocks)
var u32 uint32
offt := uint64(0)
// read blocks
for i := 0; i < blocks; i += 1 {
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
ino.Blocks[i] = u32
ino.BlocksOfft[i] = offt
offt += uint64(u32) & 0xfffff // 1MB-1, since max block size is 1MB
}
if ino.FragBlock != 0xffffffff {
// this has a fragment instead of last block
ino.Blocks = append(ino.Blocks, 0xffffffff) // special code
}
//log.Printf("squashfs: read extended file success, sparse=%d size=%d fragblock=%x", ino.Sparse, ino.Size, ino.FragBlock)
case 3: // basic symlink
err = binary.Read(r, sb.order, &ino.NLink)
if err != nil {
return nil, err
}
// read symlink target length
var u32 uint32
err = binary.Read(r, sb.order, &u32)
if err != nil {
return nil, err
}
if u32 > 4096 {
// why is symlink length even stored as u32 ?
return nil, errors.New("symlink target too long")
}
ino.Size = uint64(u32)
// buffer
buf := make([]byte, u32)
_, err = io.ReadFull(r, buf)
if err != nil {
return nil, err
}
ino.SymTarget = buf
//log.Printf("squashfs: read symlink to %s", ino.SymTarget)
default:
log.Printf("squashfs: unsupported inode type %d", ino.Type)
return ino, nil
}
return ino, nil
}
func (i *Inode) ReadAt(p []byte, off int64) (int, error) {
switch i.Type {
case 2, 9: // Basic file
//log.Printf("read request off=%d len=%d", off, len(p))
if uint64(off) >= i.Size {
// no read
return 0, io.EOF
}
if uint64(off+int64(len(p))) > i.Size {
p = p[:int64(i.Size)-off]
}
// we need to know what block to start with
block := int(off / int64(i.sb.BlockSize))
offset := int(off % int64(i.sb.BlockSize))
n := 0
for {
var buf []byte
// read block
if i.Blocks[block] == 0xffffffff {
// this is a fragment, need to decode fragment
//log.Printf("frag table offset=%d", i.sb.FragTableStart)
// read table offset
sub := int64(i.FragBlock) / 512 * 8
blInfo := make([]byte, 8)
_, err := i.sb.fs.ReadAt(blInfo, int64(i.sb.FragTableStart)+sub)
if err != nil {
return n, err
}
// read table
t, err := i.sb.newTableReader(int64(i.sb.order.Uint64(blInfo)), int(i.FragBlock%512)*16)
if err != nil {
return n, err
}
//log.Printf("fragment blinfo=%v", blInfo)
var start uint64
var size uint32
err = binary.Read(t, i.sb.order, &start)
if err != nil {
return n, err
}
err = binary.Read(t, i.sb.order, &size)
if err != nil {
return n, err
}
//log.Printf("fragment at %d:%d => start=0x%x (size=0x%x) len=%d", i.FragBlock, i.FragOfft, start, size, len(p))
if size&0x1000000 == 0x1000000 {
// no compression
buf = make([]byte, size&(0x1000000-1))
_, err = i.sb.fs.ReadAt(buf, int64(start))
if err != nil {
return n, err
}
} else {
// read fragment
buf = make([]byte, size)
_, err = i.sb.fs.ReadAt(buf, int64(start))
if err != nil {
return n, err
}
// decompress
buf, err = i.sb.Comp.decompress(buf)
if err != nil {
return n, err
}
}
if i.FragOfft != 0 {
buf = buf[i.FragOfft:]
}
} else if i.Blocks[block] == 0 {
// this part of the file contains only zeroes
buf = make([]byte, i.sb.BlockSize)
} else {
buf = make([]byte, i.Blocks[block]&0xfffff)
_, err := i.sb.fs.ReadAt(buf, int64(i.StartBlock+i.BlocksOfft[block]))
if err != nil {
return n, err
}
// check for compression
if i.Blocks[block]&0x1000000 == 0 {
// compressed
buf, err = i.sb.Comp.decompress(buf)
if err != nil {
return n, err
}
}
}
// check offset
if offset > 0 {
buf = buf[offset:]
}
// copy
l := copy(p, buf)
n += l
if l == len(p) {
// end of copy
return n, nil
}
// advance out ptr
p = p[l:]
// next block
block += 1
offset = 0
}
}
return 0, fs.ErrInvalid
}
// lookupRelativeInode finds the given inode in the directory
func (i *Inode) lookupRelativeInode(name string) (*Inode, error) {
// TODO: handle indexes
switch i.Type {
case 1, 8:
// basic/extended dir, we need to iterate (cache data?)
var di *DirIndexEntry
for _, t := range i.DirIndex {
if strings.Compare(name, t.Name) < 0 {
// went too far or no index (ie. basic dir)
break
}
di = t
}
dr, err := i.sb.dirReader(i, di)
if err != nil {
return nil, err
}
for {
ename, inoR, err := dr.next()
if err != nil {
if err == io.EOF {
return nil, fs.ErrNotExist
}
return nil, err
}
if di != nil && ename > name {
// if the dir is indexed and we're past our lookup, it means the file does not exist
return nil, fs.ErrNotExist
}
if name == ename {
// found, load the inode from its ref
found, err := i.sb.GetInodeRef(inoR)
if err != nil {
return nil, err
}
// cache info
i.sb.setInodeRefCache(found.Ino, inoR)
// return
return found, nil
}
}
}
return nil, fs.ErrInvalid
}
// Mode returns the inode's mode as fs.FileMode
func (i *Inode) Mode() fs.FileMode {
return unixToMode(uint32(i.Perm)) | i.Type.Mode()
}
// IsDir returns true if the inode is a directory inode.
func (i *Inode) IsDir() bool {
switch i.Type {
case 1, 8:
return true
}
return false
}
// Readlink returns the inode's link
func (i *Inode) Readlink() ([]byte, error) {
switch i.Type {
case 3, 10:
return i.SymTarget, nil
}
return nil, fs.ErrInvalid
}
// AddRef atomatically increments the inode's refcount and returns the new value. This is mainly useful when
// using fuse and can be safely ignored.
func (i *Inode) AddRef(count uint64) uint64 {
return atomic.AddUint64(&i.refcnt, count)
}
// DelRef atomatically decrements the inode's refcount and returns the new value. This is mainly useful when
// using fuse and can be safely ignored.
func (i *Inode) DelRef(count uint64) uint64 {
return atomic.AddUint64(&i.refcnt, ^(count - 1))
}
// GetUid returns inode's owner uid, or zero if an error happens
func (i *Inode) GetUid() uint32 {
if len(i.sb.idTable) >= int(i.UidIdx) {
return i.sb.idTable[i.UidIdx]
}
return 0
}
// GetGid returns inode's group id, or zero if an error happens
func (i *Inode) GetGid() uint32 {
if len(i.sb.idTable) >= int(i.GidIdx) {
return i.sb.idTable[i.GidIdx]
}
return 0
}