kitten desktop-ui portal.go: Compat with XDG spec

1. Make sure we coalesce XDG_DATA_HOME as per the spec
  On my machine /etc/profile.d/flatpak.sh sets
  XDG_DATA_DIRS=$HOME/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
  but XDG_DATA_HOME is unset.

2. Update directory creation logic to find-or-create
   `enable_portal`'s current behavior is to first find a writable directory and
   write the portal definition to it, then fall back to creating a directory
   in the first available configuration directory.
   This is incorrect in the case where one of the locations in
   XDG_DATA_DIRS already has a directory, we should still prioritize
   XDG_DATA_HOME.
This commit is contained in:
Nathan Monfils
2025-08-04 15:24:49 +02:00
parent bdf3b9300e
commit ff1c8ab917

View File

@@ -313,12 +313,24 @@ func show_settings(opts *ShowSettingsOptions) (err error) {
}
var DataDirs = sync.OnceValue(func() (ans []string) {
d := os.Getenv("XDG_DATA_DIRS")
if d == "" {
d = "/usr/local/share/:/usr/share/"
// $XDG_DATA_DIRS defines the preference-ordered set of base directories
// to search for data files **in addition to the $XDG_DATA_HOME** base
// directory. The directories in $XDG_DATA_DIRS should be separated with
// a colon ':'.
// https://specifications.freedesktop.org/basedir-spec/0.8/#variables
data_dirs := os.Getenv("XDG_DATA_DIRS")
if data_dirs == "" {
data_dirs = "/usr/local/share/:/usr/share/"
}
all := []string{os.Getenv("XDG_DATA_HOME")}
all = append(all, strings.Split(d, ":")...)
data_home := os.Getenv("XDG_DATA_HOME")
if data_home == "" {
data_home = os.Getenv("HOME") + "/.local/share"
}
all := []string{data_home}
all = append(all, strings.Split(data_dirs, ":")...)
seen := map[string]bool{}
for _, x := range all {
if !seen[x] {
@@ -381,21 +393,13 @@ func enable_portal() (err error) {
}
portals_dir := ""
for _, x := range WritableDataDirs() {
// Find-or-create the first available xdg-desktop-portals/portals directory
q := filepath.Join(x, "xdg-desktop-portal", "portals")
if unix.Access(q, unix.W_OK) == nil && IsDir(q) {
if (unix.Access(q, unix.W_OK) == nil && IsDir(q)) || (os.MkdirAll(q, 0o755) == nil) {
portals_dir = q
break
}
}
if portals_dir == "" {
for _, x := range WritableDataDirs() {
q := filepath.Join(x, "xdg-desktop-portal", "portals")
if err := os.MkdirAll(q, 0o755); err == nil {
portals_dir = q
break
}
}
}
if portals_dir == "" {
return fmt.Errorf("Could not find any writable portals directories. Make sure XDG_DATA_HOME is set and point to a directory for which you have write permission.")
}