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).
This commit is contained in:
David Ludwig 2014-11-29 10:09:30 -05:00
parent 4dab32a2bc
commit ce64b4ad3a
3 changed files with 167 additions and 49 deletions

View File

@ -119,27 +119,64 @@ Here is a rough list of what works, and what doens't:
Caveats Caveats
------- -------
#### SDL_GetPrefPath() usage #### SDL_GetPrefPath() usage when upgrading existing WinRT apps to SDL 2.0.4
SDL_GetPrefPath() is available for use in WinRT apps, however the following SDL 2.0.4 fixes two bugs found in SDL_GetPrefPath() which can affect
should be noted: an app's save data. These bugs only apply to WinRT apps (and not
Windows Desktop / Win32 apps, or to apps on any other SDL platform).
In particular, for older versions of SDL (anything before 2.0.4):
1. It will return different path types, by default, depending on the WinRT 1. SDL_GetPrefPath() would return an invalid path, one in which attempts
platform. Windows Phone apps will default to using the app's "local" path, to write files to would fail, in many cases. Some of the path elements
whereas Windows Store (i.e. non-Phone) apps will default to using the app's returned by SDL_GetPrefPath() would not get created (as done on other
"roaming" path. This behavior can be changed by calling SDL_SetHint() with SDL platforms). Files could be written to this path, however apps would
the key, SDL_HINT_WINRT_PREF_PATH_ROOT, and a value of either "local" or need to explicitly create the missing directories first.
"roaming".
2. SDL_GetPrefPath() would return a path inside a WinRT 'Roaming' folder,
the contents of which could get automatically synchronized across multiple
devices, by Windows. This process could occur while an app was running.
Apps which were not explicitly built to handle this scenario could
have their SDL_GetPrefPath-backed save data swapped out by Windows at
unexpected times, which raised potential for data-loss (if apps weren't
designed to support live file-synchronization.)
2. Windows Phone 8.0 does not provide apps access to a "roaming" folder.
Attempts to make SDL_GetPrefPath() return a roaming folder on Windows
Phone 8.0 will be ignored (and a path inside the "local" folder will be
used instead).
Further details on this can be found in the documentation for SDL_GetPrefPath(), starting with SDL 2.0.4, addresses these by:
SDL_HINT_WINRT_PREF_PATH_ROOT, in SDL_hints.h, as well as the docs for
SDL_WinRT_Path, SDL_WinRTGetFSPathUNICODE, and SDL_WinRTGetFSPathUTF8, 1. making sure that SDL_GetPrefPath() returns a directory in which data
in SDL_system.h. can be written to immediately, without first needing to create directories.
2. basing SDL_GetPrefPath() off of a non-Roaming / 'Local' folder, the
contents of which do not get automatically synchronized across devices,
and which may be safer in terms of data-integrity.
Apps can, at their discretion, choose to utilize WinRT's Roaming
functionality by calling the following before calling SDL_GetPrefPath():
SDL_SetHint(SDL_HINT_WINRT_PREF_PATH_ROOT, "roaming");
Alternatively, to restore SDL_GetPrefPath()'s old behavior (found in
SDL 2.0.3, and in many pre-2.0.4 versions of SDL found on hg.libsdl.org),
whereby a Roaming path is returned for Windows Store apps, and a Local
folder is returned for Windows Phone apps, use the following code:
SDL_SetHint(SDL_HINT_WINRT_PREF_PATH_ROOT, "old");
Before using Roaming data in any capacity, it is highly recommended that
one read the following:
1. Microsoft's documentation on the Roaming data. Details on this can be
found on MSDN, at:
[Guidelines for roaming app data](http://msdn.microsoft.com/en-us/library/windows/apps/hh465094.aspx).
2. the SDL documentation for SDL_HINT_WINRT_PREF_PATH_ROOT, which is
listed inside SDL_hints.h.
Please note that Roaming support is not available on Windows Phone 8.0,
due to limitations in the OS itself. Attempts to use it will fail, with
SDL_GetPrefPath() returning NULL (if SDL_HINT_WINRT_PREF_PATH_ROOT is
set to "roaming" on that platform). Windows Phone 8.1 does not have this
limitation, and does support Roaming data.

View File

@ -495,16 +495,25 @@ extern "C" {
* \brief A variable that dictates what SDL_GetPrefPath() returns in WinRT apps. * \brief A variable that dictates what SDL_GetPrefPath() returns in WinRT apps.
* *
* The variable can be set to the following values: * The variable can be set to the following values:
* "local" - Use the app's 'local' folder to store data; default for * * "local" - Use the app's 'local' folder to store data.
* Windows Phone apps. * * "roaming" - Use the app's 'roaming' folder to store data.
* "roaming" - Use the app's 'roaming' folder to store data; default for * On Windows Phone 8.0, this setting is not supported due to
* Windows Store (non-Phone) apps. On Windows Phone 8.0, this * limitations in the OS itself. Attempts to use this (via
* setting will be ignored (and the 'local' folder will be used * SDL_GetPrefPath()) on Windows Phone 8.0 will fail, with
* instead), as the OS does not support roaming folders. * SDL_GetPrefPath() returning NULL. (Windows Phone 8.1 does,
* however, support roaming folders.)
* * "old" - Use the app's 'local' folder on Windows Phone, and 'roaming'
* on non-Phone versions of WinRT. This mimics behavior found
* in SDL 2.0.3's implementation of SDL_GetPrefPath() for WinRT
* (and was changed for SDL 2.0.4, further details of which are
* in the "Caveats" section of SDL's
* [WinRT README file](README-winrt.md).
* *
* Details on 'local' verses 'roaming' folders can be found on MSDN, in the * The default is to use the app's "local" folder.
* documentation for WinRT's Windows.Storage.ApplicationData class, which is *
* available at http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata * Details on 'local' verses 'roaming' folders can be found on MSDN, in
* the documentation for WinRT's Windows.Storage.ApplicationData class,
* (available at http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata ).
* *
* The application's local and roaming paths may, alternatively, be retrieved * The application's local and roaming paths may, alternatively, be retrieved
* via the SDL_WinRTGetFSPathUTF8() and SDL_WinRTGetFSPathUNICODE() functions, * via the SDL_WinRTGetFSPathUTF8() and SDL_WinRTGetFSPathUNICODE() functions,

View File

@ -144,49 +144,121 @@ SDL_GetPrefPath(const char *org, const char *app)
* without violating Microsoft's app-store requirements. * without violating Microsoft's app-store requirements.
*/ */
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP /* Default to using a Local/non-Roaming path. WinRT will often attempt
/* A 'Roaming' folder is not available in Windows Phone 8.0, however a * to synchronize files in Roaming paths, and will do so while an app is
* 'Local' folder is. Use the 'Local' folder in order to preserve * running. Using a Local path prevents the possibility that an app's
* compatibility with Windows Phone 8.0, and with app-installs that have * save-data files will get changed from underneath it, without it
* been updated from 8.0-based, to 8.1-based apps. * being ready.
*
* This behavior can be changed via use of the
* SDL_HINT_WINRT_PREF_PATH_ROOT hint.
*/ */
SDL_WinRT_Path pathType = SDL_WINRT_PATH_LOCAL_FOLDER; SDL_WinRT_Path pathType = SDL_WINRT_PATH_LOCAL_FOLDER;
#else
/* A 'Roaming' folder is available on Windows 8 and 8.1. Use that.
*/
SDL_WinRT_Path pathType = SDL_WINRT_PATH_ROAMING_FOLDER;
#endif
const char * hint = SDL_GetHint(SDL_HINT_WINRT_PREF_PATH_ROOT); const char * hint = SDL_GetHint(SDL_HINT_WINRT_PREF_PATH_ROOT);
if (hint) { if (hint) {
if (SDL_strcasecmp(hint, "local") == 0) { if (SDL_strcasecmp(hint, "local") == 0) {
pathType = SDL_WINRT_PATH_LOCAL_FOLDER; pathType = SDL_WINRT_PATH_LOCAL_FOLDER;
} } else if (SDL_strcasecmp(hint, "roaming") == 0) {
#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8) #if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
else if (SDL_strcasecmp(hint, "roaming") == 0) {
pathType = SDL_WINRT_PATH_ROAMING_FOLDER; pathType = SDL_WINRT_PATH_ROAMING_FOLDER;
} #else
/* Don't apply a 'Roaming' path on Windows Phone 8.0. Roaming
* data is not supported by that version of the operating system.
*/
SDL_SetError("A Roaming path was specified via SDL_HINT_WINRT_PREF_PATH_ROOT, but Roaming is not supported on Windows Phone 8.0");
return NULL;
#endif #endif
} else if (SDL_strcasecmp(hint, "old") == 0) {
/* Older versions of SDL/WinRT, including 2.0.3, would return a
* pref-path that used a Roaming folder on non-Phone versions of
* Windows, such as Windows 8.0 and Windows 8.1. This has since
* been reverted to using a Local folder, in order to prevent
* problems arising from WinRT automatically synchronizing files
* during an app's lifetime. In case this functionality is
* desired, setting SDL_HINT_WINRT_PREF_PATH_ROOT to "old" will
* trigger the older behavior.
*/
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
pathType = SDL_WINRT_PATH_LOCAL_FOLDER;
#else
pathType = SDL_WINRT_PATH_ROAMING_FOLDER;
#endif
}
} }
const char * srcPath = SDL_WinRTGetFSPathUTF8(pathType); const WCHAR * srcPath = NULL;
size_t destPathLen; WCHAR path[MAX_PATH];
char * destPath = NULL; char *retval = NULL;
WCHAR* worg = NULL;
WCHAR* wapp = NULL;
size_t new_wpath_len = 0;
BOOL api_result = FALSE;
if (!srcPath) { srcPath = SDL_WinRTGetFSPathUNICODE(pathType);
SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError()); if ( ! srcPath) {
SDL_SetError("Unable to find a source path");
return NULL; return NULL;
} }
destPathLen = SDL_strlen(srcPath) + SDL_strlen(org) + SDL_strlen(app) + 4; if (SDL_wcslen(srcPath) >= MAX_PATH) {
destPath = (char *) SDL_malloc(destPathLen); SDL_SetError("Path too long.");
if (!destPath) { return NULL;
}
SDL_wcslcpy(path, srcPath, SDL_arraysize(path));
worg = WIN_UTF8ToString(org);
if (worg == NULL) {
SDL_OutOfMemory(); SDL_OutOfMemory();
return NULL; return NULL;
} }
SDL_snprintf(destPath, destPathLen, "%s\\%s\\%s\\", srcPath, org, app); wapp = WIN_UTF8ToString(app);
return destPath; if (wapp == NULL) {
SDL_free(worg);
SDL_OutOfMemory();
return NULL;
}
new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3;
if ((new_wpath_len + 1) > MAX_PATH) {
SDL_free(worg);
SDL_free(wapp);
SDL_SetError("Path too long.");
return NULL;
}
SDL_wcslcat(path, L"\\", new_wpath_len + 1);
SDL_wcslcat(path, worg, new_wpath_len + 1);
SDL_free(worg);
api_result = CreateDirectoryW(path, NULL);
if (api_result == FALSE) {
if (GetLastError() != ERROR_ALREADY_EXISTS) {
SDL_free(wapp);
WIN_SetError("Couldn't create a prefpath.");
return NULL;
}
}
SDL_wcslcat(path, L"\\", new_wpath_len + 1);
SDL_wcslcat(path, wapp, new_wpath_len + 1);
SDL_free(wapp);
api_result = CreateDirectoryW(path, NULL);
if (api_result == FALSE) {
if (GetLastError() != ERROR_ALREADY_EXISTS) {
WIN_SetError("Couldn't create a prefpath.");
return NULL;
}
}
SDL_wcslcat(path, L"\\", new_wpath_len + 1);
retval = WIN_StringToUTF8(path);
return retval;
} }
#endif /* __WINRT__ */ #endif /* __WINRT__ */