summaryrefslogtreecommitdiff
path: root/internal/app/siftool/file.go
blob: 1722a1f9a6e4723b917d7feab8d0081026c4b75a (plain)
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
// Copyright (c) 2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.

package siftool

import (
	"os"

	"github.com/sylabs/sif/v2/pkg/sif"
)

// withFileImage calls fn with a FileImage loaded from path.
func withFileImage(path string, writable bool, fn func(*sif.FileImage) error) (err error) {
	flag := os.O_RDONLY
	if writable {
		flag = os.O_RDWR
	}

	f, err := sif.LoadContainerFromPath(path, sif.OptLoadWithFlag(flag))
	if err != nil {
		return err
	}
	defer func() {
		if uerr := f.UnloadContainer(); uerr != nil && err == nil {
			err = uerr
		}
	}()

	return fn(f)
}