milkytracker / MilkyTracker

An FT2 compatible music tracker
http://milkytracker.github.io/
Other
1.72k stars 162 forks source link

Support for the XDG Base Directory Specification #12

Closed Earnestly closed 9 years ago

Earnestly commented 10 years ago

This is a feature request for supporting the XDG Base Directory Specification.

The specification works around a bug during the early UNIX v2 rewrite which caused files prepended with a '.' to be ignored from the output of ls. While this "bug" has become a feature for some, it has also become a headache for users when developers continue to assume HOME is a great place to dump configuration files and local caches.

To address these issues XDG Basedir was formed to give developers a standard location for these files (much like how / is well structured with /usr/share, /var and /etc) and giving the users control over where they are placed in their HOME.

If you were to support the XDG specification the following locations would change:

# XDG_CONFIG_HOME should expand to what the user set or fallback to "$HOME"/.config"
"$HOME"/.milkytracker_config → "$XDG_CONFIG_HOME"/milkytracker/config

A basic patch might look something like this; although I'm not sure what milkytracker_temp is for. (Note: This was done long ago, I haven't invested much into milkytracker for the last year or more.)

_(Edit: I noticed now that I don't correctly reset home to getenv("HOME"); if it fails to get XDG_CONFIG_HOME, oops :P)_

diff --git a/src/ppui/osinterface/posix/PPSystem_POSIX.cpp b/src/ppui/osinterface/posix/PPSystem_POSIX.cpp
index 49f2869..e683779 100644
--- a/src/ppui/osinterface/posix/PPSystem_POSIX.cpp
+++ b/src/ppui/osinterface/posix/PPSystem_POSIX.cpp
@@ -59,28 +59,28 @@ const SYSCHAR* System::getTempFileName()
    // create something that "might" work out
    if (buffer == NULL)
    {
-       char *home = getenv("HOME");
+       char *home = getenv("XDG_CONFIG_HOME");
        if(home)
        {
            strcpy(buffer, home);
-           strcat(buffer, "/.milkytracker_temp");
+           strcat(buffer, "/milkytracker/temp");
        }
        else
-           strcpy(buffer, "milkytracker_temp");
+           strcpy(buffer, ".config/milkytracker/temp");
    }
    return buffer;
 }

 const SYSCHAR* System::getConfigFileName()
 {
-   char *home = getenv("HOME");
+   char *home = getenv("XDG_CONFIG_HOME");
    if(home)
    {
        strcpy(buffer, home);
-       strcat(buffer, "/.milkytracker_config");
+       strcat(buffer, "/milkytracker/config");
    }
    else
-       strcpy(buffer, "milkytracker_config");
+       strcpy(buffer, ".config/milkytracker/config");

    return buffer;
 }

Thanks o/

Deltafire commented 10 years ago

Looks good. The temporary files probably shouldn't go into the config directory though, a better place would be /tmp - perhaps using the suggestions in the comments; and of course we'd need to fall back to $HOME if $XDG_CONFIG_HOME isn't set.

Earnestly commented 10 years ago

Regarding temporary files the correct location would likely be XDG_CACHE_HOME (there is XDG_RUNTIME_DIR but that is typically for sockets or fifos that are bound to the user's login session).

The way the standard works is that these environment variables shouldn't be set by default and the fallback should be used. Each variable has a defined fallback, for example nn the case of XDG_CONFIG_HOME not being set, you should fallback to HOME/.config.

Likewise for XDG_CACHE_HOME, the fallback is HOME/.cache

For legacy projects moving to XDG they do often add a clause which first looks for the old file location, such as HOME/.progn/config and only then falling back to the XDG Base Dir. You may want to consider doing that here.

Earnestly commented 9 years ago

Wow, thank you, I had forgotten about this.