Cleaned up some FIXMEs.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 22 Mar 2012 00:21:28 -0400
changeset 1248 632895970ed6
parent 1247 4ea4710d4863
child 1249 1a5ef6c908f0
Cleaned up some FIXMEs.
src/archiver_dir.c
src/physfs.c
src/physfs_internal.h
src/platform_posix.c
src/platform_windows.c
--- a/src/archiver_dir.c	Thu Mar 22 00:05:10 2012 -0400
+++ b/src/archiver_dir.c	Thu Mar 22 00:21:28 2012 -0400
@@ -98,8 +98,10 @@
     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
     {
--- a/src/physfs.c	Thu Mar 22 00:05:10 2012 -0400
+++ b/src/physfs.c	Thu Mar 22 00:21:28 2012 -0400
@@ -1104,7 +1104,7 @@
 
 static char *calculateUserDir(void)
 {
-    char *retval = __PHYSFS_platformGetUserDir();
+    char *retval = __PHYSFS_platformCalcUserDir();
     if (retval == NULL)
     {
         const char dirsep = __PHYSFS_platformDirSeparator;
--- a/src/physfs_internal.h	Thu Mar 22 00:05:10 2012 -0400
+++ b/src/physfs_internal.h	Thu Mar 22 00:21:28 2012 -0400
@@ -633,12 +633,12 @@
  */
 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. */
--- a/src/platform_posix.c	Thu Mar 22 00:05:10 2012 -0400
+++ b/src/platform_posix.c	Thu Mar 22 00:21:28 2012 -0400
@@ -123,7 +123,7 @@
 } /* __PHYSFS_platformGetUserName */
 
 
-char *__PHYSFS_platformGetUserDir(void)
+char *__PHYSFS_platformCalcUserDir(void)
 {
     char *retval = __PHYSFS_platformCopyEnvironmentVariable("HOME");
 
@@ -142,7 +142,7 @@
         retval = getUserDirByUID();
 
     return retval;
-} /* __PHYSFS_platformGetUserDir */
+} /* __PHYSFS_platformCalcUserDir */
 
 
 void __PHYSFS_platformEnumerateFiles(const char *dirname,
--- a/src/platform_windows.c	Thu Mar 22 00:05:10 2012 -0400
+++ b/src/platform_windows.c	Thu Mar 22 00:21:28 2012 -0400
@@ -88,10 +88,6 @@
     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 @@
 } /* 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 @@
 } /* __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_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 @@
         drivesWithMediaBitmap = 0;
     } /* if */
 
-    if (libUserEnv)
-        FreeLibrary(libUserEnv);
-    libUserEnv = NULL;
-    allocator.Free(userDir);
-    userDir = NULL;
     return 1; /* It's all good */
 } /* __PHYSFS_platformDeinit */