Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sylabs/sif
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.14.0
Choose a base ref
...
head repository: sylabs/sif
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.14.1
Choose a head ref
  • 6 commits
  • 6 files changed
  • 2 contributors

Commits on Sep 18, 2023

  1. Verified

    This commit was signed with the committer’s verified signature.
    tri-adam Adam Hughes
    Copy the full SHA
    d2f7780 View commit details
  2. Merge pull request #326 from tri-adam/typo-fix

    docs: fix typo in SetMetadata
    tri-adam authored Sep 18, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    c423a2c View commit details

Commits on Sep 19, 2023

  1. fix: delete object

    wobito committed Sep 19, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    wobito Adrian Wobito
    Copy the full SHA
    87e47a2 View commit details
  2. Merge pull request #327 from wobito/fix-delete-object

    fix: delete object
    tri-adam authored Sep 19, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e1f38f0 View commit details
  3. Verified

    This commit was signed with the committer’s verified signature.
    tri-adam Adam Hughes
    Copy the full SHA
    59cf700 View commit details

Commits on Sep 20, 2023

  1. Merge pull request #328 from tri-adam/refactor-zero

    refactor: simplify zero implementation
    tri-adam authored Sep 20, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    cac8e77 View commit details
85 changes: 31 additions & 54 deletions pkg/sif/create.go
Original file line number Diff line number Diff line change
@@ -322,51 +322,6 @@ func CreateContainerAtPath(path string, opts ...CreateOpt) (*FileImage, error) {
return f, nil
}

func zeroData(fimg *FileImage, descr *rawDescriptor) error {
// first, move to data object offset
if _, err := fimg.rw.Seek(descr.Offset, io.SeekStart); err != nil {
return err
}

var zero [4096]byte
n := descr.Size
upbound := int64(4096)
for {
if n < 4096 {
upbound = n
}

if _, err := fimg.rw.Write(zero[:upbound]); err != nil {
return err
}
n -= 4096
if n <= 0 {
break
}
}

return nil
}

func resetDescriptor(fimg *FileImage, index int) error {
// If we remove the primary partition, set the global header Arch field to HdrArchUnknown
// to indicate that the SIF file doesn't include a primary partition and no dependency
// on any architecture exists.
if fimg.rds[index].isPartitionOfType(PartPrimSys) {
fimg.h.Arch = hdrArchUnknown
}

offset := fimg.h.DescriptorsOffset + int64(index)*int64(binary.Size(fimg.rds[0]))

// first, move to descriptor offset
if _, err := fimg.rw.Seek(offset, io.SeekStart); err != nil {
return err
}

var emptyDesc rawDescriptor
return binary.Write(fimg.rw, binary.LittleEndian, emptyDesc)
}

// addOpts accumulates object add options.
type addOpts struct {
t time.Time
@@ -448,6 +403,26 @@ func (f *FileImage) isLast(d *rawDescriptor) bool {
return isLast
}

// zeroReader is an io.Reader that returns a stream of zero-bytes.
type zeroReader struct{}

func (zeroReader) Read(b []byte) (int, error) {
for i := range b {
b[i] = 0
}
return len(b), nil
}

// zero overwrites the data object described by d with a stream of zero bytes.
func (f *FileImage) zero(d *rawDescriptor) error {
if _, err := f.rw.Seek(d.Offset, io.SeekStart); err != nil {
return err
}

_, err := io.CopyN(f.rw, zeroReader{}, d.Size)
return err
}

// truncateAt truncates f at the start of the padded data object described by d.
func (f *FileImage) truncateAt(d *rawDescriptor) error {
start := d.Offset + d.Size - d.SizeWithPadding
@@ -531,7 +506,7 @@ func (f *FileImage) DeleteObject(id uint32, opts ...DeleteOpt) error {
}

if do.zero {
if err := zeroData(f, d); err != nil {
if err := f.zero(d); err != nil {
return fmt.Errorf("%w", err)
}
}
@@ -547,15 +522,17 @@ func (f *FileImage) DeleteObject(id uint32, opts ...DeleteOpt) error {
f.h.DescriptorsFree++
f.h.ModifiedAt = do.t.Unix()

index := 0
for i, od := range f.rds {
if od.ID == id {
index = i
break
}
// If we remove the primary partition, set the global header Arch field to HdrArchUnknown
// to indicate that the SIF file doesn't include a primary partition and no dependency
// on any architecture exists.
if d.isPartitionOfType(PartPrimSys) {
f.h.Arch = hdrArchUnknown
}

if err := resetDescriptor(f, index); err != nil {
// Reset rawDescripter with empty struct
*d = rawDescriptor{}

if err := f.writeDescriptors(); err != nil {
return fmt.Errorf("%w", err)
}

@@ -678,7 +655,7 @@ func (f *FileImage) SetPrimPart(id uint32, opts ...SetOpt) error {
return nil
}

// SetMetadata the metadata of the data object with id to md, according to opts.
// SetMetadata sets the metadata of the data object with id to md, according to opts.
//
// By default, the image/object modification times are set to the current time for
// non-deterministic images, and unset otherwise. To override this, consider using
63 changes: 63 additions & 0 deletions pkg/sif/create_test.go
Original file line number Diff line number Diff line change
@@ -548,6 +548,69 @@ func TestDeleteObject(t *testing.T) {
}
}

func TestDeleteObjectAndAddObject(t *testing.T) {
tests := []struct {
name string
id uint32
opts []DeleteOpt
}{
{
name: "Compact",
id: 2,
opts: []DeleteOpt{
OptDeleteCompact(true),
},
},
{
name: "NoCompact",
id: 2,
},
{
name: "Zero",
id: 2,
opts: []DeleteOpt{
OptDeleteZero(true),
},
},
{
name: "ZeroCompact",
id: 2,
opts: []DeleteOpt{
OptDeleteZero(true),
OptDeleteCompact(true),
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var b Buffer

f, err := CreateContainer(&b,
OptCreateDeterministic(),
OptCreateWithDescriptors(
getDescriptorInput(t, DataGeneric, []byte("abc")),
getDescriptorInput(t, DataGeneric, []byte("def")),
),
)
if err != nil {
t.Fatal(err)
}

if err := f.DeleteObject(tt.id, tt.opts...); err != nil {
t.Fatal(err)
}

if err := f.AddObject(getDescriptorInput(t, DataGeneric, []byte("ghi"))); err != nil {
t.Fatal(err)
}

g := goldie.New(t, goldie.WithTestNameForDir(true))
g.Assert(t, tt.name, b.Bytes())
})
}
}

func TestSetPrimPart(t *testing.T) {
tests := []struct {
name string
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.