diff --git a/image.go b/image.go index 1a48f5b74e..9fb34ab37f 100644 --- a/image.go +++ b/image.go @@ -17,6 +17,7 @@ import ( "github.com/containers/buildah/define" "github.com/containers/buildah/docker" "github.com/containers/buildah/internal/mkcw" + "github.com/containers/buildah/internal/tmpdir" "github.com/containers/image/v5/docker/reference" "github.com/containers/image/v5/image" "github.com/containers/image/v5/manifest" @@ -374,7 +375,7 @@ func (i *containerImageRef) NewImageSource(ctx context.Context, sc *types.System logrus.Debugf("layer list: %q", layers) // Make a temporary directory to hold blobs. - path, err := os.MkdirTemp(os.TempDir(), define.Package) + path, err := os.MkdirTemp(tmpdir.GetTempDir(), define.Package) if err != nil { return nil, fmt.Errorf("creating temporary directory to hold layer blobs: %w", err) } diff --git a/internal/tmpdir/tmpdir_test.go b/internal/tmpdir/tmpdir_test.go index 3da9048afd..ea7d673fae 100644 --- a/internal/tmpdir/tmpdir_test.go +++ b/internal/tmpdir/tmpdir_test.go @@ -2,6 +2,7 @@ package tmpdir import ( "os" + "path/filepath" "testing" "github.com/containers/common/pkg/config" @@ -26,6 +27,14 @@ func TestGetTempDir(t *testing.T) { err = os.Unsetenv("TMPDIR") require.NoError(t, err) + // relative TMPDIR should be automatically converted to absolute + err = os.Setenv("TMPDIR", ".") + require.NoError(t, err) + tmpdir = GetTempDir() + assert.True(t, filepath.IsAbs(tmpdir), "path from GetTempDir should always be absolute") + err = os.Unsetenv("TMPDIR") + require.NoError(t, err) + f, err := os.CreateTemp("", "containers.conf-") require.NoError(t, err) // close and remove the temporary file at the end of the program diff --git a/pkg/sshagent/sshagent.go b/pkg/sshagent/sshagent.go index 712e10fed3..ec28482b0f 100644 --- a/pkg/sshagent/sshagent.go +++ b/pkg/sshagent/sshagent.go @@ -11,6 +11,7 @@ import ( "sync" "time" + "github.com/containers/buildah/internal/tmpdir" "github.com/opencontainers/selinux/go-selinux" "github.com/sirupsen/logrus" "golang.org/x/crypto/ssh" @@ -79,7 +80,7 @@ func (a *AgentServer) Serve(processLabel string) (string, error) { if err != nil { return "", err } - serveDir, err := os.MkdirTemp("", ".buildah-ssh-sock") + serveDir, err := os.MkdirTemp(tmpdir.GetTempDir(), ".buildah-ssh-sock") if err != nil { return "", err } diff --git a/run_freebsd.go b/run_freebsd.go index ad21e6db7b..ff70022072 100644 --- a/run_freebsd.go +++ b/run_freebsd.go @@ -16,6 +16,7 @@ import ( "github.com/containers/buildah/copier" "github.com/containers/buildah/define" "github.com/containers/buildah/internal" + "github.com/containers/buildah/internal/tmpdir" "github.com/containers/buildah/pkg/jail" "github.com/containers/buildah/pkg/overlay" "github.com/containers/buildah/pkg/parse" @@ -72,7 +73,7 @@ func setChildProcess() error { } func (b *Builder) Run(command []string, options RunOptions) error { - p, err := os.MkdirTemp("", Package) + p, err := os.MkdirTemp(tmpdir.GetTempDir(), define.Package) if err != nil { return err } diff --git a/run_linux.go b/run_linux.go index 2d9ba51d17..586b706daf 100644 --- a/run_linux.go +++ b/run_linux.go @@ -19,6 +19,7 @@ import ( "github.com/containers/buildah/copier" "github.com/containers/buildah/define" "github.com/containers/buildah/internal" + "github.com/containers/buildah/internal/tmpdir" "github.com/containers/buildah/internal/volumes" "github.com/containers/buildah/pkg/overlay" "github.com/containers/buildah/pkg/parse" @@ -71,7 +72,7 @@ func setChildProcess() error { // Run runs the specified command in the container's root filesystem. func (b *Builder) Run(command []string, options RunOptions) error { - p, err := os.MkdirTemp("", define.Package) + p, err := os.MkdirTemp(tmpdir.GetTempDir(), define.Package) if err != nil { return err } @@ -499,7 +500,7 @@ func setupSlirp4netnsNetwork(config *config.Config, netns, cid string, options [ Mask: res.Subnet.Mask, }} netStatus := map[string]nettypes.StatusBlock{ - slirp4netns.BinaryName: nettypes.StatusBlock{ + slirp4netns.BinaryName: { Interfaces: map[string]nettypes.NetInterface{ "tap0": { Subnets: []nettypes.NetAddress{{IPNet: subnet}}, @@ -541,7 +542,7 @@ func setupPasta(config *config.Config, netns string, options []string) (func(), Mask: net.IPv4Mask(255, 255, 255, 0), }} netStatus := map[string]nettypes.StatusBlock{ - slirp4netns.BinaryName: nettypes.StatusBlock{ + slirp4netns.BinaryName: { Interfaces: map[string]nettypes.NetInterface{ "tap0": { Subnets: []nettypes.NetAddress{{IPNet: subnet}},