Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion filesystem/iso9660/directoryentry.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func (de *directoryEntry) statT() *StatT {
s.UID = e.uid
s.GID = e.gid
s.NLink = e.linkCount
s.Inode = uint32(e.serial)
s.Inode = e.serial
case rockRidgeSymlink:
s.RockRidge = true
if !e.continued {
Expand Down
124 changes: 109 additions & 15 deletions filesystem/iso9660/finalize.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package iso9660

import (
"bytes"
"fmt"
"io"
"io/fs"
Expand All @@ -9,6 +10,7 @@ import (
"path"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -100,7 +102,7 @@ type finalizeFileInfo struct {
// content in memory content of file. If this is anything other than nil, including a zero-length slice,
// then this content is used, rather than anything on disk.
content []byte
serial uint64
serial uint32
}

func finalizeFileInfoFromFile(p, fullPath string, fi fs.FileInfo) (*finalizeFileInfo, error) {
Expand Down Expand Up @@ -393,7 +395,14 @@ func (fi *finalizeFileInfo) toJolietDirectory(fsm *FileSystem, jolietLocation, p
parent.location = parentJolietLocation

entries := []*directoryEntry{self, parent}
for _, child := range fi.children {
// Joliet identifiers use the original names, which can sort differently
// from the shortened ISO 9660 identifiers in fi.children. Compare the
// bytes that are written, since UCS-2 order differs from string order.
children := append([]*finalizeFileInfo(nil), fi.children...)
sort.Slice(children, func(i, j int) bool {
return bytes.Compare(ucs2StringToBytes(jolietFilename(children[i])), ucs2StringToBytes(jolietFilename(children[j]))) < 0
})
for _, child := range children {
de := child.toJolietDirectoryEntry(fsm, false, false)
entries = append(entries, de)
}
Expand Down Expand Up @@ -697,20 +706,49 @@ func (fsm *FileSystem) Finalize(options FinalizeOptions) error {
accessTime: now,
changeTime: now,
}
// make it the first file
files = append([]*finalizeFileInfo{catEntry}, files...)

// if we were not told to hide the catalog, add it to its parent
if !options.ElTorito.HideBootCatalog {
var parent *finalizeFileInfo
parent, err = root.findEntry(path.Dir(catname))
if err != nil {
return fmt.Errorf("error finding parent for boot catalog %s: %v", catname, err)
parent, err := root.findEntry(path.Dir(catname))
if err != nil {
return fmt.Errorf("error finding parent for boot catalog %s: %v", catname, err)
}
if parent == nil && !options.ElTorito.HideBootCatalog {
return fmt.Errorf("unable to find parent directory for boot catalog %s", catname)
}
var existing *finalizeFileInfo
if parent != nil {
for _, child := range parent.children {
if child.name == catEntry.name {
existing = child
break
}
}
if parent == nil {
return fmt.Errorf("unable to find parent directory for boot catalog %s", catname)
}
// trueChild marks the placeholder of a relocated directory
if existing != nil && (existing.IsDir() || existing.trueChild != nil || existing.mode&os.ModeSymlink != 0) {
if !options.ElTorito.HideBootCatalog {
return fmt.Errorf("boot catalog %s collides with a directory or symlink", catname)
}
// a hidden catalog has no directory record, so nothing collides
existing = nil
}
if existing != nil {
if options.ElTorito.HideBootCatalog {
parent.children = slices.DeleteFunc(parent.children, func(child *finalizeFileInfo) bool { return child == existing })
files = slices.DeleteFunc(files, func(file *finalizeFileInfo) bool { return file == existing })
} else {
catEntry = existing
}
}
catEntry.content = bootcat
catEntry.size = catSize
catEntry.blocks = calculateBlocks(catSize, fsm.blocksize)
if existing == nil || options.ElTorito.HideBootCatalog {
files = append([]*finalizeFileInfo{catEntry}, files...)
if !options.ElTorito.HideBootCatalog {
if err := giveUniqueIdentifier(catEntry, parent.children); err != nil {
return fmt.Errorf("unable to name boot catalog %s: %w", catname, err)
}
parent.addChild(catEntry)
}
parent.addChild(catEntry)
}
for _, e := range options.ElTorito.Entries {
var parent, child *finalizeFileInfo
Expand All @@ -733,11 +771,21 @@ func (fsm *FileSystem) Finalize(options FinalizeOptions) error {
if child == nil {
return fmt.Errorf("unable to find image child %s: %v", e.BootFile, err)
}
if child == catEntry {
return fmt.Errorf("boot image file %s is also the boot catalog", e.BootFile)
}
// save the child so we can add location late
e.size = uint32(child.size)
child.elToritoEntry = e
}
}
// Directory records must be ordered by their ISO 9660 file identifiers.
// Do this before calculating directory sizes, since record order affects padding.
for _, dir := range dirs {
sort.Slice(dir.children, func(i, j int) bool {
return lessISOIdentifier(dir.children[i], dir.children[j])
})
}

var size, ceBlocks int
for _, dir := range dirs {
Expand Down Expand Up @@ -1250,7 +1298,7 @@ func walkTree(workspace string) ([]*finalizeFileInfo, map[string]*finalizeFileIn
fileList = make([]*finalizeFileInfo, 0)
collisionGroups = make(map[string]*collisionGroup)
entry *finalizeFileInfo
serial uint64
serial uint32
)
err := filepath.WalkDir(workspace, func(actualPath string, d fs.DirEntry, err error) error {
if err != nil {
Expand Down Expand Up @@ -1410,6 +1458,52 @@ func resolveCollisionGroup(group *collisionGroup) error {
return fmt.Errorf("too many filename collisions for: %s", name)
}

// lessISOIdentifier follows ECMA-119's directory order: file name first,
// then extension. Comparing the whole identifier puts A.1;1 before A.;1.
func lessISOIdentifier(a, b *finalizeFileInfo) bool {
Comment thread
deitch marked this conversation as resolved.
Resolved
if a.shortname != b.shortname {
return a.shortname < b.shortname
}
if a.extension != b.extension {
return a.extension < b.extension
}
// The name and the extension can be equal for two different identifiers:
// a directory FOO and a file FOO.;1, which has no extension.
// walkTree prevents this pair in a source directory. But Rock Ridge
// relocation moves a deep directory into the root and does no such check.
// ECMA-119 does not give an order for this pair. The directory goes
// first, as xorriso and genisoimage write it.
return a.Name() < b.Name()
}

// giveUniqueIdentifier keeps an unrelated source file intact when the
// generated catalog's ISO 9660 identifier would otherwise collide with it.
func giveUniqueIdentifier(entry *finalizeFileInfo, siblings []*finalizeFileInfo) error {
used := make(map[string]bool, len(siblings))
for _, sibling := range siblings {
used[sibling.Name()] = true
}
if !used[entry.Name()] {
return nil
}
base := entry.shortname
limit := 1
for digits := 1; digits < 8; digits++ {
limit *= 10
prefix := base
if len(prefix) > 8-digits {
prefix = prefix[:8-digits]
}
for n := 0; n < limit; n++ {
entry.shortname = fmt.Sprintf("%s%0*d", prefix, digits, n)
if !used[entry.Name()] {
return nil
}
}
}
return fmt.Errorf("too many ISO 9660 identifier collisions for %s", entry.name)
}

func calculateBlocks(size, blocksize int64) uint32 {
blocks := uint32(size / blocksize)
// add one for partial
Expand Down
Loading
Loading