Skip to content

Commit

Permalink
Fall back to /tmp if the identified tmp directory is in a r/o filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
pjcdawkins committed Jan 13, 2025
1 parent da20e47 commit a925f87
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
5 changes: 0 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package config_test

import (
_ "embed"
"io/fs"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -51,10 +50,6 @@ func TestFromYAML(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, filepath.Join(homeDir, cnf.Application.WritableUserDir), writableDir)

_, err = cnf.TempDir()
assert.ErrorIs(t, err, fs.ErrNotExist)

require.NoError(t, os.Mkdir(filepath.Join(tempDir, "tmp"), 0o700))
d, err := cnf.TempDir()
assert.NoError(t, err)
assert.Equal(t, filepath.Join(tempDir, "tmp", cnf.Application.TempSubDir), d)
Expand Down
24 changes: 12 additions & 12 deletions internal/config/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package config

import (
"errors"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
)

// HomeDir returns the user's home directory, which can be overridden with the {ENV_PREFIX}HOME variable.
Expand All @@ -28,7 +28,7 @@ func (c *Config) WritableUserDir() (string, error) {
return "", err
}
path := filepath.Join(hd, c.Application.WritableUserDir)
if err := mkDirIfNotExists(path); err != nil {
if err := os.MkdirAll(path, 0o700); err != nil {
return "", err
}
c.writableUserDir = path
Expand Down Expand Up @@ -66,18 +66,18 @@ func (c *Config) TempDir() (string, error) {
}

path := filepath.Join(d, c.Application.TempSubDir)
if err := mkDirIfNotExists(path); err != nil {
return "", err

// If the subdirectory cannot be created due to a read-only filesystem, fall back to /tmp.
if err := os.MkdirAll(path, 0o700); err != nil {
if !errors.Is(err, syscall.EROFS) {
return "", err
}
path = filepath.Join(os.TempDir(), c.Application.TempSubDir)
if err := os.MkdirAll(path, 0o700); err != nil {
return "", err
}
}
c.cacheDir = path

return path, nil
}

func mkDirIfNotExists(path string) error {
err := os.Mkdir(path, 0o700)
if errors.Is(err, fs.ErrExist) {
return nil
}
return err
}

0 comments on commit a925f87

Please sign in to comment.