Skip to content

Commit 2335a68

Browse files
authored
Merge pull request #99 from adrg/improve-runtime-file
Update xdg.RuntimeFile logic
2 parents 3b346cd + 221e506 commit 2335a68

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Sensible fallback locations are used for the folders which are not set.
7979
| <kbd><b>XDG_CONFIG_DIRS</b></kbd> | <kbd>/etc/xdg</kbd> | <kbd>~/Library/Preferences</kbd><br/><kbd>/Library/Application&nbsp;Support</kbd><br/><kbd>/Library/Preferences</kbd><br/><kbd>&#126;/.config</kbd> | <kbd>/lib</kbd> |
8080
| <kbd><b>XDG_STATE_HOME</b></kbd> | <kbd>~/.local/state</kbd> | <kbd>~/Library/Application&nbsp;Support</kbd> | <kbd>$home/lib/state</kbd> |
8181
| <kbd><b>XDG_CACHE_HOME</b></kbd> | <kbd>~/.cache</kbd> | <kbd>~/Library/Caches</kbd> | <kbd>$home/lib/cache</kbd> |
82-
| <kbd><b>XDG_RUNTIME_DIR</b></kbd> | <kbd>/run/user/UID</kbd> | <kbd>~/Library/Application&nbsp;Support</kbd> | <kbd>/tmp</kbd> |
82+
| <kbd><b>XDG_RUNTIME_DIR</b></kbd> | <kbd>/run/user/$UID</kbd> | <kbd>~/Library/Application&nbsp;Support</kbd> | <kbd>/tmp</kbd> |
8383
| <kbd><b>XDG_BIN_HOME</b></kbd> | <kbd>~/.local/bin</kbd> | <kbd>~/.local/bin</kbd> | <kbd>$home/bin</kbd> |
8484

8585
</details>

base_dirs.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package xdg
22

3-
import "github.com/adrg/xdg/internal/pathutil"
3+
import (
4+
"os"
5+
6+
"github.com/adrg/xdg/internal/pathutil"
7+
)
48

59
// XDG Base Directory environment variables.
610
const (
@@ -48,7 +52,13 @@ func (bd baseDirectories) cacheFile(relPath string) (string, error) {
4852
}
4953

5054
func (bd baseDirectories) runtimeFile(relPath string) (string, error) {
51-
return pathutil.Create(relPath, []string{bd.runtime})
55+
var paths []string
56+
for _, p := range pathutil.Unique([]string{bd.runtime, os.TempDir()}) {
57+
if pathutil.Exists(p) {
58+
paths = append(paths, p)
59+
}
60+
}
61+
return pathutil.Create(relPath, paths)
5262
}
5363

5464
func (bd baseDirectories) searchDataFile(relPath string) (string, error) {

internal/pathutil/pathutil.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7-
"strings"
87
)
98

109
// Unique eliminates the duplicate paths from the provided slice and returns
@@ -52,7 +51,6 @@ func First(paths []string) string {
5251
// relative to the selected parent path.
5352
func Create(name string, paths []string) (string, error) {
5453
searchedPaths := make([]string, 0, len(paths))
55-
5654
for _, p := range paths {
5755
p = filepath.Join(p, name)
5856

@@ -67,16 +65,15 @@ func Create(name string, paths []string) (string, error) {
6765
searchedPaths = append(searchedPaths, dir)
6866
}
6967

70-
return "", fmt.Errorf("could not create any of the following paths: %s",
71-
strings.Join(searchedPaths, ", "))
68+
return "", fmt.Errorf("could not create any of the following paths: %v",
69+
searchedPaths)
7270
}
7371

7472
// Search searches for the file with the specified `name` in the provided
7573
// slice of `paths`. The `name` parameter must contain the name of the file,
7674
// but it can also contain a set of parent directories.
7775
func Search(name string, paths []string) (string, error) {
7876
searchedPaths := make([]string, 0, len(paths))
79-
8077
for _, p := range paths {
8178
p = filepath.Join(p, name)
8279
if Exists(p) {
@@ -86,8 +83,8 @@ func Search(name string, paths []string) (string, error) {
8683
searchedPaths = append(searchedPaths, filepath.Dir(p))
8784
}
8885

89-
return "", fmt.Errorf("could not locate `%s` in any of the following paths: %s",
90-
filepath.Base(name), strings.Join(searchedPaths, ", "))
86+
return "", fmt.Errorf("could not locate `%s` in any of the following paths: %v",
87+
filepath.Base(name), searchedPaths)
9188
}
9289

9390
// EnvPath returns the value of the environment variable with the specified

xdg.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,9 @@ func CacheFile(relPath string) (string, error) {
161161
// The relPath parameter must contain the name of the runtime file, and
162162
// optionally, a set of parent directories (e.g. appname/app.pid).
163163
// If the specified directories do not exist, they will be created relative
164-
// to the base runtime directory. On failure, an error containing the
165-
// attempted paths is returned.
164+
// to the base runtime directory. If the base runtime directory does not exist,
165+
// the operating system's temporary directory is used as a fallback. On failure,
166+
// an error containing the attempted paths is returned.
166167
func RuntimeFile(relPath string) (string, error) {
167168
return baseDirs.runtimeFile(relPath)
168169
}

xdg_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,20 @@ func TestInvalidPaths(t *testing.T) {
199199
require.Error(t, err)
200200
}
201201
}
202+
203+
func TestNonExistentRuntimeDir(t *testing.T) {
204+
var (
205+
envRuntimeDirVar = "XDG_RUNTIME_DIR"
206+
originalRuntimeDir = xdg.RuntimeDir
207+
nonExistentRuntimeDir = filepath.Join(xdg.Home, "runtime")
208+
)
209+
defer os.Setenv(envRuntimeDirVar, originalRuntimeDir)
210+
211+
require.NoError(t, os.Setenv(envRuntimeDirVar, nonExistentRuntimeDir))
212+
xdg.Reload()
213+
require.Equal(t, nonExistentRuntimeDir, xdg.RuntimeDir)
214+
215+
p, err := xdg.RuntimeFile("app.pid")
216+
require.NoError(t, err)
217+
require.Equal(t, filepath.Clean(os.TempDir()), filepath.Dir(p))
218+
}

0 commit comments

Comments
 (0)