Skip to content

Commit

Permalink
refactor: improve compaction logic for AddObject
Browse files Browse the repository at this point in the history
Previously, an add operation would simply append the data object to the
end of the data section. If one or more object(s) had previously been
deleted from the image, this could result in wasted space. Now, we trim
the data section based on in-use objects before adding the new object.
Update tests to reflect this new behaviour.
  • Loading branch information
tri-adam committed Jul 8, 2024
1 parent e3aa617 commit 8783e3b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion pkg/sif/create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2024, Sylabs Inc. All rights reserved.
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
// Copyright (c) 2017, Yannick Cote <[email protected]> All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
Expand Down Expand Up @@ -55,6 +55,20 @@ func writeDataObjectAt(ws io.WriteSeeker, offsetUnaligned int64, di DescriptorIn
return nil
}

// calculatedDataSize calculates the size of the data section based on the in-use descriptors.
func (f *FileImage) calculatedDataSize() int64 {
dataEnd := f.DataOffset()

f.WithDescriptors(func(d Descriptor) bool {
if objectEnd := d.Offset() + d.Size(); dataEnd < objectEnd {
dataEnd = objectEnd
}
return false
})

return dataEnd - f.DataOffset()
}

var (
errInsufficientCapacity = errors.New("insufficient descriptor capacity to add data object(s) to image")
errPrimaryPartition = errors.New("image already contains a primary partition")
Expand All @@ -80,6 +94,8 @@ func (f *FileImage) writeDataObject(i int, di DescriptorInput, t time.Time) erro
d := &f.rds[i]
d.ID = uint32(i) + 1

f.h.DataSize = f.calculatedDataSize()

if err := writeDataObjectAt(f.rw, f.h.DataOffset+f.h.DataSize, di, t, d); err != nil {
return err
}
Expand Down
Binary file modified pkg/sif/testdata/TestDeleteObjectAndAddObject/NoCompact.golden
Binary file not shown.
Binary file modified pkg/sif/testdata/TestDeleteObjectAndAddObject/Zero.golden
Binary file not shown.

0 comments on commit 8783e3b

Please sign in to comment.