Skip to content

Commit

Permalink
Merge pull request #99 from adrg/improve-runtime-file
Browse files Browse the repository at this point in the history
Update xdg.RuntimeFile logic
  • Loading branch information
adrg authored Oct 29, 2024
2 parents 3b346cd + 221e506 commit 2335a68
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Sensible fallback locations are used for the folders which are not set.
| <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> |
| <kbd><b>XDG_STATE_HOME</b></kbd> | <kbd>~/.local/state</kbd> | <kbd>~/Library/Application&nbsp;Support</kbd> | <kbd>$home/lib/state</kbd> |
| <kbd><b>XDG_CACHE_HOME</b></kbd> | <kbd>~/.cache</kbd> | <kbd>~/Library/Caches</kbd> | <kbd>$home/lib/cache</kbd> |
| <kbd><b>XDG_RUNTIME_DIR</b></kbd> | <kbd>/run/user/UID</kbd> | <kbd>~/Library/Application&nbsp;Support</kbd> | <kbd>/tmp</kbd> |
| <kbd><b>XDG_RUNTIME_DIR</b></kbd> | <kbd>/run/user/$UID</kbd> | <kbd>~/Library/Application&nbsp;Support</kbd> | <kbd>/tmp</kbd> |
| <kbd><b>XDG_BIN_HOME</b></kbd> | <kbd>~/.local/bin</kbd> | <kbd>~/.local/bin</kbd> | <kbd>$home/bin</kbd> |

</details>
Expand Down
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
11 changes: 4 additions & 7 deletions internal/pathutil/pathutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"
)

// Unique eliminates the duplicate paths from the provided slice and returns
Expand Down Expand Up @@ -52,7 +51,6 @@ func First(paths []string) string {
// relative to the selected parent path.
func Create(name string, paths []string) (string, error) {
searchedPaths := make([]string, 0, len(paths))

for _, p := range paths {
p = filepath.Join(p, name)

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

return "", fmt.Errorf("could not create any of the following paths: %s",
strings.Join(searchedPaths, ", "))
return "", fmt.Errorf("could not create any of the following paths: %v",
searchedPaths)
}

// Search searches for the file with the specified `name` in the provided
// slice of `paths`. The `name` parameter must contain the name of the file,
// but it can also contain a set of parent directories.
func Search(name string, paths []string) (string, error) {
searchedPaths := make([]string, 0, len(paths))

for _, p := range paths {
p = filepath.Join(p, name)
if Exists(p) {
Expand All @@ -86,8 +83,8 @@ func Search(name string, paths []string) (string, error) {
searchedPaths = append(searchedPaths, filepath.Dir(p))
}

return "", fmt.Errorf("could not locate `%s` in any of the following paths: %s",
filepath.Base(name), strings.Join(searchedPaths, ", "))
return "", fmt.Errorf("could not locate `%s` in any of the following paths: %v",
filepath.Base(name), searchedPaths)
}

// EnvPath returns the value of the environment variable with the specified
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
17 changes: 17 additions & 0 deletions xdg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,20 @@ func TestInvalidPaths(t *testing.T) {
require.Error(t, err)
}
}

func TestNonExistentRuntimeDir(t *testing.T) {
var (
envRuntimeDirVar = "XDG_RUNTIME_DIR"
originalRuntimeDir = xdg.RuntimeDir
nonExistentRuntimeDir = filepath.Join(xdg.Home, "runtime")
)
defer os.Setenv(envRuntimeDirVar, originalRuntimeDir)

require.NoError(t, os.Setenv(envRuntimeDirVar, nonExistentRuntimeDir))
xdg.Reload()
require.Equal(t, nonExistentRuntimeDir, xdg.RuntimeDir)

p, err := xdg.RuntimeFile("app.pid")
require.NoError(t, err)
require.Equal(t, filepath.Clean(os.TempDir()), filepath.Dir(p))
}

0 comments on commit 2335a68

Please sign in to comment.