Skip to content

Commit

Permalink
Merge pull request #68 from tri-adam/create-container-fix-v1
Browse files Browse the repository at this point in the history
Return Usable FileImage from CreateContainer (v1)
  • Loading branch information
tri-adam authored Jul 20, 2021
2 parents cde6de8 + 8d1ba54 commit 31385ed
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
8 changes: 6 additions & 2 deletions internal/app/siftool/modif.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ func New(path string) error {
ID: id,
}

_, err = sif.CreateContainer(cinfo)
return err
fimg, err := sif.CreateContainer(cinfo)
if err != nil {
return err
}

return fimg.UnloadContainer()
}

// AddOptions contains the options when adding a section to a SIF file.
Expand Down
21 changes: 10 additions & 11 deletions pkg/integrity/testdata/gen_sifs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"golang.org/x/crypto/openpgp"
)

func createImage(path string, dis []sif.DescriptorInput) error {
func createImage(path string, dis []sif.DescriptorInput) (*sif.FileImage, error) {
id, err := uuid.NewV4()
if err != nil {
return err
return nil, err
}

ci := sif.CreateInfo{
Expand All @@ -31,8 +31,7 @@ func createImage(path string, dis []sif.DescriptorInput) error {
InputDescr: dis,
}

_, err = sif.CreateContainer(ci)
return err
return sif.CreateContainer(ci)
}

func getEntity() (*openpgp.Entity, error) {
Expand Down Expand Up @@ -146,18 +145,18 @@ func generateImages() error {
for _, image := range images {
path := filepath.Join("images", image.path)

if err := createImage(path, image.dis); err != nil {
return err
}

f, err := sif.LoadContainer(path, false)
f, err := createImage(path, image.dis)
if err != nil {
return err
}
defer f.UnloadContainer() // nolint:errcheck
defer func() {
if err := f.UnloadContainer(); err != nil {
log.Printf("failed to unload container: %v", err)
}
}()

if image.sign {
s, err := integrity.NewSigner(&f, image.signOpts...)
s, err := integrity.NewSigner(f, image.signOpts...)
if err != nil {
return err
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/sif/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ func writeHeader(fimg *FileImage) error {
// CreateContainer is responsible for the creation of a new SIF container
// file. It takes the creation information specification as input
// and produces an output file as specified in the input data.
//
// On success, a FileImage is returned. The caller must call UnloadContainer
// to ensure resources are released.
func CreateContainer(cinfo CreateInfo) (fimg *FileImage, err error) {
fimg = &FileImage{}
fimg.DescrArr = make([]Descriptor, DescrNumEntries)
Expand All @@ -241,7 +244,11 @@ func CreateContainer(cinfo CreateInfo) (fimg *FileImage, err error) {
if err != nil {
return nil, fmt.Errorf("container file creation failed: %s", err)
}
defer fimg.Fp.Close()
defer func() {
if err != nil {
fimg.Fp.Close()
}
}()

// set file pointer to start of data section */
if _, err = fimg.Fp.Seek(DataStartOffset, 0); err != nil {
Expand Down
14 changes: 12 additions & 2 deletions pkg/sif/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ func TestCreateContainer(t *testing.T) {
}

// test container creation without any input descriptors
if _, err := CreateContainer(cinfo); err != nil {
fimg, err := CreateContainer(cinfo)
if err != nil {
t.Error("CreateContainer(cinfo): should allow empty input descriptor list")
}

if err := fimg.UnloadContainer(); err != nil {
t.Error(err)
}

// data we need to create a definition file descriptor
definput := DescriptorInput{
Datatype: DataDeffile,
Expand Down Expand Up @@ -141,9 +146,14 @@ func TestCreateContainer(t *testing.T) {
cinfo.InputDescr = append(cinfo.InputDescr, parinput)

// test container creation with two partition input descriptors
if _, err := CreateContainer(cinfo); err != nil {
fimg, err = CreateContainer(cinfo)
if err != nil {
t.Errorf("CreateContainer(cinfo): CreateContainer(): %s", err)
}

if err := fimg.UnloadContainer(); err != nil {
t.Error(err)
}
}

func TestAddDelObject(t *testing.T) {
Expand Down

0 comments on commit 31385ed

Please sign in to comment.