From 6ea2b786d4eb715e580f73e210a322aac10adc9f Mon Sep 17 00:00:00 2001 From: Ralph Seichter Date: Tue, 8 Oct 2024 16:53:58 +0200 Subject: [PATCH] xdg: determine user runtime directory in a robust manner Instead of relying on systemd-style /run/user/NNN directories, which are unavailable on platforms like OpenRC Gentoo Linux, create either the per user /tmp/aerc-NNN directory, or fall back to using /tmp as a last resort. Changelog-fixed: Failure to create IPC socket on Gentoo. Signed-off-by: Ralph Seichter Reviewed-by: Jason Cox Acked-by: Robin Jarry --- lib/xdg/xdg.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/xdg/xdg.go b/lib/xdg/xdg.go index c1eaab03..6060c2bc 100644 --- a/lib/xdg/xdg.go +++ b/lib/xdg/xdg.go @@ -69,7 +69,20 @@ func DataPath(paths ...string) string { // ugly: there's no other way to allow mocking a function in go... var userRuntimePath = func() string { - return filepath.Join("/run/user", strconv.Itoa(os.Getuid())) + uid := strconv.Itoa(os.Getuid()) + path := filepath.Join("/run/user", uid) + fi, err := os.Stat(path) + if err != nil || !fi.Mode().IsDir() { + // OpenRC does not create /run/user. TMUX and Neovim + // create /tmp/$program-$uid instead. Mimic that. + path = filepath.Join(os.TempDir(), "aerc-"+uid) + err = os.MkdirAll(path, 0o700) + if err != nil { + // Fallback to /tmp if all else fails. + path = os.TempDir() + } + } + return path } // Return a path relative to the user runtime dir