From 60aa0e460ccd1c4aa8a0ed8b1a56732a5a5959d2 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 21 Mar 2012 23:59:43 -0400 Subject: [PATCH] Make sure the prefdir has a final dirsep on it. This matches the behaviour of PHYSFS_getBaseDir() and PHYSFS_getUserDir(). --- src/physfs.c | 30 +++++++++++++++++++----------- src/physfs_internal.h | 5 +++-- src/platform_beos.cpp | 4 ++-- src/platform_macosx.c | 4 ++-- src/platform_unix.c | 4 ++-- src/platform_windows.c | 4 ++-- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/physfs.c b/src/physfs.c index 62ecdb3c..37c4ec12 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -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 */ diff --git a/src/physfs_internal.h b/src/physfs_internal.h index a60d4dba..97425c2d 100644 --- a/src/physfs_internal.h +++ b/src/physfs_internal.h @@ -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. */ diff --git a/src/platform_beos.cpp b/src/platform_beos.cpp index 9b09c8c1..8b0fe98a 100644 --- a/src/platform_beos.cpp +++ b/src/platform_beos.cpp @@ -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 */ diff --git a/src/platform_macosx.c b/src/platform_macosx.c index cc44ed91..380e7471 100644 --- a/src/platform_macosx.c +++ b/src/platform_macosx.c @@ -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 */ diff --git a/src/platform_unix.c b/src/platform_unix.c index 20e42a96..93b5fde7 100644 --- a/src/platform_unix.c +++ b/src/platform_unix.c @@ -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 */ diff --git a/src/platform_windows.c b/src/platform_windows.c index f0780b9c..d737a181 100644 --- a/src/platform_windows.c +++ b/src/platform_windows.c @@ -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 */