Xdg Basedir Directories for Windows

Where should I write a user specific log file to (and be XDG base directory compatible)

This is, for the moment, unclear.

Different software seem to handle this in different ways (imsettings puts it in $XDG_CACHE_HOME,
profanity in $XDG_DATA_HOME).

Debian, however, has a proposal which I can get behind (emphasis mine):

This is a recurring request/complaint (see this or this) on the xdg-freedesktop mailing list to introduce another directory for state information that does not belong in any of the existing categories (see also home-dir.proposal. Examples for this information are:

  • history files of shells, repls, anything that uses libreadline
  • logfiles
  • state of application windows on exit
  • recently opened files
  • last time application was run
  • emacs: bookmarks, ido last directories, backups, auto-save files, auto-save-list

The above example information is not essential data. However it should still persist on reboots of the system unlike cache data that a user might consider putting in a TMPFS. On the other hand the data is rather volatile and does not make sense to be checked into a VCS. The files are also not the data files that an application works on.

A default folder for a future STATE category might be: $HOME/.local/state

This would effectively introduce another environment variable since $XDG_DATA_HOME usually points to $HOME/.local/share and this hypothetical environment variable ($XDG_STATE_HOME?) would point to $HOME/.local/state

If you really want to adhere to the current standard I would place my log files in $XDG_CACHE_HOME since log files aren't required to run the program.

How to get known paths for linux

Linux is an operating system kernel. It does not have a concept of user directories.

There are several Linux distributions. The filesystem structure is determined by the distro. Most distros conform to POSIX standard, and follow (to varying degree) the Filesystem Hierarchy Standard by Linux Foundation, which is similar to the directory structures of other UNIX like systems. That said, distributions generally allow the user to use the file system in unconventional configurations. For example, they don't typically force users home directory to be under /home.

POSIX specifies a few environment variables that are relevant to this context:

HOME

The system shall initialize this variable at the time of login to be a pathname of the user's home directory.

TMPDIR

This variable shall represent a pathname of a directory made available for programs that need a place to create temporary files.

Environment variables can be accessed using std::getenv in C++.


On desktop systems, the directory structure is also determined to some degree by the desktop environment, of which there are several available. freedesktop.org produces unofficial specifications for interoperability of different desktop environments. On DE's conforming to XDG Base Directory Specification should following environment variables be available:

$XDG_DATA_HOME defines the base directory relative to which user specific data files should be stored. If $XDG_DATA_HOME is either not set or empty, a default equal to $HOME/.local/share should be used.

$XDG_CONFIG_HOME defines the base directory relative to which user specific configuration files should be stored. If $XDG_CONFIG_HOME is either not set or empty, a default equal to $HOME/.config should be used.

$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 seperated with a colon ':'.

If $XDG_DATA_DIRS is either not set or empty, a value equal to /usr/local/share/:/usr/share/ should be used.

freedesktop.org also provides a utility xdg-user-dirs:

xdg-user-dirs is a tool to help manage "well known" user directories like the desktop folder and the music folder. It also handles localization (i.e. translation) of the filenames.

$(XDG_CONFIG_HOME)/user-dirs.dirs specifies the current set of directories for the user. This file is in a shell format, so its easy to access from a shell script. This file can also be modified by users (manually or via applications) to change the directories used.


So, in case of FOLDERID_RoamingAppData, you should probably use one of $XDG_x depending on the use case, falling back to the appropriate default relative to $HOME as specified.

How Can I Find Default User Directories in Elixir

Not fully Elixir, but there is Erlang functions for that - filename:basedir/{2,3}

This function take the type of the directory as a first argument, which is one of the following atoms:

  • user_cache
  • user_config
  • user_data
  • user_log
  • site_config
  • site_data

And application name (also as atom) as a second argument.

Third argument may be a map containing:

  • os - OS type used for the paths creation, there are 3 recognisable values - windows, darwin (macOS), and linux (which will use XDG Base Directory Spec). Any other value will be treated as linux. If not provided, then OS is automatically detected by os:type/0 call.
  • author - used only on Windows
  • version - used only on Windows

Loading a config file from operation system independent place in python

Try:

os.path.expanduser('~/.programname')

On linux this will return:

>>> import os
>>> os.path.expanduser('~/.programname')
'/home/user/.programname'

On windows this will return:

>>> import os
>>> os.path.expanduser('~/.programname')
'C:\\Documents and Settings\\user/.programname'

Which is a little ugly, so you'll probably want to do this:

>>> import os
>>> os.path.join(os.path.expanduser('~'), '.programname')
'C:\\Documents and Settings\\user\\.programname'

EDIT: For what it's worth, the following apps on my Windows machine create their config folders in my Documents and Settings\user folder:

  • Android
  • AgroUML
  • Gimp
  • IPython

EDIT 2: Oh wow, I just noticed I put /user/.programname instead of /home/user/.programname for the linux example. Fixed.

What is the cross-platform way of obtaining the path to the local application data directory?

You could probably say something like (contradict me if I am wrong, or if this a bad approach)

private String workingDirectory;
//here, we assign the name of the OS, according to Java, to a variable...
private String OS = (System.getProperty("os.name")).toUpperCase();
//to determine what the workingDirectory is.
//if it is some version of Windows
if (OS.contains("WIN"))
{
//it is simply the location of the "AppData" folder
workingDirectory = System.getenv("AppData");
}
//Otherwise, we assume Linux or Mac
else
{
//in either case, we would start in the user's home directory
workingDirectory = System.getProperty("user.home");
//if we are on a Mac, we are not done, we look for "Application Support"
workingDirectory += "/Library/Application Support";
}
//we are now free to set the workingDirectory to the subdirectory that is our
//folder.

Note that, in this code, I am taking full advantage that Java treats '/' the same as '\\' when dealing with directories. Windows uses '\\' as pathSeparator, but it is happy with '/', too. (At least Windows 7 is.) It is also case-insensitive on it's environment variables; we could have just as easily said workingDirectory = System.getenv("APPDATA"); and it would have worked just as well.

Equivalents of XDG_CONFIG_HOME and XDG_DATA_HOME on Mac OS X?

I would use ~/Library/Application Support/script_name/. The subdirectories inside Application Support are used conventionally by various apps, including Apple's own softwares. But it's not enforced by the OS and not tied to apps inside /Applications. So you're perfectly free to create your own directory in it.

For the directory structure of OS X in general, see this Apple document.



Related Topics



Leave a reply



Submit