Make sure the prefdir has a final dirsep on it.

This matches the behaviour of PHYSFS_getBaseDir() and PHYSFS_getUserDir().
This commit is contained in:
Ryan C. Gordon 2012-03-21 23:59:43 -04:00
parent 43367c0c29
commit 60aa0e460c
6 changed files with 30 additions and 21 deletions

View File

@ -1382,6 +1382,7 @@ const char *PHYSFS_getPrefDir(const char *org, const char *app)
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 @@ const char *PHYSFS_getPrefDir(const char *org, const char *app)
prefDir = __PHYSFS_platformCalcPrefDir(org, app);
BAIL_IF_MACRO(!prefDir, ERRPASS, NULL);
if (__PHYSFS_platformStat(prefDir, &exists, &statbuf))
return prefDir;
assert(strlen(prefDir) > 0);
endstr = prefDir + (strlen(prefDir) - 1);
assert(*endstr == dirsep);
*endstr = '\0'; /* mask out the final dirsep for now. */
for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
if (!__PHYSFS_platformStat(prefDir, &exists, &statbuf))
{
*ptr = '\0';
__PHYSFS_platformMkDir(prefDir);
*ptr = dirsep;
} /* for */
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 (!__PHYSFS_platformMkDir(prefDir))
{
allocator.Free(prefDir);
prefDir = NULL;
} /* if */
} /* if */
*endstr = dirsep; /* readd the final dirsep. */
return prefDir;
} /* PHYSFS_getPrefDir */

View File

@ -645,8 +645,9 @@ char *__PHYSFS_platformGetUserDir(void);
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.
*/

View File

@ -188,10 +188,10 @@ char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
/* !!! 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 */

View File

@ -294,10 +294,10 @@ char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
/* !!! 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 */

View File

@ -315,10 +315,10 @@ char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
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 */

View File

@ -462,7 +462,7 @@ char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
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 @@ char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
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 */