adrg/xdg

Parse ~/.config/user-dirs.dirs file

mateusz834 opened this issue · 7 comments

I am testing this library using fedora and arch Linux and it seems like none of the distributions are setting the env vars for XDG User Directories.

[a@localhost-live test]$ cat /home/a/.config/user-dirs.dirs
# This file is written by xdg-user-dirs-update
# If you want to change or add directories, just edit the line you're
# interested in. All local changes will be retained on the next run.
# Format is XDG_xxx_DIR="$HOME/yyy", where yyy is a shell-escaped
# homedir-relative path, or XDG_xxx_DIR="/yyy", where /yyy is an
# absolute path. No other format is supported.
# 
XDG_DESKTOP_DIR="$HOME/Pulpit"
XDG_DOWNLOAD_DIR="$HOME/Pobrane"
XDG_TEMPLATES_DIR="$HOME/Szablony"
XDG_PUBLICSHARE_DIR="$HOME/Publiczny"
XDG_DOCUMENTS_DIR="$HOME/Dokumenty"
XDG_MUSIC_DIR="$HOME/Muzyka"
XDG_PICTURES_DIR="$HOME/Obrazy"
XDG_VIDEOS_DIR="$HOME/Wideo"
[a@localhost-live test]$ ./main 
2022/02/04 18:14:32 Desktop directory: /home/a/Desktop
2022/02/04 18:14:32 Download directory: /home/a/Downloads
2022/02/04 18:14:32 Documents directory: /home/a/Documents
2022/02/04 18:14:32 Music directory: /home/a/Music
2022/02/04 18:14:32 Pictures directory: /home/a/Pictures
2022/02/04 18:14:32 Videos directory: /home/a/Videos
2022/02/04 18:14:32 Templates directory: /home/a/Templates
2022/02/04 18:14:32 Public directory: /home/a/Public

Firefox for example reads the ~/.config/user-dirs.dirs directly.

[a@localhost-live test]$ cat firefox-strace | grep .config/user-dirs
openat(AT_FDCWD, "/home/a/.config/user-dirs.dirs", O_RDONLY) = 53

Also it looks like XDG_*_DIR env vars do not override the user-dirs.dirs file, checked in firefox/chromium

const (
	envDesktopDir     = "XDG_DESKTOP_DIR"
	envDownloadDir    = "XDG_DOWNLOAD_DIR"
	envDocumentsDir   = "XDG_DOCUMENTS_DIR"
	envMusicDir       = "XDG_MUSIC_DIR"
	envPicturesDir    = "XDG_PICTURES_DIR"
	envVideosDir      = "XDG_VIDEOS_DIR"
	envTemplatesDir   = "XDG_TEMPLATES_DIR"
	envPublicShareDir = "XDG_PUBLICSHARE_DIR"
)
adrg commented

Hi @mateusz834. Thank you for your interest in the library. Reading user-dirs.dirs is already a task in my to-do list.
I'll start working on this. Hopefully, I can release a new version sometime next week (depending on my free time).

Also it looks like XDG_*_DIR env vars do not override the user-dirs.dirs file, checked in firefox/chromium

Yeah, that seems to be the case. I think the priority to should be:
user-dirs.dirs values > environment variables > defaults

It even look like that env vars are completely ignored by firefox/chromium.
I removed the user-dirs.dirs file and set the XDG_DOWNLOAD_DIR env var to a different directory and the downloaded file was saved in the default directory.

But for backwards compatibility I think that the order should be:
environment variables > user-dirs.dirs values > defaults

Maybe someone is relying on the env vars now and after the change it will suddenly use values from user-dirs.dirs??

adrg commented

Ideally, there would be no breaking changes. I try to steer clear of them as much as possible. However, in this case, I think the default behavior going forward should be to rely on the values provided by user-dirs.dirs, then fall back to other sources.

I made a quick search through GitHub and it seems most people are using this package for base directories rather than user directories. I think very few people customize user directory environment variables anyway, especially if they're ignored by other applications. In any case, the change will be documented in the release notes.

Alternatively, I could introduce an option to control whether user-dirs.dirs has priority. However, I think it clutters the package interface unnecessarily.

kajes commented

Any updates on this? I recently found this library and I guess I'm one of the edge cases that need the parsing of user-dirs.dirs.

You may be looking for this?

USER-DIRS.CONF(5)                          File Formats                          USER-DIRS.CONF(5)

NAME
       user-dirs.conf - configuration for xdg-user-dirs-update

DESCRIPTION
       The /etc/xdg/user-dirs.conf file is a text file that controls the behavior of the xdg-user-dirs-update
       command. Users can have their own ~/.config/user-dirs.conf file, which overrides the system-wide
       configuration.

       The following keys are recognised:

       enabled=boolean
           When set to False, xdg-user-dirs-update will not change the XDG user dirs configuration.

       filename_encoding=encoding
           This sets the filename encoding to use.  encoding can be an explicit encoding name, such as UTF-8,
           or "locale", which means the encoding of the users locale will be used.

       Lines beginning with a # character are ignored.

ENVIRONMENT
       XDG_CONFIG_DIRS
           The system-wide user-dirs.conf file is located in this directory. The default is /etc/xdg.

       XDG_CONFIG_HOME
           The per-user user-dirs.conf file is located in this directory. The default is $HOME/.config.

SEE ALSO
       xdg-user-dirs-update(1)

XDG
adrg commented

Released version v0.5.0 which adds support for reading user-dirs.dirs config file on relevant Unix-like operating systems.
Closing this issue as it has been addressed. Please reopen if necessary.