Fixed bug 3639 - SDL_GetPrefPath returns a path with two consecutive slashes on Unix if org is omitted
authorSam Lantinga
Fri, 11 Aug 2017 11:32:00 -0700
changeset 11222083133ba8f9b
parent 11221 4753f2320927
child 11223 62db9336e07b
Fixed bug 3639 - SDL_GetPrefPath returns a path with two consecutive slashes on Unix if org is omitted

Fabian Greffrath

we use SDL_GetPrefPath() in Chocolate Doom to get a reasonable directory to save and restore config files and savegames:

https://github.com/chocolate-doom/chocolate-doom/blob/sdl2-branch/src/m_config.c#L2162

However, since there is no "organization" behind Chocolate Doom and there is really only one "product" called Chocolate Doom, we pass an empty string for the org parameter and the package string for app.

This leads to two consecutive slashes in the path returned by SDL_GetPrefPath() like this:

/home/user/.local/share//chocolate-doom/

While this is harmless, it sure looks bad.

I believe that it should be possible to either pass a NULL pointer for the org parameter or at least have the function detect an empty string as a means to express "there is no origanization, just a single product". The generation of the path string to be returned by the function will have to get adapted accordingly.
src/filesystem/cocoa/SDL_sysfilesystem.m
src/filesystem/emscripten/SDL_sysfilesystem.c
src/filesystem/haiku/SDL_sysfilesystem.cc
src/filesystem/nacl/SDL_sysfilesystem.c
src/filesystem/unix/SDL_sysfilesystem.c
src/filesystem/windows/SDL_sysfilesystem.c
src/filesystem/winrt/SDL_sysfilesystem.cpp
test/testfilesystem.c
     1.1 --- a/src/filesystem/cocoa/SDL_sysfilesystem.m	Fri Aug 11 10:42:26 2017 -0700
     1.2 +++ b/src/filesystem/cocoa/SDL_sysfilesystem.m	Fri Aug 11 11:32:00 2017 -0700
     1.3 @@ -71,8 +71,15 @@
     1.4  SDL_GetPrefPath(const char *org, const char *app)
     1.5  { @autoreleasepool
     1.6  {
     1.7 +    if (!app) {
     1.8 +        SDL_InvalidParamError("app");
     1.9 +        return NULL;
    1.10 +    }
    1.11 +    if (!org) {
    1.12 +        org = "";
    1.13 +    }
    1.14 +
    1.15      char *retval = NULL;
    1.16 -
    1.17      NSArray *array = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    1.18  
    1.19      if ([array count] > 0) {  /* we only want the first item in the list. */
    1.20 @@ -85,7 +92,11 @@
    1.21                  SDL_OutOfMemory();
    1.22              } else {
    1.23                  char *ptr;
    1.24 -                SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
    1.25 +                if (*org) {
    1.26 +                    SDL_snprintf(retval, len, "%s/%s/%s/", base, org, app);
    1.27 +                } else {
    1.28 +                    SDL_snprintf(retval, len, "%s/%s/", base, app);
    1.29 +                }
    1.30                  for (ptr = retval+1; *ptr; ptr++) {
    1.31                      if (*ptr == '/') {
    1.32                          *ptr = '\0';
     2.1 --- a/src/filesystem/emscripten/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
     2.2 +++ b/src/filesystem/emscripten/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
     2.3 @@ -46,6 +46,14 @@
     2.4      char *retval;
     2.5      size_t len = 0;
     2.6  
     2.7 +    if (!app) {
     2.8 +        SDL_InvalidParamError("app");
     2.9 +        return NULL;
    2.10 +    }
    2.11 +    if (!org) {
    2.12 +        org = "";
    2.13 +    }
    2.14 +
    2.15      len = SDL_strlen(append) + SDL_strlen(org) + SDL_strlen(app) + 3;
    2.16      retval = (char *) SDL_malloc(len);
    2.17      if (!retval) {
    2.18 @@ -53,7 +61,11 @@
    2.19          return NULL;
    2.20      }
    2.21  
    2.22 -    SDL_snprintf(retval, len, "%s%s/%s/", append, org, app);
    2.23 +    if (*org) {
    2.24 +        SDL_snprintf(retval, len, "%s%s/%s/", append, org, app);
    2.25 +    } else {
    2.26 +        SDL_snprintf(retval, len, "%s%s/", append, app);
    2.27 +    }
    2.28  
    2.29      if (mkdir(retval, 0700) != 0 && errno != EEXIST) {
    2.30          SDL_SetError("Couldn't create directory '%s': '%s'", retval, strerror(errno));
     3.1 --- a/src/filesystem/haiku/SDL_sysfilesystem.cc	Fri Aug 11 10:42:26 2017 -0700
     3.2 +++ b/src/filesystem/haiku/SDL_sysfilesystem.cc	Fri Aug 11 11:32:00 2017 -0700
     3.3 @@ -77,6 +77,15 @@
     3.4      const char *home = SDL_getenv("HOME");
     3.5      const char *append = "/config/settings/";
     3.6      size_t len = SDL_strlen(home);
     3.7 +
     3.8 +    if (!app) {
     3.9 +        SDL_InvalidParamError("app");
    3.10 +        return NULL;
    3.11 +    }
    3.12 +    if (!org) {
    3.13 +        org = "";
    3.14 +    }
    3.15 +
    3.16      if (!len || (home[len - 1] == '/')) {
    3.17          ++append; // home empty or ends with separator, skip the one from append
    3.18      }
    3.19 @@ -85,7 +94,11 @@
    3.20      if (!retval) {
    3.21          SDL_OutOfMemory();
    3.22      } else {
    3.23 -        SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
    3.24 +        if (*org) {
    3.25 +            SDL_snprintf(retval, len, "%s%s%s/%s/", home, append, org, app);
    3.26 +        } else {
    3.27 +            SDL_snprintf(retval, len, "%s%s%s/", home, append, app);
    3.28 +        }
    3.29          create_directory(retval, 0700);  // Haiku api: creates missing dirs
    3.30      }
    3.31  
     4.1 --- a/src/filesystem/nacl/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
     4.2 +++ b/src/filesystem/nacl/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
     4.3 @@ -40,3 +40,4 @@
     4.4  
     4.5  #endif /* SDL_FILESYSTEM_NACL */
     4.6  
     4.7 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- a/src/filesystem/unix/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
     5.2 +++ b/src/filesystem/unix/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
     5.3 @@ -180,6 +180,14 @@
     5.4      char *ptr = NULL;
     5.5      size_t len = 0;
     5.6  
     5.7 +    if (!app) {
     5.8 +        SDL_InvalidParamError("app");
     5.9 +        return NULL;
    5.10 +    }
    5.11 +    if (!org) {
    5.12 +        org = "";
    5.13 +    }
    5.14 +
    5.15      if (!envr) {
    5.16          /* You end up with "$HOME/.local/share/Game Name 2" */
    5.17          envr = SDL_getenv("HOME");
    5.18 @@ -204,7 +212,11 @@
    5.19          return NULL;
    5.20      }
    5.21  
    5.22 -    SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
    5.23 +    if (*org) {
    5.24 +        SDL_snprintf(retval, len, "%s%s%s/%s/", envr, append, org, app);
    5.25 +    } else {
    5.26 +        SDL_snprintf(retval, len, "%s%s%s/", envr, append, app);
    5.27 +    }
    5.28  
    5.29      for (ptr = retval+1; *ptr; ptr++) {
    5.30          if (*ptr == '/') {
     6.1 --- a/src/filesystem/windows/SDL_sysfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
     6.2 +++ b/src/filesystem/windows/SDL_sysfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
     6.3 @@ -118,6 +118,14 @@
     6.4      size_t new_wpath_len = 0;
     6.5      BOOL api_result = FALSE;
     6.6  
     6.7 +    if (!app) {
     6.8 +        SDL_InvalidParamError("app");
     6.9 +        return NULL;
    6.10 +    }
    6.11 +    if (!org) {
    6.12 +        org = "";
    6.13 +    }
    6.14 +
    6.15      if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path))) {
    6.16          WIN_SetError("Couldn't locate our prefpath");
    6.17          return NULL;
    6.18 @@ -145,8 +153,10 @@
    6.19          return NULL;
    6.20      }
    6.21  
    6.22 -    lstrcatW(path, L"\\");
    6.23 -    lstrcatW(path, worg);
    6.24 +    if (*worg) {
    6.25 +        lstrcatW(path, L"\\");
    6.26 +        lstrcatW(path, worg);
    6.27 +    }
    6.28      SDL_free(worg);
    6.29  
    6.30      api_result = CreateDirectoryW(path, NULL);
     7.1 --- a/src/filesystem/winrt/SDL_sysfilesystem.cpp	Fri Aug 11 10:42:26 2017 -0700
     7.2 +++ b/src/filesystem/winrt/SDL_sysfilesystem.cpp	Fri Aug 11 11:32:00 2017 -0700
     7.3 @@ -152,6 +152,14 @@
     7.4      size_t new_wpath_len = 0;
     7.5      BOOL api_result = FALSE;
     7.6  
     7.7 +    if (!app) {
     7.8 +        SDL_InvalidParamError("app");
     7.9 +        return NULL;
    7.10 +    }
    7.11 +    if (!org) {
    7.12 +        org = "";
    7.13 +    }
    7.14 +
    7.15      srcPath = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_LOCAL_FOLDER);
    7.16      if ( ! srcPath) {
    7.17          SDL_SetError("Unable to find a source path");
    7.18 @@ -186,9 +194,11 @@
    7.19          return NULL;
    7.20      }
    7.21  
    7.22 -    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
    7.23 -    SDL_wcslcat(path, worg, new_wpath_len + 1);
    7.24 -    SDL_free(worg);
    7.25 +    if (*worg) {
    7.26 +        SDL_wcslcat(path, L"\\", new_wpath_len + 1);
    7.27 +        SDL_wcslcat(path, worg, new_wpath_len + 1);
    7.28 +        SDL_free(worg);
    7.29 +    }
    7.30  
    7.31      api_result = CreateDirectoryW(path, NULL);
    7.32      if (api_result == FALSE) {
    7.33 @@ -219,3 +229,5 @@
    7.34  }
    7.35  
    7.36  #endif /* __WINRT__ */
    7.37 +
    7.38 +/* vi: set ts=4 sw=4 expandtab: */
     8.1 --- a/test/testfilesystem.c	Fri Aug 11 10:42:26 2017 -0700
     8.2 +++ b/test/testfilesystem.c	Fri Aug 11 11:32:00 2017 -0700
     8.3 @@ -46,6 +46,15 @@
     8.4      SDL_Log("pref path: '%s'\n", pref_path); 
     8.5      SDL_free(pref_path);
     8.6  
     8.7 +    pref_path = SDL_GetPrefPath(NULL, "testfilesystem");
     8.8 +    if(pref_path == NULL){
     8.9 +      SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't find pref path without organization: %s\n",
    8.10 +                   SDL_GetError());
    8.11 +      return 1;
    8.12 +    }
    8.13 +    SDL_Log("pref path: '%s'\n", pref_path); 
    8.14 +    SDL_free(pref_path);
    8.15 +
    8.16      SDL_Quit();
    8.17      return 0;
    8.18  }