Skip to content

Commit

Permalink
Make sure the prefdir has a final dirsep on it.
Browse files Browse the repository at this point in the history
This matches the behaviour of PHYSFS_getBaseDir() and PHYSFS_getUserDir().
  • Loading branch information
icculus committed Mar 22, 2012
1 parent 43367c0 commit 60aa0e4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 21 deletions.
30 changes: 19 additions & 11 deletions src/physfs.c
Expand Up @@ -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);
Expand All @@ -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 */

Expand Down
5 changes: 3 additions & 2 deletions src/physfs_internal.h
Expand Up @@ -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.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/platform_beos.cpp
Expand Up @@ -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 */

Expand Down
4 changes: 2 additions & 2 deletions src/platform_macosx.c
Expand Up @@ -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 */

Expand Down
4 changes: 2 additions & 2 deletions src/platform_unix.c
Expand Up @@ -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 */

Expand Down
4 changes: 2 additions & 2 deletions src/platform_windows.c
Expand Up @@ -462,15 +462,15 @@ 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)
{
allocator.Free(utf8);
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 */

Expand Down

0 comments on commit 60aa0e4

Please sign in to comment.