xdgbasedir: A Tcl Module to Access the XDG Base Directory Specification

  /     /   Programming     Tcl/Tk    

Unix has traditionally lacked a consistent way of storing user specific and system wide configuration and support files. This has lead to a mess of dot files in a user's home directory and other associated files being all over the file system. The XDG Base Directory Specification describes a simple and clean way to locate these files across all Unix-like systems. The xdgbasedir module allows you to easily access this specification from Tcl.

Why Use the XDG Base Directory Specification?

By storing an applications files using this spec. it makes it much easier for the user to find the files associated with your application so that they can edit and back them up. For the application, it makes it much easier to work with files in a consistent manner on whichever Unix platform it is running.

Ultimately the spec. allows you to turn this:

.                     Downloads           .lives            .scummvm
..                    .fontconfig         .lives-dir        .scummvmrc
.android              .FontForge          livestmp          .sitecopy
.aptitude             .gcjwebplugin       .local            .sitecopyrc
archive               .gconf              .lyrics           .slrnrc
.avm                  .gconfd             .mbox             .ssh
.balazar              .gegl-0.0           .mcoprc           .synaptic
.bash_history         .gem                .mess             .tdfsb
.bash_logout          .gitconfig          .metacity         Templates
.bash_profile         .gitk               Music             .themes
.bashrc               .gnash              .nexuiz           .thumbnails
.bochsrc              .gnome              .openMSX          .tremulous
.bogofilter           .gnome2             .openoffice.org   .tsclient
.bundler              gpodder-downloads   .orbit            .tvtime
.cache                .gramps             .pan2             Video
.Catapult             .gtkrc-1.2-gnome2   .pcmanx           .vim
.cgobanrc             .gtwitter           Pictures          .viminfo
.civserver_history    .gvfs               .pingus           .vimrc
.config               .gvimrc             Podcasts          .vlc
.conky                .hplip              Public            .w3m
.conkyrc              .ICEauthority       .purple           .wapi
.corewars             .icons              .q3a              .wmii
data                  .inkscape           .qt               .wmii-3.5
.dbus                 .irb-history        retro_archive     .Xauthority
Desktop               .jigdo-lite         retro             .xawtv
dev                   .libreoffice        rtb               .xchat2
.dmenu_cache          .liferea            .rvm
.dmrc                 .lincity            .sane
Documents             .liquidwarrc        .scorched3d

Into this:

.         .config   Documents           Music      retro
..        data      Downloads           Pictures   retro_archive
archive   Desktop   gpodder-downloads   Public     Templates
.cache    dev       .local              Podcasts   Videos

How Should I Locate Files Using the Specification?

A brief explanation of the spec. follows, for more look at the XDG Base Directory Specification recommended by freedesktop.org.

The specification uses six environmental variables to locate files. These are:

$XDG_DATA_HOMEFor user-specific data files.
$XDG_CONFIG_HOMEFor user-specific configuration files.
$XDG_CACHE_HOMEFor user-specific non-essential data.
$XDG_RUNTIME_DIRFor user-specific runtime files.
$XDG_DATA_DIRSA list of directories, in order of preference, which should be searched for data files.
$XDG_CONFIG_DIRSA list of directories, in order of preference, which should be searched for configuration files.

For each directory or for each directory in the list of directories (in the case of the environmental variables ending with _DIRS), you would normally append the name of your application. So for example if the $XDG_DATA_HOME variable returned ~/.local/share, and your application was called myapp, you should look in ~/.local/share/myapp/ for that application's user-specific data files.

The xdgbasedir module for Tcl

To ease working with the XDG Base Directory Specification from Tcl, I created the xdgbasedir module.

To access the XDG directories you would typically specify the subdirectory that these directories will be relative to. The subdirectory is normally the name of the application, myapp in this example:

package require xdgbasedir

puts "XDG_DATA_HOME: [XDG::DATA_HOME myapp]"

puts "XDG_DATA_DIRS: [XDG::DATA_DIRS myapp]"

The XDG procs ending in _DIRS return a list of directories in order of preference.

Getting xdgbasedir

You can download xdgbasedir from the archive page on github, and read the installation instructions in the README.md file.

Creative Commons License
xdgbasedir: A Tcl Module to Access the XDG Base Directory Specification by Lawrence Woodman is licensed under a Creative Commons Attribution 4.0 International License.

Related Articles

Compiling a Tcl Script into an Executable

Locating Tcl scripts to load from an executable can be awkward if you want to make your program cross-platform. An easier way is to compile a Tcl script directly into the executable and let that scrip...   Read More

Embedding an SDL Surface in a Tk Window

Tk is great, but sometimes it just isn't fast enough. SDL is fast, but has no support for input dialogs and other GUI conventions. By embedding an SDL surface in a Tk window you get the best of both ...   Read More

Advent of Code 2018: 25 Days, 25 Languages

There are lots of different programming challenges, but this year one in particular has caught my attention and I have decided to take part. Advent of Code starts 1st December with daily programming p...   Read More

Beware of Immutable Lists for F# Parallel Processing

With F#, the list often feels like the default choice of data structure. It is immutable and hence easy to reason about, however its use can come at a great cost. If you are using lists to process la...   Read More

Adding a Basic Stub to a Vic-20 Assembly Language Program

To make machine language programs more friendly it is nice to add a basic stub which contains a line with a SYS statement to start the code. This is easy to do on the Vic-20 and the process gives you ...   Read More

Sign up to get new articles straight to your inbox.

Delivered by FeedBurner


blog comments powered by Disqus