Files
kopia/internal/tempfile/tempfile_linux.go
Julio López e3fc6e012d refactor(general): leverage os.CreateTemp (#4513)
* use `os.CreateTemp` in `tempfile.CreateAutoDelete`
* refactor `TestTempFile`: add `VerifyTempfile` test helper
* add test for `createUnixFallback`
* rename function to `tempfile.CreateAutoDelete`
* remove the `dir` parameter to `tempfile.CreateAutoDelete`,
  only an empty string was passed, apart from test cases.
* guard against panic in TestShadowCopy
2025-04-22 20:55:11 -07:00

34 lines
733 B
Go

package tempfile
import (
"errors"
"os"
"syscall"
"golang.org/x/sys/unix"
)
const permissions = 0o600
// CreateAutoDelete creates a temporary file that will be automatically deleted on close.
func CreateAutoDelete() (*os.File, error) {
dir := os.TempDir()
// on reasonably modern Linux (3.11 and above) O_TMPFILE is supported,
// which creates invisible, unlinked file in a given directory.
fd, err := unix.Open(dir, unix.O_RDWR|unix.O_TMPFILE|unix.O_CLOEXEC, permissions)
if err == nil {
return os.NewFile(uintptr(fd), ""), nil
}
if errors.Is(err, syscall.EISDIR) || errors.Is(err, syscall.EOPNOTSUPP) {
return createUnixFallback()
}
return nil, &os.PathError{
Op: "open",
Path: dir,
Err: err,
}
}