-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.go
More file actions
193 lines (173 loc) · 3.86 KB
/
Copy pathfile.go
File metadata and controls
193 lines (173 loc) · 3.86 KB
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
package cfb
import (
"io"
"io/ioutil"
"strings"
)
type File struct {
header *FileHeader
r io.ReaderAt
fat []uint32
minifat []uint32
directoryEntries []*DirectoryEntry
}
func New(r io.ReaderAt) (*File, error) {
f := &File{r: r}
h, err := readFileHeader(&offsetReader{f.r, 0})
if err != nil {
return nil, err
}
f.header = h
if err := f.buildFAT(); err != nil {
return nil, err
}
if err := f.buildMiniFAT(); err != nil {
return nil, err
}
if err := f.buildDirectoryTree(); err != nil {
return nil, err
}
return f, nil
}
func (f *File) Header() *FileHeader {
return f.header
}
func (f *File) Objects() ([]Object, error) {
var os []Object
for _, d := range f.directoryEntries[1:] {
o, err := d.object()
if err != nil {
return nil, err
}
os = append(os, o)
}
return os, nil
}
func (f *File) Get(path string) (Object, error) {
ps := strings.Split(path, "/")
d := f.directoryEntries[0]
for _, p := range ps {
var t *DirectoryEntry
found := false
for _, c := range d.children {
if c.Name() == p {
t = c
found = true
break
}
}
if !found {
return nil, ErrObjectNotFound
}
d = t
}
return d.object()
}
func (f *File) buildFAT() error {
sectors := make([]uint32, f.header.raw.NumberOfFATSectors)
copy(sectors, f.header.raw.DIFAT[:f.header.raw.NumberOfFATSectors])
if f.header.raw.NumberOfDIFATSectors > 0 {
s := f.header.raw.FirstDIFATSectorLocation
for i := 0; i < int(f.header.raw.NumberOfDIFATSectors); i++ {
b, err := f.readSector(s)
if err != nil {
return err
}
difat, err := bytesToUint32s(b)
if err != nil {
return err
}
sectors = append(sectors, difat[:len(difat)-1]...)
s = difat[len(difat)-1]
}
}
var b []byte
for _, s := range sectors {
chunk, err := f.readSector(s)
if err != nil {
return err
}
b = append(b, chunk...)
}
fat, err := bytesToUint32s(b)
if err != nil {
return err
}
f.fat = fat
return nil
}
func (f *File) buildMiniFAT() error {
sr, err := newSectorReader(f.r, f.header.SectorSize(), f.header.raw.FirstMiniFATSectorLocation, f.fat, func(sector uint32) int64 {
return int64((sector + 1) * f.header.SectorSize())
})
if err != nil {
return err
}
b, err := ioutil.ReadAll(sr)
if err != nil {
return err
}
minifat, err := bytesToUint32s(b)
if err != nil {
return err
}
f.minifat = minifat
return nil
}
func (f *File) readDirectoryEntries() ([]*DirectoryEntry, error) {
var ds []*DirectoryEntry
sr, err := newSectorReader(f.r, f.header.SectorSize(), f.header.raw.FirstDirectorySectorLocation, f.fat, func(sector uint32) int64 {
return int64((sector + 1) * f.header.SectorSize())
})
if err != nil {
return nil, err
}
for i := 0; ; i++ {
d, err := readDirectoryEntry(sr)
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
if d.Type() == UnknownObject {
continue
}
d.f = f
d.id = i
ds = append(ds, d)
}
return ds, nil
}
func (f *File) buildDirectoryTree() error {
ds, err := f.readDirectoryEntries()
if err != nil {
return err
}
var walk func(uint32, *DirectoryEntry, []string)
walk = func(id uint32, parent *DirectoryEntry, prefixes []string) {
if id == noStream {
return
}
d := ds[id]
parent.children = append(parent.children, d)
walk(d.raw.LeftSiblingID, parent, prefixes)
walk(d.raw.RightSiblingID, parent, prefixes)
ps := make([]string, len(prefixes))
copy(ps, prefixes)
ps = append(ps, d.Name())
d.path = ps
walk(d.raw.ChildID, d, ps)
}
walk(ds[0].raw.ChildID, ds[0], []string{})
f.directoryEntries = ds
return nil
}
func (f *File) readSector(sector uint32) ([]byte, error) {
b := make([]byte, f.header.SectorSize())
if n, err := f.r.ReadAt(b, int64((sector+1)*f.header.SectorSize())); err != nil {
return nil, err
} else if n != int(f.header.SectorSize()) {
return nil, ErrInsufficientData
}
return b, nil
}