Skip to content

Commit e30581b

Browse files
committed
libimage should be using containers.conf for tmpdir
if image_copy_tmp_dir is set in containers.conf it needs to be used in the systemcontext for BigFilesTemporaryDir value. Fixes: containers/podman#14091 Signed-off-by: Daniel J Walsh <[email protected]>
1 parent a2ec40d commit e30581b

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

libimage/runtime.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"strings"
88

9+
"github.com/containers/common/pkg/config"
910
"github.com/containers/image/v5/docker/reference"
1011
"github.com/containers/image/v5/pkg/shortnames"
1112
storageTransport "github.com/containers/image/v5/storage"
@@ -22,13 +23,16 @@ import (
2223
var json = jsoniter.ConfigCompatibleWithStandardLibrary
2324

2425
// tmpdir returns a path to a temporary directory.
25-
func tmpdir() string {
26-
tmpdir := os.Getenv("TMPDIR")
27-
if tmpdir == "" {
28-
tmpdir = "/var/tmp"
26+
func tmpdir() (string, error) {
27+
var tmpdir string
28+
defaultContainerConfig, err := config.Default()
29+
if err == nil {
30+
tmpdir, err = defaultContainerConfig.ImageCopyTmpDir()
31+
if err == nil {
32+
return tmpdir, nil
33+
}
2934
}
30-
31-
return tmpdir
35+
return tmpdir, err
3236
}
3337

3438
// RuntimeOptions allow for creating a customized Runtime.
@@ -103,7 +107,11 @@ func RuntimeFromStore(store storage.Store, options *RuntimeOptions) (*Runtime, e
103107
systemContext = types.SystemContext{}
104108
}
105109
if systemContext.BigFilesTemporaryDir == "" {
106-
systemContext.BigFilesTemporaryDir = tmpdir()
110+
tmpdir, err := tmpdir()
111+
if err != nil {
112+
return nil, err
113+
}
114+
systemContext.BigFilesTemporaryDir = tmpdir
107115
}
108116

109117
setRegistriesConfPath(&systemContext)

libimage/runtime_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"testing"
77

8+
"github.com/containers/common/pkg/config"
89
"github.com/containers/image/v5/types"
910
"github.com/containers/storage"
1011
"github.com/containers/storage/pkg/reexec"
@@ -46,7 +47,9 @@ func testNewRuntime(t *testing.T, options ...testNewRuntimeOptions) (runtime *Ru
4647

4748
runtime, err = RuntimeFromStoreOptions(&RuntimeOptions{SystemContext: systemContext}, storeOptions)
4849
require.NoError(t, err)
49-
require.Equal(t, runtime.systemContext.BigFilesTemporaryDir, tmpdir())
50+
tmpd, err := tmpdir()
51+
require.NoError(t, err)
52+
require.Equal(t, runtime.systemContext.BigFilesTemporaryDir, tmpd)
5053

5154
cleanup = func() {
5255
_ = runtime.Shutdown(true)
@@ -57,3 +60,40 @@ func testNewRuntime(t *testing.T, options ...testNewRuntimeOptions) (runtime *Ru
5760
require.NotNil(t, sys)
5861
return runtime, cleanup
5962
}
63+
64+
func TestTmpdir(t *testing.T) {
65+
tmpStr := "TMPDIR"
66+
tmp, tmpSet := os.LookupEnv(tmpStr)
67+
68+
confStr := "CONTAINERS_CONF"
69+
conf, confSet := os.LookupEnv(confStr)
70+
71+
os.Setenv(confStr, "testdata/containers.conf")
72+
config.Reload()
73+
tmpd, err := tmpdir()
74+
require.NoError(t, err)
75+
require.Equal(t, "/tmp/from/containers.conf", tmpd)
76+
77+
if confSet {
78+
os.Setenv(confStr, conf)
79+
} else {
80+
os.Unsetenv(confStr)
81+
}
82+
config.Reload()
83+
84+
os.Unsetenv(tmpStr)
85+
tmpd, err = tmpdir()
86+
require.NoError(t, err)
87+
require.Equal(t, "/var/tmp", tmpd)
88+
89+
os.Setenv(tmpStr, "/tmp/test")
90+
tmpd, err = tmpdir()
91+
require.NoError(t, err)
92+
require.Equal(t, "/tmp/test", tmpd)
93+
if tmpSet {
94+
os.Setenv(tmpStr, tmp)
95+
} else {
96+
os.Unsetenv(tmpStr)
97+
}
98+
99+
}

libimage/testdata/containers.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[engine]
2+
# Default location for storing temporary container image content. Can be overridden with the TMPDIR environment
3+
# variable. If you specify "storage", then the location of the
4+
# container/storage tmp directory will be used.
5+
image_copy_tmp_dir="/tmp/from/containers.conf"

0 commit comments

Comments
 (0)