win32.c 25 KB

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