platform_winrt.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. * Windows Runtime support routines for PhysicsFS.
  3. *
  4. * Please see the file LICENSE.txt in the source's root directory.
  5. *
  6. * This file is based on "platform_windows.c" by Ryan C. Gordon and Gregory S. Read.
  7. * Windows Runtime-specific additions and changes are made by Martin "T-Bone" Ahrnbom.
  8. *
  9. * Instructions for setting up PhysFS in a WinRT Universal 8.1 app in Visual Studio 2013:
  10. * (hopefully these instructions will be somewhat valid with Windows 10 Apps as well...)
  11. *
  12. * 1. In Visual Studio 2013 (or later?), hit File -> New -> Project...
  13. * 2. On the left, navigate to Templates -> Visual C++ -> Store Apps -> Universal Apps
  14. * 3. In the middle of the window, select "DLL (Universal Apps)"
  15. * 4. Near the bottom, give the project a name (like PhysFS-WinRT) and choose a location
  16. * 5. In the Solution Explorer (to the right typically), delete all .cpp and .h files in
  17. * PhysFS-WinRT.Shared except targetver.h.
  18. * 6. In Windows Explorer, find the "src" folder of the PhysFS source code. Select all files
  19. * in the folder.
  20. * Drag and drop all of the source files onto PhysFS-WinRT-Shared in Visual Studio.
  21. * 7. In Windows Explorer, find the file called "physfs.h". Copy this file into a folder of its own somewhere.
  22. * I suggest naming that folder "include". This will be your "include" folder for any projects using PhysFS.
  23. * 8. In the Solution Explorer, right click on PhysFS-WinRT.Windows and select Properties. Make sure that "Configuration" is set to
  24. * "All Configurations" and "Platform" is set to "All Platforms" near the top of the window.
  25. * 9. On the left, select "Precompiled Headers". Change "Precompiled Header" from "Use (/Yu)" to "Not Using Precompiled Headers".
  26. * 10. On the left, navigate to Configuration Properties -> C/C++ -> Preprocessor. In Preprocessor Definitions, add "_CRT_SECURE_NO_WARNINGS"
  27. * 11. Hit the OK button.
  28. * 12. Repeat steps 8-11 but for PhysFS-WinRT.WindowsPhone.
  29. * 13. In the Solution Explorer, find "platform_winrt.cpp" in PhysFS-WinRT.Shared. Right click on it and select "Properties".
  30. * 14. On the left, navigate to Configuration Properties -> C/C++ -> General. On the right, change "Consume Windows Runtime Extensions"
  31. * from "No" to "Yes (/ZW)". Hit "OK".
  32. * 15. Near the top of the Visual Studio window, select BUILD -> Batch Build... Hit "Select All", and then "Build".
  33. * 16. Now you have a bunch of .dll and .lib files, as well as an include folder that you can use in your projects!
  34. * 17. ???
  35. * 18. Profit!
  36. */
  37. /* !!! FIXME: remove the tabstops from this file. */
  38. /* !!! FIXME: maybe just merge this back into platform_windows.c? */
  39. #define __PHYSICSFS_INTERNAL__
  40. #include "physfs_platforms.h"
  41. #ifdef PHYSFS_PLATFORM_WINRT
  42. #include "physfs_internal.h"
  43. #include <windows.h>
  44. #include <errno.h>
  45. #include <ctype.h>
  46. #include <time.h>
  47. #define LOWORDER_UINT64(pos) ((PHYSFS_uint32) (pos & 0xFFFFFFFF))
  48. #define HIGHORDER_UINT64(pos) ((PHYSFS_uint32) ((pos >> 32) & 0xFFFFFFFF))
  49. /*
  50. * Users without the platform SDK don't have this defined. The original docs
  51. * for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
  52. * work as desired.
  53. */
  54. #define PHYSFS_INVALID_SET_FILE_POINTER 0xFFFFFFFF
  55. /* just in case... */
  56. #define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
  57. /* Not defined before the Vista SDK. */
  58. #define PHYSFS_IO_REPARSE_TAG_SYMLINK 0xA000000C
  59. #define UTF8_TO_UNICODE_STACK(w_assignto, str) { \
  60. if (str == NULL) \
  61. w_assignto = NULL; \
  62. else { \
  63. const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) + 1) * 2); \
  64. w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
  65. if (w_assignto != NULL) \
  66. PHYSFS_utf8ToUtf16(str, (PHYSFS_uint16 *) w_assignto, len); \
  67. } \
  68. } \
  69. /* Note this counts WCHARs, not codepoints! */
  70. static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
  71. {
  72. PHYSFS_uint64 len = 0;
  73. while (*(wstr++))
  74. len++;
  75. return len;
  76. } /* wStrLen */
  77. /* !!! FIXME: do we really need readonly? If not, do we need this struct? */
  78. typedef struct
  79. {
  80. HANDLE handle;
  81. int readonly;
  82. } WinApiFile;
  83. static HANDLE detectCDThreadHandle = NULL;
  84. static HWND detectCDHwnd = 0;
  85. static volatile int initialDiscDetectionComplete = 0;
  86. static volatile DWORD drivesWithMediaBitmap = 0;
  87. static PHYSFS_ErrorCode errcodeFromWinApiError(const DWORD err)
  88. {
  89. /*
  90. * win32 error codes are sort of a tricky thing; Microsoft intentionally
  91. * doesn't list which ones a given API might trigger, there are several
  92. * with overlapping and unclear meanings...and there's 16 thousand of
  93. * them in Windows 7. It looks like the ones we care about are in the
  94. * first 500, but I can't say this list is perfect; we might miss
  95. * important values or misinterpret others.
  96. *
  97. * Don't treat this list as anything other than a work in progress.
  98. */
  99. switch (err)
  100. {
  101. case ERROR_SUCCESS: return PHYSFS_ERR_OK;
  102. case ERROR_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
  103. case ERROR_NETWORK_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
  104. case ERROR_NOT_READY: return PHYSFS_ERR_IO;
  105. case ERROR_CRC: return PHYSFS_ERR_IO;
  106. case ERROR_SEEK: return PHYSFS_ERR_IO;
  107. case ERROR_SECTOR_NOT_FOUND: return PHYSFS_ERR_IO;
  108. case ERROR_NOT_DOS_DISK: return PHYSFS_ERR_IO;
  109. case ERROR_WRITE_FAULT: return PHYSFS_ERR_IO;
  110. case ERROR_READ_FAULT: return PHYSFS_ERR_IO;
  111. case ERROR_DEV_NOT_EXIST: return PHYSFS_ERR_IO;
  112. /* !!! FIXME: ?? case ELOOP: return PHYSFS_ERR_SYMLINK_LOOP; */
  113. case ERROR_BUFFER_OVERFLOW: return PHYSFS_ERR_BAD_FILENAME;
  114. case ERROR_INVALID_NAME: return PHYSFS_ERR_BAD_FILENAME;
  115. case ERROR_BAD_PATHNAME: return PHYSFS_ERR_BAD_FILENAME;
  116. case ERROR_DIRECTORY: return PHYSFS_ERR_BAD_FILENAME;
  117. case ERROR_FILE_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
  118. case ERROR_PATH_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
  119. case ERROR_DELETE_PENDING: return PHYSFS_ERR_NOT_FOUND;
  120. case ERROR_INVALID_DRIVE: return PHYSFS_ERR_NOT_FOUND;
  121. case ERROR_HANDLE_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
  122. case ERROR_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
  123. /* !!! FIXME: ?? case ENOTDIR: return PHYSFS_ERR_NOT_FOUND; */
  124. /* !!! FIXME: ?? case EISDIR: return PHYSFS_ERR_NOT_A_FILE; */
  125. case ERROR_WRITE_PROTECT: return PHYSFS_ERR_READ_ONLY;
  126. case ERROR_LOCK_VIOLATION: return PHYSFS_ERR_BUSY;
  127. case ERROR_SHARING_VIOLATION: return PHYSFS_ERR_BUSY;
  128. case ERROR_CURRENT_DIRECTORY: return PHYSFS_ERR_BUSY;
  129. case ERROR_DRIVE_LOCKED: return PHYSFS_ERR_BUSY;
  130. case ERROR_PATH_BUSY: return PHYSFS_ERR_BUSY;
  131. case ERROR_BUSY: return PHYSFS_ERR_BUSY;
  132. case ERROR_NOT_ENOUGH_MEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
  133. case ERROR_OUTOFMEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
  134. case ERROR_DIR_NOT_EMPTY: return PHYSFS_ERR_DIR_NOT_EMPTY;
  135. default: return PHYSFS_ERR_OS_ERROR;
  136. } /* switch */
  137. } /* errcodeFromWinApiError */
  138. static inline PHYSFS_ErrorCode errcodeFromWinApi(void)
  139. {
  140. return errcodeFromWinApiError(GetLastError());
  141. } /* errcodeFromWinApi */
  142. typedef BOOL(WINAPI *fnSTEM)(DWORD, LPDWORD b);
  143. static DWORD pollDiscDrives(void)
  144. {
  145. // We don't do discs
  146. return 0;
  147. } /* pollDiscDrives */
  148. static LRESULT CALLBACK detectCDWndProc(HWND hwnd, UINT msg,
  149. WPARAM wp, LPARAM lparam)
  150. {
  151. return FALSE;
  152. } /* detectCDWndProc */
  153. static DWORD WINAPI detectCDThread(LPVOID lpParameter)
  154. {
  155. return 0;
  156. } /* detectCDThread */
  157. void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
  158. {
  159. return;
  160. } /* __PHYSFS_platformDetectAvailableCDs */
  161. static char *unicodeToUtf8Heap(const WCHAR *w_str)
  162. {
  163. char *retval = NULL;
  164. if (w_str != NULL)
  165. {
  166. void *ptr = NULL;
  167. const PHYSFS_uint64 len = (wStrLen(w_str) * 4) + 1;
  168. retval = (char*)allocator.Malloc(len);
  169. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  170. PHYSFS_utf8FromUtf16((const PHYSFS_uint16 *)w_str, retval, len);
  171. ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
  172. if (ptr != NULL)
  173. retval = (char *)ptr;
  174. } /* if */
  175. return retval;
  176. } /* unicodeToUtf8Heap */
  177. char *__PHYSFS_platformCalcBaseDir(const char *argv0)
  178. {
  179. const wchar_t* path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
  180. wchar_t path2[1024];
  181. wcscpy_s(path2, path);
  182. wcscat_s(path2, L"\\");
  183. return unicodeToUtf8Heap(path2);
  184. } /* __PHYSFS_platformCalcBaseDir */
  185. char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
  186. {
  187. const wchar_t* path = Windows::Storage::ApplicationData::Current->LocalFolder->Path->Data();
  188. wchar_t path2[1024];
  189. wcscpy_s(path2, path);
  190. wcscat_s(path2, L"\\");
  191. return unicodeToUtf8Heap(path2);
  192. } /* __PHYSFS_platformCalcPrefDir */
  193. char *__PHYSFS_platformCalcUserDir(void)
  194. {
  195. return __PHYSFS_platformCalcPrefDir(NULL, NULL);
  196. } /* __PHYSFS_platformCalcUserDir */
  197. void *__PHYSFS_platformGetThreadID(void)
  198. {
  199. return ((void *)((size_t)GetCurrentThreadId()));
  200. } /* __PHYSFS_platformGetThreadID */
  201. static int isSymlinkAttrs(const DWORD attr, const DWORD tag)
  202. {
  203. return ((attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
  204. (tag == PHYSFS_IO_REPARSE_TAG_SYMLINK));
  205. } /* isSymlinkAttrs */
  206. void __PHYSFS_platformEnumerateFiles(const char *dirname,
  207. PHYSFS_EnumFilesCallback callback,
  208. const char *origdir,
  209. void *callbackdata)
  210. {
  211. HANDLE dir = INVALID_HANDLE_VALUE;
  212. WIN32_FIND_DATAW entw;
  213. size_t len = strlen(dirname);
  214. char *searchPath = NULL;
  215. WCHAR *wSearchPath = NULL;
  216. /* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
  217. searchPath = (char *)__PHYSFS_smallAlloc(len + 3);
  218. if (searchPath == NULL)
  219. return;
  220. /* Copy current dirname */
  221. strcpy(searchPath, dirname);
  222. /* if there's no '\\' at the end of the path, stick one in there. */
  223. if (searchPath[len - 1] != '\\')
  224. {
  225. searchPath[len++] = '\\';
  226. searchPath[len] = '\0';
  227. } /* if */
  228. /* Append the "*" to the end of the string */
  229. strcat(searchPath, "*");
  230. UTF8_TO_UNICODE_STACK(wSearchPath, searchPath);
  231. if (!wSearchPath)
  232. return; /* oh well. */
  233. //dir = FindFirstFileW(wSearchPath, &entw);
  234. dir = FindFirstFileExW(wSearchPath, FindExInfoStandard, &entw, FindExSearchNameMatch, NULL, 0);
  235. __PHYSFS_smallFree(wSearchPath);
  236. __PHYSFS_smallFree(searchPath);
  237. if (dir == INVALID_HANDLE_VALUE)
  238. return;
  239. do
  240. {
  241. const DWORD attr = entw.dwFileAttributes;
  242. const DWORD tag = entw.dwReserved0;
  243. const WCHAR *fn = entw.cFileName;
  244. char *utf8;
  245. if ((fn[0] == '.') && (fn[1] == '\0'))
  246. continue;
  247. if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
  248. continue;
  249. utf8 = unicodeToUtf8Heap(fn);
  250. if (utf8 != NULL)
  251. {
  252. callback(callbackdata, origdir, utf8);
  253. allocator.Free(utf8);
  254. } /* if */
  255. } while (FindNextFileW(dir, &entw) != 0);
  256. FindClose(dir);
  257. } /* __PHYSFS_platformEnumerateFiles */
  258. int __PHYSFS_platformMkDir(const char *path)
  259. {
  260. WCHAR *wpath;
  261. DWORD rc;
  262. UTF8_TO_UNICODE_STACK(wpath, path);
  263. rc = CreateDirectoryW(wpath, NULL);
  264. __PHYSFS_smallFree(wpath);
  265. BAIL_IF(rc == 0, errcodeFromWinApi(), 0);
  266. return 1;
  267. } /* __PHYSFS_platformMkDir */
  268. int __PHYSFS_platformInit(void)
  269. {
  270. return 1; /* It's all good */
  271. } /* __PHYSFS_platformInit */
  272. int __PHYSFS_platformDeinit(void)
  273. {
  274. return 1; /* It's all good */
  275. } /* __PHYSFS_platformDeinit */
  276. static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
  277. {
  278. HANDLE fileh;
  279. WinApiFile *retval;
  280. WCHAR *wfname;
  281. UTF8_TO_UNICODE_STACK(wfname, fname);
  282. BAIL_IF(!wfname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  283. //fileh = CreateFileW(wfname, mode, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
  284. fileh = CreateFile2(wfname, mode, FILE_SHARE_READ | FILE_SHARE_WRITE, creation, NULL);
  285. __PHYSFS_smallFree(wfname);
  286. BAIL_IF(fileh == INVALID_HANDLE_VALUE, errcodeFromWinApi(), NULL);
  287. retval = (WinApiFile *)allocator.Malloc(sizeof(WinApiFile));
  288. if (!retval)
  289. {
  290. CloseHandle(fileh);
  291. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  292. } /* if */
  293. retval->readonly = rdonly;
  294. retval->handle = fileh;
  295. return retval;
  296. } /* doOpen */
  297. void *__PHYSFS_platformOpenRead(const char *filename)
  298. {
  299. return doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1);
  300. } /* __PHYSFS_platformOpenRead */
  301. void *__PHYSFS_platformOpenWrite(const char *filename)
  302. {
  303. return doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0);
  304. } /* __PHYSFS_platformOpenWrite */
  305. void *__PHYSFS_platformOpenAppend(const char *filename)
  306. {
  307. void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
  308. if (retval != NULL)
  309. {
  310. HANDLE h = ((WinApiFile *)retval)->handle;
  311. //DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
  312. const LARGE_INTEGER zero = { 0 };
  313. DWORD rc = SetFilePointerEx(h, zero, NULL, FILE_END);
  314. if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
  315. {
  316. const PHYSFS_ErrorCode err = errcodeFromWinApi();
  317. CloseHandle(h);
  318. allocator.Free(retval);
  319. BAIL(err, NULL);
  320. } /* if */
  321. } /* if */
  322. return retval;
  323. } /* __PHYSFS_platformOpenAppend */
  324. PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
  325. {
  326. HANDLE Handle = ((WinApiFile *)opaque)->handle;
  327. PHYSFS_sint64 totalRead = 0;
  328. if (!__PHYSFS_ui64FitsAddressSpace(len))
  329. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  330. while (len > 0)
  331. {
  332. const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD)len;
  333. DWORD numRead = 0;
  334. if (!ReadFile(Handle, buf, thislen, &numRead, NULL))
  335. BAIL(errcodeFromWinApi(), -1);
  336. len -= (PHYSFS_uint64)numRead;
  337. totalRead += (PHYSFS_sint64)numRead;
  338. if (numRead != thislen)
  339. break;
  340. } /* while */
  341. return totalRead;
  342. } /* __PHYSFS_platformRead */
  343. PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
  344. PHYSFS_uint64 len)
  345. {
  346. HANDLE Handle = ((WinApiFile *)opaque)->handle;
  347. PHYSFS_sint64 totalWritten = 0;
  348. if (!__PHYSFS_ui64FitsAddressSpace(len))
  349. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  350. while (len > 0)
  351. {
  352. const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD)len;
  353. DWORD numWritten = 0;
  354. if (!WriteFile(Handle, buffer, thislen, &numWritten, NULL))
  355. BAIL(errcodeFromWinApi(), -1);
  356. len -= (PHYSFS_uint64)numWritten;
  357. totalWritten += (PHYSFS_sint64)numWritten;
  358. if (numWritten != thislen)
  359. break;
  360. } /* while */
  361. return totalWritten;
  362. } /* __PHYSFS_platformWrite */
  363. int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
  364. {
  365. HANDLE Handle = ((WinApiFile *)opaque)->handle;
  366. BOOL rc;
  367. LARGE_INTEGER li;
  368. li.LowPart = LOWORDER_UINT64(pos);
  369. li.HighPart = HIGHORDER_UINT64(pos);
  370. rc = SetFilePointerEx(Handle, li, NULL, FILE_BEGIN);
  371. if (!rc && (GetLastError() != NO_ERROR))
  372. {
  373. BAIL(errcodeFromWinApi(), 0);
  374. } /* if */
  375. return 1; /* No error occured */
  376. } /* __PHYSFS_platformSeek */
  377. PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
  378. {
  379. HANDLE Handle = ((WinApiFile *)opaque)->handle;
  380. PHYSFS_sint64 retval;
  381. BOOL rc;
  382. LARGE_INTEGER zero;
  383. zero.QuadPart = 0;
  384. LARGE_INTEGER out;
  385. rc = SetFilePointerEx(Handle, zero, &out, FILE_CURRENT);
  386. if (!rc)
  387. {
  388. BAIL(errcodeFromWinApi(), -1);
  389. } /* if */
  390. else
  391. {
  392. retval = out.QuadPart;
  393. assert(retval >= 0);
  394. } /* else */
  395. return retval;
  396. } /* __PHYSFS_platformTell */
  397. PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
  398. {
  399. HANDLE Handle = ((WinApiFile *)opaque)->handle;
  400. PHYSFS_sint64 retval;
  401. FILE_STANDARD_INFO file_info = { 0 };
  402. const BOOL res = GetFileInformationByHandleEx(Handle, FileStandardInfo, &file_info, sizeof(file_info));
  403. if (res) {
  404. retval = file_info.EndOfFile.QuadPart;
  405. assert(retval >= 0);
  406. }
  407. else {
  408. PHYSFS_setErrorCode(PHYSFS_ERR_NOT_FOUND);
  409. }
  410. return retval;
  411. } /* __PHYSFS_platformFileLength */
  412. int __PHYSFS_platformFlush(void *opaque)
  413. {
  414. WinApiFile *fh = ((WinApiFile *)opaque);
  415. if (!fh->readonly)
  416. BAIL_IF(!FlushFileBuffers(fh->handle), errcodeFromWinApi(), 0);
  417. return 1;
  418. } /* __PHYSFS_platformFlush */
  419. void __PHYSFS_platformClose(void *opaque)
  420. {
  421. HANDLE Handle = ((WinApiFile *)opaque)->handle;
  422. (void)CloseHandle(Handle); /* ignore errors. You should have flushed! */
  423. allocator.Free(opaque);
  424. } /* __PHYSFS_platformClose */
  425. static int doPlatformDelete(LPWSTR wpath)
  426. {
  427. //const int isdir = (GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY);
  428. int isdir = 0;
  429. WIN32_FILE_ATTRIBUTE_DATA file_info;
  430. const BOOL res = GetFileAttributesEx(wpath, GetFileExInfoStandard, &file_info);
  431. if (res) {
  432. isdir = (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
  433. }
  434. const BOOL rc = (isdir) ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
  435. BAIL_IF(!rc, errcodeFromWinApi(), 0);
  436. return 1; /* if you made it here, it worked. */
  437. } /* doPlatformDelete */
  438. int __PHYSFS_platformDelete(const char *path)
  439. {
  440. int retval = 0;
  441. LPWSTR wpath = NULL;
  442. UTF8_TO_UNICODE_STACK(wpath, path);
  443. BAIL_IF(!wpath, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  444. retval = doPlatformDelete(wpath);
  445. __PHYSFS_smallFree(wpath);
  446. return retval;
  447. } /* __PHYSFS_platformDelete */
  448. void *__PHYSFS_platformCreateMutex(void)
  449. {
  450. LPCRITICAL_SECTION lpcs;
  451. lpcs = (LPCRITICAL_SECTION)allocator.Malloc(sizeof(CRITICAL_SECTION));
  452. BAIL_IF(!lpcs, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  453. //InitializeCriticalSection(lpcs);
  454. InitializeCriticalSectionEx(lpcs, 2000, 0);
  455. return lpcs;
  456. } /* __PHYSFS_platformCreateMutex */
  457. void __PHYSFS_platformDestroyMutex(void *mutex)
  458. {
  459. DeleteCriticalSection((LPCRITICAL_SECTION)mutex);
  460. allocator.Free(mutex);
  461. } /* __PHYSFS_platformDestroyMutex */
  462. int __PHYSFS_platformGrabMutex(void *mutex)
  463. {
  464. EnterCriticalSection((LPCRITICAL_SECTION)mutex);
  465. return 1;
  466. } /* __PHYSFS_platformGrabMutex */
  467. void __PHYSFS_platformReleaseMutex(void *mutex)
  468. {
  469. LeaveCriticalSection((LPCRITICAL_SECTION)mutex);
  470. } /* __PHYSFS_platformReleaseMutex */
  471. static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
  472. {
  473. SYSTEMTIME st_utc;
  474. SYSTEMTIME st_localtz;
  475. TIME_ZONE_INFORMATION tzi;
  476. DWORD tzid;
  477. PHYSFS_sint64 retval;
  478. struct tm tm;
  479. BOOL rc;
  480. BAIL_IF(!FileTimeToSystemTime(ft, &st_utc), errcodeFromWinApi(), -1);
  481. tzid = GetTimeZoneInformation(&tzi);
  482. BAIL_IF(tzid == TIME_ZONE_ID_INVALID, errcodeFromWinApi(), -1);
  483. rc = SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz);
  484. BAIL_IF(!rc, errcodeFromWinApi(), -1);
  485. /* Convert to a format that mktime() can grok... */
  486. tm.tm_sec = st_localtz.wSecond;
  487. tm.tm_min = st_localtz.wMinute;
  488. tm.tm_hour = st_localtz.wHour;
  489. tm.tm_mday = st_localtz.wDay;
  490. tm.tm_mon = st_localtz.wMonth - 1;
  491. tm.tm_year = st_localtz.wYear - 1900;
  492. tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
  493. tm.tm_yday = -1;
  494. tm.tm_isdst = -1;
  495. /* Convert to a format PhysicsFS can grok... */
  496. retval = (PHYSFS_sint64)mktime(&tm);
  497. BAIL_IF(retval == -1, PHYSFS_ERR_OS_ERROR, -1);
  498. return retval;
  499. } /* FileTimeToPhysfsTime */
  500. int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
  501. {
  502. WIN32_FILE_ATTRIBUTE_DATA winstat;
  503. WCHAR *wstr = NULL;
  504. DWORD err = 0;
  505. BOOL rc = 0;
  506. UTF8_TO_UNICODE_STACK(wstr, filename);
  507. BAIL_IF(!wstr, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  508. rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
  509. err = (!rc) ? GetLastError() : 0;
  510. __PHYSFS_smallFree(wstr);
  511. BAIL_IF(!rc, errcodeFromWinApiError(err), 0);
  512. st->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
  513. st->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
  514. st->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
  515. if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  516. {
  517. st->filetype = PHYSFS_FILETYPE_DIRECTORY;
  518. st->filesize = 0;
  519. } /* if */
  520. else if (winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE))
  521. {
  522. /* !!! FIXME: what are reparse points? */
  523. st->filetype = PHYSFS_FILETYPE_OTHER;
  524. /* !!! FIXME: don't rely on this */
  525. st->filesize = 0;
  526. } /* else if */
  527. /* !!! FIXME: check for symlinks on Vista. */
  528. else
  529. {
  530. st->filetype = PHYSFS_FILETYPE_REGULAR;
  531. st->filesize = (((PHYSFS_uint64)winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow;
  532. } /* else */
  533. st->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
  534. return 1;
  535. } /* __PHYSFS_platformStat */
  536. #endif /* PHYSFS_PLATFORM_WINRT */