windows.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /*
  2. * Windows support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
  7. */
  8. #define __PHYSICSFS_INTERNAL__
  9. #include "physfs_platforms.h"
  10. #ifdef PHYSFS_PLATFORM_WINDOWS
  11. /* Forcibly disable UNICODE, since we manage this ourselves. */
  12. #ifdef UNICODE
  13. #undef UNICODE
  14. #endif
  15. #include <windows.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <errno.h>
  20. #include <ctype.h>
  21. #include <time.h>
  22. #include "physfs_internal.h"
  23. #define LOWORDER_UINT64(pos) (PHYSFS_uint32) \
  24. (pos & 0x00000000FFFFFFFF)
  25. #define HIGHORDER_UINT64(pos) (PHYSFS_uint32) \
  26. (((pos & 0xFFFFFFFF00000000) >> 32) & 0x00000000FFFFFFFF)
  27. /*
  28. * Users without the platform SDK don't have this defined. The original docs
  29. * for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
  30. * work as desired.
  31. */
  32. #define PHYSFS_INVALID_SET_FILE_POINTER 0xFFFFFFFF
  33. /* just in case... */
  34. #define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
  35. #define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
  36. if (str == NULL) \
  37. w_assignto = NULL; \
  38. else { \
  39. const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) * 4) + 1); \
  40. w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
  41. if (w_assignto != NULL) \
  42. PHYSFS_utf8ToUcs2(str, (PHYSFS_uint16 *) w_assignto, len); \
  43. } \
  44. } \
  45. static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
  46. {
  47. PHYSFS_uint64 len = 0;
  48. while (*(wstr++))
  49. len++;
  50. return(len);
  51. } /* wStrLen */
  52. static char *unicodeToUtf8Heap(const WCHAR *w_str)
  53. {
  54. char *retval = NULL;
  55. if (w_str != NULL)
  56. {
  57. void *ptr = NULL;
  58. const PHYSFS_uint64 len = (wStrLen(w_str) * 4) + 1;
  59. retval = allocator.Malloc(len);
  60. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  61. PHYSFS_utf8FromUcs2((const PHYSFS_uint16 *) w_str, retval, len);
  62. ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
  63. if (ptr != NULL)
  64. retval = (char *) ptr;
  65. } /* if */
  66. return(retval);
  67. } /* unicodeToUtf8Heap */
  68. static char *codepageToUtf8Heap(const char *cpstr)
  69. {
  70. char *retval = NULL;
  71. if (cpstr != NULL)
  72. {
  73. const int len = (int) (strlen(cpstr) + 1);
  74. WCHAR *wbuf = (WCHAR *) __PHYSFS_smallAlloc(len * sizeof (WCHAR));
  75. BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
  76. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, len, wbuf, len);
  77. retval = (char *) allocator.Malloc(len * 4);
  78. if (retval == NULL)
  79. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  80. else
  81. PHYSFS_utf8FromUcs2(wbuf, retval, len * 4);
  82. __PHYSFS_smallFree(wbuf);
  83. } /* if */
  84. return(retval);
  85. } /* codepageToUtf8Heap */
  86. typedef struct
  87. {
  88. HANDLE handle;
  89. int readonly;
  90. } WinApiFile;
  91. static char *userDir = NULL;
  92. /* pointers for APIs that may not exist on some Windows versions... */
  93. static HANDLE libKernel32 = NULL;
  94. static HANDLE libUserEnv = NULL;
  95. static HANDLE libAdvApi32 = NULL;
  96. static DWORD (WINAPI *pGetModuleFileNameW)(HMODULE, LPWCH, DWORD);
  97. static BOOL (WINAPI *pGetUserProfileDirectoryW)(HANDLE, LPWSTR, LPDWORD);
  98. static BOOL (WINAPI *pGetUserNameW)(LPWSTR, LPDWORD);
  99. static DWORD (WINAPI *pGetFileAttributesW)(LPCWSTR);
  100. static HANDLE (WINAPI *pFindFirstFileW)(LPCWSTR, LPWIN32_FIND_DATAW);
  101. static BOOL (WINAPI *pFindNextFileW)(HANDLE, LPWIN32_FIND_DATAW);
  102. static DWORD (WINAPI *pGetCurrentDirectoryW)(DWORD, LPWSTR);
  103. static BOOL (WINAPI *pDeleteFileW)(LPCWSTR);
  104. static BOOL (WINAPI *pRemoveDirectoryW)(LPCWSTR);
  105. static BOOL (WINAPI *pCreateDirectoryW)(LPCWSTR, LPSECURITY_ATTRIBUTES);
  106. static BOOL (WINAPI *pGetFileAttributesExA)
  107. (LPCSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
  108. static BOOL (WINAPI *pGetFileAttributesExW)
  109. (LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID);
  110. static DWORD (WINAPI *pFormatMessageW)
  111. (DWORD, LPCVOID, DWORD, DWORD, LPWSTR, DWORD, va_list *);
  112. static HANDLE (WINAPI *pCreateFileW)
  113. (LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
  114. /*
  115. * Fallbacks for missing Unicode functions on Win95/98/ME. These are filled
  116. * into the function pointers if looking up the real Unicode entry points
  117. * in the system DLLs fails, so they're never used on WinNT/XP/Vista/etc.
  118. * They make an earnest effort to convert to/from UTF-8 and UCS-2 to
  119. * the user's current codepage.
  120. */
  121. static BOOL WINAPI fallbackGetUserNameW(LPWSTR buf, LPDWORD len)
  122. {
  123. const DWORD cplen = *len;
  124. char *cpstr = __PHYSFS_smallAlloc(cplen);
  125. BOOL retval = GetUserNameA(cpstr, len);
  126. if (buf != NULL)
  127. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, cpstr, cplen, buf, *len);
  128. __PHYSFS_smallFree(cpstr);
  129. return(retval);
  130. } /* fallbackGetUserNameW */
  131. static DWORD WINAPI fallbackFormatMessageW(DWORD dwFlags, LPCVOID lpSource,
  132. DWORD dwMessageId, DWORD dwLangId,
  133. LPWSTR lpBuf, DWORD nSize,
  134. va_list *Arguments)
  135. {
  136. char *cpbuf = (char *) __PHYSFS_smallAlloc(nSize);
  137. DWORD retval = FormatMessageA(dwFlags, lpSource, dwMessageId, dwLangId,
  138. cpbuf, nSize, Arguments);
  139. if (retval > 0)
  140. MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
  141. __PHYSFS_smallFree(cpbuf);
  142. return(retval);
  143. } /* fallbackFormatMessageW */
  144. static DWORD WINAPI fallbackGetModuleFileNameW(HMODULE hMod, LPWCH lpBuf,
  145. DWORD nSize)
  146. {
  147. char *cpbuf = (char *) __PHYSFS_smallAlloc(nSize);
  148. DWORD retval = GetModuleFileNameA(hMod, cpbuf, nSize);
  149. if (retval > 0)
  150. MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,lpBuf,nSize);
  151. __PHYSFS_smallFree(cpbuf);
  152. return(retval);
  153. } /* fallbackGetModuleFileNameW */
  154. static DWORD WINAPI fallbackGetFileAttributesW(LPCWSTR fname)
  155. {
  156. DWORD retval = 0;
  157. const int buflen = (int) (wStrLen(fname) + 1);
  158. char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
  159. WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
  160. retval = GetFileAttributesA(cpstr);
  161. __PHYSFS_smallFree(cpstr);
  162. return(retval);
  163. } /* fallbackGetFileAttributesW */
  164. static DWORD WINAPI fallbackGetCurrentDirectoryW(DWORD buflen, LPWSTR buf)
  165. {
  166. DWORD retval = 0;
  167. char *cpbuf = NULL;
  168. if (buf != NULL)
  169. cpbuf = (char *) __PHYSFS_smallAlloc(buflen);
  170. retval = GetCurrentDirectoryA(buflen, cpbuf);
  171. if (cpbuf != NULL)
  172. {
  173. MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,cpbuf,retval,buf,buflen);
  174. __PHYSFS_smallFree(cpbuf);
  175. } /* if */
  176. return(retval);
  177. } /* fallbackGetCurrentDirectoryW */
  178. static BOOL WINAPI fallbackRemoveDirectoryW(LPCWSTR dname)
  179. {
  180. BOOL retval = 0;
  181. const int buflen = (int) (wStrLen(dname) + 1);
  182. char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
  183. WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
  184. retval = RemoveDirectoryA(cpstr);
  185. __PHYSFS_smallFree(cpstr);
  186. return(retval);
  187. } /* fallbackRemoveDirectoryW */
  188. static BOOL WINAPI fallbackCreateDirectoryW(LPCWSTR dname,
  189. LPSECURITY_ATTRIBUTES attr)
  190. {
  191. BOOL retval = 0;
  192. const int buflen = (int) (wStrLen(dname) + 1);
  193. char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
  194. WideCharToMultiByte(CP_ACP, 0, dname, buflen, cpstr, buflen, NULL, NULL);
  195. retval = CreateDirectoryA(cpstr, attr);
  196. __PHYSFS_smallFree(cpstr);
  197. return(retval);
  198. } /* fallbackCreateDirectoryW */
  199. static BOOL WINAPI fallbackDeleteFileW(LPCWSTR fname)
  200. {
  201. BOOL retval = 0;
  202. const int buflen = (int) (wStrLen(fname) + 1);
  203. char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
  204. WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
  205. retval = DeleteFileA(cpstr);
  206. __PHYSFS_smallFree(cpstr);
  207. return(retval);
  208. } /* fallbackDeleteFileW */
  209. static HANDLE WINAPI fallbackCreateFileW(LPCWSTR fname,
  210. DWORD dwDesiredAccess, DWORD dwShareMode,
  211. LPSECURITY_ATTRIBUTES lpSecurityAttrs,
  212. DWORD dwCreationDisposition,
  213. DWORD dwFlagsAndAttrs, HANDLE hTemplFile)
  214. {
  215. HANDLE retval;
  216. const int buflen = (int) (wStrLen(fname) + 1);
  217. char *cpstr = (char *) __PHYSFS_smallAlloc(buflen);
  218. WideCharToMultiByte(CP_ACP, 0, fname, buflen, cpstr, buflen, NULL, NULL);
  219. retval = CreateFileA(cpstr, dwDesiredAccess, dwShareMode, lpSecurityAttrs,
  220. dwCreationDisposition, dwFlagsAndAttrs, hTemplFile);
  221. __PHYSFS_smallFree(cpstr);
  222. return(retval);
  223. } /* fallbackCreateFileW */
  224. /* A blatant abuse of pointer casting... */
  225. static int symLookup(HMODULE dll, void **addr, const char *sym)
  226. {
  227. return( (*addr = GetProcAddress(dll, sym)) != NULL );
  228. } /* symLookup */
  229. static int findApiSymbols(void)
  230. {
  231. HMODULE dll = NULL;
  232. #define LOOKUP_NOFALLBACK(x) { symLookup(dll, (void **) &p##x, #x); }
  233. // #define LOOKUP(x) { \
  234. // if (!symLookup(dll, (void **) &p##x, #x)) \
  235. #define LOOKUP(x) { \
  236. p##x = fallback##x; \
  237. }
  238. dll = libUserEnv = LoadLibraryA("userenv.dll");
  239. if (dll != NULL)
  240. LOOKUP_NOFALLBACK(GetUserProfileDirectoryW);
  241. /* !!! FIXME: what do they call advapi32.dll on Win64? */
  242. dll = libAdvApi32 = LoadLibraryA("advapi32.dll");
  243. if (dll != NULL)
  244. LOOKUP(GetUserNameW);
  245. /* !!! FIXME: what do they call kernel32.dll on Win64? */
  246. dll = libKernel32 = LoadLibraryA("kernel32.dll");
  247. if (dll != NULL)
  248. {
  249. LOOKUP_NOFALLBACK(GetFileAttributesExA);
  250. // LOOKUP_NOFALLBACK(GetFileAttributesExW);
  251. LOOKUP(GetModuleFileNameW);
  252. LOOKUP(FormatMessageW);
  253. // LOOKUP_NOFALLBACK(FindFirstFileW);
  254. // LOOKUP_NOFALLBACK(FindNextFileW);
  255. LOOKUP(GetFileAttributesW);
  256. LOOKUP(GetCurrentDirectoryW);
  257. LOOKUP(CreateDirectoryW);
  258. LOOKUP(RemoveDirectoryW);
  259. LOOKUP(CreateFileW);
  260. LOOKUP(DeleteFileW);
  261. } /* if */
  262. #undef LOOKUP_NOFALLBACK
  263. #undef LOOKUP
  264. return(1);
  265. } /* findApiSymbols */
  266. const char *__PHYSFS_platformDirSeparator = "\\";
  267. /*
  268. * Figure out what the last failing Windows API call was, and
  269. * generate a human-readable string for the error message.
  270. *
  271. * The return value is a static buffer that is overwritten with
  272. * each call to this function.
  273. */
  274. static const char *winApiStrError(void)
  275. {
  276. static char utf8buf[255];
  277. WCHAR msgbuf[255];
  278. WCHAR *ptr;
  279. DWORD rc = pFormatMessageW(
  280. FORMAT_MESSAGE_FROM_SYSTEM |
  281. FORMAT_MESSAGE_IGNORE_INSERTS,
  282. NULL, GetLastError(),
  283. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  284. msgbuf, __PHYSFS_ARRAYLEN(msgbuf),
  285. NULL);
  286. /* chop off newlines. */
  287. for (ptr = msgbuf; *ptr; ptr++)
  288. {
  289. if ((*ptr == '\n') || (*ptr == '\r'))
  290. {
  291. *ptr = '\0';
  292. break;
  293. } /* if */
  294. } /* for */
  295. /* may truncate, but oh well. */
  296. PHYSFS_utf8FromUcs2((PHYSFS_uint16 *) msgbuf, utf8buf, sizeof (utf8buf));
  297. return((const char *) utf8buf);
  298. } /* winApiStrError */
  299. static char *getExePath(void)
  300. {
  301. DWORD buflen = 64;
  302. int success = 0;
  303. LPWSTR modpath = NULL;
  304. char *retval = NULL;
  305. while (1)
  306. {
  307. DWORD rc;
  308. void *ptr;
  309. if ( !(ptr = allocator.Realloc(modpath, buflen*sizeof(WCHAR))) )
  310. {
  311. allocator.Free(modpath);
  312. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  313. } /* if */
  314. modpath = (LPWSTR) ptr;
  315. rc = pGetModuleFileNameW(NULL, modpath, buflen);
  316. if (rc == 0)
  317. {
  318. allocator.Free(modpath);
  319. BAIL_MACRO(winApiStrError(), NULL);
  320. } /* if */
  321. if (rc < buflen)
  322. {
  323. buflen = rc;
  324. break;
  325. } /* if */
  326. buflen *= 2;
  327. } /* while */
  328. if (buflen > 0) /* just in case... */
  329. {
  330. WCHAR *ptr = (modpath + buflen) - 1;
  331. while (ptr != modpath)
  332. {
  333. if (*ptr == '\\')
  334. break;
  335. ptr--;
  336. } /* while */
  337. if ((ptr == modpath) && (*ptr != '\\'))
  338. __PHYSFS_setError(ERR_GETMODFN_NO_DIR);
  339. else
  340. {
  341. *(ptr + 1) = '\0'; /* chop off filename. */
  342. retval = unicodeToUtf8Heap(modpath);
  343. } /* else */
  344. } /* else */
  345. allocator.Free(modpath);
  346. return(retval); /* w00t. */
  347. } /* getExePath */
  348. /*
  349. * Try to make use of GetUserProfileDirectoryW(), which isn't available on
  350. * some common variants of Win32. If we can't use this, we just punt and
  351. * use the physfs base dir for the user dir, too.
  352. *
  353. * On success, module-scope variable (userDir) will have a pointer to
  354. * a malloc()'d string of the user's profile dir, and a non-zero value is
  355. * returned. If we can't determine the profile dir, (userDir) will
  356. * be NULL, and zero is returned.
  357. */
  358. static int determineUserDir(void)
  359. {
  360. if (userDir != NULL)
  361. return(1); /* already good to go. */
  362. /*
  363. * GetUserProfileDirectoryW() is only available on NT 4.0 and later.
  364. * This means Win95/98/ME (and CE?) users have to do without, so for
  365. * them, we'll default to the base directory when we can't get the
  366. * function pointer. Since this is originally an NT API, we don't
  367. * offer a non-Unicode fallback.
  368. */
  369. if (pGetUserProfileDirectoryW != NULL)
  370. {
  371. HANDLE accessToken = NULL; /* Security handle to process */
  372. HANDLE processHandle = GetCurrentProcess();
  373. if (OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken))
  374. {
  375. DWORD psize = 0;
  376. WCHAR dummy = 0;
  377. LPWSTR wstr = NULL;
  378. BOOL rc = 0;
  379. /*
  380. * Should fail. Will write the size of the profile path in
  381. * psize. Also note that the second parameter can't be
  382. * NULL or the function fails.
  383. */
  384. rc = pGetUserProfileDirectoryW(accessToken, &dummy, &psize);
  385. assert(!rc); /* !!! FIXME: handle this gracefully. */
  386. /* Allocate memory for the profile directory */
  387. wstr = (LPWSTR) __PHYSFS_smallAlloc(psize * sizeof (WCHAR));
  388. if (wstr != NULL)
  389. {
  390. if (pGetUserProfileDirectoryW(accessToken, wstr, &psize))
  391. userDir = unicodeToUtf8Heap(wstr);
  392. __PHYSFS_smallFree(wstr);
  393. } /* else */
  394. } /* if */
  395. CloseHandle(accessToken);
  396. } /* if */
  397. if (userDir == NULL) /* couldn't get profile for some reason. */
  398. {
  399. /* Might just be a non-NT system; resort to the basedir. */
  400. userDir = getExePath();
  401. BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* STILL failed?! */
  402. } /* if */
  403. return(1); /* We made it: hit the showers. */
  404. } /* determineUserDir */
  405. static BOOL mediaInDrive(const char *drive)
  406. {
  407. UINT oldErrorMode;
  408. DWORD tmp;
  409. BOOL retval;
  410. /* Prevent windows warning message appearing when checking media size */
  411. oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
  412. /* If this function succeeds, there's media in the drive */
  413. retval = GetVolumeInformationA(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0);
  414. /* Revert back to old windows error handler */
  415. SetErrorMode(oldErrorMode);
  416. return(retval);
  417. } /* mediaInDrive */
  418. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  419. {
  420. /* !!! FIXME: Can CD drives be non-drive letter paths? */
  421. /* !!! FIXME: (so can they be Unicode paths?) */
  422. char drive_str[4] = "x:\\";
  423. char ch;
  424. for (ch = 'A'; ch <= 'Z'; ch++)
  425. {
  426. drive_str[0] = ch;
  427. if (GetDriveType(drive_str) == DRIVE_CDROM && mediaInDrive(drive_str))
  428. cb(data, drive_str);
  429. } /* for */
  430. } /* __PHYSFS_platformDetectAvailableCDs */
  431. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  432. {
  433. if ((argv0 != NULL) && (strchr(argv0, '\\') != NULL))
  434. return(NULL); /* default behaviour can handle this. */
  435. return(getExePath());
  436. } /* __PHYSFS_platformCalcBaseDir */
  437. char *__PHYSFS_platformGetUserName(void)
  438. {
  439. DWORD bufsize = 0;
  440. char *retval = NULL;
  441. if (pGetUserNameW(NULL, &bufsize) == 0) /* This SHOULD fail. */
  442. {
  443. LPWSTR wbuf = (LPWSTR) __PHYSFS_smallAlloc(bufsize * sizeof (WCHAR));
  444. BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
  445. if (pGetUserNameW(wbuf, &bufsize) == 0) /* ?! */
  446. __PHYSFS_setError(winApiStrError());
  447. else
  448. retval = unicodeToUtf8Heap(wbuf);
  449. __PHYSFS_smallFree(wbuf);
  450. } /* if */
  451. return(retval);
  452. } /* __PHYSFS_platformGetUserName */
  453. char *__PHYSFS_platformGetUserDir(void)
  454. {
  455. char *retval = (char *) allocator.Malloc(strlen(userDir) + 1);
  456. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  457. strcpy(retval, userDir); /* calculated at init time. */
  458. return(retval);
  459. } /* __PHYSFS_platformGetUserDir */
  460. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  461. {
  462. return((PHYSFS_uint64) GetCurrentThreadId());
  463. } /* __PHYSFS_platformGetThreadID */
  464. static int doPlatformExists(LPWSTR wpath)
  465. {
  466. BAIL_IF_MACRO
  467. (
  468. pGetFileAttributesW(wpath) == PHYSFS_INVALID_FILE_ATTRIBUTES,
  469. winApiStrError(), 0
  470. );
  471. return(1);
  472. } /* doPlatformExists */
  473. int __PHYSFS_platformExists(const char *fname)
  474. {
  475. int retval = 0;
  476. LPWSTR wpath;
  477. UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
  478. BAIL_IF_MACRO(wpath == NULL, ERR_OUT_OF_MEMORY, 0);
  479. retval = doPlatformExists(wpath);
  480. __PHYSFS_smallFree(wpath);
  481. return(retval);
  482. } /* __PHYSFS_platformExists */
  483. int __PHYSFS_platformIsSymLink(const char *fname)
  484. {
  485. /* !!! FIXME: Vista has symlinks. Recheck this. */
  486. return(0); /* no symlinks on win32. */
  487. } /* __PHYSFS_platformIsSymlink */
  488. int __PHYSFS_platformIsDirectory(const char *fname)
  489. {
  490. int retval = 0;
  491. LPWSTR wpath;
  492. UTF8_TO_UNICODE_STACK_MACRO(wpath, fname);
  493. BAIL_IF_MACRO(wpath == NULL, ERR_OUT_OF_MEMORY, 0);
  494. retval = ((pGetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  495. __PHYSFS_smallFree(wpath);
  496. return(retval);
  497. } /* __PHYSFS_platformIsDirectory */
  498. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  499. const char *dirName,
  500. const char *append)
  501. {
  502. int len = ((prepend) ? strlen(prepend) : 0) +
  503. ((append) ? strlen(append) : 0) +
  504. strlen(dirName) + 1;
  505. char *retval = (char *) allocator.Malloc(len);
  506. char *p;
  507. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  508. if (prepend)
  509. strcpy(retval, prepend);
  510. else
  511. retval[0] = '\0';
  512. strcat(retval, dirName);
  513. if (append)
  514. strcat(retval, append);
  515. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  516. *p = '\\';
  517. return(retval);
  518. } /* __PHYSFS_platformCvtToDependent */
  519. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  520. int omitSymLinks,
  521. PHYSFS_EnumFilesCallback callback,
  522. const char *origdir,
  523. void *callbackdata)
  524. {
  525. const int unicode = (pFindFirstFileW != NULL) && (pFindNextFileW != NULL);
  526. HANDLE dir = INVALID_HANDLE_VALUE;
  527. WIN32_FIND_DATA ent;
  528. WIN32_FIND_DATAW entw;
  529. size_t len = strlen(dirname);
  530. char *searchPath = NULL;
  531. WCHAR *wSearchPath = NULL;
  532. char *utf8 = NULL;
  533. /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
  534. searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
  535. if (searchPath == NULL)
  536. return;
  537. /* Copy current dirname */
  538. strcpy(searchPath, dirname);
  539. /* if there's no '\\' at the end of the path, stick one in there. */
  540. if (searchPath[len - 1] != '\\')
  541. {
  542. searchPath[len++] = '\\';
  543. searchPath[len] = '\0';
  544. } /* if */
  545. /* Append the "*" to the end of the string */
  546. strcat(searchPath, "*");
  547. UTF8_TO_UNICODE_STACK_MACRO(wSearchPath, searchPath);
  548. if (wSearchPath == NULL)
  549. return; /* oh well. */
  550. if (unicode)
  551. dir = pFindFirstFileW(wSearchPath, &entw);
  552. else
  553. {
  554. const int len = (int) (wStrLen(wSearchPath) + 1);
  555. char *cp = (char *) __PHYSFS_smallAlloc(len);
  556. if (cp != NULL)
  557. {
  558. WideCharToMultiByte(CP_ACP, 0, wSearchPath, len, cp, len, 0, 0);
  559. dir = FindFirstFileA(cp, &ent);
  560. __PHYSFS_smallFree(cp);
  561. } /* if */
  562. } /* else */
  563. __PHYSFS_smallFree(wSearchPath);
  564. __PHYSFS_smallFree(searchPath);
  565. if (dir == INVALID_HANDLE_VALUE)
  566. return;
  567. if (unicode)
  568. {
  569. do
  570. {
  571. const WCHAR *fn = entw.cFileName;
  572. if ((fn[0] == '.') && (fn[1] == '\0'))
  573. continue;
  574. if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
  575. continue;
  576. utf8 = unicodeToUtf8Heap(entw.cFileName);
  577. if (utf8 != NULL)
  578. {
  579. callback(callbackdata, origdir, utf8);
  580. allocator.Free(utf8);
  581. } /* if */
  582. } while (pFindNextFileW(dir, &entw) != 0);
  583. } /* if */
  584. else /* ANSI fallback. */
  585. {
  586. do
  587. {
  588. const char *fn = ent.cFileName;
  589. if ((fn[0] == '.') && (fn[1] == '\0'))
  590. continue;
  591. if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
  592. continue;
  593. utf8 = codepageToUtf8Heap(ent.cFileName);
  594. if (utf8 != NULL)
  595. {
  596. callback(callbackdata, origdir, utf8);
  597. allocator.Free(utf8);
  598. } /* if */
  599. } while (FindNextFileA(dir, &ent) != 0);
  600. } /* else */
  601. FindClose(dir);
  602. } /* __PHYSFS_platformEnumerateFiles */
  603. char *__PHYSFS_platformCurrentDir(void)
  604. {
  605. char *retval = NULL;
  606. WCHAR *wbuf = NULL;
  607. DWORD buflen = 0;
  608. buflen = pGetCurrentDirectoryW(buflen, NULL);
  609. wbuf = (WCHAR *) __PHYSFS_smallAlloc((buflen + 2) * sizeof (WCHAR));
  610. BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
  611. pGetCurrentDirectoryW(buflen, wbuf);
  612. if (wbuf[buflen - 2] == '\\')
  613. wbuf[buflen-1] = '\0'; /* just in case... */
  614. else
  615. {
  616. wbuf[buflen - 1] = '\\';
  617. wbuf[buflen] = '\0';
  618. } /* else */
  619. retval = unicodeToUtf8Heap(wbuf);
  620. __PHYSFS_smallFree(wbuf);
  621. return(retval);
  622. } /* __PHYSFS_platformCurrentDir */
  623. /* this could probably use a cleanup. */
  624. char *__PHYSFS_platformRealPath(const char *path)
  625. {
  626. /* !!! FIXME: try GetFullPathName() instead? */
  627. /* this function should be UTF-8 clean. */
  628. char *retval = NULL;
  629. char *p = NULL;
  630. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  631. BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
  632. retval = (char *) allocator.Malloc(MAX_PATH);
  633. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  634. /*
  635. * If in \\server\path format, it's already an absolute path.
  636. * We'll need to check for "." and ".." dirs, though, just in case.
  637. */
  638. if ((path[0] == '\\') && (path[1] == '\\'))
  639. strcpy(retval, path);
  640. else
  641. {
  642. char *currentDir = __PHYSFS_platformCurrentDir();
  643. if (currentDir == NULL)
  644. {
  645. allocator.Free(retval);
  646. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  647. } /* if */
  648. if (path[1] == ':') /* drive letter specified? */
  649. {
  650. /*
  651. * Apparently, "D:mypath" is the same as "D:\\mypath" if
  652. * D: is not the current drive. However, if D: is the
  653. * current drive, then "D:mypath" is a relative path. Ugh.
  654. */
  655. if (path[2] == '\\') /* maybe an absolute path? */
  656. strcpy(retval, path);
  657. else /* definitely an absolute path. */
  658. {
  659. if (path[0] == currentDir[0]) /* current drive; relative. */
  660. {
  661. strcpy(retval, currentDir);
  662. strcat(retval, path + 2);
  663. } /* if */
  664. else /* not current drive; absolute. */
  665. {
  666. retval[0] = path[0];
  667. retval[1] = ':';
  668. retval[2] = '\\';
  669. strcpy(retval + 3, path + 2);
  670. } /* else */
  671. } /* else */
  672. } /* if */
  673. else /* no drive letter specified. */
  674. {
  675. if (path[0] == '\\') /* absolute path. */
  676. {
  677. retval[0] = currentDir[0];
  678. retval[1] = ':';
  679. strcpy(retval + 2, path);
  680. } /* if */
  681. else
  682. {
  683. strcpy(retval, currentDir);
  684. strcat(retval, path);
  685. } /* else */
  686. } /* else */
  687. allocator.Free(currentDir);
  688. } /* else */
  689. /* (whew.) Ok, now take out "." and ".." path entries... */
  690. p = retval;
  691. while ( (p = strstr(p, "\\.")) != NULL)
  692. {
  693. /* it's a "." entry that doesn't end the string. */
  694. if (p[2] == '\\')
  695. memmove(p + 1, p + 3, strlen(p + 3) + 1);
  696. /* it's a "." entry that ends the string. */
  697. else if (p[2] == '\0')
  698. p[0] = '\0';
  699. /* it's a ".." entry. */
  700. else if (p[2] == '.')
  701. {
  702. char *prevEntry = p - 1;
  703. while ((prevEntry != retval) && (*prevEntry != '\\'))
  704. prevEntry--;
  705. if (prevEntry == retval) /* make it look like a "." entry. */
  706. memmove(p + 1, p + 2, strlen(p + 2) + 1);
  707. else
  708. {
  709. if (p[3] != '\0') /* doesn't end string. */
  710. *prevEntry = '\0';
  711. else /* ends string. */
  712. memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
  713. p = prevEntry;
  714. } /* else */
  715. } /* else if */
  716. else
  717. {
  718. p++; /* look past current char. */
  719. } /* else */
  720. } /* while */
  721. /* shrink the retval's memory block if possible... */
  722. p = (char *) allocator.Realloc(retval, strlen(retval) + 1);
  723. if (p != NULL)
  724. retval = p;
  725. return(retval);
  726. } /* __PHYSFS_platformRealPath */
  727. int __PHYSFS_platformMkDir(const char *path)
  728. {
  729. WCHAR *wpath;
  730. DWORD rc;
  731. UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
  732. rc = pCreateDirectoryW(wpath, NULL);
  733. __PHYSFS_smallFree(wpath);
  734. BAIL_IF_MACRO(rc == 0, winApiStrError(), 0);
  735. return(1);
  736. } /* __PHYSFS_platformMkDir */
  737. int __PHYSFS_platformInit(void)
  738. {
  739. BAIL_IF_MACRO(!findApiSymbols(), NULL, 0);
  740. BAIL_IF_MACRO(!determineUserDir(), NULL, 0);
  741. return(1); /* It's all good */
  742. } /* __PHYSFS_platformInit */
  743. int __PHYSFS_platformDeinit(void)
  744. {
  745. HANDLE *libs[] = { &libKernel32, &libUserEnv, &libAdvApi32, NULL };
  746. int i;
  747. allocator.Free(userDir);
  748. userDir = NULL;
  749. for (i = 0; libs[i] != NULL; i++)
  750. {
  751. const HANDLE lib = *(libs[i]);
  752. if (lib)
  753. FreeLibrary(lib);
  754. *(libs[i]) = NULL;
  755. } /* for */
  756. return(1); /* It's all good */
  757. } /* __PHYSFS_platformDeinit */
  758. static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
  759. {
  760. HANDLE fileHandle;
  761. WinApiFile *retval;
  762. WCHAR *wfname;
  763. UTF8_TO_UNICODE_STACK_MACRO(wfname, fname);
  764. BAIL_IF_MACRO(wfname == NULL, ERR_OUT_OF_MEMORY, NULL);
  765. fileHandle = pCreateFileW(wfname, mode, FILE_SHARE_READ, NULL,
  766. creation, FILE_ATTRIBUTE_NORMAL, NULL);
  767. __PHYSFS_smallFree(wfname);
  768. BAIL_IF_MACRO
  769. (
  770. fileHandle == INVALID_HANDLE_VALUE,
  771. winApiStrError(), NULL
  772. );
  773. retval = (WinApiFile *) allocator.Malloc(sizeof (WinApiFile));
  774. if (retval == NULL)
  775. {
  776. CloseHandle(fileHandle);
  777. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  778. } /* if */
  779. retval->readonly = rdonly;
  780. retval->handle = fileHandle;
  781. return(retval);
  782. } /* doOpen */
  783. void *__PHYSFS_platformOpenRead(const char *filename)
  784. {
  785. return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
  786. } /* __PHYSFS_platformOpenRead */
  787. void *__PHYSFS_platformOpenWrite(const char *filename)
  788. {
  789. return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
  790. } /* __PHYSFS_platformOpenWrite */
  791. void *__PHYSFS_platformOpenAppend(const char *filename)
  792. {
  793. void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
  794. if (retval != NULL)
  795. {
  796. HANDLE h = ((WinApiFile *) retval)->handle;
  797. DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
  798. if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
  799. {
  800. const char *err = winApiStrError();
  801. CloseHandle(h);
  802. allocator.Free(retval);
  803. BAIL_MACRO(err, NULL);
  804. } /* if */
  805. } /* if */
  806. return(retval);
  807. } /* __PHYSFS_platformOpenAppend */
  808. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  809. PHYSFS_uint32 size, PHYSFS_uint32 count)
  810. {
  811. HANDLE Handle = ((WinApiFile *) opaque)->handle;
  812. DWORD CountOfBytesRead;
  813. PHYSFS_sint64 retval;
  814. /* Read data from the file */
  815. /* !!! FIXME: uint32 might be a greater # than DWORD */
  816. if(!ReadFile(Handle, buffer, count * size, &CountOfBytesRead, NULL))
  817. {
  818. BAIL_MACRO(winApiStrError(), -1);
  819. } /* if */
  820. else
  821. {
  822. /* Return the number of "objects" read. */
  823. /* !!! FIXME: What if not the right amount of bytes was read to make an object? */
  824. retval = CountOfBytesRead / size;
  825. } /* else */
  826. return(retval);
  827. } /* __PHYSFS_platformRead */
  828. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  829. PHYSFS_uint32 size, PHYSFS_uint32 count)
  830. {
  831. HANDLE Handle = ((WinApiFile *) opaque)->handle;
  832. DWORD CountOfBytesWritten;
  833. PHYSFS_sint64 retval;
  834. /* Read data from the file */
  835. /* !!! FIXME: uint32 might be a greater # than DWORD */
  836. if(!WriteFile(Handle, buffer, count * size, &CountOfBytesWritten, NULL))
  837. {
  838. BAIL_MACRO(winApiStrError(), -1);
  839. } /* if */
  840. else
  841. {
  842. /* Return the number of "objects" read. */
  843. /* !!! FIXME: What if not the right number of bytes was written? */
  844. retval = CountOfBytesWritten / size;
  845. } /* else */
  846. return(retval);
  847. } /* __PHYSFS_platformWrite */
  848. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  849. {
  850. HANDLE Handle = ((WinApiFile *) opaque)->handle;
  851. DWORD HighOrderPos;
  852. DWORD *pHighOrderPos;
  853. DWORD rc;
  854. /* Get the high order 32-bits of the position */
  855. HighOrderPos = HIGHORDER_UINT64(pos);
  856. /*
  857. * MSDN: "If you do not need the high-order 32 bits, this
  858. * pointer must be set to NULL."
  859. */
  860. pHighOrderPos = (HighOrderPos) ? &HighOrderPos : NULL;
  861. /*
  862. * !!! FIXME: MSDN: "Windows Me/98/95: If the pointer
  863. * !!! FIXME: lpDistanceToMoveHigh is not NULL, then it must
  864. * !!! FIXME: point to either 0, INVALID_SET_FILE_POINTER, or
  865. * !!! FIXME: the sign extension of the value of lDistanceToMove.
  866. * !!! FIXME: Any other value will be rejected."
  867. */
  868. /* Move pointer "pos" count from start of file */
  869. rc = SetFilePointer(Handle, LOWORDER_UINT64(pos),
  870. pHighOrderPos, FILE_BEGIN);
  871. if ( (rc == PHYSFS_INVALID_SET_FILE_POINTER) &&
  872. (GetLastError() != NO_ERROR) )
  873. {
  874. BAIL_MACRO(winApiStrError(), 0);
  875. } /* if */
  876. return(1); /* No error occured */
  877. } /* __PHYSFS_platformSeek */
  878. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  879. {
  880. HANDLE Handle = ((WinApiFile *) opaque)->handle;
  881. DWORD HighPos = 0;
  882. DWORD LowPos;
  883. PHYSFS_sint64 retval;
  884. /* Get current position */
  885. LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
  886. if ( (LowPos == PHYSFS_INVALID_SET_FILE_POINTER) &&
  887. (GetLastError() != NO_ERROR) )
  888. {
  889. BAIL_MACRO(winApiStrError(), 0);
  890. } /* if */
  891. else
  892. {
  893. /* Combine the high/low order to create the 64-bit position value */
  894. retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
  895. assert(retval >= 0);
  896. } /* else */
  897. return(retval);
  898. } /* __PHYSFS_platformTell */
  899. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  900. {
  901. HANDLE Handle = ((WinApiFile *) opaque)->handle;
  902. DWORD SizeHigh;
  903. DWORD SizeLow;
  904. PHYSFS_sint64 retval;
  905. SizeLow = GetFileSize(Handle, &SizeHigh);
  906. if ( (SizeLow == PHYSFS_INVALID_SET_FILE_POINTER) &&
  907. (GetLastError() != NO_ERROR) )
  908. {
  909. BAIL_MACRO(winApiStrError(), -1);
  910. } /* if */
  911. else
  912. {
  913. /* Combine the high/low order to create the 64-bit position value */
  914. retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
  915. assert(retval >= 0);
  916. } /* else */
  917. return(retval);
  918. } /* __PHYSFS_platformFileLength */
  919. int __PHYSFS_platformEOF(void *opaque)
  920. {
  921. PHYSFS_sint64 FilePosition;
  922. int retval = 0;
  923. /* Get the current position in the file */
  924. if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
  925. {
  926. /* Non-zero if EOF is equal to the file length */
  927. retval = FilePosition == __PHYSFS_platformFileLength(opaque);
  928. } /* if */
  929. return(retval);
  930. } /* __PHYSFS_platformEOF */
  931. int __PHYSFS_platformFlush(void *opaque)
  932. {
  933. WinApiFile *fh = ((WinApiFile *) opaque);
  934. if (!fh->readonly)
  935. BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), winApiStrError(), 0);
  936. return(1);
  937. } /* __PHYSFS_platformFlush */
  938. int __PHYSFS_platformClose(void *opaque)
  939. {
  940. HANDLE Handle = ((WinApiFile *) opaque)->handle;
  941. BAIL_IF_MACRO(!CloseHandle(Handle), winApiStrError(), 0);
  942. allocator.Free(opaque);
  943. return(1);
  944. } /* __PHYSFS_platformClose */
  945. static int doPlatformDelete(LPWSTR wpath)
  946. {
  947. /* If filename is a folder */
  948. if (pGetFileAttributesW(wpath) == FILE_ATTRIBUTE_DIRECTORY)
  949. {
  950. BAIL_IF_MACRO(!pRemoveDirectoryW(wpath), winApiStrError(), 0);
  951. } /* if */
  952. else
  953. {
  954. BAIL_IF_MACRO(!pDeleteFileW(wpath), winApiStrError(), 0);
  955. } /* else */
  956. return(1); /* if you made it here, it worked. */
  957. } /* doPlatformDelete */
  958. int __PHYSFS_platformDelete(const char *path)
  959. {
  960. int retval = 0;
  961. LPWSTR wpath;
  962. UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
  963. BAIL_IF_MACRO(wpath == NULL, ERR_OUT_OF_MEMORY, 0);
  964. retval = doPlatformDelete(wpath);
  965. __PHYSFS_smallFree(wpath);
  966. return(retval);
  967. } /* __PHYSFS_platformDelete */
  968. /*
  969. * !!! FIXME: why aren't we using Critical Sections instead of Mutexes?
  970. * !!! FIXME: mutexes on Windows are for cross-process sync. CritSects are
  971. * !!! FIXME: mutexes for threads in a single process and are faster.
  972. */
  973. void *__PHYSFS_platformCreateMutex(void)
  974. {
  975. return((void *) CreateMutex(NULL, FALSE, NULL));
  976. } /* __PHYSFS_platformCreateMutex */
  977. void __PHYSFS_platformDestroyMutex(void *mutex)
  978. {
  979. CloseHandle((HANDLE) mutex);
  980. } /* __PHYSFS_platformDestroyMutex */
  981. int __PHYSFS_platformGrabMutex(void *mutex)
  982. {
  983. return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
  984. } /* __PHYSFS_platformGrabMutex */
  985. void __PHYSFS_platformReleaseMutex(void *mutex)
  986. {
  987. ReleaseMutex((HANDLE) mutex);
  988. } /* __PHYSFS_platformReleaseMutex */
  989. static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
  990. {
  991. SYSTEMTIME st_utc;
  992. SYSTEMTIME st_localtz;
  993. TIME_ZONE_INFORMATION tzi;
  994. DWORD tzid;
  995. PHYSFS_sint64 retval;
  996. struct tm tm;
  997. BAIL_IF_MACRO(!FileTimeToSystemTime(ft, &st_utc), winApiStrError(), -1);
  998. tzid = GetTimeZoneInformation(&tzi);
  999. BAIL_IF_MACRO(tzid == TIME_ZONE_ID_INVALID, winApiStrError(), -1);
  1000. /* (This API is unsupported and fails on non-NT systems. */
  1001. if (!SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz))
  1002. {
  1003. /* do it by hand. Grumble... */
  1004. ULARGE_INTEGER ui64;
  1005. FILETIME new_ft;
  1006. ui64.LowPart = ft->dwLowDateTime;
  1007. ui64.HighPart = ft->dwHighDateTime;
  1008. if (tzid == TIME_ZONE_ID_STANDARD)
  1009. tzi.Bias += tzi.StandardBias;
  1010. else if (tzid == TIME_ZONE_ID_DAYLIGHT)
  1011. tzi.Bias += tzi.DaylightBias;
  1012. /* convert from minutes to 100-nanosecond increments... */
  1013. ui64.QuadPart -= (((LONGLONG) tzi.Bias) * (600000000));
  1014. /* Move it back into a FILETIME structure... */
  1015. new_ft.dwLowDateTime = ui64.LowPart;
  1016. new_ft.dwHighDateTime = ui64.HighPart;
  1017. /* Convert to something human-readable... */
  1018. if (!FileTimeToSystemTime(&new_ft, &st_localtz))
  1019. BAIL_MACRO(winApiStrError(), -1);
  1020. } /* if */
  1021. /* Convert to a format that mktime() can grok... */
  1022. tm.tm_sec = st_localtz.wSecond;
  1023. tm.tm_min = st_localtz.wMinute;
  1024. tm.tm_hour = st_localtz.wHour;
  1025. tm.tm_mday = st_localtz.wDay;
  1026. tm.tm_mon = st_localtz.wMonth - 1;
  1027. tm.tm_year = st_localtz.wYear - 1900;
  1028. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  1029. tm.tm_yday = -1;
  1030. tm.tm_isdst = -1;
  1031. /* Convert to a format PhysicsFS can grok... */
  1032. retval = (PHYSFS_sint64) mktime(&tm);
  1033. BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
  1034. return(retval);
  1035. } /* FileTimeToPhysfsTime */
  1036. PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
  1037. {
  1038. PHYSFS_sint64 retval = -1;
  1039. WIN32_FILE_ATTRIBUTE_DATA attr;
  1040. int rc = 0;
  1041. memset(&attr, '\0', sizeof (attr));
  1042. /* GetFileAttributesEx didn't show up until Win98 and NT4. */
  1043. if ((pGetFileAttributesExW != NULL) || (pGetFileAttributesExA != NULL))
  1044. {
  1045. WCHAR *wstr;
  1046. UTF8_TO_UNICODE_STACK_MACRO(wstr, fname);
  1047. if (wstr != NULL) /* if NULL, maybe the fallback will work. */
  1048. {
  1049. if (pGetFileAttributesExW != NULL) /* NT/XP/Vista/etc system. */
  1050. rc = pGetFileAttributesExW(wstr, GetFileExInfoStandard, &attr);
  1051. else /* Win98/ME system */
  1052. {
  1053. const int len = (int) (wStrLen(wstr) + 1);
  1054. char *cp = (char *) __PHYSFS_smallAlloc(len);
  1055. if (cp != NULL)
  1056. {
  1057. WideCharToMultiByte(CP_ACP, 0, wstr, len, cp, len, 0, 0);
  1058. rc = pGetFileAttributesExA(cp, GetFileExInfoStandard, &attr);
  1059. __PHYSFS_smallFree(cp);
  1060. } /* if */
  1061. } /* else */
  1062. __PHYSFS_smallFree(wstr);
  1063. } /* if */
  1064. } /* if */
  1065. if (rc) /* had API entry point and it worked. */
  1066. {
  1067. /* 0 return value indicates an error or not supported */
  1068. if ( (attr.ftLastWriteTime.dwHighDateTime != 0) ||
  1069. (attr.ftLastWriteTime.dwLowDateTime != 0) )
  1070. {
  1071. retval = FileTimeToPhysfsTime(&attr.ftLastWriteTime);
  1072. } /* if */
  1073. } /* if */
  1074. /* GetFileTime() has been in the Win32 API since the start. */
  1075. if (retval == -1) /* try a fallback... */
  1076. {
  1077. FILETIME ft;
  1078. BOOL rc;
  1079. const char *err;
  1080. WinApiFile *f = (WinApiFile *) __PHYSFS_platformOpenRead(fname);
  1081. BAIL_IF_MACRO(f == NULL, NULL, -1)
  1082. rc = GetFileTime(f->handle, NULL, NULL, &ft);
  1083. err = winApiStrError();
  1084. CloseHandle(f->handle);
  1085. allocator.Free(f);
  1086. BAIL_IF_MACRO(!rc, err, -1);
  1087. retval = FileTimeToPhysfsTime(&ft);
  1088. } /* if */
  1089. return(retval);
  1090. } /* __PHYSFS_platformGetLastModTime */
  1091. /* !!! FIXME: Don't use C runtime for allocators? */
  1092. int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
  1093. {
  1094. return(0); /* just use malloc() and friends. */
  1095. } /* __PHYSFS_platformSetDefaultAllocator */
  1096. #endif /* PHYSFS_PLATFORM_WINDOWS */
  1097. /* end of windows.c ... */