From 800775a49c0a7877af5dca22104b90dc7e788cd0 Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Thu, 31 Oct 2024 15:10:47 +0200 Subject: [PATCH 1/4] Update xdg.SearchRuntimeFile to also look in temporary directory --- base_dirs.go | 2 +- xdg.go | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/base_dirs.go b/base_dirs.go index 0cdca02..65f3e3f 100644 --- a/base_dirs.go +++ b/base_dirs.go @@ -78,5 +78,5 @@ func (bd baseDirectories) searchCacheFile(relPath string) (string, error) { } func (bd baseDirectories) searchRuntimeFile(relPath string) (string, error) { - return pathutil.Search(relPath, []string{bd.runtime}) + return pathutil.Search(relPath, pathutil.Unique([]string{bd.runtime, os.TempDir()})) } diff --git a/xdg.go b/xdg.go index 059d695..b1f97cc 100644 --- a/xdg.go +++ b/xdg.go @@ -202,8 +202,11 @@ func SearchCacheFile(relPath string) (string, error) { // SearchRuntimeFile searches for the specified file in the runtime search path. // 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 -// file cannot be found, an error specifying the searched path is returned. +// optionally, a set of parent directories (e.g. appname/app.pid). The runtime +// file is also searched in the operating system's temporary directory in order +// to cover cases in which the runtime base directory does not exist or is not +// accessible. If the file cannot be found, an error specifying the searched +// path is returned. func SearchRuntimeFile(relPath string) (string, error) { return baseDirs.searchRuntimeFile(relPath) } From d9f76be86d944bf2b9bdb8544952111e2533f3ad Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Thu, 31 Oct 2024 15:11:25 +0200 Subject: [PATCH 2/4] Improve non-existent runtime directory test case --- xdg_test.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/xdg_test.go b/xdg_test.go index 53d0604..47b1392 100644 --- a/xdg_test.go +++ b/xdg_test.go @@ -202,6 +202,7 @@ func TestInvalidPaths(t *testing.T) { func TestNonExistentRuntimeDir(t *testing.T) { var ( + runtimeFiles = []string{"app.pid", "appname/app.pid"} envRuntimeDirVar = "XDG_RUNTIME_DIR" originalRuntimeDir = xdg.RuntimeDir nonExistentRuntimeDir = filepath.Join(xdg.Home, "runtime") @@ -212,7 +213,18 @@ func TestNonExistentRuntimeDir(t *testing.T) { 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)) + for _, runtimeFile := range runtimeFiles { + suggestedPath, err := xdg.RuntimeFile(runtimeFile) + require.NoError(t, err) + require.Equal(t, true, strings.HasPrefix(suggestedPath, os.TempDir())) + + f, err := os.Create(suggestedPath) + require.NoError(t, err) + require.NoError(t, f.Close()) + defer os.Remove(suggestedPath) + + foundPath, err := xdg.SearchRuntimeFile(runtimeFile) + require.NoError(t, err) + require.Equal(t, suggestedPath, foundPath) + } } From 88111eba52ac2a211b97194266db5207c975c266 Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Thu, 31 Oct 2024 16:04:14 +0200 Subject: [PATCH 3/4] Minor example update in README.md and doc.go --- README.md | 3 +++ doc.go | 3 +++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 9ccfb62..cf16c5a 100644 --- a/README.md +++ b/README.md @@ -207,6 +207,9 @@ func main() { // ConfigFile takes one parameter which must contain the name of the file, // but it can also contain a set of parent directories. If the directories // don't exist, they will be created relative to the base config directory. + // It is recommended for files to be saved inside an application directory + // relative to the base directory rather than directly inside the base + // directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`). configFilePath, err := xdg.ConfigFile("appname/config.yaml") if err != nil { log.Fatal(err) diff --git a/doc.go b/doc.go index ddbaae4..22ba478 100644 --- a/doc.go +++ b/doc.go @@ -48,6 +48,9 @@ XDG Base Directory // ConfigFile takes one parameter which must contain the name of the file, // but it can also contain a set of parent directories. If the directories // don't exist, they will be created relative to the base config directory. + // It is recommended for files to be saved inside an application directory + // relative to the base directory rather than directly inside the base + // directory (e.g. `appname/config.yaml` instead of `appname-config.yaml`). configFilePath, err := xdg.ConfigFile("appname/config.yaml") if err != nil { log.Fatal(err) From 71a81eccf3e9ac9ebf03e8c11ca3ed60a06eac7f Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Thu, 31 Oct 2024 18:13:59 +0200 Subject: [PATCH 4/4] Minor xdg.SearchRuntimeFile function documentation update --- xdg.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xdg.go b/xdg.go index b1f97cc..f4f1d4f 100644 --- a/xdg.go +++ b/xdg.go @@ -206,7 +206,7 @@ func SearchCacheFile(relPath string) (string, error) { // file is also searched in the operating system's temporary directory in order // to cover cases in which the runtime base directory does not exist or is not // accessible. If the file cannot be found, an error specifying the searched -// path is returned. +// paths is returned. func SearchRuntimeFile(relPath string) (string, error) { return baseDirs.searchRuntimeFile(relPath) }