Skip to content

Latest commit

 

History

History
1028 lines (848 loc) · 30.6 KB

physfs_platform_windows.c

File metadata and controls

1028 lines (848 loc) · 30.6 KB
 
Aug 23, 2001
Aug 23, 2001
1
/*
Mar 11, 2007
Mar 11, 2007
2
* Windows support routines for PhysicsFS.
Aug 23, 2001
Aug 23, 2001
3
*
Mar 11, 2007
Mar 11, 2007
4
* Please see the file LICENSE.txt in the source's root directory.
Aug 23, 2001
Aug 23, 2001
5
*
Apr 13, 2002
Apr 13, 2002
6
* This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
Aug 23, 2001
Aug 23, 2001
7
8
*/
Mar 11, 2007
Mar 11, 2007
9
#define __PHYSICSFS_INTERNAL__
Aug 17, 2017
Aug 17, 2017
10
#include "physfs_platforms.h"
Mar 11, 2007
Mar 11, 2007
11
12
#ifdef PHYSFS_PLATFORM_WINDOWS
Jul 10, 2002
Jul 10, 2002
13
Mar 10, 2012
Mar 10, 2012
14
/* Forcibly disable UNICODE macro, since we manage this ourselves. */
Mar 26, 2007
Mar 26, 2007
15
16
17
18
#ifdef UNICODE
#undef UNICODE
#endif
Aug 17, 2017
Aug 17, 2017
19
20
21
22
#if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS 1
#endif
Mar 10, 2012
Mar 10, 2012
23
#define WIN32_LEAN_AND_MEAN 1
Aug 23, 2001
Aug 23, 2001
24
#include <windows.h>
Jul 24, 2017
Jul 24, 2017
25
26
#ifndef PHYSFS_PLATFORM_WINRT
Mar 10, 2012
Mar 10, 2012
27
#include <userenv.h>
Mar 22, 2012
Mar 22, 2012
28
#include <shlobj.h>
Jul 24, 2017
Jul 24, 2017
29
30
31
#endif
#if !defined(PHYSFS_NO_CDROM_SUPPORT)
Mar 12, 2012
Mar 12, 2012
32
#include <dbt.h>
Jul 24, 2017
Jul 24, 2017
33
34
#endif
Jun 29, 2002
Jun 29, 2002
35
#include <errno.h>
Aug 29, 2001
Aug 29, 2001
36
#include <ctype.h>
Jun 7, 2002
Jun 7, 2002
37
#include <time.h>
May 6, 2002
May 6, 2002
38
Jul 18, 2017
Jul 18, 2017
39
40
41
#ifdef allocator /* apparently Windows 10 SDK conflicts here. */
#undef allocator
#endif
Aug 17, 2017
Aug 17, 2017
42
43
#include "physfs_internal.h"
Jul 18, 2017
Jul 18, 2017
44
Mar 25, 2007
Mar 25, 2007
45
46
47
48
49
50
/*
* Users without the platform SDK don't have this defined. The original docs
* for SetFilePointer() just said to compare with 0xFFFFFFFF, so this should
* work as desired.
*/
#define PHYSFS_INVALID_SET_FILE_POINTER 0xFFFFFFFF
Mar 25, 2007
Mar 25, 2007
51
Mar 25, 2007
Mar 25, 2007
52
53
/* just in case... */
#define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
Jun 29, 2002
Jun 29, 2002
54
Apr 1, 2007
Apr 1, 2007
55
/* Not defined before the Vista SDK. */
Jul 9, 2017
Jul 9, 2017
56
#define PHYSFS_FILE_ATTRIBUTE_REPARSE_POINT 0x400
Apr 1, 2007
Apr 1, 2007
57
58
59
#define PHYSFS_IO_REPARSE_TAG_SYMLINK 0xA000000C
Jul 6, 2017
Jul 6, 2017
60
#define UTF8_TO_UNICODE_STACK(w_assignto, str) { \
Mar 25, 2007
Mar 25, 2007
61
62
63
if (str == NULL) \
w_assignto = NULL; \
else { \
Aug 15, 2017
Aug 15, 2017
64
const size_t len = (PHYSFS_uint64) ((strlen(str) + 1) * 2); \
Mar 26, 2007
Mar 26, 2007
65
66
w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
if (w_assignto != NULL) \
Mar 20, 2012
Mar 20, 2012
67
PHYSFS_utf8ToUtf16(str, (PHYSFS_uint16 *) w_assignto, len); \
Mar 25, 2007
Mar 25, 2007
68
69
} \
} \
Jun 29, 2002
Jun 29, 2002
70
Mar 20, 2012
Mar 20, 2012
71
/* Note this counts WCHARs, not codepoints! */
Mar 26, 2007
Mar 26, 2007
72
73
74
75
76
static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
{
PHYSFS_uint64 len = 0;
while (*(wstr++))
len++;
Jan 28, 2010
Jan 28, 2010
77
return len;
Mar 26, 2007
Mar 26, 2007
78
79
80
81
82
83
84
85
86
87
} /* wStrLen */
static char *unicodeToUtf8Heap(const WCHAR *w_str)
{
char *retval = NULL;
if (w_str != NULL)
{
void *ptr = NULL;
const PHYSFS_uint64 len = (wStrLen(w_str) * 4) + 1;
retval = allocator.Malloc(len);
Jul 6, 2017
Jul 6, 2017
88
BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 20, 2012
Mar 20, 2012
89
PHYSFS_utf8FromUtf16((const PHYSFS_uint16 *) w_str, retval, len);
Mar 26, 2007
Mar 26, 2007
90
91
92
93
ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
if (ptr != NULL)
retval = (char *) ptr;
} /* if */
Jan 28, 2010
Jan 28, 2010
94
return retval;
Mar 26, 2007
Mar 26, 2007
95
96
} /* unicodeToUtf8Heap */
Jul 24, 2017
Jul 24, 2017
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Some older APIs aren't in WinRT (only the "Ex" version, etc).
Since non-WinRT might not have the "Ex" version, we tapdance to use
the perfectly-fine-and-available-even-on-Win95 API on non-WinRT targets. */
static inline HANDLE winFindFirstFileW(const WCHAR *path, LPWIN32_FIND_DATAW d)
{
#ifdef PHYSFS_PLATFORM_WINRT
return FindFirstFileExW(path, FindExInfoStandard, d,
FindExSearchNameMatch, NULL, 0);
#else
return FindFirstFileW(path, d);
#endif
} /* winFindFirstFileW */
static inline BOOL winInitializeCriticalSection(LPCRITICAL_SECTION lpcs)
{
#ifdef PHYSFS_PLATFORM_WINRT
return InitializeCriticalSectionEx(lpcs, 2000, 0);
#else
InitializeCriticalSection(lpcs);
return TRUE;
#endif
} /* winInitializeCriticalSection */
static inline HANDLE winCreateFileW(const WCHAR *wfname, const DWORD mode,
const DWORD creation)
{
const DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE;
#ifdef PHYSFS_PLATFORM_WINRT
return CreateFile2(wfname, mode, share, creation, NULL);
#else
return CreateFileW(wfname, mode, share, NULL, creation,
FILE_ATTRIBUTE_NORMAL, NULL);
#endif
} /* winCreateFileW */
static BOOL winSetFilePointer(HANDLE h, const PHYSFS_sint64 pos,
PHYSFS_sint64 *_newpos, const DWORD whence)
{
#ifdef PHYSFS_PLATFORM_WINRT
LARGE_INTEGER lipos;
LARGE_INTEGER linewpos;
BOOL rc;
lipos.QuadPart = (LONGLONG) pos;
rc = SetFilePointerEx(h, lipos, &linewpos, whence);
if (_newpos)
*_newpos = (PHYSFS_sint64) linewpos.QuadPart;
return rc;
#else
const LONG low = (LONG) (pos & 0xFFFFFFFF);
LONG high = (LONG) ((pos >> 32) & 0xFFFFFFFF);
const DWORD rc = SetFilePointer(h, low, &high, whence);
/* 0xFFFFFFFF could be valid, so you have to check GetLastError too! */
if (_newpos)
*_newpos = ((PHYSFS_sint64) rc) | (((PHYSFS_sint64) high) << 32);
if ((rc == PHYSFS_INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
return FALSE;
return TRUE;
#endif
} /* winSetFilePointer */
static PHYSFS_sint64 winGetFileSize(HANDLE h)
{
#ifdef PHYSFS_PLATFORM_WINRT
FILE_STANDARD_INFO info;
const BOOL rc = GetFileInformationByHandleEx(h, FileStandardInfo,
&info, sizeof (info));
return rc ? (PHYSFS_sint64) info.EndOfFile.QuadPart : -1;
#else
DWORD high = 0;
const DWORD rc = GetFileSize(h, &high);
if ((rc == PHYSFS_INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
return -1;
return (PHYSFS_sint64) ((((PHYSFS_uint64) high) << 32) | rc);
#endif
} /* winGetFileSize */
Mar 26, 2007
Mar 26, 2007
174
Mar 20, 2012
Mar 20, 2012
175
176
177
178
179
180
181
182
183
184
185
186
187
188
static PHYSFS_ErrorCode errcodeFromWinApiError(const DWORD err)
{
/*
* win32 error codes are sort of a tricky thing; Microsoft intentionally
* doesn't list which ones a given API might trigger, there are several
* with overlapping and unclear meanings...and there's 16 thousand of
* them in Windows 7. It looks like the ones we care about are in the
* first 500, but I can't say this list is perfect; we might miss
* important values or misinterpret others.
*
* Don't treat this list as anything other than a work in progress.
*/
switch (err)
Jun 29, 2002
Jun 29, 2002
189
{
Mar 20, 2012
Mar 20, 2012
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
case ERROR_SUCCESS: return PHYSFS_ERR_OK;
case ERROR_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
case ERROR_NETWORK_ACCESS_DENIED: return PHYSFS_ERR_PERMISSION;
case ERROR_NOT_READY: return PHYSFS_ERR_IO;
case ERROR_CRC: return PHYSFS_ERR_IO;
case ERROR_SEEK: return PHYSFS_ERR_IO;
case ERROR_SECTOR_NOT_FOUND: return PHYSFS_ERR_IO;
case ERROR_NOT_DOS_DISK: return PHYSFS_ERR_IO;
case ERROR_WRITE_FAULT: return PHYSFS_ERR_IO;
case ERROR_READ_FAULT: return PHYSFS_ERR_IO;
case ERROR_DEV_NOT_EXIST: return PHYSFS_ERR_IO;
case ERROR_BUFFER_OVERFLOW: return PHYSFS_ERR_BAD_FILENAME;
case ERROR_INVALID_NAME: return PHYSFS_ERR_BAD_FILENAME;
case ERROR_BAD_PATHNAME: return PHYSFS_ERR_BAD_FILENAME;
case ERROR_DIRECTORY: return PHYSFS_ERR_BAD_FILENAME;
Nov 28, 2012
Nov 28, 2012
205
206
207
208
case ERROR_FILE_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
case ERROR_PATH_NOT_FOUND: return PHYSFS_ERR_NOT_FOUND;
case ERROR_DELETE_PENDING: return PHYSFS_ERR_NOT_FOUND;
case ERROR_INVALID_DRIVE: return PHYSFS_ERR_NOT_FOUND;
Mar 20, 2012
Mar 20, 2012
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
case ERROR_HANDLE_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
case ERROR_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
case ERROR_WRITE_PROTECT: return PHYSFS_ERR_READ_ONLY;
case ERROR_LOCK_VIOLATION: return PHYSFS_ERR_BUSY;
case ERROR_SHARING_VIOLATION: return PHYSFS_ERR_BUSY;
case ERROR_CURRENT_DIRECTORY: return PHYSFS_ERR_BUSY;
case ERROR_DRIVE_LOCKED: return PHYSFS_ERR_BUSY;
case ERROR_PATH_BUSY: return PHYSFS_ERR_BUSY;
case ERROR_BUSY: return PHYSFS_ERR_BUSY;
case ERROR_NOT_ENOUGH_MEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
case ERROR_OUTOFMEMORY: return PHYSFS_ERR_OUT_OF_MEMORY;
case ERROR_DIR_NOT_EMPTY: return PHYSFS_ERR_DIR_NOT_EMPTY;
default: return PHYSFS_ERR_OS_ERROR;
} /* switch */
} /* errcodeFromWinApiError */
static inline PHYSFS_ErrorCode errcodeFromWinApi(void)
{
return errcodeFromWinApiError(GetLastError());
} /* errcodeFromWinApi */
Jun 29, 2002
Jun 29, 2002
229
Jun 10, 2002
Jun 10, 2002
230
Jul 24, 2017
Jul 24, 2017
231
232
233
234
235
#if defined(PHYSFS_NO_CDROM_SUPPORT)
#define detectAvailableCDs(cb, data)
#define deinitCDThread()
#else
static HANDLE detectCDThreadHandle = NULL;
Aug 5, 2017
Aug 5, 2017
236
static HWND detectCDHwnd = NULL;
Jul 24, 2017
Jul 24, 2017
237
238
static volatile DWORD drivesWithMediaBitmap = 0;
Mar 12, 2012
Mar 12, 2012
239
typedef BOOL (WINAPI *fnSTEM)(DWORD, LPDWORD b);
Jun 7, 2002
Jun 7, 2002
240
Mar 12, 2012
Mar 12, 2012
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
static DWORD pollDiscDrives(void)
{
/* Try to use SetThreadErrorMode(), which showed up in Windows 7. */
HANDLE lib = LoadLibraryA("kernel32.dll");
fnSTEM stem = NULL;
char drive[4] = { 'x', ':', '\\', '\0' };
DWORD oldErrorMode = 0;
DWORD drives = 0;
DWORD i;
if (lib)
stem = (fnSTEM) GetProcAddress(lib, "SetThreadErrorMode");
if (stem)
stem(SEM_FAILCRITICALERRORS, &oldErrorMode);
else
oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
Jun 7, 2002
Jun 7, 2002
258
Mar 12, 2012
Mar 12, 2012
259
260
261
262
263
264
265
/* Do detection. This may block if a disc is spinning up. */
for (i = 'A'; i <= 'Z'; i++)
{
DWORD tmp = 0;
drive[0] = (char) i;
if (GetDriveTypeA(drive) != DRIVE_CDROM)
continue;
Jun 7, 2002
Jun 7, 2002
266
Mar 12, 2012
Mar 12, 2012
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* If this function succeeds, there's media in the drive */
if (GetVolumeInformationA(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0))
drives |= (1 << (i - 'A'));
} /* for */
if (stem)
stem(oldErrorMode, NULL);
else
SetErrorMode(oldErrorMode);
if (lib)
FreeLibrary(lib);
return drives;
} /* pollDiscDrives */
static LRESULT CALLBACK detectCDWndProc(HWND hwnd, UINT msg,
WPARAM wp, LPARAM lparam)
{
PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR) lparam;
PDEV_BROADCAST_VOLUME lpdbv = (PDEV_BROADCAST_VOLUME) lparam;
const int removed = (wp == DBT_DEVICEREMOVECOMPLETE);
if (msg == WM_DESTROY)
return 0;
else if ((msg != WM_DEVICECHANGE) ||
((wp != DBT_DEVICEARRIVAL) && (wp != DBT_DEVICEREMOVECOMPLETE)) ||
(lpdb->dbch_devicetype != DBT_DEVTYP_VOLUME) ||
((lpdbv->dbcv_flags & DBTF_MEDIA) == 0))
{
return DefWindowProcW(hwnd, msg, wp, lparam);
} /* else if */
if (removed)
drivesWithMediaBitmap &= ~lpdbv->dbcv_unitmask;
else
drivesWithMediaBitmap |= lpdbv->dbcv_unitmask;
return TRUE;
} /* detectCDWndProc */
Aug 5, 2017
Aug 5, 2017
310
static DWORD WINAPI detectCDThread(LPVOID arg)
Mar 12, 2012
Mar 12, 2012
311
{
Aug 5, 2017
Aug 5, 2017
312
HANDLE initialDiscDetectionComplete = *((HANDLE *) arg);
Mar 12, 2012
Mar 12, 2012
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const char *classname = "PhysicsFSDetectCDCatcher";
const char *winname = "PhysicsFSDetectCDMsgWindow";
HINSTANCE hInstance = GetModuleHandleW(NULL);
ATOM class_atom = 0;
WNDCLASSEXA wce;
MSG msg;
memset(&wce, '\0', sizeof (wce));
wce.cbSize = sizeof (wce);
wce.lpfnWndProc = detectCDWndProc;
wce.lpszClassName = classname;
wce.hInstance = hInstance;
class_atom = RegisterClassExA(&wce);
if (class_atom == 0)
{
Aug 5, 2017
Aug 5, 2017
328
SetEvent(initialDiscDetectionComplete); /* let main thread go on. */
Mar 12, 2012
Mar 12, 2012
329
330
331
332
333
334
335
336
337
return 0;
} /* if */
detectCDHwnd = CreateWindowExA(0, classname, winname, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, HWND_DESKTOP, NULL, hInstance, NULL);
if (detectCDHwnd == NULL)
{
Aug 5, 2017
Aug 5, 2017
338
SetEvent(initialDiscDetectionComplete); /* let main thread go on. */
Mar 12, 2012
Mar 12, 2012
339
340
341
342
343
344
345
346
UnregisterClassA(classname, hInstance);
return 0;
} /* if */
/* We'll get events when discs come and go from now on. */
/* Do initial detection, possibly blocking awhile... */
drivesWithMediaBitmap = pollDiscDrives();
Aug 5, 2017
Aug 5, 2017
347
348
SetEvent(initialDiscDetectionComplete); /* let main thread go on. */
Mar 12, 2012
Mar 12, 2012
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
do
{
const BOOL rc = GetMessageW(&msg, detectCDHwnd, 0, 0);
if ((rc == 0) || (rc == -1))
break; /* don't care if WM_QUIT or error break this loop. */
TranslateMessage(&msg);
DispatchMessageW(&msg);
} while (1);
/* we've been asked to quit. */
DestroyWindow(detectCDHwnd);
UnregisterClassA(classname, hInstance);
return 0;
} /* detectCDThread */
Jun 29, 2002
Jun 29, 2002
364
Jul 24, 2017
Jul 24, 2017
365
static void detectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Aug 23, 2001
Aug 23, 2001
366
{
Mar 12, 2012
Mar 12, 2012
367
368
369
370
371
372
373
374
375
376
377
378
379
char drive_str[4] = { 'x', ':', '\\', '\0' };
DWORD drives = 0;
DWORD i;
/*
* If you poll a drive while a user is inserting a disc, the OS will
* block this thread until the drive has spun up. So we swallow the risk
* once for initial detection, and spin a thread that will get device
* events thereafter, for apps that use this interface to poll for
* disc insertion.
*/
if (!detectCDThreadHandle)
{
Aug 5, 2017
Aug 5, 2017
380
381
HANDLE initialDetectDone = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!initialDetectDone)
Mar 12, 2012
Mar 12, 2012
382
383
return; /* oh well. */
Aug 5, 2017
Aug 5, 2017
384
385
386
387
388
389
390
391
detectCDThreadHandle = CreateThread(NULL, 0, detectCDThread,
&initialDetectDone, 0, NULL);
if (detectCDThreadHandle)
WaitForSingleObject(initialDetectDone, INFINITE);
CloseHandle(initialDetectDone);
if (!detectCDThreadHandle)
return; /* oh well. */
Mar 12, 2012
Mar 12, 2012
392
393
394
395
} /* if */
drives = drivesWithMediaBitmap; /* whatever the thread has seen, we take. */
for (i = 'A'; i <= 'Z'; i++)
Aug 23, 2001
Aug 23, 2001
396
{
Mar 12, 2012
Mar 12, 2012
397
398
399
if (drives & (1 << (i - 'A')))
{
drive_str[0] = (char) i;
Sep 29, 2004
Sep 29, 2004
400
cb(data, drive_str);
Mar 12, 2012
Mar 12, 2012
401
} /* if */
Aug 23, 2001
Aug 23, 2001
402
} /* for */
Jul 24, 2017
Jul 24, 2017
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
} /* detectAvailableCDs */
static void deinitCDThread(void)
{
if (detectCDThreadHandle)
{
if (detectCDHwnd)
PostMessageW(detectCDHwnd, WM_QUIT, 0, 0);
CloseHandle(detectCDThreadHandle);
detectCDThreadHandle = NULL;
drivesWithMediaBitmap = 0;
} /* if */
} /* deinitCDThread */
#endif
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
{
detectAvailableCDs(cb, data);
Sep 29, 2004
Sep 29, 2004
422
} /* __PHYSFS_platformDetectAvailableCDs */
Aug 23, 2001
Aug 23, 2001
423
Jul 24, 2017
Jul 24, 2017
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#ifdef PHYSFS_PLATFORM_WINRT
static char *calcDirAppendSep(const WCHAR *wdir)
{
size_t len;
void *ptr;
char *retval;
BAIL_IF(!wdir, errcodeFromWinApi(), NULL);
retval = unicodeToUtf8Heap(wdir);
BAIL_IF_ERRPASS(!retval, NULL);
len = strlen(retval);
ptr = allocator.Realloc(retval, len + 2);
if (!ptr)
{
allocator.Free(retval);
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
} /* if */
retval = (char *) ptr;
retval[len] = '\\';
retval[len+1] = '\0';
return retval;
} /* calcDirAppendSep */
#endif
Aug 23, 2001
Aug 23, 2001
446
Oct 9, 2001
Oct 9, 2001
447
448
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
Jul 24, 2017
Jul 24, 2017
449
450
451
452
#ifdef PHYSFS_PLATFORM_WINRT
return calcDirAppendSep((const WCHAR *) __PHYSFS_winrtCalcBaseDir());
#else
char *retval = NULL;
Mar 10, 2012
Mar 10, 2012
453
454
DWORD buflen = 64;
LPWSTR modpath = NULL;
Oct 9, 2001
Oct 9, 2001
455
Mar 10, 2012
Mar 10, 2012
456
457
458
459
460
461
462
463
while (1)
{
DWORD rc;
void *ptr;
if ( (ptr = allocator.Realloc(modpath, buflen*sizeof(WCHAR))) == NULL )
{
allocator.Free(modpath);
Jul 6, 2017
Jul 6, 2017
464
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 10, 2012
Mar 10, 2012
465
466
467
468
469
470
471
} /* if */
modpath = (LPWSTR) ptr;
rc = GetModuleFileNameW(NULL, modpath, buflen);
if (rc == 0)
{
allocator.Free(modpath);
Jul 6, 2017
Jul 6, 2017
472
BAIL(errcodeFromWinApi(), NULL);
Mar 10, 2012
Mar 10, 2012
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
} /* if */
if (rc < buflen)
{
buflen = rc;
break;
} /* if */
buflen *= 2;
} /* while */
if (buflen > 0) /* just in case... */
{
WCHAR *ptr = (modpath + buflen) - 1;
while (ptr != modpath)
{
if (*ptr == '\\')
break;
ptr--;
} /* while */
if ((ptr == modpath) && (*ptr != '\\'))
Nov 30, 2012
Nov 30, 2012
495
PHYSFS_setErrorCode(PHYSFS_ERR_OTHER_ERROR); /* oh well. */
Mar 10, 2012
Mar 10, 2012
496
497
else
{
Mar 23, 2012
Mar 23, 2012
498
*(ptr+1) = '\0'; /* chop off filename. */
Mar 10, 2012
Mar 10, 2012
499
500
501
502
503
504
retval = unicodeToUtf8Heap(modpath);
} /* else */
} /* else */
allocator.Free(modpath);
return retval; /* w00t. */
Jul 24, 2017
Jul 24, 2017
505
#endif
Aug 23, 2001
Aug 23, 2001
506
507
508
} /* __PHYSFS_platformCalcBaseDir */
Mar 22, 2012
Mar 22, 2012
509
510
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
{
Jul 24, 2017
Jul 24, 2017
511
512
513
#ifdef PHYSFS_PLATFORM_WINRT
return calcDirAppendSep((const WCHAR *) __PHYSFS_winrtCalcPrefDir());
#else
Mar 22, 2012
Mar 22, 2012
514
515
516
517
518
519
520
/*
* Vista and later has a new API for this, but SHGetFolderPath works there,
* and apparently just wraps the new API. This is the new way to do it:
*
* SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_CREATE,
* NULL, &wszPath);
*/
Mar 22, 2012
Mar 22, 2012
521
522
WCHAR path[MAX_PATH];
Mar 22, 2012
Mar 22, 2012
523
524
525
526
char *utf8 = NULL;
size_t len = 0;
char *retval = NULL;
Mar 22, 2012
Mar 22, 2012
527
528
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
NULL, 0, path)))
Jul 6, 2017
Jul 6, 2017
529
BAIL(PHYSFS_ERR_OS_ERROR, NULL);
Mar 22, 2012
Mar 22, 2012
530
Mar 22, 2012
Mar 22, 2012
531
utf8 = unicodeToUtf8Heap(path);
Jul 6, 2017
Jul 6, 2017
532
BAIL_IF_ERRPASS(!utf8, NULL);
Mar 22, 2012
Mar 22, 2012
533
len = strlen(utf8) + strlen(org) + strlen(app) + 4;
Mar 22, 2012
Mar 22, 2012
534
535
536
537
retval = allocator.Malloc(len);
if (!retval)
{
allocator.Free(utf8);
Jul 6, 2017
Jul 6, 2017
538
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 22, 2012
Mar 22, 2012
539
540
} /* if */
Jul 9, 2017
Jul 9, 2017
541
snprintf(retval, len, "%s\\%s\\%s\\", utf8, org, app);
Aug 20, 2013
Aug 20, 2013
542
allocator.Free(utf8);
Mar 22, 2012
Mar 22, 2012
543
return retval;
Jul 24, 2017
Jul 24, 2017
544
#endif
Mar 22, 2012
Mar 22, 2012
545
546
547
} /* __PHYSFS_platformCalcPrefDir */
Mar 22, 2012
Mar 22, 2012
548
char *__PHYSFS_platformCalcUserDir(void)
Aug 23, 2001
Aug 23, 2001
549
{
Jul 24, 2017
Jul 24, 2017
550
551
552
#ifdef PHYSFS_PLATFORM_WINRT
return calcDirAppendSep((const WCHAR *) __PHYSFS_winrtCalcPrefDir());
#else
Mar 22, 2012
Mar 22, 2012
553
554
555
556
557
558
559
typedef BOOL (WINAPI *fnGetUserProfDirW)(HANDLE, LPWSTR, LPDWORD);
fnGetUserProfDirW pGetDir = NULL;
HANDLE lib = NULL;
HANDLE accessToken = NULL; /* Security handle to process */
char *retval = NULL;
lib = LoadLibraryA("userenv.dll");
Jul 6, 2017
Jul 6, 2017
560
BAIL_IF(!lib, errcodeFromWinApi(), NULL);
Mar 22, 2012
Mar 22, 2012
561
pGetDir=(fnGetUserProfDirW) GetProcAddress(lib,"GetUserProfileDirectoryW");
Jul 6, 2017
Jul 6, 2017
562
GOTO_IF(!pGetDir, errcodeFromWinApi(), done);
Mar 22, 2012
Mar 22, 2012
563
564
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken))
Jul 6, 2017
Jul 6, 2017
565
GOTO(errcodeFromWinApi(), done);
Mar 22, 2012
Mar 22, 2012
566
567
568
569
570
571
572
573
574
else
{
DWORD psize = 0;
LPWSTR wstr = NULL;
BOOL rc = 0;
/*
* Should fail. Will write the size of the profile path in
* psize. Also note that the second parameter can't be
Mar 18, 2019
Mar 18, 2019
575
576
* NULL or the function fails on Windows XP, but has to be NULL on
* Windows 10 or it will fail. :(
Mar 22, 2012
Mar 22, 2012
577
*/
Oct 4, 2018
Oct 4, 2018
578
rc = pGetDir(accessToken, NULL, &psize);
Aug 5, 2017
Aug 5, 2017
579
GOTO_IF(rc, PHYSFS_ERR_OS_ERROR, done); /* should have failed! */
Mar 22, 2012
Mar 22, 2012
580
Mar 18, 2019
Mar 18, 2019
581
582
if (psize == 0) /* probably on Windows XP, try a different way. */
{
Mar 18, 2019
Mar 18, 2019
583
WCHAR x = 0;
Mar 18, 2019
Mar 18, 2019
584
585
586
587
588
rc = pGetDir(accessToken, &x, &psize);
GOTO_IF(rc, PHYSFS_ERR_OS_ERROR, done); /* should have failed! */
GOTO_IF(!psize, PHYSFS_ERR_OS_ERROR, done); /* Uhoh... */
} /* if */
Mar 22, 2012
Mar 22, 2012
589
/* Allocate memory for the profile directory */
Mar 23, 2012
Mar 23, 2012
590
wstr = (LPWSTR) __PHYSFS_smallAlloc((psize + 1) * sizeof (WCHAR));
Mar 22, 2012
Mar 22, 2012
591
592
593
if (wstr != NULL)
{
if (pGetDir(accessToken, wstr, &psize))
Mar 23, 2012
Mar 23, 2012
594
595
596
597
598
599
600
{
/* Make sure it ends in a dirsep. We allocated +1 for this. */
if (wstr[psize - 2] != '\\')
{
wstr[psize - 1] = '\\';
wstr[psize - 0] = '\0';
} /* if */
Mar 22, 2012
Mar 22, 2012
601
retval = unicodeToUtf8Heap(wstr);
Mar 23, 2012
Mar 23, 2012
602
} /* if */
Mar 22, 2012
Mar 22, 2012
603
604
605
606
607
__PHYSFS_smallFree(wstr);
} /* if */
} /* if */
done:
Aug 5, 2017
Aug 5, 2017
608
609
if (accessToken)
CloseHandle(accessToken);
Mar 22, 2012
Mar 22, 2012
610
611
FreeLibrary(lib);
return retval; /* We made it: hit the showers. */
Jul 24, 2017
Jul 24, 2017
612
#endif
Mar 22, 2012
Mar 22, 2012
613
} /* __PHYSFS_platformCalcUserDir */
Aug 23, 2001
Aug 23, 2001
614
615
Jul 24, 2017
Jul 24, 2017
616
617
618
619
620
621
int __PHYSFS_platformInit(void)
{
return 1; /* It's all good */
} /* __PHYSFS_platformInit */
Aug 6, 2017
Aug 6, 2017
622
void __PHYSFS_platformDeinit(void)
Jul 24, 2017
Jul 24, 2017
623
624
625
626
627
{
deinitCDThread();
} /* __PHYSFS_platformDeinit */
Sep 6, 2009
Sep 6, 2009
628
void *__PHYSFS_platformGetThreadID(void)
Aug 23, 2001
Aug 23, 2001
629
{
Jan 28, 2010
Jan 28, 2010
630
return ( (void *) ((size_t) GetCurrentThreadId()) );
Aug 23, 2001
Aug 23, 2001
631
632
} /* __PHYSFS_platformGetThreadID */
Jul 24, 2017
Jul 24, 2017
633
Aug 19, 2017
Aug 19, 2017
634
PHYSFS_EnumerateCallbackResult __PHYSFS_platformEnumerate(const char *dirname,
Aug 12, 2017
Aug 12, 2017
635
636
PHYSFS_EnumerateCallback callback,
const char *origdir, void *callbackdata)
Aug 23, 2001
Aug 23, 2001
637
{
Aug 19, 2017
Aug 19, 2017
638
PHYSFS_EnumerateCallbackResult retval = PHYSFS_ENUM_OK;
Mar 26, 2007
Mar 26, 2007
639
640
HANDLE dir = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW entw;
Jun 8, 2002
Jun 8, 2002
641
size_t len = strlen(dirname);
Mar 26, 2007
Mar 26, 2007
642
643
char *searchPath = NULL;
WCHAR *wSearchPath = NULL;
Jun 7, 2002
Jun 7, 2002
644
Jun 8, 2002
Jun 8, 2002
645
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
Mar 26, 2007
Mar 26, 2007
646
searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
Aug 19, 2017
Aug 19, 2017
647
BAIL_IF(!searchPath, PHYSFS_ERR_OUT_OF_MEMORY, PHYSFS_ENUM_ERROR);
Jun 10, 2002
Jun 10, 2002
648
Jun 7, 2002
Jun 7, 2002
649
/* Copy current dirname */
Mar 26, 2007
Mar 26, 2007
650
strcpy(searchPath, dirname);
Jun 8, 2002
Jun 8, 2002
651
652
/* if there's no '\\' at the end of the path, stick one in there. */
Mar 26, 2007
Mar 26, 2007
653
if (searchPath[len - 1] != '\\')
Jun 8, 2002
Jun 8, 2002
654
{
Mar 26, 2007
Mar 26, 2007
655
656
searchPath[len++] = '\\';
searchPath[len] = '\0';
Jun 8, 2002
Jun 8, 2002
657
658
} /* if */
Jun 7, 2002
Jun 7, 2002
659
/* Append the "*" to the end of the string */
Mar 26, 2007
Mar 26, 2007
660
strcat(searchPath, "*");
Aug 23, 2001
Aug 23, 2001
661
Jul 6, 2017
Jul 6, 2017
662
UTF8_TO_UNICODE_STACK(wSearchPath, searchPath);
Aug 12, 2017
Aug 12, 2017
663
__PHYSFS_smallFree(searchPath);
Aug 19, 2017
Aug 19, 2017
664
BAIL_IF_ERRPASS(!wSearchPath, PHYSFS_ENUM_ERROR);
Aug 23, 2001
Aug 23, 2001
665
Jul 24, 2017
Jul 24, 2017
666
dir = winFindFirstFileW(wSearchPath, &entw);
Mar 26, 2007
Mar 26, 2007
667
__PHYSFS_smallFree(wSearchPath);
Aug 19, 2017
Aug 19, 2017
668
BAIL_IF(dir==INVALID_HANDLE_VALUE, errcodeFromWinApi(), PHYSFS_ENUM_ERROR);
Aug 23, 2001
Aug 23, 2001
669
Mar 10, 2012
Mar 10, 2012
670
do
Mar 26, 2007
Mar 26, 2007
671
{
Mar 10, 2012
Mar 10, 2012
672
673
const WCHAR *fn = entw.cFileName;
char *utf8;
Mar 25, 2007
Mar 25, 2007
674
Aug 12, 2017
Aug 12, 2017
675
676
677
678
679
if (fn[0] == '.') /* ignore "." and ".." */
{
if ((fn[1] == '\0') || ((fn[1] == '.') && (fn[2] == '\0')))
continue;
} /* if */
Mar 10, 2012
Mar 10, 2012
680
681
utf8 = unicodeToUtf8Heap(fn);
Aug 12, 2017
Aug 12, 2017
682
683
684
if (utf8 == NULL)
retval = -1;
else
Mar 26, 2007
Mar 26, 2007
685
{
Aug 12, 2017
Aug 12, 2017
686
retval = callback(callbackdata, origdir, utf8);
Mar 10, 2012
Mar 10, 2012
687
allocator.Free(utf8);
Aug 19, 2017
Aug 19, 2017
688
if (retval == PHYSFS_ENUM_ERROR)
Aug 12, 2017
Aug 12, 2017
689
PHYSFS_setErrorCode(PHYSFS_ERR_APP_CALLBACK);
Aug 12, 2017
Aug 12, 2017
690
} /* else */
Aug 19, 2017
Aug 19, 2017
691
} while ((retval == PHYSFS_ENUM_OK) && (FindNextFileW(dir, &entw) != 0));
Aug 23, 2001
Aug 23, 2001
692
693
FindClose(dir);
Aug 12, 2017
Aug 12, 2017
694
695
696
return retval;
} /* __PHYSFS_platformEnumerate */
Aug 23, 2001
Aug 23, 2001
697
698
699
700
int __PHYSFS_platformMkDir(const char *path)
{
Mar 26, 2007
Mar 26, 2007
701
702
WCHAR *wpath;
DWORD rc;
Jul 6, 2017
Jul 6, 2017
703
UTF8_TO_UNICODE_STACK(wpath, path);
Mar 10, 2012
Mar 10, 2012
704
rc = CreateDirectoryW(wpath, NULL);
Mar 26, 2007
Mar 26, 2007
705
__PHYSFS_smallFree(wpath);
Jul 6, 2017
Jul 6, 2017
706
BAIL_IF(rc == 0, errcodeFromWinApi(), 0);
Jan 28, 2010
Jan 28, 2010
707
return 1;
Aug 23, 2001
Aug 23, 2001
708
709
} /* __PHYSFS_platformMkDir */
Jun 10, 2002
Jun 10, 2002
710
Aug 2, 2017
Aug 2, 2017
711
static HANDLE doOpen(const char *fname, DWORD mode, DWORD creation)
Jun 29, 2002
Jun 29, 2002
712
{
Mar 10, 2012
Mar 10, 2012
713
HANDLE fileh;
Mar 26, 2007
Mar 26, 2007
714
WCHAR *wfname;
Jun 29, 2002
Jun 29, 2002
715
Jul 6, 2017
Jul 6, 2017
716
717
UTF8_TO_UNICODE_STACK(wfname, fname);
BAIL_IF(!wfname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 24, 2017
Jul 24, 2017
718
719
fileh = winCreateFileW(wfname, mode, creation);
Mar 26, 2007
Mar 26, 2007
720
__PHYSFS_smallFree(wfname);
Jun 29, 2002
Jun 29, 2002
721
Aug 2, 2017
Aug 2, 2017
722
723
if (fileh == INVALID_HANDLE_VALUE)
BAIL(errcodeFromWinApi(), INVALID_HANDLE_VALUE);
Mar 24, 2002
Mar 24, 2002
724
Aug 2, 2017
Aug 2, 2017
725
return fileh;
Jun 29, 2002
Jun 29, 2002
726
727
} /* doOpen */
Mar 24, 2002
Mar 24, 2002
728
Apr 3, 2002
Apr 3, 2002
729
730
void *__PHYSFS_platformOpenRead(const char *filename)
{
Aug 2, 2017
Aug 2, 2017
731
732
HANDLE h = doOpen(filename, GENERIC_READ, OPEN_EXISTING);
return (h == INVALID_HANDLE_VALUE) ? NULL : (void *) h;
Jun 29, 2002
Jun 29, 2002
733
} /* __PHYSFS_platformOpenRead */
Apr 3, 2002
Apr 3, 2002
734
735
736
737
void *__PHYSFS_platformOpenWrite(const char *filename)
{
Aug 2, 2017
Aug 2, 2017
738
739
HANDLE h = doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS);
return (h == INVALID_HANDLE_VALUE) ? NULL : (void *) h;
Jun 29, 2002
Jun 29, 2002
740
} /* __PHYSFS_platformOpenWrite */
Apr 3, 2002
Apr 3, 2002
741
742
743
744
void *__PHYSFS_platformOpenAppend(const char *filename)
{
Aug 2, 2017
Aug 2, 2017
745
746
747
748
HANDLE h = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS);
BAIL_IF_ERRPASS(h == INVALID_HANDLE_VALUE, NULL);
if (!winSetFilePointer(h, 0, NULL, FILE_END))
Jun 29, 2002
Jun 29, 2002
749
{
Aug 2, 2017
Aug 2, 2017
750
751
752
const PHYSFS_ErrorCode err = errcodeFromWinApi();
CloseHandle(h);
BAIL(err, NULL);
Jun 29, 2002
Jun 29, 2002
753
754
} /* if */
Aug 2, 2017
Aug 2, 2017
755
return (void *) h;
Jun 29, 2002
Jun 29, 2002
756
} /* __PHYSFS_platformOpenAppend */
Apr 3, 2002
Apr 3, 2002
757
758
Aug 21, 2010
Aug 21, 2010
759
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
Apr 3, 2002
Apr 3, 2002
760
{
Aug 2, 2017
Aug 2, 2017
761
HANDLE h = (HANDLE) opaque;
Mar 22, 2012
Mar 22, 2012
762
PHYSFS_sint64 totalRead = 0;
Apr 3, 2002
Apr 3, 2002
763
Mar 20, 2012
Mar 20, 2012
764
if (!__PHYSFS_ui64FitsAddressSpace(len))
Jul 6, 2017
Jul 6, 2017
765
BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
Apr 3, 2002
Apr 3, 2002
766
Mar 22, 2012
Mar 22, 2012
767
768
769
770
while (len > 0)
{
const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
DWORD numRead = 0;
Aug 2, 2017
Aug 2, 2017
771
if (!ReadFile(h, buf, thislen, &numRead, NULL))
Jul 6, 2017
Jul 6, 2017
772
BAIL(errcodeFromWinApi(), -1);
Mar 22, 2012
Mar 22, 2012
773
774
775
776
777
778
779
len -= (PHYSFS_uint64) numRead;
totalRead += (PHYSFS_sint64) numRead;
if (numRead != thislen)
break;
} /* while */
return totalRead;
Jun 29, 2002
Jun 29, 2002
780
781
} /* __PHYSFS_platformRead */
Apr 3, 2002
Apr 3, 2002
782
Jun 29, 2002
Jun 29, 2002
783
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
Jan 21, 2011
Jan 21, 2011
784
PHYSFS_uint64 len)
Apr 3, 2002
Apr 3, 2002
785
{
Aug 2, 2017
Aug 2, 2017
786
HANDLE h = (HANDLE) opaque;
Mar 22, 2012
Mar 22, 2012
787
PHYSFS_sint64 totalWritten = 0;
Apr 3, 2002
Apr 3, 2002
788
Mar 20, 2012
Mar 20, 2012
789
if (!__PHYSFS_ui64FitsAddressSpace(len))
Jul 6, 2017
Jul 6, 2017
790
BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
Apr 3, 2002
Apr 3, 2002
791
Mar 22, 2012
Mar 22, 2012
792
793
794
795
while (len > 0)
{
const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
DWORD numWritten = 0;
Aug 2, 2017
Aug 2, 2017
796
if (!WriteFile(h, buffer, thislen, &numWritten, NULL))
Jul 6, 2017
Jul 6, 2017
797
BAIL(errcodeFromWinApi(), -1);
Mar 22, 2012
Mar 22, 2012
798
799
800
801
802
803
804
len -= (PHYSFS_uint64) numWritten;
totalWritten += (PHYSFS_sint64) numWritten;
if (numWritten != thislen)
break;
} /* while */
return totalWritten;
Jun 29, 2002
Jun 29, 2002
805
806
} /* __PHYSFS_platformWrite */
Apr 3, 2002
Apr 3, 2002
807
808
809
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
Aug 2, 2017
Aug 2, 2017
810
HANDLE h = (HANDLE) opaque;
Jul 24, 2017
Jul 24, 2017
811
const PHYSFS_sint64 spos = (PHYSFS_sint64) pos;
Aug 2, 2017
Aug 2, 2017
812
BAIL_IF(!winSetFilePointer(h,spos,NULL,FILE_BEGIN), errcodeFromWinApi(), 0);
Jan 28, 2010
Jan 28, 2010
813
return 1; /* No error occured */
Jun 29, 2002
Jun 29, 2002
814
} /* __PHYSFS_platformSeek */
Apr 3, 2002
Apr 3, 2002
815
816
817
818
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
Aug 2, 2017
Aug 2, 2017
819
HANDLE h = (HANDLE) opaque;
Jul 24, 2017
Jul 24, 2017
820
PHYSFS_sint64 pos = 0;
Aug 2, 2017
Aug 2, 2017
821
BAIL_IF(!winSetFilePointer(h,0,&pos,FILE_CURRENT), errcodeFromWinApi(), -1);
Jul 24, 2017
Jul 24, 2017
822
return pos;
Jun 29, 2002
Jun 29, 2002
823
} /* __PHYSFS_platformTell */
Apr 3, 2002
Apr 3, 2002
824
825
Jun 29, 2002
Jun 29, 2002
826
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
Apr 3, 2002
Apr 3, 2002
827
{
Aug 2, 2017
Aug 2, 2017
828
HANDLE h = (HANDLE) opaque;
Jul 24, 2017
Jul 24, 2017
829
830
const PHYSFS_sint64 retval = winGetFileSize(h);
BAIL_IF(retval < 0, errcodeFromWinApi(), -1);
Jan 28, 2010
Jan 28, 2010
831
return retval;
Jun 29, 2002
Jun 29, 2002
832
833
} /* __PHYSFS_platformFileLength */
Apr 3, 2002
Apr 3, 2002
834
835
836
int __PHYSFS_platformFlush(void *opaque)
{
Aug 2, 2017
Aug 2, 2017
837
838
HANDLE h = (HANDLE) opaque;
BAIL_IF(!FlushFileBuffers(h), errcodeFromWinApi(), 0);
Jan 28, 2010
Jan 28, 2010
839
return 1;
Jun 29, 2002
Jun 29, 2002
840
} /* __PHYSFS_platformFlush */
Apr 3, 2002
Apr 3, 2002
841
842
Aug 30, 2010
Aug 30, 2010
843
void __PHYSFS_platformClose(void *opaque)
Apr 3, 2002
Apr 3, 2002
844
{
Aug 2, 2017
Aug 2, 2017
845
846
HANDLE h = (HANDLE) opaque;
(void) CloseHandle(h); /* ignore errors. You should have flushed! */
Jun 29, 2002
Jun 29, 2002
847
} /* __PHYSFS_platformClose */
Apr 3, 2002
Apr 3, 2002
848
849
Mar 26, 2007
Mar 26, 2007
850
static int doPlatformDelete(LPWSTR wpath)
Apr 3, 2002
Apr 3, 2002
851
{
Jul 24, 2017
Jul 24, 2017
852
853
854
855
856
857
858
859
860
WIN32_FILE_ATTRIBUTE_DATA info;
if (!GetFileAttributesExW(wpath, GetFileExInfoStandard, &info))
BAIL(errcodeFromWinApi(), 0);
else
{
const int isdir = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
const BOOL rc = isdir ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
BAIL_IF(!rc, errcodeFromWinApi(), 0);
} /* else */
Jan 28, 2010
Jan 28, 2010
861
return 1; /* if you made it here, it worked. */
Mar 26, 2007
Mar 26, 2007
862
863
864
865
866
867
} /* doPlatformDelete */
int __PHYSFS_platformDelete(const char *path)
{
int retval = 0;
Mar 10, 2012
Mar 10, 2012
868
LPWSTR wpath = NULL;
Jul 6, 2017
Jul 6, 2017
869
870
UTF8_TO_UNICODE_STACK(wpath, path);
BAIL_IF(!wpath, PHYSFS_ERR_OUT_OF_MEMORY, 0);
Mar 26, 2007
Mar 26, 2007
871
872
retval = doPlatformDelete(wpath);
__PHYSFS_smallFree(wpath);
Jan 28, 2010
Jan 28, 2010
873
return retval;
Jun 29, 2002
Jun 29, 2002
874
} /* __PHYSFS_platformDelete */
Apr 3, 2002
Apr 3, 2002
875
876
877
878
void *__PHYSFS_platformCreateMutex(void)
{
Mar 22, 2012
Mar 22, 2012
879
880
LPCRITICAL_SECTION lpcs;
lpcs = (LPCRITICAL_SECTION) allocator.Malloc(sizeof (CRITICAL_SECTION));
Jul 6, 2017
Jul 6, 2017
881
BAIL_IF(!lpcs, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 24, 2017
Jul 24, 2017
882
883
884
885
886
887
888
if (!winInitializeCriticalSection(lpcs))
{
allocator.Free(lpcs);
BAIL(errcodeFromWinApi(), NULL);
} /* if */
Mar 22, 2012
Mar 22, 2012
889
return lpcs;
Jun 29, 2002
Jun 29, 2002
890
891
} /* __PHYSFS_platformCreateMutex */
Apr 3, 2002
Apr 3, 2002
892
893
894
void __PHYSFS_platformDestroyMutex(void *mutex)
{
Mar 22, 2012
Mar 22, 2012
895
896
DeleteCriticalSection((LPCRITICAL_SECTION) mutex);
allocator.Free(mutex);
Jun 29, 2002
Jun 29, 2002
897
898
} /* __PHYSFS_platformDestroyMutex */
Apr 3, 2002
Apr 3, 2002
899
900
901
int __PHYSFS_platformGrabMutex(void *mutex)
{
Mar 22, 2012
Mar 22, 2012
902
903
EnterCriticalSection((LPCRITICAL_SECTION) mutex);
return 1;
Jun 29, 2002
Jun 29, 2002
904
} /* __PHYSFS_platformGrabMutex */
Apr 3, 2002
Apr 3, 2002
905
906
907
908
void __PHYSFS_platformReleaseMutex(void *mutex)
{
Mar 22, 2012
Mar 22, 2012
909
LeaveCriticalSection((LPCRITICAL_SECTION) mutex);
Jun 29, 2002
Jun 29, 2002
910
911
} /* __PHYSFS_platformReleaseMutex */
Apr 3, 2002
Apr 3, 2002
912
Jun 29, 2002
Jun 29, 2002
913
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
Jun 10, 2002
Jun 10, 2002
914
915
916
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
Jun 29, 2002
Jun 29, 2002
917
918
919
TIME_ZONE_INFORMATION tzi;
DWORD tzid;
PHYSFS_sint64 retval;
Jun 10, 2002
Jun 10, 2002
920
struct tm tm;
Mar 10, 2012
Mar 10, 2012
921
BOOL rc;
Jun 10, 2002
Jun 10, 2002
922
Jul 6, 2017
Jul 6, 2017
923
BAIL_IF(!FileTimeToSystemTime(ft, &st_utc), errcodeFromWinApi(), -1);
Jun 29, 2002
Jun 29, 2002
924
tzid = GetTimeZoneInformation(&tzi);
Jul 6, 2017
Jul 6, 2017
925
BAIL_IF(tzid == TIME_ZONE_ID_INVALID, errcodeFromWinApi(), -1);
Mar 10, 2012
Mar 10, 2012
926
rc = SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz);
Jul 6, 2017
Jul 6, 2017
927
BAIL_IF(!rc, errcodeFromWinApi(), -1);
Jun 29, 2002
Jun 29, 2002
928
929
/* Convert to a format that mktime() can grok... */
Jun 10, 2002
Jun 10, 2002
930
931
932
933
934
935
tm.tm_sec = st_localtz.wSecond;
tm.tm_min = st_localtz.wMinute;
tm.tm_hour = st_localtz.wHour;
tm.tm_mday = st_localtz.wDay;
tm.tm_mon = st_localtz.wMonth - 1;
tm.tm_year = st_localtz.wYear - 1900;
Jun 29, 2002
Jun 29, 2002
936
tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
Jun 10, 2002
Jun 10, 2002
937
938
tm.tm_yday = -1;
tm.tm_isdst = -1;
Jun 29, 2002
Jun 29, 2002
939
940
941
/* Convert to a format PhysicsFS can grok... */
retval = (PHYSFS_sint64) mktime(&tm);
Jul 6, 2017
Jul 6, 2017
942
BAIL_IF(retval == -1, PHYSFS_ERR_OS_ERROR, -1);
Jan 28, 2010
Jan 28, 2010
943
return retval;
Jun 29, 2002
Jun 29, 2002
944
945
} /* FileTimeToPhysfsTime */
Nov 30, 2012
Nov 30, 2012
946
Jul 9, 2017
Jul 9, 2017
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
/* check for symlinks. These exist in NTFS 3.1 (WinXP), even though
they aren't really available to userspace before Vista. I wonder
what would happen if you put an NTFS disk with a symlink on it
into an XP machine, though; would this flag get set?
NTFS symlinks are a form of "reparse point" (junction, volume mount,
etc), so if the REPARSE_POINT attribute is set, check for the symlink
tag thereafter. This assumes you already read in the file attributes. */
static int isSymlink(const WCHAR *wpath, const DWORD attr)
{
WIN32_FIND_DATAW w32dw;
HANDLE h;
if ((attr & PHYSFS_FILE_ATTRIBUTE_REPARSE_POINT) == 0)
return 0; /* not a reparse point? Definitely not a symlink. */
Jul 24, 2017
Jul 24, 2017
962
h = winFindFirstFileW(wpath, &w32dw);
Jul 9, 2017
Jul 9, 2017
963
964
965
966
if (h == INVALID_HANDLE_VALUE)
return 0; /* ...maybe the file just vanished...? */
FindClose(h);
Jul 9, 2017
Jul 9, 2017
967
return (w32dw.dwReserved0 == PHYSFS_IO_REPARSE_TAG_SYMLINK);
Jul 9, 2017
Jul 9, 2017
968
969
970
} /* isSymlink */
Oct 26, 2017
Oct 26, 2017
971
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st, const int follow)
Feb 15, 2010
Feb 15, 2010
972
973
974
{
WIN32_FILE_ATTRIBUTE_DATA winstat;
WCHAR *wstr = NULL;
Mar 10, 2012
Mar 10, 2012
975
DWORD err = 0;
Feb 15, 2010
Feb 15, 2010
976
BOOL rc = 0;
Jul 9, 2017
Jul 9, 2017
977
int issymlink = 0;
Feb 15, 2010
Feb 15, 2010
978
Jul 6, 2017
Jul 6, 2017
979
980
UTF8_TO_UNICODE_STACK(wstr, filename);
BAIL_IF(!wstr, PHYSFS_ERR_OUT_OF_MEMORY, 0);
Mar 10, 2012
Mar 10, 2012
981
rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
Jul 9, 2017
Jul 9, 2017
982
983
984
985
if (!rc)
err = GetLastError();
else /* check for symlink while wstr is still available */
Oct 26, 2017
Oct 26, 2017
986
issymlink = !follow && isSymlink(wstr, winstat.dwFileAttributes);
Jul 9, 2017
Jul 9, 2017
987
Feb 15, 2010
Feb 15, 2010
988
__PHYSFS_smallFree(wstr);
Jul 6, 2017
Jul 6, 2017
989
BAIL_IF(!rc, errcodeFromWinApiError(err), 0);
Feb 15, 2010
Feb 15, 2010
990
Nov 30, 2012
Nov 30, 2012
991
992
993
st->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
st->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
st->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
Feb 15, 2010
Feb 15, 2010
994
Jul 9, 2017
Jul 9, 2017
995
if (issymlink)
Feb 15, 2010
Feb 15, 2010
996
{
Jul 9, 2017
Jul 9, 2017
997
st->filetype = PHYSFS_FILETYPE_SYMLINK;
Nov 30, 2012
Nov 30, 2012
998
st->filesize = 0;
Feb 15, 2010
Feb 15, 2010
999
1000
} /* if */