fix(general): tempfile.Create on Linux (x64/arm64) (#4504)

- Add `TestCreateSucceedsWhenDirIsNotSpecified`
- Use `os.TempDir` when `dir` is not specified (empty string)

Ref:
- Fixes #4331
- Fixes #2415
This commit is contained in:
Julio López
2025-04-15 22:21:57 -07:00
committed by GitHub
parent cf0c3fa4a5
commit 97ff2a17fb
2 changed files with 11 additions and 0 deletions

View File

@@ -10,6 +10,8 @@
// Create creates a temporary file that will be automatically deleted on close.
func Create(dir string) (*os.File, error) {
dir = tempDirOr(dir)
// 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)

View File

@@ -36,3 +36,12 @@ func TestTempFile(t *testing.T) {
require.NoError(t, err)
require.Empty(t, files)
}
func TestCreateSucceedsWhenDirIsNotSpecified(t *testing.T) {
f, err := tempfile.Create("")
require.NoError(t, err)
err = f.Close()
require.NoError(t, err)
}