Make sure the prefdir has a final dirsep on it.
This matches the behaviour of PHYSFS_getBaseDir() and PHYSFS_getUserDir().
--- a/src/physfs.c Wed Mar 21 23:52:44 2012 -0400
+++ b/src/physfs.c Wed Mar 21 23:59:43 2012 -0400
@@ -1382,6 +1382,7 @@
const char dirsep = __PHYSFS_platformDirSeparator;
PHYSFS_Stat statbuf;
char *ptr = NULL;
+ char *endstr = NULL;
int exists = 0;
BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
@@ -1394,22 +1395,29 @@
prefDir = __PHYSFS_platformCalcPrefDir(org, app);
BAIL_IF_MACRO(!prefDir, ERRPASS, NULL);
- if (__PHYSFS_platformStat(prefDir, &exists, &statbuf))
- return prefDir;
-
- for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
+ assert(strlen(prefDir) > 0);
+ endstr = prefDir + (strlen(prefDir) - 1);
+ assert(*endstr == dirsep);
+ *endstr = '\0'; /* mask out the final dirsep for now. */
+
+ if (!__PHYSFS_platformStat(prefDir, &exists, &statbuf))
{
- *ptr = '\0';
- __PHYSFS_platformMkDir(prefDir);
- *ptr = dirsep;
- } /* for */
-
- if (!__PHYSFS_platformMkDir(prefDir))
- {
- allocator.Free(prefDir);
- prefDir = NULL;
+ for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
+ {
+ *ptr = '\0';
+ __PHYSFS_platformMkDir(prefDir);
+ *ptr = dirsep;
+ } /* for */
+
+ if (!__PHYSFS_platformMkDir(prefDir))
+ {
+ allocator.Free(prefDir);
+ prefDir = NULL;
+ } /* if */
} /* if */
+ *endstr = dirsep; /* readd the final dirsep. */
+
return prefDir;
} /* PHYSFS_getPrefDir */
--- a/src/physfs_internal.h Wed Mar 21 23:52:44 2012 -0400
+++ b/src/physfs_internal.h Wed Mar 21 23:59:43 2012 -0400
@@ -645,8 +645,9 @@
const char *__PHYSFS_getUserDir(void); /* not deprecated internal version. */
/*
- * Get the platform-specific pref dir.
- * Caller will allocator.Free() the retval if it's not NULL. If it's NULL,
+ * Get the platform-specific pref dir. You must make sure the string ends
+ * with a dir separator.
+ * Caller will allocator.Free() the retval if it's not NULL. If it's NULL,
* it's a total failure. Caller will make missing directories if necessary;
* this just reports the final path.
*/
--- a/src/platform_beos.cpp Wed Mar 21 23:52:44 2012 -0400
+++ b/src/platform_beos.cpp Wed Mar 21 23:59:43 2012 -0400
@@ -188,10 +188,10 @@
/* !!! FIXME: there's a real API to determine this */
const char *userdir = __PHYSFS_getUserDir();
const char *append = "config/settings/";
- const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 1;
+ const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 2;
char *retval = allocator.Malloc(len);
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
- snprintf(retval, len, "%s%s%s", userdir, append, app);
+ snprintf(retval, len, "%s%s%s/", userdir, append, app);
return retval;
} /* __PHYSFS_platformCalcPrefDir */
--- a/src/platform_macosx.c Wed Mar 21 23:52:44 2012 -0400
+++ b/src/platform_macosx.c Wed Mar 21 23:59:43 2012 -0400
@@ -294,10 +294,10 @@
/* !!! FIXME: there's a real API to determine this */
const char *userdir = __PHYSFS_getUserDir();
const char *append = "Library/Application Support/";
- const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 1;
+ const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 2;
char *retval = allocator.Malloc(len);
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
- snprintf(retval, len, "%s%s%s", userdir, append, app);
+ snprintf(retval, len, "%s%s%s/", userdir, append, app);
return retval;
} /* __PHYSFS_platformCalcPrefDir */
--- a/src/platform_unix.c Wed Mar 21 23:52:44 2012 -0400
+++ b/src/platform_unix.c Wed Mar 21 23:59:43 2012 -0400
@@ -315,10 +315,10 @@
append = ".local/share/";
} /* if */
- len = strlen(envr) + strlen(append) + strlen(app) + 1;
+ len = strlen(envr) + strlen(append) + strlen(app) + 2;
retval = (char *) allocator.Malloc(len);
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
- snprintf(retval, len, "%s%s%s", envr, append, app);
+ snprintf(retval, len, "%s%s%s/", envr, append, app);
return retval;
} /* __PHYSFS_platformCalcPrefDir */
--- a/src/platform_windows.c Wed Mar 21 23:52:44 2012 -0400
+++ b/src/platform_windows.c Wed Mar 21 23:59:43 2012 -0400
@@ -462,7 +462,7 @@
utf8 = unicodeToUtf8Heap(path);
BAIL_IF_MACRO(!utf8, ERRPASS, NULL);
- len = strlen(utf8) + strlen(org) + strlen(app) + 3;
+ len = strlen(utf8) + strlen(org) + strlen(app) + 4;
retval = allocator.Malloc(len);
if (!retval)
{
@@ -470,7 +470,7 @@
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
} /* if */
- sprintf(retval, "%s\\%s\\%s", utf8, org, app);
+ sprintf(retval, "%s\\%s\\%s\\", utf8, org, app);
return retval;
} /* __PHYSFS_platformCalcPrefDir */