diff --git a/base_dirs.go b/base_dirs.go index fc482e2..0cdca02 100644 --- a/base_dirs.go +++ b/base_dirs.go @@ -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 ( @@ -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) { diff --git a/xdg.go b/xdg.go index 654d1d7..059d695 100644 --- a/xdg.go +++ b/xdg.go @@ -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) }