diff --git a/pkg/systemd/quadlet/unitdirs.go b/pkg/systemd/quadlet/unitdirs.go index ee3b8932788..e14153cdb3d 100644 --- a/pkg/systemd/quadlet/unitdirs.go +++ b/pkg/systemd/quadlet/unitdirs.go @@ -11,6 +11,7 @@ import ( "strings" "go.podman.io/podman/v6/pkg/logiface" + "go.podman.io/podman/v6/pkg/util" ) // This returns whether a file has an extension recognized as a valid Quadlet unit type. @@ -230,6 +231,12 @@ func getRootlessDirs(paths *searchPaths, nonNumericFilter, userLevelFilter func( } configDir, err := os.UserConfigDir() + if os.Getenv("XDG_CONFIG_HOME") == "" { + home, found := os.LookupEnv("HOME") + if !found || home == "" || home == string(os.PathSeparator) { + configDir, err = util.GetRootlessConfigHomeDir() + } + } if err != nil { logiface.Errorf("Warning: %v", err) return diff --git a/pkg/util/utils_linux_test.go b/pkg/util/utils_linux_test.go index 9d75a20c8f4..1c0a2fcf4de 100644 --- a/pkg/util/utils_linux_test.go +++ b/pkg/util/utils_linux_test.go @@ -1,9 +1,31 @@ package util import ( + "os/user" + "path/filepath" "testing" ) +func TestGetRootlessConfigHomeDirWithRootHome(t *testing.T) { + u, err := user.Current() + if err != nil { + t.Fatal(err) + } + + t.Setenv("XDG_CONFIG_HOME", "") + t.Setenv("HOME", "/") + + configDir, err := GetRootlessConfigHomeDir() + if err != nil { + t.Fatal(err) + } + + expected := filepath.Join(u.HomeDir, ".config") + if configDir != expected { + t.Fatalf("expected %q, got %q", expected, configDir) + } +} + func TestIsVirtualConsoleDevice(t *testing.T) { testcases := []struct { expectedResult bool diff --git a/pkg/util/utils_supported.go b/pkg/util/utils_supported.go index 2c1469f8869..36ee0321962 100644 --- a/pkg/util/utils_supported.go +++ b/pkg/util/utils_supported.go @@ -6,7 +6,10 @@ package util // should work to take darwin from this import ( + "os" + "os/user" "path/filepath" + "strconv" "go.podman.io/podman/v6/pkg/rootless" "go.podman.io/storage/pkg/homedir" @@ -22,6 +25,13 @@ func GetRootlessRuntimeDir() (string, error) { // GetRootlessConfigHomeDir returns the config home directory when running as non root func GetRootlessConfigHomeDir() (string, error) { + if os.Getenv("XDG_CONFIG_HOME") == "" && os.Getenv("HOME") == string(os.PathSeparator) { + u, err := user.LookupId(strconv.Itoa(rootless.GetRootlessUID())) + if err == nil && u.HomeDir != "" { + return filepath.Join(u.HomeDir, ".config"), nil + } + } + return homedir.GetConfigHome() }