forked from kdomanski/iso9660
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_reader.go
219 lines (182 loc) · 5.16 KB
/
image_reader.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
package iso9660
import (
"fmt"
"io"
"os"
"strings"
"time"
)
// Image is a wrapper around an image file that allows reading its ISO9660 data
type Image struct {
ra io.ReaderAt
volumeDescriptors []volumeDescriptor
}
// OpenImage returns an Image reader reating from a given file
func OpenImage(ra io.ReaderAt) (*Image, error) {
i := &Image{ra: ra}
if err := i.readVolumes(); err != nil {
return nil, err
}
return i, nil
}
func (i *Image) readVolumes() error {
buffer := make([]byte, sectorSize)
// skip the 16 sectors of system area
for sector := 16; ; sector++ {
if _, err := i.ra.ReadAt(buffer, int64(sector)*int64(sectorSize)); err != nil {
return err
}
var vd volumeDescriptor
if err := vd.UnmarshalBinary(buffer); err != nil {
return err
}
i.volumeDescriptors = append(i.volumeDescriptors, vd)
if vd.Header.Type == volumeTypeTerminator {
break
}
}
return nil
}
// RootDir returns the File structure corresponding to the root directory
// of the first primary volume
func (i *Image) RootDir() (*File, error) {
for _, vd := range i.volumeDescriptors {
if vd.Type() == volumeTypePrimary {
return &File{de: vd.Primary.RootDirectoryEntry, ra: i.ra, children: nil}, nil
}
}
return nil, os.ErrNotExist
}
// File is a os.FileInfo-compatible wrapper around an ISO9660 directory entry
type File struct {
ra io.ReaderAt
de *DirectoryEntry
children []*File
}
var _ os.FileInfo = &File{}
// IsDir returns true if the entry is a directory or false otherwise
func (f *File) IsDir() bool {
return f.de.FileFlags&dirFlagDir != 0
}
// ModTime returns the entry's recording time
func (f *File) ModTime() time.Time {
return time.Time(f.de.RecordingDateTime)
}
// Mode returns os.FileMode flag set with the os.ModeDir flag enabled in case of directories
func (f *File) Mode() os.FileMode {
var mode os.FileMode
if f.IsDir() {
mode |= os.ModeDir
}
if f.de.FileFlags & 32 != 0 {
mode |= os.ModeSymlink
}
return mode
}
// Name returns the base name of the given entry
func (f *File) Name() string {
if f.IsDir() {
return f.de.Identifier
}
// drop the version part
// assume only one ';'
fileIdentifier := strings.Split(f.de.Identifier, ";")[0]
// split into filename and extension
// assume only only one '.'
splitFileIdentifier := strings.Split(fileIdentifier, ".")
// there's no dot in the name, thus no extension
if len(splitFileIdentifier) == 1 {
return splitFileIdentifier[0]
}
// extension is empty, return just the name without a dot
if len(splitFileIdentifier[1]) == 0 {
return splitFileIdentifier[0]
}
// return file with extension
return fileIdentifier
}
// Size returns the size in bytes of the extent occupied by the file or directory
func (f *File) Size() int64 {
return int64(f.de.ExtentLength)
}
// Sys returns nil
func (f *File) Sys() interface{} {
return nil
}
// GetChildren returns the chilren entries in case of a directory
// or an error in case of a file
func (f *File) GetChildren() ([]*File, error) {
if !f.IsDir() {
return nil, fmt.Errorf("%s is not a directory", f.Name())
}
if f.children != nil {
return f.children, nil
}
baseOffset := uint32(f.de.ExtentLocation) * sectorSize
buffer := make([]byte, sectorSize)
for bytesProcessed := uint32(0); bytesProcessed < uint32(f.de.ExtentLength); bytesProcessed += sectorSize {
if _, err := f.ra.ReadAt(buffer, int64(baseOffset+bytesProcessed)); err != nil {
return nil, nil
}
for i := uint32(0); i < sectorSize; {
entryLength := uint32(buffer[i])
if entryLength == 0 {
break
}
if i+entryLength > sectorSize {
return nil, fmt.Errorf("reading directory entries: DE outside of sector boundries")
}
newDE := &DirectoryEntry{}
if err := newDE.UnmarshalBinary(buffer[i : i+entryLength]); err != nil {
return nil, err
}
i += entryLength
if newDE.Identifier == string([]byte{0}) || newDE.Identifier == string([]byte{1}) {
continue
}
// use Rock Ridge extension to get full name and symbolic link flag
wrkBuf := newDE.SystemUse
for tag, buf := getTag(wrkBuf); tag != ""; tag, buf = getTag(wrkBuf) {
if tag == "PX" {
fileMode, err := UnmarshalInt32LSBMSB(buf[1:9])
if err != nil {
return nil, err
}
if fileMode & 0120000 == 0120000 {
newDE.FileFlags |= 32 // this bit a reserved, set to indicate a symlink
}
} else if tag == "NM" {
if buf[1] == 0x00 { // any set flag value means that alternative name shouldn't be used
newDE.Identifier = string(buf[2:])
}
}
wrkBuf = wrkBuf[len(buf)+3:]
}
newFile := &File{ra: f.ra,
de: newDE,
children: nil,
}
f.children = append(f.children, newFile)
}
}
return f.children, nil
}
func getTag(buf []byte) (string, []byte) {
if len(buf) < 3 {
return "", nil
}
width := int(buf[2])
if len(buf) < width {
return "", nil
}
return string(buf[:2]), buf[3:width]
}
// Reader returns a reader that allows to read the file's data.
// If File is a directory, it returns nil.
func (f *File) Reader() io.Reader {
if f.IsDir() {
return nil
}
baseOffset := int64(f.de.ExtentLocation) * int64(sectorSize)
return io.NewSectionReader(f.ra, baseOffset, int64(f.de.ExtentLength))
}