WinRT: bug and data-integrity fixes for SDL_GetPrefPath()
authorDavid Ludwig <dludwig@pobox.com>
Sat, 29 Nov 2014 10:09:30 -0500
changeset 9247eddb899239fe
parent 9246 a761913e5e91
child 9248 dabd5df43970
WinRT: bug and data-integrity fixes for SDL_GetPrefPath()

This change does a few things, all with regards to the WinRT implementation of
SDL_GetPrefPath():

1. it fixes a bug whereby SDL_GetPrefPath() did not create the directory it
returned. On other SDL platforms, SDL_GetPrefPath() will create separate
directories for its 'org' and 'app' folders. Without this, attempts to create
files in the pref-path would fail, unless those directories were first created
by the app, or by some other library the app used. This change makes sure
that these directories get created, before SDL_GetPrefPath() returns to its
caller(s).


2. it defaults to having SDL_GetPrefPath() return a WinRT 'Local' folder
on all platforms. Previously, for Windows Store apps, it would have used a
different, 'Roaming' folder. Files in Roaming folders can be automatically,
and synchronized across multiple devices by Windows. This synchronization can
happen while the app runs, with new files being copied into a running app's
pref-path. Unless an app is specifically designed to handle this scenario,
there is a chance that save-data could be overwritten in unwanted or
unexpected ways.

The default is now to use a Local folder, which does not get synchronized, and
which is arguably a bit safer to use. Apps that wish to use Roaming folders
can do so by setting SDL_HINT_WINRT_PREF_PATH_ROOT to "roaming", however it
is recommended that one first read Microsoft's documentation for Roaming
files, a link to which is provided in README-winrt.md.

To preserve older pref-path selection behavior (found in SDL 2.0.3, as well as
many pre-2.0.4 versions of SDL from hg.libsdl.org), which uses a Roaming path
in Windows Store apps, and a Local path in Windows Phone, set
SDL_HINT_WINRT_PREF_PATH_ROOT to "old".

Please note that Roaming paths are not supported on Windows Phone 8.0, due to
limitations in the OS itself. Attempts to use this will fail.
(Windows Phone 8.1 does not have this limitation, however.)


