From 0bd452ad9b773dd389ef9a858d8d48b922bf5ec6 Mon Sep 17 00:00:00 2001 From: Ophestra Umiker Date: Mon, 15 Jul 2024 16:25:44 +0900 Subject: [PATCH] util: PulseAudio cookie discovery This appears to be how a regular PulseAudio client discovers the PulseAudio cookie. Signed-off-by: Ophestra Umiker --- util.go | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/util.go b/util.go index 2de8f3b..1a50743 100644 --- a/util.go +++ b/util.go @@ -7,6 +7,7 @@ import ( "io/fs" "os" "os/exec" + "path" ) const ( @@ -29,9 +30,46 @@ func sdBooted() bool { return true } +// Try various ways to discover the current user's PulseAudio authentication cookie. +func discoverPulseCookie() string { + if p, ok := os.LookupEnv(pulseCookie); ok { + return p + } + + if p, ok := os.LookupEnv(home); ok { + p = path.Join(p, ".pulse-cookie") + if s, err := os.Stat(p); err != nil { + if !errors.Is(err, fs.ErrNotExist) { + fatal("Error accessing PulseAudio cookie:", err) + // unreachable + return p + } + } else if !s.IsDir() { + return p + } + } + + if p, ok := os.LookupEnv(xdgConfigHome); ok { + p = path.Join(p, "pulse", "cookie") + if s, err := os.Stat(p); err != nil { + if !errors.Is(err, fs.ErrNotExist) { + fatal("Error accessing PulseAudio cookie:", err) + // unreachable + return p + } + } else if !s.IsDir() { + return p + } + } + + fatal(fmt.Sprintf("Cannot locate PulseAudio cookie (tried $%s, $%s/pulse/cookie, $%s/.pulse-cookie)", + pulseCookie, xdgConfigHome, home)) + return "" +} + func which(file string) (string, bool) { - path, err := exec.LookPath(file) - return path, err == nil + p, err := exec.LookPath(file) + return p, err == nil } func copyFile(dst, src string) error {