win32.c 23 KB

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