Skip to content

Commit

Permalink
Update xdg.RuntimeFile to only use the base runtime directory if it e…
Browse files Browse the repository at this point in the history
…xists

If the base runtime directory exists, it is used. Otherwise the
operating system's temporary directory is used as a fallback.
  • Loading branch information
adrg committed Oct 29, 2024
1 parent 3b346cd commit 40d3792
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
14 changes: 12 additions & 2 deletions base_dirs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package xdg

import "github.com/adrg/xdg/internal/pathutil"
import (
"os"

"github.com/adrg/xdg/internal/pathutil"
)

// XDG Base Directory environment variables.
const (
Expand Down Expand Up @@ -48,7 +52,13 @@ func (bd baseDirectories) cacheFile(relPath string) (string, error) {
}

func (bd baseDirectories) runtimeFile(relPath string) (string, error) {
return pathutil.Create(relPath, []string{bd.runtime})
var paths []string
for _, p := range pathutil.Unique([]string{bd.runtime, os.TempDir()}) {
if pathutil.Exists(p) {
paths = append(paths, p)
}
}
return pathutil.Create(relPath, paths)
}

func (bd baseDirectories) searchDataFile(relPath string) (string, error) {
Expand Down
5 changes: 3 additions & 2 deletions xdg.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ func CacheFile(relPath string) (string, error) {
// The relPath parameter must contain the name of the runtime file, and
// optionally, a set of parent directories (e.g. appname/app.pid).
// If the specified directories do not exist, they will be created relative
// to the base runtime directory. On failure, an error containing the
// attempted paths is returned.
// to the base runtime directory. If the base runtime directory does not exist,
// the operating system's temporary directory is used as a fallback. On failure,
// an error containing the attempted paths is returned.
func RuntimeFile(relPath string) (string, error) {
return baseDirs.runtimeFile(relPath)
}
Expand Down

0 comments on commit 40d3792

Please sign in to comment.