win32.c 23 KB

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