1package daemonrpc
2
3import (
4 "fmt"
5 "os"
6 "path/filepath"
7 "runtime"
8)
9
10// runtimeDir returns the base directory for daemon runtime files.
11// Linux: $XDG_RUNTIME_DIR/matcha/
12// macOS: ~/Library/Caches/matcha/
13func runtimeDir() string {
14 switch runtime.GOOS {
15 case "darwin":
16 home, _ := os.UserHomeDir()
17 return filepath.Join(home, "Library", "Caches", "matcha")
18 default: // linux and others
19 if dir := os.Getenv("XDG_RUNTIME_DIR"); dir != "" {
20 return filepath.Join(dir, "matcha")
21 }
22 // Fallback: /tmp/matcha-<uid>
23 return filepath.Join(os.TempDir(), "matcha-"+uidStr())
24 }
25}
26
27func uidStr() string {
28 return fmt.Sprintf("%d", os.Getuid())
29}
30
31// SocketPath returns the path to the daemon's Unix domain socket.
32func SocketPath() string {
33 return filepath.Join(runtimeDir(), "daemon.sock")
34}
35
36// PIDPath returns the path to the daemon's PID file.
37func PIDPath() string {
38 return filepath.Join(runtimeDir(), "daemon.pid")
39}
40
41// EnsureRuntimeDir creates the runtime directory if it doesn't exist.
42func EnsureRuntimeDir() error {
43 return os.MkdirAll(runtimeDir(), 0700)
44}