Skip to content

Commit

Permalink
Cleaned up some FIXMEs.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Mar 22, 2012
1 parent 59c6310 commit b1d4afc
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 81 deletions.
4 changes: 3 additions & 1 deletion src/archiver_dir.c
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/physfs.c
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/physfs_internal.h
Expand Up @@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions src/platform_posix.c
Expand Up @@ -123,7 +123,7 @@ char *__PHYSFS_platformGetUserName(void)
} /* __PHYSFS_platformGetUserName */


char *__PHYSFS_platformGetUserDir(void)
char *__PHYSFS_platformCalcUserDir(void)
{
char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");

Expand All @@ -142,7 +142,7 @@ char *__PHYSFS_platformGetUserDir(void)
retval = getUserDirByUID();

return retval;
} /* __PHYSFS_platformGetUserDir */
} /* __PHYSFS_platformCalcUserDir */


void __PHYSFS_platformEnumerateFiles(const char *dirname,
Expand Down
121 changes: 46 additions & 75 deletions src/platform_windows.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 */

Expand All @@ -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 */

Expand Down

0 comments on commit b1d4afc

Please sign in to comment.