Should A Program Create XDG Folders?
Asked Answered
T

2

6

Let's say I'm writing a program, and I want it to follow the XDG Base Directory Specification for where it puts its files (app foo uses $XDG_CONFIG_HOME/foo as the directory for configuration files if XDG_CONFIG_HOME is set and non-blank, or ~/.config/foo, or fails with an error if the home directory can't even be resolved).

Is there a correct/specified behavior for the situation where for example XDG_CONFIG_HOME is set and non-blank, but that directory doesn't exist? Or if there is no such variable, and ~/.config doesn't exist? Is it expected that my program attempt to create it? Or is the non-existance of that directory considered an error on the environment's/system's part, and my program should avoid doing anything about it (just bail with an error)?

Note: I'm not asking if I should create ~/.config/foo - obviously that's a yes; I'm asking if I should create ~/.config itself, if it doesn't exist.

(To be more pedantic: obviously some program should create them - the question is whether it's solely the system's/desktop's/user's job to do so, or if any program should try creating the relevant directories if they don't exist?)

I've tried reading the XDG Base Directory Specification, which says that when attempting to write its files, the program may create the requisite directory, but it's unclear if this is referring to just the application's specific/"personal" sub-directory in the XDG base directories, or if this is meant for the XDG base directories themselves.

P.S. Usually I have a good idea of what tags to use, but here I'm really uncertain: please edit this post or suggest improvements to give it proper tags.

Trypanosome answered 23/4, 2016 at 8:26 Comment(0)
T
3

I've settled on my own answer after a few years of thinking:

  1. never create non-standard base XDG directories, but
  2. it may be okay to automatically create the standard XDG base directories, and
  3. you should automatically create any of your application's subdirectories within the base directories.

I think it can be good to be automatically helpful, but it is also very important to not worsen a user's mistakes.

If I write XDG_DATA_HOME=~/.locals/hare in my environment variable configuration, I might have wanted that, but it's much more likely that I made a typo of ~/.local/share. So the most helpful, least disruptive, and least wrong thing to do in that case is to report the lack of the requested XDG base directory.

So, if the user has specified a custom XDG base directory, and that base directory does not exist, never try to create it. Don't put your user in a situation where for example next to their standard ~/.config directory they get a ~/.configs or ~/.comfig directory which also contains some of their configurations, until one day they fix the typo and suddenly their programs behave as if they were reset to the defaults. This is a situation where early detection of mistakes is the most helpful thing to do in the long run, so tell the user immediately "this doesn't exist".

But if the user has not asked for a custom base directory, and you're about to use the known, standard location, then it's fine to try to automatically create it, if you ensure you only create it with reasonable ownership, permissions, and so on.

Finally, when the base XDG directory exists, most apps should probably make their own subdirectory inside that, and you definitely should create that app-specific directory, and any other subdirectories inside that one, automatically.

Trypanosome answered 23/4, 2016 at 8:26 Comment(0)
H
4

From the XDG Base Directory Specification:

If, when attempting to write a file, the destination directory is non-existant an attempt should be made to create it with permission 0700. If the destination directory exists already the permissions should not be changed. The application should be prepared to handle the case where the file could not be written, either because the directory was non-existant and could not be created, or for any other reason. In such case it may chose to present an error message to the user.

I would interpret this so application should try to create the XDG base directory (or any directory required for the destination) and only display an error if it is unable to do so.

Homager answered 11/11, 2019 at 14:52 Comment(1)
I never said this but I appreciated it, a couple years ago, when your answer first brought this part of the spec to my attention. Here, have my delayed upvote. I now think this guidance might be either referring to the application's subdirectory within the base directory, or it might have been written with the assumption of the standard base directory location. In both of those cases, this guidance is very reasonable. But if it really means to cover custom user-specified base directories too, then I judge the specification to be wrong in this regard (reasoning in my answer).Trypanosome
T
3

I've settled on my own answer after a few years of thinking:

  1. never create non-standard base XDG directories, but
  2. it may be okay to automatically create the standard XDG base directories, and
  3. you should automatically create any of your application's subdirectories within the base directories.

I think it can be good to be automatically helpful, but it is also very important to not worsen a user's mistakes.

If I write XDG_DATA_HOME=~/.locals/hare in my environment variable configuration, I might have wanted that, but it's much more likely that I made a typo of ~/.local/share. So the most helpful, least disruptive, and least wrong thing to do in that case is to report the lack of the requested XDG base directory.

So, if the user has specified a custom XDG base directory, and that base directory does not exist, never try to create it. Don't put your user in a situation where for example next to their standard ~/.config directory they get a ~/.configs or ~/.comfig directory which also contains some of their configurations, until one day they fix the typo and suddenly their programs behave as if they were reset to the defaults. This is a situation where early detection of mistakes is the most helpful thing to do in the long run, so tell the user immediately "this doesn't exist".

But if the user has not asked for a custom base directory, and you're about to use the known, standard location, then it's fine to try to automatically create it, if you ensure you only create it with reasonable ownership, permissions, and so on.

Finally, when the base XDG directory exists, most apps should probably make their own subdirectory inside that, and you definitely should create that app-specific directory, and any other subdirectories inside that one, automatically.

Trypanosome answered 23/4, 2016 at 8:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.