From b1d4afcbd3d19e79034ba0be94b97974cdb4cb93 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Thu, 22 Mar 2012 00:21:28 -0400 Subject: [PATCH] Cleaned up some FIXMEs. --- src/archiver_dir.c | 4 +- src/physfs.c | 2 +- src/physfs_internal.h | 4 +- src/platform_posix.c | 4 +- src/platform_windows.c | 121 ++++++++++++++++------------------------- 5 files changed, 54 insertions(+), 81 deletions(-) diff --git a/src/archiver_dir.c b/src/archiver_dir.c index 045c48c..3af0707 100644 --- a/src/archiver_dir.c +++ b/src/archiver_dir.c @@ -98,8 +98,10 @@ static PHYSFS_Io *doOpen(dvoid *opaque, const char *name, io = __PHYSFS_createNativeIo(f, mode); if (io == NULL) { - PHYSFS_Stat statbuf; /* !!! FIXME: this changes the error message. */ + const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode(); + PHYSFS_Stat statbuf; __PHYSFS_platformStat(f, fileExists, &statbuf); + __PHYSFS_setError(err); } /* if */ else { diff --git a/src/physfs.c b/src/physfs.c index 37c4ec1..9e3cff5 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -1104,7 +1104,7 @@ static int freeDirHandle(DirHandle *dh, FileHandle *openList) static char *calculateUserDir(void) { - char *retval = __PHYSFS_platformGetUserDir(); + char *retval = __PHYSFS_platformCalcUserDir(); if (retval == NULL) { const char dirsep = __PHYSFS_platformDirSeparator; diff --git a/src/physfs_internal.h b/src/physfs_internal.h index 97425c2..c6880d8 100644 --- a/src/physfs_internal.h +++ b/src/physfs_internal.h @@ -633,12 +633,12 @@ char *__PHYSFS_platformCalcBaseDir(const char *argv0); */ char *__PHYSFS_platformGetUserName(void); -/* !!! FIXME: should this be CalcUserDir, to match CalcBaseDir? +/* * Get the platform-specific user dir. * Caller will allocator.Free() the retval if it's not NULL. If it's NULL, * the userdir will default to basedir/username. */ -char *__PHYSFS_platformGetUserDir(void); +char *__PHYSFS_platformCalcUserDir(void); /* This is the cached version from PHYSFS_init(). This is a fast call. */ diff --git a/src/platform_posix.c b/src/platform_posix.c index f2cfe44..c493aa3 100644 --- a/src/platform_posix.c +++ b/src/platform_posix.c @@ -123,7 +123,7 @@ char *__PHYSFS_platformGetUserName(void) } /* __PHYSFS_platformGetUserName */ -char *__PHYSFS_platformGetUserDir(void) +char *__PHYSFS_platformCalcUserDir(void) { char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME"); @@ -142,7 +142,7 @@ char *__PHYSFS_platformGetUserDir(void) retval = getUserDirByUID(); return retval; -} /* __PHYSFS_platformGetUserDir */ +} /* __PHYSFS_platformCalcUserDir */ void __PHYSFS_platformEnumerateFiles(const char *dirname, diff --git a/src/platform_windows.c b/src/platform_windows.c index d737a18..3c7eb8b 100644 --- a/src/platform_windows.c +++ b/src/platform_windows.c @@ -88,10 +88,6 @@ typedef struct int readonly; } WinApiFile; - -/* !!! FIXME: we cache userDir in physfs.c during PHYSFS_init(), too. */ -static char *userDir = NULL; -static HANDLE libUserEnv = NULL; static HANDLE detectCDThreadHandle = NULL; static HWND detectCDHwnd = 0; static volatile int initialDiscDetectionComplete = 0; @@ -156,61 +152,6 @@ static inline PHYSFS_ErrorCode errcodeFromWinApi(void) } /* errcodeFromWinApi */ - -/* - * On success, module-scope variable (userDir) will have a pointer to - * a malloc()'d string of the user's profile dir, and a non-zero value is - * returned. If we can't determine the profile dir, (userDir) will - * be NULL, and zero is returned. - */ -static int determineUserDir(void) -{ - typedef BOOL (WINAPI *fnGetUserProfDirW)(HANDLE, LPWSTR, LPDWORD); - fnGetUserProfDirW pGetDir = NULL; - - HANDLE accessToken = NULL; /* Security handle to process */ - - if (userDir != NULL) - return 1; /* already good to go. */ - - pGetDir = (fnGetUserProfDirW) - GetProcAddress(libUserEnv, "GetUserProfileDirectoryW"); - BAIL_IF_MACRO(pGetDir == NULL, errcodeFromWinApi(), 0); - - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken)) - BAIL_MACRO(errcodeFromWinApi(), 0); - else - { - DWORD psize = 0; - WCHAR dummy = 0; - LPWSTR wstr = NULL; - BOOL rc = 0; - - /* - * Should fail. Will write the size of the profile path in - * psize. Also note that the second parameter can't be - * NULL or the function fails. - */ - rc = pGetDir(accessToken, &dummy, &psize); - assert(!rc); /* !!! FIXME: handle this gracefully. */ - (void) rc; - - /* Allocate memory for the profile directory */ - wstr = (LPWSTR) __PHYSFS_smallAlloc(psize * sizeof (WCHAR)); - if (wstr != NULL) - { - if (pGetDir(accessToken, wstr, &psize)) - userDir = unicodeToUtf8Heap(wstr); - __PHYSFS_smallFree(wstr); - } /* if */ - - CloseHandle(accessToken); - } /* if */ - - return 1; /* We made it: hit the showers. */ -} /* determineUserDir */ - - typedef BOOL (WINAPI *fnSTEM)(DWORD, LPDWORD b); static DWORD pollDiscDrives(void) @@ -495,13 +436,53 @@ char *__PHYSFS_platformGetUserName(void) } /* __PHYSFS_platformGetUserName */ -char *__PHYSFS_platformGetUserDir(void) +char *__PHYSFS_platformCalcUserDir(void) { - char *retval = (char *) allocator.Malloc(strlen(userDir) + 1); - BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); - strcpy(retval, userDir); /* calculated at init time. */ - return retval; -} /* __PHYSFS_platformGetUserDir */ + typedef BOOL (WINAPI *fnGetUserProfDirW)(HANDLE, LPWSTR, LPDWORD); + fnGetUserProfDirW pGetDir = NULL; + HANDLE lib = NULL; + HANDLE accessToken = NULL; /* Security handle to process */ + char *retval = NULL; + + lib = LoadLibraryA("userenv.dll"); + BAIL_IF_MACRO(!lib, errcodeFromWinApi(), NULL); + pGetDir=(fnGetUserProfDirW) GetProcAddress(lib,"GetUserProfileDirectoryW"); + GOTO_IF_MACRO(!pGetDir, errcodeFromWinApi(), done); + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken)) + GOTO_MACRO(errcodeFromWinApi(), done); + else + { + DWORD psize = 0; + WCHAR dummy = 0; + LPWSTR wstr = NULL; + BOOL rc = 0; + + /* + * Should fail. Will write the size of the profile path in + * psize. Also note that the second parameter can't be + * NULL or the function fails. + */ + rc = pGetDir(accessToken, &dummy, &psize); + assert(!rc); /* !!! FIXME: handle this gracefully. */ + (void) rc; + + /* Allocate memory for the profile directory */ + wstr = (LPWSTR) __PHYSFS_smallAlloc(psize * sizeof (WCHAR)); + if (wstr != NULL) + { + if (pGetDir(accessToken, wstr, &psize)) + retval = unicodeToUtf8Heap(wstr); + __PHYSFS_smallFree(wstr); + } /* if */ + + CloseHandle(accessToken); + } /* if */ + +done: + FreeLibrary(lib); + return retval; /* We made it: hit the showers. */ +} /* __PHYSFS_platformCalcUserDir */ void *__PHYSFS_platformGetThreadID(void) @@ -598,11 +579,6 @@ int __PHYSFS_platformMkDir(const char *path) int __PHYSFS_platformInit(void) { - libUserEnv = LoadLibraryA("userenv.dll"); - BAIL_IF_MACRO(libUserEnv == NULL, errcodeFromWinApi(), 0); - - /* !!! FIXME: why do we precalculate this? */ - BAIL_IF_MACRO(!determineUserDir(), ERRPASS, 0); return 1; /* It's all good */ } /* __PHYSFS_platformInit */ @@ -619,11 +595,6 @@ int __PHYSFS_platformDeinit(void) drivesWithMediaBitmap = 0; } /* if */ - if (libUserEnv) - FreeLibrary(libUserEnv); - libUserEnv = NULL; - allocator.Free(userDir); - userDir = NULL; return 1; /* It's all good */ } /* __PHYSFS_platformDeinit */