File: tmpdir.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (44 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download | duplicates (2)
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
33
34
35
36
37
38
39
40
41
42
43
44
package tmpdir

import (
	"os"
	"runtime"

	"github.com/containers/image/v5/types"
)

// unixTempDirForBigFiles is the directory path to store big files on non Windows systems.
// You can override this at build time with
// -ldflags '-X github.com/containers/image/v5/internal/tmpdir.unixTempDirForBigFiles=$your_path'
var unixTempDirForBigFiles = builtinUnixTempDirForBigFiles

// builtinUnixTempDirForBigFiles is the directory path to store big files.
// Do not use the system default of os.TempDir(), usually /tmp, because with systemd it could be a tmpfs.
// 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 {
	if sys != nil && sys.BigFilesTemporaryDir != "" {
		return sys.BigFilesTemporaryDir
	}
	var temporaryDirectoryForBigFiles string
	if runtime.GOOS == "windows" {
		temporaryDirectoryForBigFiles = os.TempDir()
	} else {
		temporaryDirectoryForBigFiles = unixTempDirForBigFiles
	}
	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)
}