win32.c 23 KB

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