forked from kdomanski/iso9660
-
Notifications
You must be signed in to change notification settings - Fork 3
/
image_writer.go
544 lines (465 loc) · 13.9 KB
/
image_writer.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
package iso9660
import (
"container/list"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"os"
"path"
"runtime"
"sort"
"strings"
"time"
)
const (
primaryVolumeDirectoryIdentifierMaxLength = 31 // ECMA-119 7.6.3
primaryVolumeFileIdentifierMaxLength = 30 // ECMA-119 7.5
)
var (
// ErrFileTooLarge is returned when trying to process a file of size greater
// than 4GB, which due to the 32-bit address limitation is not possible
// except with ISO 9660-Level 3
ErrFileTooLarge = errors.New("file is exceeding the maximum file size of 4GB")
ErrIsDir = errors.New("is a directory")
)
// ImageWriter is responsible for staging an image's contents
// and writing them to an image.
type ImageWriter struct {
Primary *PrimaryVolumeDescriptorBody
Catalog string // Catalog is the path of the boot catalog on disk. Defaults to "BOOT.CAT"
root *itemDir
vd []*volumeDescriptor
boot []*BootCatalogEntry // boot entries
}
// NewWriter creates a new ImageWrite.
func NewWriter() (*ImageWriter, error) {
now := time.Now()
Primary := &PrimaryVolumeDescriptorBody{
SystemIdentifier: runtime.GOOS,
VolumeIdentifier: "UNNAMED",
VolumeSpaceSize: 0, // this will be calculated upon finalization of disk
VolumeSetSize: 1,
VolumeSequenceNumber: 1,
LogicalBlockSize: int16(sectorSize),
PathTableSize: 0,
TypeLPathTableLoc: 0,
OptTypeLPathTableLoc: 0,
TypeMPathTableLoc: 0,
OptTypeMPathTableLoc: 0,
RootDirectoryEntry: nil, // this will be calculated upon finalization of disk
VolumeSetIdentifier: "",
PublisherIdentifier: "",
DataPreparerIdentifier: "",
ApplicationIdentifier: "github.com/KarpelesLab/iso9660",
CopyrightFileIdentifier: "",
AbstractFileIdentifier: "",
BibliographicFileIdentifier: "",
VolumeCreationDateAndTime: VolumeDescriptorTimestampFromTime(now),
VolumeModificationDateAndTime: VolumeDescriptorTimestampFromTime(now),
VolumeExpirationDateAndTime: VolumeDescriptorTimestamp{},
VolumeEffectiveDateAndTime: VolumeDescriptorTimestampFromTime(now),
FileStructureVersion: 1,
ApplicationUsed: [512]byte{},
}
return &ImageWriter{
root: newDir(),
Primary: Primary,
Catalog: "BOOT.CAT",
vd: []*volumeDescriptor{
{
Header: volumeDescriptorHeader{
Type: volumeTypePrimary,
Identifier: standardIdentifierBytes,
Version: 1,
},
Primary: Primary,
},
},
}, nil
}
// AddBootEntry adds a El Torito boot entry to the image.
// Typical usage (BootCatalogEntry defaults to X86 with no emulation)
//
// err = AddBootEntry(&BootCatalogEntry{BootInfoTable: true}, NewItemFile("syslinux/isolinux.bin"), "isolinux/isolinux.bin")
func (iw *ImageWriter) AddBootEntry(boot *BootCatalogEntry, data Item, filePath string) error {
directoryPath, fileName := manglePath(filePath)
pos, err := iw.getDir(directoryPath)
if err != nil {
return err
}
if _, ok := pos.children[fileName]; ok {
// duplicate
return os.ErrExist
}
item, err := NewItemReader(data)
if err != nil {
return err
}
if boot.BootInfoTable {
// we need to be able to modify this file, grab it and store it into memory
item, err = bufferizeItem(item)
if err != nil {
return err
}
}
dirPath := path.Join(directoryPath, fileName)
item.meta().dirPath = dirPath
pos.children[fileName] = item
boot.file = item
// add boot record
iw.boot = append(iw.boot, boot)
return nil
}
func (iw *ImageWriter) getDir(directoryPath string) (*itemDir, error) {
dp := strings.Split(directoryPath, "/")
pos := iw.root
for _, seg := range dp {
if seg == "" {
continue
}
if v, ok := pos.children[seg]; ok {
if rV, ok := v.(*itemDir); ok {
pos = rV
continue
}
// trying to create a directory on top of a file → problem
return nil, ErrIsDir
}
// not found → add
n := newDir()
pos.children[seg] = n
pos = n
}
return pos, nil
}
// AddFile adds a file to the ImageWriter.
// All path components are mangled to match basic ISO9660 filename requirements.
func (iw *ImageWriter) AddFile(data io.Reader, filePath string) error {
directoryPath, fileName := manglePath(filePath)
pos, err := iw.getDir(directoryPath)
if err != nil {
return err
}
if _, ok := pos.children[fileName]; ok {
// duplicate
return os.ErrExist
}
item, err := NewItemReader(data)
if err != nil {
return err
}
dirPath := path.Join(directoryPath, fileName)
item.meta().dirPath = dirPath
pos.children[fileName] = item
return nil
}
// AddLocalFile adds a file to the ImageWriter from the local filesystem.
// localPath must be an existing and readable file, and filePath will be the path
// on the ISO image.
func (iw *ImageWriter) AddLocalFile(localPath, filePath string) error {
buf, err := NewItemFile(localPath)
if err != nil {
return fmt.Errorf("unable to add local file: %w", err)
}
return iw.AddFile(buf, filePath)
}
func recursiveDirSectorCount(dir *itemDir) uint32 {
// count sectors required for everything in a given dir (typically root)
sec := dir.sectors() // own data space
for _, sub := range dir.children {
switch v := sub.(type) {
case *itemDir:
sec += recursiveDirSectorCount(v)
default:
sec += v.sectors()
}
}
return sec
}
type writeContext struct {
iw *ImageWriter
w io.Writer
timestamp RecordingTimestamp
freeSectorPointer uint32
itemsToWrite *list.List // simple fifo used during
items []Item // items in the right order for final write
writeSecPos uint32
emptySector []byte // a sector-sized buffer of zeroes
}
// allocSectors will allocate a number of sectors and return the first free position
func (wc *writeContext) allocSectors(it Item) uint32 {
res := wc.freeSectorPointer
wc.freeSectorPointer += it.sectors()
wc.items = append(wc.items, it) // items are stored in allocated order
it.meta().targetSector = res
return res
}
func (wc *writeContext) createDEForRoot() (*DirectoryEntry, error) {
extentLengthInSectors := wc.iw.root.sectors()
extentLocation := wc.allocSectors(wc.iw.root)
de := &DirectoryEntry{
ExtendedAtributeRecordLength: 0,
ExtentLocation: int32(extentLocation),
ExtentLength: int32(extentLengthInSectors * sectorSize),
RecordingDateTime: wc.timestamp,
FileFlags: dirFlagDir,
FileUnitSize: 0, // 0 for non-interleaved write
InterleaveGap: 0, // not interleaved
VolumeSequenceNumber: 1, // we only have one volume
Identifier: string([]byte{0}),
SystemUse: []byte{},
}
return de, nil
}
func (wc *writeContext) processDirectory(dir *itemDir, ownEntry *DirectoryEntry, parentEntry *DirectoryEntry, targetSector uint32) error {
buf := dir.buf
bufPos := 0
currentDE := ownEntry.Clone()
currentDE.Identifier = string([]byte{0})
parentDE := parentEntry.Clone()
parentDE.Identifier = string([]byte{1})
currentDEData, err := currentDE.MarshalBinary()
if err != nil {
return err
}
parentDEData, err := parentDE.MarshalBinary()
if err != nil {
return err
}
n, err := buf.Write(currentDEData)
if err != nil {
return err
}
bufPos += n
n, err = buf.Write(parentDEData)
if err != nil {
return err
}
bufPos += n
// here we need to proceed in alphabetical order so tests aren't broken
names := make([]string, 0, len(dir.children))
for name := range dir.children {
names = append(names, name)
}
sort.Slice(names, func(i, j int) bool { return names[i] < names[j] })
for _, name := range names {
c := dir.children[name]
var (
fileFlags byte
extentLength uint32
)
var de *DirectoryEntry
if c.Size() > int64(math.MaxUint32) {
return ErrFileTooLarge
}
extentLength = uint32(c.Size())
if _, ok := c.(*itemDir); ok {
// this is a directory
fileFlags = dirFlagDir
} else {
de = c.meta().ownEntry // grab de in case file was already included in disk
fileFlags = 0
}
if de == nil {
extentLocation := wc.allocSectors(c)
de = &DirectoryEntry{
ExtendedAtributeRecordLength: 0,
ExtentLocation: int32(extentLocation),
ExtentLength: int32(extentLength),
RecordingDateTime: wc.timestamp,
FileFlags: fileFlags,
FileUnitSize: 0, // 0 for non-interleaved write
InterleaveGap: 0, // not interleaved
VolumeSequenceNumber: 1, // we only have one volume
Identifier: name,
SystemUse: []byte{},
}
c.meta().set(de, ownEntry)
// queue this child for processing if directory
if fileFlags == dirFlagDir {
wc.itemsToWrite.PushBack(c)
}
}
data, err := de.MarshalBinary()
if err != nil {
return err
}
if uint32(bufPos+len(data)) > sectorSize {
// unless we reached the exact end of the sector
if uint32(bufPos) < sectorSize {
// need to add some bytes
buf.Write(wc.emptySector[:sectorSize-uint32(bufPos)])
}
bufPos = 0
}
_, err = buf.Write(data)
if err != nil {
return err
}
}
return nil
}
func (wc *writeContext) processAll() error {
// Generate disk header
rootDE, err := wc.createDEForRoot()
if err != nil {
return fmt.Errorf("creating root directory descriptor: %s", err)
}
// store rootDE pointer in primary
wc.iw.Primary.RootDirectoryEntry = rootDE
wc.iw.root.meta().set(rootDE, rootDE)
// Write disk data
wc.itemsToWrite.PushBack(wc.iw.root)
for item := wc.itemsToWrite.Front(); wc.itemsToWrite.Len() > 0; item = wc.itemsToWrite.Front() {
it := item.Value.(Item)
var err error
if cV, ok := it.(*itemDir); ok {
err = wc.processDirectory(cV, it.meta().ownEntry, it.meta().parentEntry, it.meta().targetSector)
if err != nil {
return fmt.Errorf("processing %s: %s", it.meta().dirPath, err)
}
}
wc.itemsToWrite.Remove(item)
}
return nil
}
// writeSector writes one or more sector(s) to the stream, checking the passed
// position is correct. If buffer is not rounded to a sector position, extra
// zeroes will be written to disk.
func (wc *writeContext) writeSector(buffer []byte, sector uint32) error {
// ensure our position in the stream is correct
if sector != wc.writeSecPos {
// invalid location
return errors.New("invalid write: sector position is not valid")
}
_, err := wc.w.Write(buffer)
if err != nil {
return err
}
secCnt := uint32(len(buffer)) / sectorSize
if secBytes := uint32(len(buffer)) % sectorSize; secBytes != 0 {
secCnt += 1
// add zeroes using wc.emptySector (which is a sector-sized buffer of zeroes)
extra := sectorSize - secBytes
wc.w.Write(wc.emptySector[:extra])
}
wc.writeSecPos += secCnt
return nil
}
// writeSectorBuf will copy the given buffer to the image, after checking its
// position is accurate.
func (wc *writeContext) writeSectorBuf(buf Item) error {
if buf.meta().targetSector != wc.writeSecPos {
// invalid location
return errors.New("invalid write: sector position is not valid")
}
n, err := io.Copy(wc.w, buf)
if err != nil {
return err
}
secCnt := uint32(n) / sectorSize
if secBytes := uint32(n) % sectorSize; secBytes != 0 {
secCnt += 1
// add zeroes using wc.emptySector (which is a sector-sized buffer of zeroes)
extra := sectorSize - secBytes
wc.w.Write(wc.emptySector[:extra])
}
wc.writeSecPos += secCnt
return nil
}
func (wc *writeContext) writeDescriptor(pvd *volumeDescriptor, sector uint32) error {
if buffer, err := pvd.MarshalBinary(); err != nil {
return err
} else {
return wc.writeSector(buffer, sector)
}
}
func (iw *ImageWriter) WriteTo(w io.Writer) error {
vd := iw.vd
var (
err error
// variables used for boot
boot *BootVolumeDescriptorBody
bootCat []byte
bootCatInfo Item
)
if len(iw.boot) > 0 {
// we need a boot catalog, store info
boot = &BootVolumeDescriptorBody{
BootSystemIdentifier: "EL TORITO SPECIFICATION",
}
bootCat = make([]byte, 2048)
bootCatInfo = &bufferHndlr{d: bootCat}
// add boot catalog
err = iw.AddFile(bootCatInfo, iw.Catalog)
if err != nil {
return err
}
vd = append(vd, &volumeDescriptor{
Header: volumeDescriptorHeader{
Type: volumeTypeBoot,
Identifier: standardIdentifierBytes,
Version: 1,
},
Boot: boot,
})
}
// generate vd list with terminator
vd = append(vd, &volumeDescriptor{
Header: volumeDescriptorHeader{
Type: volumeTypeTerminator,
Identifier: standardIdentifierBytes,
Version: 1,
},
})
wc := writeContext{
iw: iw,
w: w,
timestamp: RecordingTimestamp{},
freeSectorPointer: uint32(16 + len(vd)), // system area (16) + descriptors
itemsToWrite: list.New(),
writeSecPos: 0,
emptySector: make([]byte, sectorSize),
}
// configure volume space size
iw.Primary.VolumeSpaceSize = int32(16 + uint32(len(vd)) + recursiveDirSectorCount(iw.root))
// processAll() will prepare the data to be written, including offsets, etc.
if err = wc.processAll(); err != nil {
return fmt.Errorf("writing files: %s", err)
}
if len(iw.boot) > 0 {
// we have a boot catalog to make!
// First, grab the location of boot catalog and store in boot record
binary.LittleEndian.PutUint32(boot.BootSystemUse[:4], bootCatInfo.meta().targetSector)
// generate catalog
data, err := encodeBootCatalogs(iw.boot)
if err != nil {
return err
}
// overwrite bootCat with data so it will be written to disk
copy(bootCat, data)
}
// write 16 sectors of zeroes
for i := uint32(0); i < 16; i++ {
if err = wc.writeSector(wc.emptySector, i); err != nil {
return err
}
}
// write volume descriptors
for i, pvd := range vd {
if err = wc.writeDescriptor(pvd, uint32(16+i)); err != nil {
return err
}
}
// this actually writes the data to the disk
for _, buf := range wc.items {
err = wc.writeSectorBuf(buf)
if err != nil {
return err
}
buf.Close()
}
return nil
}