win32.c 22 KB

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