win32.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * Win32 support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE in the source's root directory.
  5. *
  6. * This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
  7. */
  8. #include <windows.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <ctype.h>
  12. #ifndef DISABLE_NT_SUPPORT
  13. #include <userenv.h>
  14. #endif
  15. #define __PHYSICSFS_INTERNAL__
  16. #include "physfs_internal.h"
  17. #define LOWORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0x00000000FFFFFFFF)
  18. #define HIGHORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0xFFFFFFFF00000000)
  19. const char *__PHYSFS_platformDirSeparator = "\\";
  20. static HANDLE ProcessHandle = NULL; /* Current process handle */
  21. static HANDLE AccessTokenHandle = NULL; /* Security handle to process */
  22. static DWORD ProcessID; /* ID assigned to current process */
  23. static int runningNT; /* TRUE if NT derived OS */
  24. static OSVERSIONINFO OSVersionInfo; /* Information about the OS */
  25. static char *ProfileDirectory = NULL; /* User profile folder */
  26. /* Users without the platform SDK don't have this defined. The original docs
  27. for SetFilePointer() just said to compare with 0xFFFFFFF, so this should
  28. work as desired */
  29. #ifndef INVALID_SET_FILE_POINTER
  30. #define INVALID_SET_FILE_POINTER 0xFFFFFFFF
  31. #endif
  32. static const char *win32strerror(void)
  33. {
  34. static TCHAR msgbuf[255];
  35. FormatMessage(
  36. FORMAT_MESSAGE_FROM_SYSTEM |
  37. FORMAT_MESSAGE_IGNORE_INSERTS,
  38. NULL,
  39. GetLastError(),
  40. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  41. msgbuf,
  42. sizeof (msgbuf) / sizeof (TCHAR),
  43. NULL
  44. );
  45. return((const char *) msgbuf);
  46. } /* win32strerror */
  47. char **__PHYSFS_platformDetectAvailableCDs(void)
  48. {
  49. char **retval = (char **) malloc(sizeof (char *));
  50. int cd_count = 1; /* We count the NULL entry. */
  51. char drive_str[4] = "x:\\";
  52. for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
  53. {
  54. if (GetDriveType(drive_str) == DRIVE_CDROM)
  55. {
  56. /* !!! FIXME: Make sure there's really a disc in the drive? */
  57. char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
  58. if (tmp)
  59. {
  60. retval = tmp;
  61. retval[cd_count-1] = (char *) malloc(4);
  62. if (retval[cd_count-1])
  63. {
  64. strcpy(retval[cd_count-1], drive_str);
  65. cd_count++;
  66. } /* if */
  67. } /* if */
  68. } /* if */
  69. } /* for */
  70. retval[cd_count - 1] = NULL;
  71. return(retval);
  72. } /* __PHYSFS_detectAvailableCDs */
  73. static char *getExePath(const char *argv0)
  74. {
  75. char *filepart = NULL;
  76. char *retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
  77. DWORD buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
  78. retval[buflen] = '\0'; /* does API always null-terminate the string? */
  79. /* make sure the string was not truncated. */
  80. if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") == 0)
  81. {
  82. char *ptr = strrchr(retval, '\\');
  83. if (ptr != NULL)
  84. {
  85. *(ptr + 1) = '\0'; /* chop off filename. */
  86. /* free up the bytes we didn't actually use. */
  87. retval = (char *) realloc(retval, strlen(retval) + 1);
  88. if (retval != NULL)
  89. return(retval);
  90. } /* if */
  91. } /* if */
  92. /* if any part of the previous approach failed, try SearchPath()... */
  93. buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
  94. retval = (char *) realloc(retval, buflen);
  95. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  96. SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
  97. return(retval);
  98. } /* getExePath */
  99. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  100. {
  101. if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
  102. return(NULL);
  103. return(getExePath(argv0));
  104. } /* __PHYSFS_platformCalcBaseDir */
  105. char *__PHYSFS_platformGetUserName(void)
  106. {
  107. DWORD bufsize = 0;
  108. LPTSTR retval = NULL;
  109. if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
  110. {
  111. retval = (LPTSTR) malloc(bufsize);
  112. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  113. if (GetUserName(retval, &bufsize) == 0) /* ?! */
  114. {
  115. free(retval);
  116. retval = NULL;
  117. } /* if */
  118. } /* if */
  119. return((char *) retval);
  120. } /* __PHYSFS_platformGetUserName */
  121. char *__PHYSFS_platformGetUserDir(void)
  122. {
  123. return ProfileDirectory;
  124. } /* __PHYSFS_platformGetUserDir */
  125. PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
  126. {
  127. return((PHYSFS_uint64)GetCurrentThreadId());
  128. } /* __PHYSFS_platformGetThreadID */
  129. /* ...make this Cygwin AND Visual C friendly... */
  130. int __PHYSFS_platformStricmp(const char *x, const char *y)
  131. {
  132. #if (defined _MSC_VER)
  133. return(stricmp(x, y));
  134. #else
  135. int ux, uy;
  136. do
  137. {
  138. ux = toupper((int) *x);
  139. uy = toupper((int) *y);
  140. if (ux > uy)
  141. return(1);
  142. else if (ux < uy)
  143. return(-1);
  144. x++;
  145. y++;
  146. } while ((ux) && (uy));
  147. return(0);
  148. #endif
  149. } /* __PHYSFS_platformStricmp */
  150. int __PHYSFS_platformExists(const char *fname)
  151. {
  152. return(GetFileAttributes(fname) != 0xffffffff);
  153. } /* __PHYSFS_platformExists */
  154. int __PHYSFS_platformIsSymLink(const char *fname)
  155. {
  156. return(0); /* no symlinks on win32. */
  157. } /* __PHYSFS_platformIsSymlink */
  158. int __PHYSFS_platformIsDirectory(const char *fname)
  159. {
  160. return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
  161. } /* __PHYSFS_platformIsDirectory */
  162. char *__PHYSFS_platformCvtToDependent(const char *prepend,
  163. const char *dirName,
  164. const char *append)
  165. {
  166. int len = ((prepend) ? strlen(prepend) : 0) +
  167. ((append) ? strlen(append) : 0) +
  168. strlen(dirName) + 1;
  169. char *retval = malloc(len);
  170. char *p;
  171. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  172. if (prepend)
  173. strcpy(retval, prepend);
  174. else
  175. retval[0] = '\0';
  176. strcat(retval, dirName);
  177. if (append)
  178. strcat(retval, append);
  179. for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
  180. *p = '\\';
  181. return(retval);
  182. } /* __PHYSFS_platformCvtToDependent */
  183. /* Much like my college days, try to sleep for 10 milliseconds at a time... */
  184. void __PHYSFS_platformTimeslice(void)
  185. {
  186. Sleep(10);
  187. } /* __PHYSFS_platformTimeslice */
  188. LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
  189. int omitSymLinks)
  190. {
  191. LinkedStringList *retval = NULL;
  192. LinkedStringList *l = NULL;
  193. LinkedStringList *prev = NULL;
  194. HANDLE dir;
  195. WIN32_FIND_DATA ent;
  196. dir = FindFirstFile(dirname, &ent);
  197. BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
  198. while (FindNextFile(dir, &ent) != 0)
  199. {
  200. if (strcmp(ent.cFileName, ".") == 0)
  201. continue;
  202. if (strcmp(ent.cFileName, "..") == 0)
  203. continue;
  204. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  205. if (l == NULL)
  206. break;
  207. l->str = (char *) malloc(strlen(ent.cFileName) + 1);
  208. if (l->str == NULL)
  209. {
  210. free(l);
  211. break;
  212. } /* if */
  213. strcpy(l->str, ent.cFileName);
  214. if (retval == NULL)
  215. retval = l;
  216. else
  217. prev->next = l;
  218. prev = l;
  219. l->next = NULL;
  220. } /* while */
  221. FindClose(dir);
  222. return(retval);
  223. } /* __PHYSFS_platformEnumerateFiles */
  224. char *__PHYSFS_platformCurrentDir(void)
  225. {
  226. LPTSTR retval;
  227. DWORD buflen = 0;
  228. buflen = GetCurrentDirectory(buflen, NULL);
  229. retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
  230. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  231. GetCurrentDirectory(buflen, retval);
  232. if (retval[buflen - 2] != '\\')
  233. strcat(retval, "\\");
  234. return((char *) retval);
  235. } /* __PHYSFS_platformCurrentDir */
  236. /* this could probably use a cleanup. */
  237. char *__PHYSFS_platformRealPath(const char *path)
  238. {
  239. char *retval = NULL;
  240. char *p = NULL;
  241. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  242. BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
  243. retval = (char *) malloc(MAX_PATH);
  244. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  245. /*
  246. * If in \\server\path format, it's already an absolute path.
  247. * We'll need to check for "." and ".." dirs, though, just in case.
  248. */
  249. if ((path[0] == '\\') && (path[1] == '\\'))
  250. strcpy(retval, path);
  251. else
  252. {
  253. char *currentDir = __PHYSFS_platformCurrentDir();
  254. if (currentDir == NULL)
  255. {
  256. free(retval);
  257. BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
  258. } /* if */
  259. if (path[1] == ':') /* drive letter specified? */
  260. {
  261. /*
  262. * Apparently, "D:mypath" is the same as "D:\\mypath" if
  263. * D: is not the current drive. However, if D: is the
  264. * current drive, then "D:mypath" is a relative path. Ugh.
  265. */
  266. if (path[2] == '\\') /* maybe an absolute path? */
  267. strcpy(retval, path);
  268. else /* definitely an absolute path. */
  269. {
  270. if (path[0] == currentDir[0]) /* current drive; relative. */
  271. {
  272. strcpy(retval, currentDir);
  273. strcat(retval, path + 2);
  274. } /* if */
  275. else /* not current drive; absolute. */
  276. {
  277. retval[0] = path[0];
  278. retval[1] = ':';
  279. retval[2] = '\\';
  280. strcpy(retval + 3, path + 2);
  281. } /* else */
  282. } /* else */
  283. } /* if */
  284. else /* no drive letter specified. */
  285. {
  286. if (path[0] == '\\') /* absolute path. */
  287. {
  288. retval[0] = currentDir[0];
  289. retval[1] = ':';
  290. strcpy(retval + 2, path);
  291. } /* if */
  292. else
  293. {
  294. strcpy(retval, currentDir);
  295. strcat(retval, path);
  296. } /* else */
  297. } /* else */
  298. free(currentDir);
  299. } /* else */
  300. /* (whew.) Ok, now take out "." and ".." path entries... */
  301. p = retval;
  302. while ( (p = strstr(p, "\\.")) != NULL)
  303. {
  304. /* it's a "." entry that doesn't end the string. */
  305. if (p[2] == '\\')
  306. memmove(p + 1, p + 3, strlen(p + 3) + 1);
  307. /* it's a "." entry that ends the string. */
  308. else if (p[2] == '\0')
  309. p[0] = '\0';
  310. /* it's a ".." entry. */
  311. else if (p[2] == '.')
  312. {
  313. char *prevEntry = p - 1;
  314. while ((prevEntry != retval) && (*prevEntry != '\\'))
  315. prevEntry--;
  316. if (prevEntry == retval) /* make it look like a "." entry. */
  317. memmove(p + 1, p + 2, strlen(p + 2) + 1);
  318. else
  319. {
  320. if (p[3] != '\0') /* doesn't end string. */
  321. *prevEntry = '\0';
  322. else /* ends string. */
  323. memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
  324. p = prevEntry;
  325. } /* else */
  326. } /* else if */
  327. else
  328. {
  329. p++; /* look past current char. */
  330. } /* else */
  331. } /* while */
  332. /* shrink the retval's memory block if possible... */
  333. p = (char *) realloc(retval, strlen(retval) + 1);
  334. if (p != NULL)
  335. retval = p;
  336. return(retval);
  337. } /* __PHYSFS_platformRealPath */
  338. int __PHYSFS_platformMkDir(const char *path)
  339. {
  340. DWORD rc = CreateDirectory(path, NULL);
  341. BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
  342. return(1);
  343. } /* __PHYSFS_platformMkDir */
  344. /*
  345. * Get OS info and save it.
  346. *
  347. * Returns non-zero if successful, otherwise it returns zero on failure.
  348. */
  349. int getOSInfo(void)
  350. {
  351. /* Get OS info */
  352. OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
  353. if(!GetVersionEx(&OSVersionInfo))
  354. {
  355. return 0;
  356. }
  357. /* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
  358. runningNT = (OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
  359. (OSVersionInfo.dwMajorVersion > 3);
  360. return 1;
  361. }
  362. int __PHYSFS_platformInit(void)
  363. {
  364. if(!getOSInfo())
  365. {
  366. return 0;
  367. }
  368. /* Get Windows ProcessID associated with the current process */
  369. ProcessID = GetCurrentProcessId();
  370. /* Create a process handle associated with the current process ID */
  371. ProcessHandle = GetCurrentProcess();
  372. if(ProcessHandle == NULL)
  373. {
  374. /* Process handle is required by other win32 functions */
  375. return 0;
  376. }
  377. /* Default profile directory is the exe path */
  378. ProfileDirectory = getExePath(NULL);
  379. #ifndef DISABLE_NT_SUPPORT
  380. /* If running an NT system (NT/Win2k/XP, etc...) */
  381. if(runningNT)
  382. {
  383. if(!doNTInit())
  384. {
  385. /* Error initializing NT stuff */
  386. return 0;
  387. }
  388. }
  389. #endif
  390. /* It's all good */
  391. return 1;
  392. }
  393. int __PHYSFS_platformDeinit(void)
  394. {
  395. #ifndef DISABLE_NT_SUPPORT
  396. if(!doNTDeinit())
  397. return 0;
  398. #endif
  399. if(CloseHandle(ProcessHandle) != S_OK)
  400. return 0;
  401. /* It's all good */
  402. return 1;
  403. }
  404. void *__PHYSFS_platformOpenRead(const char *filename)
  405. {
  406. HANDLE FileHandle;
  407. /* Open an existing file for read only. File can be opened by others
  408. who request read access on the file only. */
  409. FileHandle = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
  410. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  411. /* If CreateFile() failed */
  412. if(FileHandle == INVALID_HANDLE_VALUE)
  413. return NULL;
  414. return (void *)FileHandle;
  415. }
  416. void *__PHYSFS_platformOpenWrite(const char *filename)
  417. {
  418. HANDLE FileHandle;
  419. /* Open an existing file for write only. File can be opened by others
  420. who request read access to the file only */
  421. FileHandle = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  422. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  423. /* If CreateFile() failed */
  424. if(FileHandle == INVALID_HANDLE_VALUE)
  425. return NULL;
  426. return (void *)FileHandle;
  427. }
  428. void *__PHYSFS_platformOpenAppend(const char *filename)
  429. {
  430. HANDLE FileHandle;
  431. /* Open an existing file for appending only. File can be opened by others
  432. who request read access to the file only. */
  433. FileHandle = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  434. TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  435. /* If CreateFile() failed */
  436. if(FileHandle == INVALID_HANDLE_VALUE)
  437. /* CreateFile might have failed because the file doesn't exist, so
  438. we'll just create a new file for writing then */
  439. return __PHYSFS_platformOpenWrite(filename);
  440. return (void *)FileHandle;
  441. }
  442. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
  443. PHYSFS_uint32 size, PHYSFS_uint32 count)
  444. {
  445. HANDLE FileHandle;
  446. DWORD CountOfBytesRead;
  447. PHYSFS_sint64 retval;
  448. /* Cast the generic handle to a Win32 handle */
  449. FileHandle = (HANDLE)opaque;
  450. /* Read data from the file */
  451. /*!!! - uint32 might be a greater # than DWORD */
  452. if(!ReadFile(FileHandle, buffer, count * size, &CountOfBytesRead, NULL))
  453. {
  454. /* Set the error to GetLastError */
  455. __PHYSFS_setError(win32strerror());
  456. /* We errored out */
  457. retval = -1;
  458. }
  459. else
  460. {
  461. /* Return the number of "objects" read. */
  462. /* !!! - What if not the right amount of bytes was read to make an object? */
  463. retval = CountOfBytesRead / size;
  464. }
  465. return retval;
  466. }
  467. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, void *buffer,
  468. PHYSFS_uint32 size, PHYSFS_uint32 count)
  469. {
  470. HANDLE FileHandle;
  471. DWORD CountOfBytesWritten;
  472. PHYSFS_sint64 retval;
  473. /* Cast the generic handle to a Win32 handle */
  474. FileHandle = (HANDLE)opaque;
  475. /* Read data from the file */
  476. /*!!! - uint32 might be a greater # than DWORD */
  477. if(!WriteFile(FileHandle, buffer, count * size, &CountOfBytesWritten, NULL))
  478. {
  479. /* Set the error to GetLastError */
  480. __PHYSFS_setError(win32strerror());
  481. /* We errored out */
  482. retval = -1;
  483. }
  484. else
  485. {
  486. /* Return the number of "objects" read. */
  487. /*!!! - What if not the right number of bytes was written? */
  488. retval = CountOfBytesWritten / size;
  489. }
  490. return retval;
  491. }
  492. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  493. {
  494. HANDLE FileHandle;
  495. int retval;
  496. DWORD HighOrderPos;
  497. /* Cast the generic handle to a Win32 handle */
  498. FileHandle = (HANDLE)opaque;
  499. /* Get the high order 32-bits of the position */
  500. HighOrderPos = HIGHORDER_UINT64(pos);
  501. /*!!! SetFilePointer needs a signed 64-bit value. */
  502. /* Move pointer "pos" count from start of file */
  503. if((SetFilePointer(FileHandle, LOWORDER_UINT64(pos), &HighOrderPos, FILE_BEGIN)
  504. == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  505. {
  506. /* An error occured. Set the error to GetLastError */
  507. __PHYSFS_setError(win32strerror());
  508. retval = 0;
  509. }
  510. else
  511. {
  512. /* No error occured */
  513. retval = 1;
  514. }
  515. return retval;
  516. }
  517. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  518. {
  519. HANDLE FileHandle;
  520. DWORD HighOrderPos = 0;
  521. DWORD LowOrderPos;
  522. PHYSFS_sint64 retval;
  523. /* Cast the generic handle to a Win32 handle */
  524. FileHandle = (HANDLE)opaque;
  525. /* Get current position */
  526. if(((LowOrderPos = SetFilePointer(FileHandle, 0, &HighOrderPos, FILE_CURRENT))
  527. == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  528. {
  529. /* Set the error to GetLastError */
  530. __PHYSFS_setError(win32strerror());
  531. /* We errored out */
  532. retval = 0;
  533. }
  534. else
  535. {
  536. /* Combine the high/low order to create the 64-bit position value */
  537. retval = HighOrderPos;
  538. retval = retval << 32;
  539. retval |= LowOrderPos;
  540. }
  541. /*!!! Can't find a file pointer routine?!?!?!!?!?*/
  542. return retval;
  543. }
  544. PHYSFS_sint64 __PHYSFS_platformFileLength(void *handle)
  545. {
  546. HANDLE FileHandle;
  547. DWORD FileSizeHigh;
  548. DWORD FileSizeLow;
  549. PHYSFS_sint64 retval;
  550. /* Cast the generic handle to a Win32 handle */
  551. FileHandle = (HANDLE)handle;
  552. /* Get the file size. Condition evaluates to TRUE if an error occured */
  553. if(((FileSizeLow = GetFileSize(FileHandle, &FileSizeHigh))
  554. == INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
  555. {
  556. /* Set the error to GetLastError */
  557. __PHYSFS_setError(win32strerror());
  558. retval = -1;
  559. }
  560. else
  561. {
  562. /* Combine the high/low order to create the 64-bit position value */
  563. retval = FileSizeHigh;
  564. retval = retval << 32;
  565. retval |= FileSizeLow;
  566. }
  567. return retval;
  568. }
  569. int __PHYSFS_platformEOF(void *opaque)
  570. {
  571. HANDLE FileHandle;
  572. PHYSFS_sint64 FilePosition;
  573. int retval = 0;
  574. /* Cast the generic handle to a Win32 handle */
  575. FileHandle = (HANDLE)opaque;
  576. /* Get the current position in the file */
  577. if((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
  578. {
  579. /* Non-zero if EOF is equal to the file length */
  580. retval = FilePosition == __PHYSFS_platformFileLength(opaque);
  581. }
  582. return retval;
  583. }
  584. int __PHYSFS_platformFlush(void *opaque)
  585. {
  586. HANDLE FileHandle;
  587. int retval;
  588. /* Cast the generic handle to a Win32 handle */
  589. FileHandle = (HANDLE)opaque;
  590. /* Close the file */
  591. if(!(retval = FlushFileBuffers(FileHandle)))
  592. {
  593. /* Set the error to GetLastError */
  594. __PHYSFS_setError(win32strerror());
  595. }
  596. return retval;
  597. }
  598. int __PHYSFS_platformClose(void *opaque)
  599. {
  600. HANDLE FileHandle;
  601. int retval;
  602. /* Cast the generic handle to a Win32 handle */
  603. FileHandle = (HANDLE)opaque;
  604. /* Close the file */
  605. if(!(retval = CloseHandle(FileHandle)))
  606. {
  607. /* Set the error to GetLastError */
  608. __PHYSFS_setError(win32strerror());
  609. }
  610. return retval;
  611. }
  612. /*
  613. * Remove a file or directory entry in the actual filesystem. (path) is
  614. * specified in platform-dependent notation. Note that this deletes files
  615. * _and_ directories, so you might need to do some determination.
  616. * Non-empty directories should report an error and not delete themselves
  617. * or their contents.
  618. *
  619. * Deleting a symlink should remove the link, not what it points to.
  620. *
  621. * On error, return zero and set the error message. Return non-zero on success.
  622. */
  623. int __PHYSFS_platformDelete(const char *path)
  624. {
  625. int retval;
  626. /* If filename is a folder */
  627. if(GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY)
  628. {
  629. retval = RemoveDirectory(path);
  630. }
  631. else
  632. {
  633. retval = DeleteFile(path);
  634. }
  635. if(!retval)
  636. {
  637. /* Set the error to GetLastError */
  638. __PHYSFS_setError(win32strerror());
  639. }
  640. return retval;
  641. }
  642. void *__PHYSFS_platformCreateMutex(void)
  643. {
  644. return (void *)CreateMutex(NULL, FALSE, NULL);
  645. }
  646. void __PHYSFS_platformDestroyMutex(void *mutex)
  647. {
  648. CloseHandle((HANDLE)mutex);
  649. }
  650. int __PHYSFS_platformGrabMutex(void *mutex)
  651. {
  652. int retval;
  653. if(WaitForSingleObject((HANDLE)mutex, INFINITE) == WAIT_FAILED)
  654. {
  655. /* Our wait failed for some unknown reason */
  656. retval = 1;
  657. }
  658. else
  659. {
  660. /* Good to go */
  661. retval = 0;
  662. }
  663. return retval;
  664. }
  665. void __PHYSFS_platformReleaseMutex(void *mutex)
  666. {
  667. ReleaseMutex((HANDLE)mutex);
  668. }
  669. /* NT specific functions go in here so they don't get compiled when not NT */
  670. #ifndef DISABLE_NT_SUPPORT
  671. /*
  672. * Uninitialize any NT specific stuff done in doNTInit().
  673. *
  674. * Return zero if there was a catastrophic failure and non-zero otherwise.
  675. */
  676. static int doNTDeinit(void)
  677. {
  678. if(CloseHandle(AccessTokenHandle) != S_OK)
  679. {
  680. return 0;
  681. }
  682. free(ProfileDirectory);
  683. /* It's all good */
  684. return 1;
  685. }
  686. /*
  687. * Initialize any NT specific stuff. This includes any OS based on NT.
  688. *
  689. * Return zero if there was a catastrophic failure and non-zero otherwise.
  690. */
  691. static int doNTInit(void)
  692. {
  693. DWORD pathsize = 0;
  694. char TempProfileDirectory[1];
  695. /* Create a process access token handle */
  696. if(!OpenProcessToken(ProcessHandle, TOKEN_QUERY, &AccessTokenHandle))
  697. {
  698. /* Access token is required by other win32 functions */
  699. return 0;
  700. }
  701. /* Should fail. Will write the size of the profile path in pathsize*/
  702. /*!!! Second parameter can't be NULL or the function fails??? */
  703. if(!GetUserProfileDirectory(AccessTokenHandle, TempProfileDirectory, &pathsize))
  704. {
  705. /* Allocate memory for the profile directory */
  706. ProfileDirectory = (char *)malloc(pathsize);
  707. BAIL_IF_MACRO(ProfileDirectory == NULL, ERR_OUT_OF_MEMORY, 0);
  708. /* Try to get the profile directory */
  709. if(!GetUserProfileDirectory(AccessTokenHandle, ProfileDirectory, &pathsize))
  710. {
  711. free(ProfileDirectory);
  712. return 0;
  713. }
  714. }
  715. /* Everything initialized okay */
  716. return 1;
  717. }
  718. #endif
  719. /* end of win32.c ... */