3. It makes SDL_GetPrefPath(), when on Windows Phone 8.0, and when
SDL_HINT_WINRT_PREF_PATH_ROOT is set to "roaming", return NULL, rather than
silently defaulting to a Local path (then switching to a Roaming path if and
when the user upgraded to Windows Phone 8.1).
docs/README-winrt.md
include/SDL_hints.h
src/filesystem/winrt/SDL_sysfilesystem.cpp
     1.1 --- a/docs/README-winrt.md	Fri Nov 28 04:51:33 2014 -0800
     1.2 +++ b/docs/README-winrt.md	Sat Nov 29 10:09:30 2014 -0500
     1.3 @@ -119,27 +119,64 @@
     1.4  Caveats
     1.5  -------
     1.6  
     1.7 -#### SDL_GetPrefPath() usage
     1.8 +#### SDL_GetPrefPath() usage when upgrading existing WinRT apps to SDL 2.0.4
     1.9  
    1.10 -SDL_GetPrefPath() is available for use in WinRT apps, however the following
    1.11 -should be noted:
    1.12 +SDL 2.0.4 fixes two bugs found in SDL_GetPrefPath() which can affect
    1.13 +an app's save data.  These bugs only apply to WinRT apps (and not
    1.14 +Windows Desktop / Win32 apps, or to apps on any other SDL platform).
    1.15 +In particular, for older versions of SDL (anything before 2.0.4):
    1.16  
    1.17 -1. It will return different path types, by default, depending on the WinRT
    1.18 -   platform.  Windows Phone apps will default to using the app's "local" path,
    1.19 -   whereas Windows Store (i.e. non-Phone) apps will default to using the app's
    1.20 -   "roaming" path.  This behavior can be changed by calling SDL_SetHint() with
    1.21 -   the key, SDL_HINT_WINRT_PREF_PATH_ROOT, and a value of either "local" or
    1.22 -   "roaming".
    1.23 +1. SDL_GetPrefPath() would return an invalid path, one in which attempts
    1.24 +   to write files to would fail, in many cases.  Some of the path elements
    1.25 +   returned by SDL_GetPrefPath() would not get created (as done on other
    1.26 +   SDL platforms).  Files could be written to this path, however apps would
    1.27 +   need to explicitly create the missing directories first.
    1.28 +   
    1.29 +2. SDL_GetPrefPath() would return a path inside a WinRT 'Roaming' folder,
    1.30 +   the contents of which could get automatically synchronized across multiple
    1.31 +   devices, by Windows.  This process could occur while an app was running.
    1.32 +   Apps which were not explicitly built to handle this scenario could
    1.33 +   have their SDL_GetPrefPath-backed save data swapped out by Windows at
    1.34 +   unexpected times, which raised potential for data-loss (if apps weren't
    1.35 +   designed to support live file-synchronization.)
    1.36  
    1.37 -2. Windows Phone 8.0 does not provide apps access to a "roaming" folder.
    1.38 -   Attempts to make SDL_GetPrefPath() return a roaming folder on Windows
    1.39 -   Phone 8.0 will be ignored (and a path inside the "local" folder will be
    1.40 -   used instead).
    1.41  
    1.42 -Further details on this can be found in the documentation for
    1.43 -SDL_HINT_WINRT_PREF_PATH_ROOT, in SDL_hints.h, as well as the docs for
    1.44 -SDL_WinRT_Path, SDL_WinRTGetFSPathUNICODE, and SDL_WinRTGetFSPathUTF8,
    1.45 -in SDL_system.h.
    1.46 +SDL_GetPrefPath(), starting with SDL 2.0.4, addresses these by:
    1.47 +
    1.48 +1. making sure that SDL_GetPrefPath() returns a directory in which data
    1.49 +   can be written to immediately, without first needing to create directories.
    1.50 +
    1.51 +2. basing SDL_GetPrefPath() off of a non-Roaming / 'Local' folder, the
    1.52 +   contents of which do not get automatically synchronized across devices,
    1.53 +   and which may be safer in terms of data-integrity.
    1.54 +   
    1.55 +   Apps can, at their discretion, choose to utilize WinRT's Roaming
    1.56 +   functionality by calling the following before calling SDL_GetPrefPath():
    1.57 +   
    1.58 +       SDL_SetHint(SDL_HINT_WINRT_PREF_PATH_ROOT, "roaming");
    1.59 +
    1.60 +   Alternatively, to restore SDL_GetPrefPath()'s old behavior (found in
    1.61 +   SDL 2.0.3, and in many pre-2.0.4 versions of SDL found on hg.libsdl.org),
    1.62 +   whereby a Roaming path is returned for Windows Store apps, and a Local
    1.63 +   folder is returned for Windows Phone apps, use the following code:
    1.64 +   
    1.65 +       SDL_SetHint(SDL_HINT_WINRT_PREF_PATH_ROOT, "old");
    1.66 +   
    1.67 +   Before using Roaming data in any capacity, it is highly recommended that
    1.68 +   one read the following:
    1.69 +   
    1.70 +   1. Microsoft's documentation on the Roaming data.  Details on this can be
    1.71 +      found on MSDN, at:
    1.72 +      [Guidelines for roaming app data](http://msdn.microsoft.com/en-us/library/windows/apps/hh465094.aspx).
    1.73 +   
    1.74 +   2. the SDL documentation for SDL_HINT_WINRT_PREF_PATH_ROOT, which is
    1.75 +      listed inside SDL_hints.h.
    1.76 +
    1.77 +   Please note that Roaming support is not available on Windows Phone 8.0,
    1.78 +   due to limitations in the OS itself.  Attempts to use it will fail, with
    1.79 +   SDL_GetPrefPath() returning NULL (if SDL_HINT_WINRT_PREF_PATH_ROOT is
    1.80 +   set to "roaming" on that platform).  Windows Phone 8.1 does not have this
    1.81 +   limitation, and does support Roaming data.
    1.82  
    1.83  
    1.84  
     2.1 --- a/include/SDL_hints.h	Fri Nov 28 04:51:33 2014 -0800
     2.2 +++ b/include/SDL_hints.h	Sat Nov 29 10:09:30 2014 -0500
     2.3 @@ -495,16 +495,25 @@
     2.4   *  \brief  A variable that dictates what SDL_GetPrefPath() returns in WinRT apps.
     2.5   *
     2.6   *  The variable can be set to the following values:
     2.7 - *    "local"   - Use the app's 'local' folder to store data; default for
     2.8 - *                Windows Phone apps.
     2.9 - *    "roaming" - Use the app's 'roaming' folder to store data; default for
    2.10 - *                Windows Store (non-Phone) apps.  On Windows Phone 8.0, this
    2.11 - *                setting will be ignored (and the 'local' folder will be used
    2.12 - *                instead), as the OS does not support roaming folders.
    2.13 + *   * "local"   - Use the app's 'local' folder to store data.
    2.14 + *   * "roaming" - Use the app's 'roaming' folder to store data.
    2.15 + *                 On Windows Phone 8.0, this setting is not supported due to
    2.16 + *                 limitations in the OS itself.  Attempts to use this (via
    2.17 + *                 SDL_GetPrefPath()) on Windows Phone 8.0 will fail, with
    2.18 + *                 SDL_GetPrefPath() returning NULL.  (Windows Phone 8.1 does,
    2.19 + *                 however, support roaming folders.)
    2.20 + *   * "old"     - Use the app's 'local' folder on Windows Phone, and 'roaming'
    2.21 + *                 on non-Phone versions of WinRT.  This mimics behavior found
    2.22 + *                 in SDL 2.0.3's implementation of SDL_GetPrefPath() for WinRT
    2.23 + *                 (and was changed for SDL 2.0.4, further details of which are
    2.24 + *                 in the "Caveats" section of SDL's
    2.25 + *                 [WinRT README file](README-winrt.md).
    2.26   *
    2.27 - *  Details on 'local' verses 'roaming' folders can be found on MSDN, in the
    2.28 - *  documentation for WinRT's Windows.Storage.ApplicationData class, which is
    2.29 - *  available at http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata
    2.30 + *  The default is to use the app's "local" folder.
    2.31 + *
    2.32 + *  Details on 'local' verses 'roaming' folders can be found on MSDN, in
    2.33 + *  the documentation for WinRT's Windows.Storage.ApplicationData class,
    2.34 + *  (available at http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata ).
    2.35   *
    2.36   *  The application's local and roaming paths may, alternatively, be retrieved
    2.37   *  via the SDL_WinRTGetFSPathUTF8() and SDL_WinRTGetFSPathUNICODE() functions,
     3.1 --- a/src/filesystem/winrt/SDL_sysfilesystem.cpp	Fri Nov 28 04:51:33 2014 -0800
     3.2 +++ b/src/filesystem/winrt/SDL_sysfilesystem.cpp	Sat Nov 29 10:09:30 2014 -0500
     3.3 @@ -144,49 +144,121 @@
     3.4       * without violating Microsoft's app-store requirements.
     3.5       */
     3.6  
     3.7 -#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
     3.8 -    /* A 'Roaming' folder is not available in Windows Phone 8.0, however a
     3.9 -     * 'Local' folder is.  Use the 'Local' folder in order to preserve
    3.10 -     * compatibility with Windows Phone 8.0, and with app-installs that have
    3.11 -     * been updated from 8.0-based, to 8.1-based apps.
    3.12 +    /* Default to using a Local/non-Roaming path.  WinRT will often attempt
    3.13 +     * to synchronize files in Roaming paths, and will do so while an app is
    3.14 +     * running.  Using a Local path prevents the possibility that an app's
    3.15 +     * save-data files will get changed from underneath it, without it
    3.16 +     * being ready.
    3.17 +     *
    3.18 +     * This behavior can be changed via use of the
    3.19 +     * SDL_HINT_WINRT_PREF_PATH_ROOT hint.
    3.20       */
    3.21      SDL_WinRT_Path pathType = SDL_WINRT_PATH_LOCAL_FOLDER;
    3.22 -#else
    3.23 -    /* A 'Roaming' folder is available on Windows 8 and 8.1.  Use that.
    3.24 -     */
    3.25 -    SDL_WinRT_Path pathType = SDL_WINRT_PATH_ROAMING_FOLDER;
    3.26 -#endif
    3.27  
    3.28      const char * hint = SDL_GetHint(SDL_HINT_WINRT_PREF_PATH_ROOT);
    3.29      if (hint) {
    3.30          if (SDL_strcasecmp(hint, "local") == 0) {
    3.31              pathType = SDL_WINRT_PATH_LOCAL_FOLDER;
    3.32 +        } else if (SDL_strcasecmp(hint, "roaming") == 0) {
    3.33 +#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
    3.34 +            pathType = SDL_WINRT_PATH_ROAMING_FOLDER;
    3.35 +#else
    3.36 +            /* Don't apply a 'Roaming' path on Windows Phone 8.0.  Roaming
    3.37 +             * data is not supported by that version of the operating system.
    3.38 +             */
    3.39 +            SDL_SetError("A Roaming path was specified via SDL_HINT_WINRT_PREF_PATH_ROOT, but Roaming is not supported on Windows Phone 8.0");
    3.40 +            return NULL;
    3.41 +#endif
    3.42 +        } else if (SDL_strcasecmp(hint, "old") == 0) {
    3.43 +            /* Older versions of SDL/WinRT, including 2.0.3, would return a
    3.44 +             * pref-path that used a Roaming folder on non-Phone versions of
    3.45 +             * Windows, such as Windows 8.0 and Windows 8.1.  This has since
    3.46 +             * been reverted to using a Local folder, in order to prevent
    3.47 +             * problems arising from WinRT automatically synchronizing files
    3.48 +             * during an app's lifetime.  In case this functionality is
    3.49 +             * desired, setting SDL_HINT_WINRT_PREF_PATH_ROOT to "old" will
    3.50 +             * trigger the older behavior.
    3.51 +             */
    3.52 +#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
    3.53 +            pathType = SDL_WINRT_PATH_LOCAL_FOLDER;
    3.54 +#else
    3.55 +            pathType = SDL_WINRT_PATH_ROAMING_FOLDER;
    3.56 +#endif
    3.57          }
    3.58 -#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
    3.59 -        else if (SDL_strcasecmp(hint, "roaming") == 0) {
    3.60 -            pathType = SDL_WINRT_PATH_ROAMING_FOLDER;
    3.61 -        }
    3.62 -#endif
    3.63      }
    3.64  
    3.65 -    const char * srcPath = SDL_WinRTGetFSPathUTF8(pathType);
    3.66 -    size_t destPathLen;
    3.67 -    char * destPath = NULL;
    3.68 +    const WCHAR * srcPath = NULL;
    3.69 +    WCHAR path[MAX_PATH];
    3.70 +    char *retval = NULL;
    3.71 +    WCHAR* worg = NULL;
    3.72 +    WCHAR* wapp = NULL;
    3.73 +    size_t new_wpath_len = 0;
    3.74 +    BOOL api_result = FALSE;
    3.75  
    3.76 -    if (!srcPath) {
    3.77 -        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
    3.78 +    srcPath = SDL_WinRTGetFSPathUNICODE(pathType);
    3.79 +    if ( ! srcPath) {
    3.80 +        SDL_SetError("Unable to find a source path");
    3.81          return NULL;
    3.82      }
    3.83  
    3.84 -    destPathLen = SDL_strlen(srcPath) + SDL_strlen(org) + SDL_strlen(app) + 4;
    3.85 -    destPath = (char *) SDL_malloc(destPathLen);
    3.86 -    if (!destPath) {
    3.87 +    if (SDL_wcslen(srcPath) >= MAX_PATH) {
    3.88 +        SDL_SetError("Path too long.");
    3.89 +        return NULL;
    3.90 +    }
    3.91 +    SDL_wcslcpy(path, srcPath, SDL_arraysize(path));
    3.92 +
    3.93 +    worg = WIN_UTF8ToString(org);
    3.94 +    if (worg == NULL) {
    3.95          SDL_OutOfMemory();
    3.96          return NULL;
    3.97      }
    3.98  
    3.99 -    SDL_snprintf(destPath, destPathLen, "%s\\%s\\%s\\", srcPath, org, app);
   3.100 -    return destPath;
   3.101 +    wapp = WIN_UTF8ToString(app);
   3.102 +    if (wapp == NULL) {
   3.103 +        SDL_free(worg);
   3.104 +        SDL_OutOfMemory();
   3.105 +        return NULL;
   3.106 +    }
   3.107 +
   3.108 +    new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3;
   3.109 +
   3.110 +    if ((new_wpath_len + 1) > MAX_PATH) {
   3.111 +        SDL_free(worg);
   3.112 +        SDL_free(wapp);
   3.113 +        SDL_SetError("Path too long.");
   3.114 +        return NULL;
   3.115 +    }
   3.116 +
   3.117 +    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
   3.118 +    SDL_wcslcat(path, worg, new_wpath_len + 1);
   3.119 +    SDL_free(worg);
   3.120 +
   3.121 +    api_result = CreateDirectoryW(path, NULL);
   3.122 +    if (api_result == FALSE) {
   3.123 +        if (GetLastError() != ERROR_ALREADY_EXISTS) {
   3.124 +            SDL_free(wapp);
   3.125 +            WIN_SetError("Couldn't create a prefpath.");
   3.126 +            return NULL;
   3.127 +        }
   3.128 +    }
   3.129 +
   3.130 +    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
   3.131 +    SDL_wcslcat(path, wapp, new_wpath_len + 1);
   3.132 +    SDL_free(wapp);
   3.133 +
   3.134 +    api_result = CreateDirectoryW(path, NULL);
   3.135 +    if (api_result == FALSE) {
   3.136 +        if (GetLastError() != ERROR_ALREADY_EXISTS) {
   3.137 +            WIN_SetError("Couldn't create a prefpath.");
   3.138 +            return NULL;
   3.139 +        }
   3.140 +    }
   3.141 +
   3.142 +    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
   3.143 +
   3.144 +    retval = WIN_StringToUTF8(path);
   3.145 +
   3.146 +    return retval;
   3.147  }
   3.148  
   3.149  #endif /* __WINRT__ */