windows.c 39 KB

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