-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdirectory_browser.go
89 lines (63 loc) · 1.99 KB
/
directory_browser.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
package ext4
import (
"bytes"
"io"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
// DirectoryBrowser provides high-level directory navigation.
type DirectoryBrowser struct {
inodeReader *InodeReader
dataSize uint64
dataRead uint64
}
func NewDirectoryBrowser(rs io.ReadSeeker, inode *Inode) *DirectoryBrowser {
en := NewExtentNavigatorWithReadSeeker(rs, inode)
ir := NewInodeReader(en)
return &DirectoryBrowser{
inodeReader: ir,
dataSize: inode.Size(),
}
}
// Next parses the next directory entry from the underlying inode data reader.
// Returns `io.EOF` when done. This will also return the "." and ".." entries.
func (db *DirectoryBrowser) Next() (de *DirectoryEntry, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): !! We should be seeing an extra `ext4_dir_entry_tail` as the last entry, which has a similar structure to a normal entry but provides a checksum. However, it doesn't appear to be present.
if db.dataRead >= db.dataSize {
return nil, io.EOF
}
raw := new(Ext4DirEntry2)
err = binary.Read(db.inodeReader, binary.LittleEndian, &raw.Inode)
log.PanicIf(err)
err = binary.Read(db.inodeReader, binary.LittleEndian, &raw.RecLen)
log.PanicIf(err)
// Read the remaining data, which is variable-length.
offset := 0
needBytes := int(raw.RecLen) - 6
record := make([]byte, needBytes)
for offset < needBytes {
n, err := db.inodeReader.Read(record[offset:])
log.PanicIf(err)
offset += n
}
b := bytes.NewBuffer(record)
err = binary.Read(b, binary.LittleEndian, &raw.NameLen)
log.PanicIf(err)
err = binary.Read(b, binary.LittleEndian, &raw.FileType)
log.PanicIf(err)
raw.Name = record[2 : 2+raw.NameLen]
// Done. Wrap up.
db.dataRead += uint64(raw.RecLen)
if db.dataRead > db.dataSize {
log.Panicf("inode size is not aligned to the directory-entry record size (we ran over): (%d) > (%d)", db.dataRead, db.dataSize)
}
de = &DirectoryEntry{
data: raw,
}
return de, nil
}