Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make temporary names container/image specific #2045

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker/internal/tarfile/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewReaderFromFile(sys *types.SystemContext, path string) (*Reader, error) {
// The caller should call .Close() on the returned archive when done.
func NewReaderFromStream(sys *types.SystemContext, inputStream io.Reader) (*Reader, error) {
// Save inputStream to a temporary file
tarCopyFile, err := os.CreateTemp(tmpdir.TemporaryDirectoryForBigFiles(sys), "docker-tar")
tarCopyFile, err := tmpdir.CreateBigFileTemp(sys, "docker-tar")
if err != nil {
return nil, fmt.Errorf("creating temporary file: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/streamdigest/stream_digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// It is the caller's responsibility to call the cleanup function, which closes and removes the temporary file.
// If an error occurs, inputInfo is not modified.
func ComputeBlobInfo(sys *types.SystemContext, stream io.Reader, inputInfo *types.BlobInfo) (io.Reader, func(), error) {
diskBlob, err := os.CreateTemp(tmpdir.TemporaryDirectoryForBigFiles(sys), "stream-blob")
diskBlob, err := tmpdir.CreateBigFileTemp(sys, "stream-blob")
if err != nil {
return nil, nil, fmt.Errorf("creating temporary on-disk layer: %w", err)
}
Expand Down
12 changes: 11 additions & 1 deletion internal/tmpdir/tmpdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ var unixTempDirForBigFiles = builtinUnixTempDirForBigFiles
// DO NOT change this, instead see unixTempDirForBigFiles above.
const builtinUnixTempDirForBigFiles = "/var/tmp"

const prefix = "container_images_"

// TemporaryDirectoryForBigFiles returns a directory for temporary (big) files.
// On non Windows systems it avoids the use of os.TempDir(), because the default temporary directory usually falls under /tmp
// which on systemd based systems could be the unsuitable tmpfs filesystem.
func TemporaryDirectoryForBigFiles(sys *types.SystemContext) string {
func temporaryDirectoryForBigFiles(sys *types.SystemContext) string {
if sys != nil && sys.BigFilesTemporaryDir != "" {
return sys.BigFilesTemporaryDir
}
Expand All @@ -32,3 +34,11 @@ func TemporaryDirectoryForBigFiles(sys *types.SystemContext) string {
}
return temporaryDirectoryForBigFiles
}

func CreateBigFileTemp(sys *types.SystemContext, name string) (*os.File, error) {
return os.CreateTemp(temporaryDirectoryForBigFiles(sys), prefix+name)
}

func MkDirBigFileTemp(sys *types.SystemContext, name string) (string, error) {
return os.MkdirTemp(temporaryDirectoryForBigFiles(sys), prefix+name)
}
54 changes: 54 additions & 0 deletions internal/tmpdir/tmpdir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tmpdir

import (
"os"
"strings"
"testing"

"github.com/containers/image/v5/types"
"github.com/stretchr/testify/assert"
)

func TestCreateBigFileTemp(t *testing.T) {
f, err := CreateBigFileTemp(nil, "")
assert.NoError(t, err)
f.Close()
mtrmac marked this conversation as resolved.
Show resolved Hide resolved
os.Remove(f.Name())

f, err = CreateBigFileTemp(nil, "foobar")
assert.NoError(t, err)
f.Close()
assert.True(t, strings.Contains(f.Name(), prefix+"foobar"))
os.Remove(f.Name())

var sys types.SystemContext
sys.BigFilesTemporaryDir = "/tmp"
f, err = CreateBigFileTemp(&sys, "foobar1")
assert.NoError(t, err)
f.Close()
assert.True(t, strings.Contains(f.Name(), "/tmp/"+prefix+"foobar1"))
os.Remove(f.Name())

sys.BigFilesTemporaryDir = "/tmp/bogus"
_, err = CreateBigFileTemp(&sys, "foobar1")
assert.Error(t, err)

}

func TestMkDirBigFileTemp(t *testing.T) {
d, err := MkDirBigFileTemp(nil, "foobar")
assert.NoError(t, err)
assert.True(t, strings.Contains(d, prefix+"foobar"))
os.RemoveAll(d)

var sys types.SystemContext
sys.BigFilesTemporaryDir = "/tmp"
d, err = MkDirBigFileTemp(&sys, "foobar1")
assert.NoError(t, err)
assert.True(t, strings.Contains(d, "/tmp/"+prefix+"foobar1"))
os.RemoveAll(d)

sys.BigFilesTemporaryDir = "/tmp/bogus"
_, err = MkDirBigFileTemp(&sys, "foobar1")
assert.Error(t, err)
}
2 changes: 1 addition & 1 deletion oci/archive/oci_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (t *tempDirOCIRef) deleteTempDir() error {
// createOCIRef creates the oci reference of the image
// If SystemContext.BigFilesTemporaryDir not "", overrides the temporary directory to use for storing big files
func createOCIRef(sys *types.SystemContext, image string) (tempDirOCIRef, error) {
dir, err := os.MkdirTemp(tmpdir.TemporaryDirectoryForBigFiles(sys), "oci")
dir, err := tmpdir.MkDirBigFileTemp(sys, "oci")
if err != nil {
return tempDirOCIRef{}, fmt.Errorf("creating temp directory: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion sif/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func newImageSource(ctx context.Context, sys *types.SystemContext, ref sifRefere
_ = sifImg.UnloadContainer()
}()

workDir, err := os.MkdirTemp(tmpdir.TemporaryDirectoryForBigFiles(sys), "sif")
workDir, err := tmpdir.MkDirBigFileTemp(sys, "sif")
if err != nil {
return nil, fmt.Errorf("creating temp directory: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion storage/storage_dest.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ type addedLayerInfo struct {
// newImageDestination sets us up to write a new image, caching blobs in a temporary directory until
// it's time to Commit() the image
func newImageDestination(sys *types.SystemContext, imageRef storageReference) (*storageImageDestination, error) {
directory, err := os.MkdirTemp(tmpdir.TemporaryDirectoryForBigFiles(sys), "storage")
directory, err := tmpdir.MkDirBigFileTemp(sys, "storage")
if err != nil {
return nil, fmt.Errorf("creating a temporary directory: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion storage/storage_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (s *storageImageSource) GetBlob(ctx context.Context, info types.BlobInfo, c
}
defer rc.Close()

tmpFile, err := os.CreateTemp(tmpdir.TemporaryDirectoryForBigFiles(s.systemContext), "")
tmpFile, err := tmpdir.CreateBigFileTemp(s.systemContext, "")
if err != nil {
return nil, 0, err
}
Expand Down