Skip to content

Latest commit

 

History

History
951 lines (776 loc) · 27.8 KB

platform_windows.c

File metadata and controls

951 lines (776 loc) · 27.8 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__
Jul 13, 2017
Jul 13, 2017
10
#include "physfs_internal.h"
Mar 11, 2007
Mar 11, 2007
11
12
#ifdef PHYSFS_PLATFORM_WINDOWS
Mar 29, 2015
Mar 29, 2015
13
#ifndef PHYSFS_PLATFORM_WINRT
Jul 10, 2002
Jul 10, 2002
14
Mar 10, 2012
Mar 10, 2012
15
/* Forcibly disable UNICODE macro, since we manage this ourselves. */
Mar 26, 2007
Mar 26, 2007
16
17
18
19
#ifdef UNICODE
#undef UNICODE
#endif
Mar 10, 2012
Mar 10, 2012
20
#define WIN32_LEAN_AND_MEAN 1
Aug 23, 2001
Aug 23, 2001
21
#include <windows.h>
Mar 10, 2012
Mar 10, 2012
22
#include <userenv.h>
Mar 22, 2012
Mar 22, 2012
23
#include <shlobj.h>
Mar 12, 2012
Mar 12, 2012
24
#include <dbt.h>
Jun 29, 2002
Jun 29, 2002
25
#include <errno.h>
Aug 29, 2001
Aug 29, 2001
26
#include <ctype.h>
Jun 7, 2002
Jun 7, 2002
27
#include <time.h>
May 6, 2002
May 6, 2002
28
Oct 1, 2007
Oct 1, 2007
29
30
#define LOWORDER_UINT64(pos) ((PHYSFS_uint32) (pos & 0xFFFFFFFF))
#define HIGHORDER_UINT64(pos) ((PHYSFS_uint32) ((pos >> 32) & 0xFFFFFFFF))
Apr 3, 2002
Apr 3, 2002
31
Mar 25, 2007
Mar 25, 2007
32
33
34
35
36
37
/*
* 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
38
Mar 25, 2007
Mar 25, 2007
39
40
/* just in case... */
#define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
Jun 29, 2002
Jun 29, 2002
41
Apr 1, 2007
Apr 1, 2007
42
/* Not defined before the Vista SDK. */
Jul 9, 2017
Jul 9, 2017
43
#define PHYSFS_FILE_ATTRIBUTE_REPARSE_POINT 0x400
Apr 1, 2007
Apr 1, 2007
44
45
46
#define PHYSFS_IO_REPARSE_TAG_SYMLINK 0xA000000C
Jul 6, 2017
Jul 6, 2017
47
#define UTF8_TO_UNICODE_STACK(w_assignto, str) { \
Mar 25, 2007
Mar 25, 2007
48
49
50
if (str == NULL) \
w_assignto = NULL; \
else { \
Jan 22, 2008
Jan 22, 2008
51
const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) + 1) * 2); \
Mar 26, 2007
Mar 26, 2007
52
53
w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
if (w_assignto != NULL) \
Mar 20, 2012
Mar 20, 2012
54
PHYSFS_utf8ToUtf16(str, (PHYSFS_uint16 *) w_assignto, len); \
Mar 25, 2007
Mar 25, 2007
55
56
} \
} \
Jun 29, 2002
Jun 29, 2002
57
Mar 20, 2012
Mar 20, 2012
58
/* Note this counts WCHARs, not codepoints! */
Mar 26, 2007
Mar 26, 2007
59
60
61
62
63
static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
{
PHYSFS_uint64 len = 0;
while (*(wstr++))
len++;
Jan 28, 2010
Jan 28, 2010
64
return len;
Mar 26, 2007
Mar 26, 2007
65
66
67
68
69
70
71
72
73
74
} /* 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
75
BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 20, 2012
Mar 20, 2012
76
PHYSFS_utf8FromUtf16((const PHYSFS_uint16 *) w_str, retval, len);
Mar 26, 2007
Mar 26, 2007
77
78
79
80
ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
if (ptr != NULL)
retval = (char *) ptr;
} /* if */
Jan 28, 2010
Jan 28, 2010
81
return retval;
Mar 26, 2007
Mar 26, 2007
82
83
} /* unicodeToUtf8Heap */
Mar 10, 2012
Mar 10, 2012
84
/* !!! FIXME: do we really need readonly? If not, do we need this struct? */
Jun 29, 2002
Jun 29, 2002
85
86
87
88
typedef struct
{
HANDLE handle;
int readonly;
Mar 26, 2007
Mar 26, 2007
89
} WinApiFile;
Aug 23, 2001
Aug 23, 2001
90
Mar 12, 2012
Mar 12, 2012
91
92
93
94
static HANDLE detectCDThreadHandle = NULL;
static HWND detectCDHwnd = 0;
static volatile int initialDiscDetectionComplete = 0;
static volatile DWORD drivesWithMediaBitmap = 0;
Mar 26, 2007
Mar 26, 2007
95
Mar 20, 2012
Mar 20, 2012
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
110
{
Mar 20, 2012
Mar 20, 2012
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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;
/* !!! FIXME: ?? case ELOOP: return PHYSFS_ERR_SYMLINK_LOOP; */
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
127
128
129
130
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
131
132
case ERROR_HANDLE_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
case ERROR_DISK_FULL: return PHYSFS_ERR_NO_SPACE;
Nov 28, 2012
Nov 28, 2012
133
/* !!! FIXME: ?? case ENOTDIR: return PHYSFS_ERR_NOT_FOUND; */
Mar 20, 2012
Mar 20, 2012
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* !!! FIXME: ?? case EISDIR: return PHYSFS_ERR_NOT_A_FILE; */
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
153
Jun 10, 2002
Jun 10, 2002
154
Mar 12, 2012
Mar 12, 2012
155
typedef BOOL (WINAPI *fnSTEM)(DWORD, LPDWORD b);
Jun 7, 2002
Jun 7, 2002
156
Mar 12, 2012
Mar 12, 2012
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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
174
Mar 12, 2012
Mar 12, 2012
175
176
177
178
179
180
181
/* 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
182
Mar 12, 2012
Mar 12, 2012
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* 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 */
static DWORD WINAPI detectCDThread(LPVOID lpParameter)
{
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)
{
initialDiscDetectionComplete = 1; /* let main thread go on. */
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)
{
initialDiscDetectionComplete = 1; /* let main thread go on. */
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();
initialDiscDetectionComplete = 1; /* let main thread go on. */
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);
do
{
const BOOL rc = GetMessage(&msg, detectCDHwnd, 0, 0);
if ((rc == 0) || (rc == -1))
break;
TranslateMessage(&msg);
DispatchMessageW(&msg);
} while (1);
UnregisterClassA(classname, hInstance);
return 0;
} /* detectCDThread */
Jun 29, 2002
Jun 29, 2002
289
Jun 7, 2002
Jun 7, 2002
290
Sep 29, 2004
Sep 29, 2004
291
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
Aug 23, 2001
Aug 23, 2001
292
{
Mar 12, 2012
Mar 12, 2012
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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)
{
initialDiscDetectionComplete = 0;
detectCDThreadHandle = CreateThread(NULL,0,detectCDThread,NULL,0,NULL);
if (detectCDThreadHandle == NULL)
return; /* oh well. */
while (!initialDiscDetectionComplete)
Sleep(50);
} /* if */
drives = drivesWithMediaBitmap; /* whatever the thread has seen, we take. */
for (i = 'A'; i <= 'Z'; i++)
Aug 23, 2001
Aug 23, 2001
317
{
Mar 12, 2012
Mar 12, 2012
318
319
320
if (drives & (1 << (i - 'A')))
{
drive_str[0] = (char) i;
Sep 29, 2004
Sep 29, 2004
321
cb(data, drive_str);
Mar 12, 2012
Mar 12, 2012
322
} /* if */
Aug 23, 2001
Aug 23, 2001
323
} /* for */
Sep 29, 2004
Sep 29, 2004
324
} /* __PHYSFS_platformDetectAvailableCDs */
Aug 23, 2001
Aug 23, 2001
325
326
Oct 9, 2001
Oct 9, 2001
327
328
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
Mar 10, 2012
Mar 10, 2012
329
330
331
DWORD buflen = 64;
LPWSTR modpath = NULL;
char *retval = NULL;
Oct 9, 2001
Oct 9, 2001
332
Mar 10, 2012
Mar 10, 2012
333
334
335
336
337
338
339
340
while (1)
{
DWORD rc;
void *ptr;
if ( (ptr = allocator.Realloc(modpath, buflen*sizeof(WCHAR))) == NULL )
{
allocator.Free(modpath);
Jul 6, 2017
Jul 6, 2017
341
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 10, 2012
Mar 10, 2012
342
343
344
345
346
347
348
} /* if */
modpath = (LPWSTR) ptr;
rc = GetModuleFileNameW(NULL, modpath, buflen);
if (rc == 0)
{
allocator.Free(modpath);
Jul 6, 2017
Jul 6, 2017
349
BAIL(errcodeFromWinApi(), NULL);
Mar 10, 2012
Mar 10, 2012
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
} /* 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
372
PHYSFS_setErrorCode(PHYSFS_ERR_OTHER_ERROR); /* oh well. */
Mar 10, 2012
Mar 10, 2012
373
374
else
{
Mar 23, 2012
Mar 23, 2012
375
*(ptr+1) = '\0'; /* chop off filename. */
Mar 10, 2012
Mar 10, 2012
376
377
378
379
380
381
retval = unicodeToUtf8Heap(modpath);
} /* else */
} /* else */
allocator.Free(modpath);
return retval; /* w00t. */
Aug 23, 2001
Aug 23, 2001
382
383
384
} /* __PHYSFS_platformCalcBaseDir */
Mar 22, 2012
Mar 22, 2012
385
386
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app)
{
Mar 22, 2012
Mar 22, 2012
387
388
389
390
391
392
393
/*
* 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
394
395
WCHAR path[MAX_PATH];
Mar 22, 2012
Mar 22, 2012
396
397
398
399
char *utf8 = NULL;
size_t len = 0;
char *retval = NULL;
Mar 22, 2012
Mar 22, 2012
400
401
if (!SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
NULL, 0, path)))
Jul 6, 2017
Jul 6, 2017
402
BAIL(PHYSFS_ERR_OS_ERROR, NULL);
Mar 22, 2012
Mar 22, 2012
403
Mar 22, 2012
Mar 22, 2012
404
utf8 = unicodeToUtf8Heap(path);
Jul 6, 2017
Jul 6, 2017
405
BAIL_IF_ERRPASS(!utf8, NULL);
Mar 22, 2012
Mar 22, 2012
406
len = strlen(utf8) + strlen(org) + strlen(app) + 4;
Mar 22, 2012
Mar 22, 2012
407
408
409
410
retval = allocator.Malloc(len);
if (!retval)
{
allocator.Free(utf8);
Jul 6, 2017
Jul 6, 2017
411
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 22, 2012
Mar 22, 2012
412
413
} /* if */
Jul 9, 2017
Jul 9, 2017
414
snprintf(retval, len, "%s\\%s\\%s\\", utf8, org, app);
Aug 20, 2013
Aug 20, 2013
415
allocator.Free(utf8);
Mar 22, 2012
Mar 22, 2012
416
return retval;
Mar 22, 2012
Mar 22, 2012
417
418
419
} /* __PHYSFS_platformCalcPrefDir */
Mar 22, 2012
Mar 22, 2012
420
char *__PHYSFS_platformCalcUserDir(void)
Aug 23, 2001
Aug 23, 2001
421
{
Mar 22, 2012
Mar 22, 2012
422
423
424
425
426
427
428
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
429
BAIL_IF(!lib, errcodeFromWinApi(), NULL);
Mar 22, 2012
Mar 22, 2012
430
pGetDir=(fnGetUserProfDirW) GetProcAddress(lib,"GetUserProfileDirectoryW");
Jul 6, 2017
Jul 6, 2017
431
GOTO_IF(!pGetDir, errcodeFromWinApi(), done);
Mar 22, 2012
Mar 22, 2012
432
433
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken))
Jul 6, 2017
Jul 6, 2017
434
GOTO(errcodeFromWinApi(), done);
Mar 22, 2012
Mar 22, 2012
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
else
{
DWORD psize = 0;
WCHAR dummy = 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
* NULL or the function fails.
*/
rc = pGetDir(accessToken, &dummy, &psize);
assert(!rc); /* !!! FIXME: handle this gracefully. */
(void) rc;
/* Allocate memory for the profile directory */
Mar 23, 2012
Mar 23, 2012
452
wstr = (LPWSTR) __PHYSFS_smallAlloc((psize + 1) * sizeof (WCHAR));
Mar 22, 2012
Mar 22, 2012
453
454
455
if (wstr != NULL)
{
if (pGetDir(accessToken, wstr, &psize))
Mar 23, 2012
Mar 23, 2012
456
457
458
459
460
461
462
{
/* 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
463
retval = unicodeToUtf8Heap(wstr);
Mar 23, 2012
Mar 23, 2012
464
} /* if */
Mar 22, 2012
Mar 22, 2012
465
466
467
468
469
470
471
472
473
474
__PHYSFS_smallFree(wstr);
} /* if */
CloseHandle(accessToken);
} /* if */
done:
FreeLibrary(lib);
return retval; /* We made it: hit the showers. */
} /* __PHYSFS_platformCalcUserDir */
Aug 23, 2001
Aug 23, 2001
475
476
Sep 6, 2009
Sep 6, 2009
477
void *__PHYSFS_platformGetThreadID(void)
Aug 23, 2001
Aug 23, 2001
478
{
Jan 28, 2010
Jan 28, 2010
479
return ( (void *) ((size_t) GetCurrentThreadId()) );
Aug 23, 2001
Aug 23, 2001
480
481
} /* __PHYSFS_platformGetThreadID */
Sep 29, 2004
Sep 29, 2004
482
void __PHYSFS_platformEnumerateFiles(const char *dirname,
Sep 18, 2005
Sep 18, 2005
483
484
PHYSFS_EnumFilesCallback callback,
const char *origdir,
Sep 29, 2004
Sep 29, 2004
485
void *callbackdata)
Aug 23, 2001
Aug 23, 2001
486
{
Mar 26, 2007
Mar 26, 2007
487
488
HANDLE dir = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW entw;
Jun 8, 2002
Jun 8, 2002
489
size_t len = strlen(dirname);
Mar 26, 2007
Mar 26, 2007
490
491
char *searchPath = NULL;
WCHAR *wSearchPath = NULL;
Jun 7, 2002
Jun 7, 2002
492
Jun 8, 2002
Jun 8, 2002
493
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
Mar 26, 2007
Mar 26, 2007
494
495
searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
if (searchPath == NULL)
Sep 29, 2004
Sep 29, 2004
496
return;
Jun 10, 2002
Jun 10, 2002
497
Jun 7, 2002
Jun 7, 2002
498
/* Copy current dirname */
Mar 26, 2007
Mar 26, 2007
499
strcpy(searchPath, dirname);
Jun 8, 2002
Jun 8, 2002
500
501
/* if there's no '\\' at the end of the path, stick one in there. */
Mar 26, 2007
Mar 26, 2007
502
if (searchPath[len - 1] != '\\')
Jun 8, 2002
Jun 8, 2002
503
{
Mar 26, 2007
Mar 26, 2007
504
505
searchPath[len++] = '\\';
searchPath[len] = '\0';
Jun 8, 2002
Jun 8, 2002
506
507
} /* if */
Jun 7, 2002
Jun 7, 2002
508
/* Append the "*" to the end of the string */
Mar 26, 2007
Mar 26, 2007
509
strcat(searchPath, "*");
Aug 23, 2001
Aug 23, 2001
510
Jul 6, 2017
Jul 6, 2017
511
UTF8_TO_UNICODE_STACK(wSearchPath, searchPath);
Mar 20, 2012
Mar 20, 2012
512
if (!wSearchPath)
Mar 26, 2007
Mar 26, 2007
513
return; /* oh well. */
Aug 23, 2001
Aug 23, 2001
514
Mar 10, 2012
Mar 10, 2012
515
dir = FindFirstFileW(wSearchPath, &entw);
Aug 23, 2001
Aug 23, 2001
516
Mar 26, 2007
Mar 26, 2007
517
518
519
520
__PHYSFS_smallFree(wSearchPath);
__PHYSFS_smallFree(searchPath);
if (dir == INVALID_HANDLE_VALUE)
return;
Aug 23, 2001
Aug 23, 2001
521
Mar 10, 2012
Mar 10, 2012
522
do
Mar 26, 2007
Mar 26, 2007
523
{
Mar 10, 2012
Mar 10, 2012
524
525
const WCHAR *fn = entw.cFileName;
char *utf8;
Mar 25, 2007
Mar 25, 2007
526
Mar 10, 2012
Mar 10, 2012
527
528
529
530
531
532
533
if ((fn[0] == '.') && (fn[1] == '\0'))
continue;
if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
continue;
utf8 = unicodeToUtf8Heap(fn);
if (utf8 != NULL)
Mar 26, 2007
Mar 26, 2007
534
{
Mar 10, 2012
Mar 10, 2012
535
536
537
538
callback(callbackdata, origdir, utf8);
allocator.Free(utf8);
} /* if */
} while (FindNextFileW(dir, &entw) != 0);
Aug 23, 2001
Aug 23, 2001
539
540
541
542
543
544
545
FindClose(dir);
} /* __PHYSFS_platformEnumerateFiles */
int __PHYSFS_platformMkDir(const char *path)
{
Mar 26, 2007
Mar 26, 2007
546
547
WCHAR *wpath;
DWORD rc;
Jul 6, 2017
Jul 6, 2017
548
UTF8_TO_UNICODE_STACK(wpath, path);
Mar 10, 2012
Mar 10, 2012
549
rc = CreateDirectoryW(wpath, NULL);
Mar 26, 2007
Mar 26, 2007
550
__PHYSFS_smallFree(wpath);
Jul 6, 2017
Jul 6, 2017
551
BAIL_IF(rc == 0, errcodeFromWinApi(), 0);
Jan 28, 2010
Jan 28, 2010
552
return 1;
Aug 23, 2001
Aug 23, 2001
553
554
} /* __PHYSFS_platformMkDir */
Jun 10, 2002
Jun 10, 2002
555
Jun 29, 2002
Jun 29, 2002
556
557
int __PHYSFS_platformInit(void)
{
Jan 28, 2010
Jan 28, 2010
558
return 1; /* It's all good */
Jun 29, 2002
Jun 29, 2002
559
} /* __PHYSFS_platformInit */
Mar 24, 2002
Mar 24, 2002
560
561
562
563
int __PHYSFS_platformDeinit(void)
{
Mar 20, 2012
Mar 20, 2012
564
if (detectCDThreadHandle)
Mar 12, 2012
Mar 12, 2012
565
566
567
568
569
570
571
572
573
{
if (detectCDHwnd)
PostMessageW(detectCDHwnd, WM_QUIT, 0, 0);
CloseHandle(detectCDThreadHandle);
detectCDThreadHandle = NULL;
initialDiscDetectionComplete = 0;
drivesWithMediaBitmap = 0;
} /* if */
Jan 28, 2010
Jan 28, 2010
574
return 1; /* It's all good */
Jun 29, 2002
Jun 29, 2002
575
576
577
578
579
} /* __PHYSFS_platformDeinit */
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
{
Mar 10, 2012
Mar 10, 2012
580
HANDLE fileh;
Mar 26, 2007
Mar 26, 2007
581
582
WinApiFile *retval;
WCHAR *wfname;
Jun 29, 2002
Jun 29, 2002
583
Jul 6, 2017
Jul 6, 2017
584
585
UTF8_TO_UNICODE_STACK(wfname, fname);
BAIL_IF(!wfname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 10, 2012
Mar 10, 2012
586
587
fileh = CreateFileW(wfname, mode, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
Mar 26, 2007
Mar 26, 2007
588
__PHYSFS_smallFree(wfname);
Jun 29, 2002
Jun 29, 2002
589
Jul 6, 2017
Jul 6, 2017
590
BAIL_IF(fileh == INVALID_HANDLE_VALUE,errcodeFromWinApi(), NULL);
Jun 29, 2002
Jun 29, 2002
591
Mar 26, 2007
Mar 26, 2007
592
retval = (WinApiFile *) allocator.Malloc(sizeof (WinApiFile));
Mar 20, 2012
Mar 20, 2012
593
if (!retval)
Jun 10, 2002
Jun 10, 2002
594
{
Mar 10, 2012
Mar 10, 2012
595
CloseHandle(fileh);
Jul 6, 2017
Jul 6, 2017
596
BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jun 10, 2002
Jun 10, 2002
597
} /* if */
Mar 24, 2002
Mar 24, 2002
598
Jun 29, 2002
Jun 29, 2002
599
retval->readonly = rdonly;
Mar 10, 2012
Mar 10, 2012
600
retval->handle = fileh;
Jan 28, 2010
Jan 28, 2010
601
return retval;
Jun 29, 2002
Jun 29, 2002
602
603
} /* doOpen */
Mar 24, 2002
Mar 24, 2002
604
Apr 3, 2002
Apr 3, 2002
605
606
void *__PHYSFS_platformOpenRead(const char *filename)
{
Jan 28, 2010
Jan 28, 2010
607
return doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1);
Jun 29, 2002
Jun 29, 2002
608
} /* __PHYSFS_platformOpenRead */
Apr 3, 2002
Apr 3, 2002
609
610
611
612
void *__PHYSFS_platformOpenWrite(const char *filename)
{
Jan 28, 2010
Jan 28, 2010
613
return doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0);
Jun 29, 2002
Jun 29, 2002
614
} /* __PHYSFS_platformOpenWrite */
Apr 3, 2002
Apr 3, 2002
615
616
617
618
void *__PHYSFS_platformOpenAppend(const char *filename)
{
Jun 29, 2002
Jun 29, 2002
619
620
621
void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
if (retval != NULL)
{
Mar 26, 2007
Mar 26, 2007
622
HANDLE h = ((WinApiFile *) retval)->handle;
Dec 8, 2003
Dec 8, 2003
623
624
DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
Jun 29, 2002
Jun 29, 2002
625
{
Mar 20, 2012
Mar 20, 2012
626
const PHYSFS_ErrorCode err = errcodeFromWinApi();
Jun 29, 2002
Jun 29, 2002
627
CloseHandle(h);
Mar 14, 2005
Mar 14, 2005
628
allocator.Free(retval);
Jul 6, 2017
Jul 6, 2017
629
BAIL(err, NULL);
Jun 29, 2002
Jun 29, 2002
630
631
632
} /* if */
} /* if */
Jan 28, 2010
Jan 28, 2010
633
return retval;
Jun 29, 2002
Jun 29, 2002
634
} /* __PHYSFS_platformOpenAppend */
Apr 3, 2002
Apr 3, 2002
635
636
Aug 21, 2010
Aug 21, 2010
637
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
Apr 3, 2002
Apr 3, 2002
638
{
Mar 26, 2007
Mar 26, 2007
639
HANDLE Handle = ((WinApiFile *) opaque)->handle;
Mar 22, 2012
Mar 22, 2012
640
PHYSFS_sint64 totalRead = 0;
Apr 3, 2002
Apr 3, 2002
641
Mar 20, 2012
Mar 20, 2012
642
if (!__PHYSFS_ui64FitsAddressSpace(len))
Jul 6, 2017
Jul 6, 2017
643
BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
Apr 3, 2002
Apr 3, 2002
644
Mar 22, 2012
Mar 22, 2012
645
646
647
648
649
while (len > 0)
{
const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
DWORD numRead = 0;
if (!ReadFile(Handle, buf, thislen, &numRead, NULL))
Jul 6, 2017
Jul 6, 2017
650
BAIL(errcodeFromWinApi(), -1);
Mar 22, 2012
Mar 22, 2012
651
652
653
654
655
656
657
len -= (PHYSFS_uint64) numRead;
totalRead += (PHYSFS_sint64) numRead;
if (numRead != thislen)
break;
} /* while */
return totalRead;
Jun 29, 2002
Jun 29, 2002
658
659
} /* __PHYSFS_platformRead */
Apr 3, 2002
Apr 3, 2002
660
Jun 29, 2002
Jun 29, 2002
661
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
Jan 21, 2011
Jan 21, 2011
662
PHYSFS_uint64 len)
Apr 3, 2002
Apr 3, 2002
663
{
Mar 26, 2007
Mar 26, 2007
664
HANDLE Handle = ((WinApiFile *) opaque)->handle;
Mar 22, 2012
Mar 22, 2012
665
PHYSFS_sint64 totalWritten = 0;
Apr 3, 2002
Apr 3, 2002
666
Mar 20, 2012
Mar 20, 2012
667
if (!__PHYSFS_ui64FitsAddressSpace(len))
Jul 6, 2017
Jul 6, 2017
668
BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
Apr 3, 2002
Apr 3, 2002
669
Mar 22, 2012
Mar 22, 2012
670
671
672
673
674
while (len > 0)
{
const DWORD thislen = (len > 0xFFFFFFFF) ? 0xFFFFFFFF : (DWORD) len;
DWORD numWritten = 0;
if (!WriteFile(Handle, buffer, thislen, &numWritten, NULL))
Jul 6, 2017
Jul 6, 2017
675
BAIL(errcodeFromWinApi(), -1);
Mar 22, 2012
Mar 22, 2012
676
677
678
679
680
681
682
len -= (PHYSFS_uint64) numWritten;
totalWritten += (PHYSFS_sint64) numWritten;
if (numWritten != thislen)
break;
} /* while */
return totalWritten;
Jun 29, 2002
Jun 29, 2002
683
684
} /* __PHYSFS_platformWrite */
Apr 3, 2002
Apr 3, 2002
685
686
687
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
Mar 26, 2007
Mar 26, 2007
688
HANDLE Handle = ((WinApiFile *) opaque)->handle;
Mar 9, 2008
Mar 9, 2008
689
690
LONG HighOrderPos;
PLONG pHighOrderPos;
Jun 29, 2002
Jun 29, 2002
691
DWORD rc;
Apr 3, 2002
Apr 3, 2002
692
693
694
695
/* Get the high order 32-bits of the position */
HighOrderPos = HIGHORDER_UINT64(pos);
Dec 8, 2003
Dec 8, 2003
696
697
698
699
700
701
/*
* MSDN: "If you do not need the high-order 32 bits, this
* pointer must be set to NULL."
*/
pHighOrderPos = (HighOrderPos) ? &HighOrderPos : NULL;
Apr 3, 2002
Apr 3, 2002
702
/* Move pointer "pos" count from start of file */
Sep 26, 2004
Sep 26, 2004
703
rc = SetFilePointer(Handle, LOWORDER_UINT64(pos),
Dec 8, 2003
Dec 8, 2003
704
pHighOrderPos, FILE_BEGIN);
Apr 3, 2002
Apr 3, 2002
705
Dec 8, 2003
Dec 8, 2003
706
707
708
if ( (rc == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
{
Jul 6, 2017
Jul 6, 2017
709
BAIL(errcodeFromWinApi(), 0);
Dec 8, 2003
Dec 8, 2003
710
711
} /* if */
Jan 28, 2010
Jan 28, 2010
712
return 1; /* No error occured */
Jun 29, 2002
Jun 29, 2002
713
} /* __PHYSFS_platformSeek */
Apr 3, 2002
Apr 3, 2002
714
715
716
717
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
Mar 26, 2007
Mar 26, 2007
718
HANDLE Handle = ((WinApiFile *) opaque)->handle;
Mar 9, 2008
Mar 9, 2008
719
LONG HighPos = 0;
Jun 29, 2002
Jun 29, 2002
720
DWORD LowPos;
Apr 3, 2002
Apr 3, 2002
721
722
723
PHYSFS_sint64 retval;
/* Get current position */
Sep 26, 2004
Sep 26, 2004
724
LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
Dec 8, 2003
Dec 8, 2003
725
726
if ( (LowPos == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
Apr 3, 2002
Apr 3, 2002
727
{
Jul 6, 2017
Jul 6, 2017
728
BAIL(errcodeFromWinApi(), -1);
Jun 29, 2002
Jun 29, 2002
729
} /* if */
May 8, 2002
May 8, 2002
730
731
732
else
{
/* Combine the high/low order to create the 64-bit position value */
Jun 29, 2002
Jun 29, 2002
733
734
735
736
retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
assert(retval >= 0);
} /* else */
Jan 28, 2010
Jan 28, 2010
737
return retval;
Jun 29, 2002
Jun 29, 2002
738
} /* __PHYSFS_platformTell */
Apr 3, 2002
Apr 3, 2002
739
740
Jun 29, 2002
Jun 29, 2002
741
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
Apr 3, 2002
Apr 3, 2002
742
{
Mar 26, 2007
Mar 26, 2007
743
HANDLE Handle = ((WinApiFile *) opaque)->handle;
Jun 29, 2002
Jun 29, 2002
744
745
DWORD SizeHigh;
DWORD SizeLow;
Apr 3, 2002
Apr 3, 2002
746
747
PHYSFS_sint64 retval;
Sep 26, 2004
Sep 26, 2004
748
SizeLow = GetFileSize(Handle, &SizeHigh);
Dec 8, 2003
Dec 8, 2003
749
750
if ( (SizeLow == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
Apr 3, 2002
Apr 3, 2002
751
{
Jul 6, 2017
Jul 6, 2017
752
BAIL(errcodeFromWinApi(), -1);
Jun 10, 2002
Jun 10, 2002
753
} /* if */
May 8, 2002
May 8, 2002
754
755
756
else
{
/* Combine the high/low order to create the 64-bit position value */
Jun 29, 2002
Jun 29, 2002
757
758
retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
assert(retval >= 0);
Jun 10, 2002
Jun 10, 2002
759
} /* else */
Apr 3, 2002
Apr 3, 2002
760
Jan 28, 2010
Jan 28, 2010
761
return retval;
Jun 29, 2002
Jun 29, 2002
762
763
} /* __PHYSFS_platformFileLength */
Apr 3, 2002
Apr 3, 2002
764
765
766
int __PHYSFS_platformFlush(void *opaque)
{
Mar 26, 2007
Mar 26, 2007
767
WinApiFile *fh = ((WinApiFile *) opaque);
Jun 29, 2002
Jun 29, 2002
768
if (!fh->readonly)
Jul 6, 2017
Jul 6, 2017
769
BAIL_IF(!FlushFileBuffers(fh->handle), errcodeFromWinApi(), 0);
Apr 3, 2002
Apr 3, 2002
770
Jan 28, 2010
Jan 28, 2010
771
return 1;
Jun 29, 2002
Jun 29, 2002
772
} /* __PHYSFS_platformFlush */
Apr 3, 2002
Apr 3, 2002
773
774
Aug 30, 2010
Aug 30, 2010
775
void __PHYSFS_platformClose(void *opaque)
Apr 3, 2002
Apr 3, 2002
776
{
Mar 26, 2007
Mar 26, 2007
777
HANDLE Handle = ((WinApiFile *) opaque)->handle;
Aug 30, 2010
Aug 30, 2010
778
(void) CloseHandle(Handle); /* ignore errors. You should have flushed! */
Mar 14, 2005
Mar 14, 2005
779
allocator.Free(opaque);
Jun 29, 2002
Jun 29, 2002
780
} /* __PHYSFS_platformClose */
Apr 3, 2002
Apr 3, 2002
781
782
Mar 26, 2007
Mar 26, 2007
783
static int doPlatformDelete(LPWSTR wpath)
Apr 3, 2002
Apr 3, 2002
784
{
Mar 10, 2012
Mar 10, 2012
785
786
const int isdir = (GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY);
const BOOL rc = (isdir) ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
Jul 6, 2017
Jul 6, 2017
787
BAIL_IF(!rc, errcodeFromWinApi(), 0);
Jan 28, 2010
Jan 28, 2010
788
return 1; /* if you made it here, it worked. */
Mar 26, 2007
Mar 26, 2007
789
790
791
792
793
794
} /* doPlatformDelete */
int __PHYSFS_platformDelete(const char *path)
{
int retval = 0;
Mar 10, 2012
Mar 10, 2012
795
LPWSTR wpath = NULL;
Jul 6, 2017
Jul 6, 2017
796
797
UTF8_TO_UNICODE_STACK(wpath, path);
BAIL_IF(!wpath, PHYSFS_ERR_OUT_OF_MEMORY, 0);
Mar 26, 2007
Mar 26, 2007
798
799
retval = doPlatformDelete(wpath);
__PHYSFS_smallFree(wpath);
Jan 28, 2010
Jan 28, 2010
800
return retval;
Jun 29, 2002
Jun 29, 2002
801
} /* __PHYSFS_platformDelete */
Apr 3, 2002
Apr 3, 2002
802
803
804
805
void *__PHYSFS_platformCreateMutex(void)
{
Mar 22, 2012
Mar 22, 2012
806
807
LPCRITICAL_SECTION lpcs;
lpcs = (LPCRITICAL_SECTION) allocator.Malloc(sizeof (CRITICAL_SECTION));
Jul 6, 2017
Jul 6, 2017
808
BAIL_IF(!lpcs, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 22, 2012
Mar 22, 2012
809
810
InitializeCriticalSection(lpcs);
return lpcs;
Jun 29, 2002
Jun 29, 2002
811
812
} /* __PHYSFS_platformCreateMutex */
Apr 3, 2002
Apr 3, 2002
813
814
815
void __PHYSFS_platformDestroyMutex(void *mutex)
{
Mar 22, 2012
Mar 22, 2012
816
817
DeleteCriticalSection((LPCRITICAL_SECTION) mutex);
allocator.Free(mutex);
Jun 29, 2002
Jun 29, 2002
818
819
} /* __PHYSFS_platformDestroyMutex */
Apr 3, 2002
Apr 3, 2002
820
821
822
int __PHYSFS_platformGrabMutex(void *mutex)
{
Mar 22, 2012
Mar 22, 2012
823
824
EnterCriticalSection((LPCRITICAL_SECTION) mutex);
return 1;
Jun 29, 2002
Jun 29, 2002
825
} /* __PHYSFS_platformGrabMutex */
Apr 3, 2002
Apr 3, 2002
826
827
828
829
void __PHYSFS_platformReleaseMutex(void *mutex)
{
Mar 22, 2012
Mar 22, 2012
830
LeaveCriticalSection((LPCRITICAL_SECTION) mutex);
Jun 29, 2002
Jun 29, 2002
831
832
} /* __PHYSFS_platformReleaseMutex */
Apr 3, 2002
Apr 3, 2002
833
Jun 29, 2002
Jun 29, 2002
834
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
Jun 10, 2002
Jun 10, 2002
835
836
837
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
Jun 29, 2002
Jun 29, 2002
838
839
840
TIME_ZONE_INFORMATION tzi;
DWORD tzid;
PHYSFS_sint64 retval;
Jun 10, 2002
Jun 10, 2002
841
struct tm tm;
Mar 10, 2012
Mar 10, 2012
842
BOOL rc;
Jun 10, 2002
Jun 10, 2002
843
Jul 6, 2017
Jul 6, 2017
844
BAIL_IF(!FileTimeToSystemTime(ft, &st_utc), errcodeFromWinApi(), -1);
Jun 29, 2002
Jun 29, 2002
845
tzid = GetTimeZoneInformation(&tzi);
Jul 6, 2017
Jul 6, 2017
846
BAIL_IF(tzid == TIME_ZONE_ID_INVALID, errcodeFromWinApi(), -1);
Mar 10, 2012
Mar 10, 2012
847
rc = SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz);
Jul 6, 2017
Jul 6, 2017
848
BAIL_IF(!rc, errcodeFromWinApi(), -1);
Jun 29, 2002
Jun 29, 2002
849
850
/* Convert to a format that mktime() can grok... */
Jun 10, 2002
Jun 10, 2002
851
852
853
854
855
856
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
857
tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
Jun 10, 2002
Jun 10, 2002
858
859
tm.tm_yday = -1;
tm.tm_isdst = -1;
Jun 29, 2002
Jun 29, 2002
860
861
862
/* Convert to a format PhysicsFS can grok... */
retval = (PHYSFS_sint64) mktime(&tm);
Jul 6, 2017
Jul 6, 2017
863
BAIL_IF(retval == -1, PHYSFS_ERR_OS_ERROR, -1);
Jan 28, 2010
Jan 28, 2010
864
return retval;
Jun 29, 2002
Jun 29, 2002
865
866
} /* FileTimeToPhysfsTime */
Nov 30, 2012
Nov 30, 2012
867
Jul 9, 2017
Jul 9, 2017
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/* 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. */
h = FindFirstFileW(wpath, &w32dw);
if (h == INVALID_HANDLE_VALUE)
return 0; /* ...maybe the file just vanished...? */
FindClose(h);
Jul 9, 2017
Jul 9, 2017
888
return (w32dw.dwReserved0 == PHYSFS_IO_REPARSE_TAG_SYMLINK);
Jul 9, 2017
Jul 9, 2017
889
890
891
} /* isSymlink */
Nov 30, 2012
Nov 30, 2012
892
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
Feb 15, 2010
Feb 15, 2010
893
894
895
{
WIN32_FILE_ATTRIBUTE_DATA winstat;
WCHAR *wstr = NULL;
Mar 10, 2012
Mar 10, 2012
896
DWORD err = 0;
Feb 15, 2010
Feb 15, 2010
897
BOOL rc = 0;
Jul 9, 2017
Jul 9, 2017
898
int issymlink = 0;
Feb 15, 2010
Feb 15, 2010
899
Jul 6, 2017
Jul 6, 2017
900
901
UTF8_TO_UNICODE_STACK(wstr, filename);
BAIL_IF(!wstr, PHYSFS_ERR_OUT_OF_MEMORY, 0);
Mar 10, 2012
Mar 10, 2012
902
rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
Jul 9, 2017
Jul 9, 2017
903
904
905
906
907
908
if (!rc)
err = GetLastError();
else /* check for symlink while wstr is still available */
issymlink = isSymlink(wstr, winstat.dwFileAttributes);
Feb 15, 2010
Feb 15, 2010
909
__PHYSFS_smallFree(wstr);
Jul 6, 2017
Jul 6, 2017
910
BAIL_IF(!rc, errcodeFromWinApiError(err), 0);
Feb 15, 2010
Feb 15, 2010
911
Nov 30, 2012
Nov 30, 2012
912
913
914
st->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
st->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
st->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
Feb 15, 2010
Feb 15, 2010
915
Jul 9, 2017
Jul 9, 2017
916
if (issymlink)
Feb 15, 2010
Feb 15, 2010
917
{
Jul 9, 2017
Jul 9, 2017
918
st->filetype = PHYSFS_FILETYPE_SYMLINK;
Nov 30, 2012
Nov 30, 2012
919
st->filesize = 0;
Feb 15, 2010
Feb 15, 2010
920
921
} /* if */
Jul 9, 2017
Jul 9, 2017
922
923
924
925
926
927
928
else if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
st->filetype = PHYSFS_FILETYPE_DIRECTORY;
st->filesize = 0;
} /* else if */
else if (winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE))
Feb 15, 2010
Feb 15, 2010
929
{
Nov 30, 2012
Nov 30, 2012
930
st->filetype = PHYSFS_FILETYPE_OTHER;
Feb 15, 2010
Feb 15, 2010
931
/* !!! FIXME: don't rely on this */
Nov 30, 2012
Nov 30, 2012
932
st->filesize = 0;
Feb 15, 2010
Feb 15, 2010
933
934
935
936
} /* else if */
else
{
Nov 30, 2012
Nov 30, 2012
937
938
st->filetype = PHYSFS_FILETYPE_REGULAR;
st->filesize = (((PHYSFS_uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow;
Feb 15, 2010
Feb 15, 2010
939
940
} /* else */
Nov 30, 2012
Nov 30, 2012
941
st->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
Feb 15, 2010
Feb 15, 2010
942
Aug 21, 2010
Aug 21, 2010
943
return 1;
Feb 15, 2010
Feb 15, 2010
944
945
} /* __PHYSFS_platformStat */
Mar 11, 2007
Mar 11, 2007
946
#endif /* PHYSFS_PLATFORM_WINDOWS */
Mar 29, 2015
Mar 29, 2015
947
#endif /* PHYSFS_PLATFORM_WINRT */
Jul 10, 2002
Jul 10, 2002
948
Mar 8, 2007
Mar 8, 2007
949
/* end of windows.c ... */