/
platform_windows.c
828 lines (662 loc) · 23.3 KB
1
/*
2
* Windows support routines for PhysicsFS.
3
*
4
* Please see the file LICENSE.txt in the source's root directory.
5
*
6
* This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
7
8
*/
9
10
11
12
#define __PHYSICSFS_INTERNAL__
#include "physfs_platforms.h"
#ifdef PHYSFS_PLATFORM_WINDOWS
13
14
/* Forcibly disable UNICODE macro, since we manage this ourselves. */
15
16
17
18
#ifdef UNICODE
#undef UNICODE
#endif
19
#define WIN32_LEAN_AND_MEAN 1
20
#include <windows.h>
21
#include <userenv.h>
22
#include <errno.h>
23
#include <ctype.h>
24
#include <time.h>
25
26
27
#include "physfs_internal.h"
28
29
#define LOWORDER_UINT64(pos) ((PHYSFS_uint32) (pos & 0xFFFFFFFF))
#define HIGHORDER_UINT64(pos) ((PHYSFS_uint32) ((pos >> 32) & 0xFFFFFFFF))
30
31
32
33
34
35
36
/*
* 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
37
38
39
/* just in case... */
#define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
40
41
42
43
44
/* Not defined before the Vista SDK. */
#define PHYSFS_IO_REPARSE_TAG_SYMLINK 0xA000000C
45
46
47
48
#define UTF8_TO_UNICODE_STACK_MACRO(w_assignto, str) { \
if (str == NULL) \
w_assignto = NULL; \
else { \
49
const PHYSFS_uint64 len = (PHYSFS_uint64) ((strlen(str) + 1) * 2); \
50
51
52
w_assignto = (WCHAR *) __PHYSFS_smallAlloc(len); \
if (w_assignto != NULL) \
PHYSFS_utf8ToUcs2(str, (PHYSFS_uint16 *) w_assignto, len); \
53
54
} \
} \
55
56
57
58
59
60
#ifndef _MSC_VER
#define _snprintf snprintf
#endif
/* !!! FIXME: this is wrong for UTF-16. */
61
62
63
64
65
static PHYSFS_uint64 wStrLen(const WCHAR *wstr)
{
PHYSFS_uint64 len = 0;
while (*(wstr++))
len++;
66
return len;
67
68
69
70
71
72
73
74
75
76
77
} /* 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);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
78
/* !!! FIXME: utf-16. */
79
80
81
82
83
PHYSFS_utf8FromUcs2((const PHYSFS_uint16 *) w_str, retval, len);
ptr = allocator.Realloc(retval, strlen(retval) + 1); /* shrink. */
if (ptr != NULL)
retval = (char *) ptr;
} /* if */
84
return retval;
85
86
} /* unicodeToUtf8Heap */
87
/* !!! FIXME: do we really need readonly? If not, do we need this struct? */
88
89
90
91
typedef struct
{
HANDLE handle;
int readonly;
92
} WinApiFile;
93
94
95
const char *__PHYSFS_platformDirSeparator = "\\";
96
static char *userDir = NULL;
97
static HANDLE libUserEnv = NULL;
98
99
100
/*
101
* Figure out what the last failing Windows API call was, and
102
103
104
105
106
* generate a human-readable string for the error message.
*
* The return value is a static buffer that is overwritten with
* each call to this function.
*/
107
static const char *winApiStrErrorByNum(const DWORD err)
108
{
109
110
111
static char utf8buf[255];
WCHAR msgbuf[255];
WCHAR *ptr;
112
113
114
DWORD rc = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
115
116
msgbuf, __PHYSFS_ARRAYLEN(msgbuf),
NULL);
117
118
119
120
if (rc == 0)
msgbuf[0] = '\0'; /* oh well. */
121
/* chop off newlines. */
122
123
124
125
for (ptr = msgbuf; *ptr; ptr++)
{
if ((*ptr == '\n') || (*ptr == '\r'))
{
126
*ptr = '\0';
127
128
129
130
break;
} /* if */
} /* for */
131
132
/* may truncate, but oh well. */
PHYSFS_utf8FromUcs2((PHYSFS_uint16 *) msgbuf, utf8buf, sizeof (utf8buf));
133
return ((const char *) utf8buf);
134
} /* winApiStrErrorByNum */
135
136
static inline const char *winApiStrError(void)
137
{
138
139
return winApiStrErrorByNum(GetLastError());
} /* winApiStrError */
140
141
/*
142
143
144
145
* On success, module-scope variable (userDir) will have a pointer to
* a malloc()'d string of the user's profile dir, and a non-zero value is
* returned. If we can't determine the profile dir, (userDir) will
* be NULL, and zero is returned.
146
*/
147
static int determineUserDir(void)
148
{
149
150
151
152
153
typedef BOOL (WINAPI *fnGetUserProfDirW)(HANDLE, LPWSTR, LPDWORD);
fnGetUserProfDirW pGetDir = NULL;
HANDLE accessToken = NULL; /* Security handle to process */
154
if (userDir != NULL)
155
return 1; /* already good to go. */
156
157
158
159
160
161
162
163
pGetDir = (fnGetUserProfDirW)
GetProcAddress(libUserEnv, "GetUserProfileDirectoryW");
BAIL_IF_MACRO(pGetDir == NULL, winApiStrError(), 0);
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &accessToken))
BAIL_MACRO(winApiStrError(), 0);
else
164
{
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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 */
wstr = (LPWSTR) __PHYSFS_smallAlloc(psize * sizeof (WCHAR));
if (wstr != NULL)
182
{
183
184
185
if (pGetDir(accessToken, wstr, &psize))
userDir = unicodeToUtf8Heap(wstr);
__PHYSFS_smallFree(wstr);
186
} /* if */
187
188
CloseHandle(accessToken);
189
190
} /* if */
191
return 1; /* We made it: hit the showers. */
192
} /* determineUserDir */
193
194
195
static BOOL mediaInDrive(const char *drive)
196
{
197
UINT oldErrorMode;
198
199
DWORD tmp;
BOOL retval;
200
201
/* Prevent windows warning message appearing when checking media size */
202
/* !!! FIXME: Windows 7 offers SetThreadErrorMode(). */
203
oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
204
205
/* If this function succeeds, there's media in the drive */
206
retval = GetVolumeInformationA(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0);
207
208
/* Revert back to old windows error handler */
209
210
SetErrorMode(oldErrorMode);
211
return retval;
212
} /* mediaInDrive */
213
214
215
216
217
/*
* !!! FIXME: move this to a thread? This function hangs if you call it while
* !!! FIXME: a drive is spinning up right after inserting a disc.
*/
218
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
219
{
220
221
/* !!! FIXME: Can CD drives be non-drive letter paths? */
/* !!! FIXME: (so can they be Unicode paths?) */
222
char drive_str[4] = "x:\\";
223
224
char ch;
for (ch = 'A'; ch <= 'Z'; ch++)
225
{
226
drive_str[0] = ch;
227
if (GetDriveTypeA(drive_str) == DRIVE_CDROM && mediaInDrive(drive_str))
228
cb(data, drive_str);
229
} /* for */
230
} /* __PHYSFS_platformDetectAvailableCDs */
231
232
233
234
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
235
236
237
DWORD buflen = 64;
LPWSTR modpath = NULL;
char *retval = NULL;
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
while (1)
{
DWORD rc;
void *ptr;
if ( (ptr = allocator.Realloc(modpath, buflen*sizeof(WCHAR))) == NULL )
{
allocator.Free(modpath);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
modpath = (LPWSTR) ptr;
rc = GetModuleFileNameW(NULL, modpath, buflen);
if (rc == 0)
{
allocator.Free(modpath);
BAIL_MACRO(winApiStrError(), NULL);
} /* 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 != '\\'))
__PHYSFS_setError(ERR_GETMODFN_NO_DIR);
else
{
*(ptr + 1) = '\0'; /* chop off filename. */
retval = unicodeToUtf8Heap(modpath);
} /* else */
} /* else */
allocator.Free(modpath);
return retval; /* w00t. */
288
289
290
291
292
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
293
DWORD bufsize = 0;
294
295
char *retval = NULL;
296
if (GetUserNameW(NULL, &bufsize) == 0) /* This SHOULD fail. */
297
{
298
299
LPWSTR wbuf = (LPWSTR) __PHYSFS_smallAlloc(bufsize * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
300
if (GetUserNameW(wbuf, &bufsize) == 0) /* ?! */
301
302
303
304
__PHYSFS_setError(winApiStrError());
else
retval = unicodeToUtf8Heap(wbuf);
__PHYSFS_smallFree(wbuf);
305
306
} /* if */
307
return retval;
308
309
310
311
312
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
313
char *retval = (char *) allocator.Malloc(strlen(userDir) + 1);
314
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
315
strcpy(retval, userDir); /* calculated at init time. */
316
return retval;
317
318
319
} /* __PHYSFS_platformGetUserDir */
320
void *__PHYSFS_platformGetThreadID(void)
321
{
322
return ( (void *) ((size_t) GetCurrentThreadId()) );
323
324
325
} /* __PHYSFS_platformGetThreadID */
326
static int isSymlinkAttrs(const DWORD attr, const DWORD tag)
327
{
328
329
return ( (attr & FILE_ATTRIBUTE_REPARSE_POINT) &&
(tag == PHYSFS_IO_REPARSE_TAG_SYMLINK) );
330
331
332
} /* isSymlinkAttrs */
333
334
335
336
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
{
337
const size_t len = ((prepend) ? strlen(prepend) : 0) +
338
339
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
340
char *retval = (char *) allocator.Malloc(len);
341
char *p;
342
343
344
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
345
346
_snprintf(retval, len, "%s%s%s",
prepend ? prepend : "", dirName, append ? append : "");
347
348
349
350
for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = '\\';
351
return retval;
352
353
354
} /* __PHYSFS_platformCvtToDependent */
355
356
void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
357
358
PHYSFS_EnumFilesCallback callback,
const char *origdir,
359
void *callbackdata)
360
{
361
362
HANDLE dir = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW entw;
363
size_t len = strlen(dirname);
364
365
char *searchPath = NULL;
WCHAR *wSearchPath = NULL;
366
367
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
368
369
searchPath = (char *) __PHYSFS_smallAlloc(len + 3);
if (searchPath == NULL)
370
return;
371
372
/* Copy current dirname */
373
strcpy(searchPath, dirname);
374
375
/* if there's no '\\' at the end of the path, stick one in there. */
376
if (searchPath[len - 1] != '\\')
377
{
378
379
searchPath[len++] = '\\';
searchPath[len] = '\0';
380
381
} /* if */
382
/* Append the "*" to the end of the string */
383
strcat(searchPath, "*");
384
385
386
387
UTF8_TO_UNICODE_STACK_MACRO(wSearchPath, searchPath);
if (wSearchPath == NULL)
return; /* oh well. */
388
389
dir = FindFirstFileW(wSearchPath, &entw);
390
391
392
393
394
__PHYSFS_smallFree(wSearchPath);
__PHYSFS_smallFree(searchPath);
if (dir == INVALID_HANDLE_VALUE)
return;
395
396
do
397
{
398
399
400
401
const DWORD attr = entw.dwFileAttributes;
const DWORD tag = entw.dwReserved0;
const WCHAR *fn = entw.cFileName;
char *utf8;
402
403
404
405
406
407
408
409
410
411
if ((fn[0] == '.') && (fn[1] == '\0'))
continue;
if ((fn[0] == '.') && (fn[1] == '.') && (fn[2] == '\0'))
continue;
if ((omitSymLinks) && (isSymlinkAttrs(attr, tag)))
continue;
utf8 = unicodeToUtf8Heap(fn);
if (utf8 != NULL)
412
{
413
414
415
416
callback(callbackdata, origdir, utf8);
allocator.Free(utf8);
} /* if */
} while (FindNextFileW(dir, &entw) != 0);
417
418
419
420
421
422
423
FindClose(dir);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
424
425
char *retval = NULL;
WCHAR *wbuf = NULL;
426
427
DWORD buflen = 0;
428
buflen = GetCurrentDirectoryW(buflen, NULL);
429
430
wbuf = (WCHAR *) __PHYSFS_smallAlloc((buflen + 2) * sizeof (WCHAR));
BAIL_IF_MACRO(wbuf == NULL, ERR_OUT_OF_MEMORY, NULL);
431
GetCurrentDirectoryW(buflen, wbuf);
432
433
if (wbuf[buflen - 2] == '\\')
434
wbuf[buflen - 1] = '\0'; /* just in case... */
435
436
437
438
439
else
{
wbuf[buflen - 1] = '\\';
wbuf[buflen] = '\0';
} /* else */
440
441
442
retval = unicodeToUtf8Heap(wbuf);
__PHYSFS_smallFree(wbuf);
443
return retval;
444
445
446
} /* __PHYSFS_platformCurrentDir */
447
/* this could probably use a cleanup. */
448
449
char *__PHYSFS_platformRealPath(const char *path)
{
450
451
452
453
454
/*
* At this point, we only use this for the user and base dir,
* and we already know those are RealPath'd by the OS for us.
*/
char *retval = (char *) allocator.Malloc(strlen(path) + 1);
455
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
456
strcpy(retval, path);
457
return retval;
458
459
460
461
462
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
463
464
465
WCHAR *wpath;
DWORD rc;
UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
466
rc = CreateDirectoryW(wpath, NULL);
467
468
__PHYSFS_smallFree(wpath);
BAIL_IF_MACRO(rc == 0, winApiStrError(), 0);
469
return 1;
470
471
} /* __PHYSFS_platformMkDir */
472
473
474
int __PHYSFS_platformInit(void)
{
475
476
477
478
libUserEnv = LoadLibraryA("userenv.dll");
BAIL_IF_MACRO(libUserEnv == NULL, winApiStrError(), 0);
/* !!! FIXME: why do we precalculate this? */
479
BAIL_IF_MACRO(!determineUserDir(), NULL, 0);
480
return 1; /* It's all good */
481
} /* __PHYSFS_platformInit */
482
483
484
485
int __PHYSFS_platformDeinit(void)
{
486
487
488
if (libUserEnv)
FreeLibrary(libUserEnv);
libUserEnv = NULL;
489
490
allocator.Free(userDir);
userDir = NULL;
491
return 1; /* It's all good */
492
493
494
495
496
} /* __PHYSFS_platformDeinit */
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
{
497
HANDLE fileh;
498
499
WinApiFile *retval;
WCHAR *wfname;
500
501
502
UTF8_TO_UNICODE_STACK_MACRO(wfname, fname);
BAIL_IF_MACRO(wfname == NULL, ERR_OUT_OF_MEMORY, NULL);
503
504
fileh = CreateFileW(wfname, mode, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
505
__PHYSFS_smallFree(wfname);
506
507
BAIL_IF_MACRO(fileh == INVALID_HANDLE_VALUE,winApiStrError(), NULL);
508
509
retval = (WinApiFile *) allocator.Malloc(sizeof (WinApiFile));
510
if (retval == NULL)
511
{
512
CloseHandle(fileh);
513
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
514
} /* if */
515
516
retval->readonly = rdonly;
517
retval->handle = fileh;
518
return retval;
519
520
} /* doOpen */
521
522
523
void *__PHYSFS_platformOpenRead(const char *filename)
{
524
return doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1);
525
} /* __PHYSFS_platformOpenRead */
526
527
528
529
void *__PHYSFS_platformOpenWrite(const char *filename)
{
530
return doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0);
531
} /* __PHYSFS_platformOpenWrite */
532
533
534
535
void *__PHYSFS_platformOpenAppend(const char *filename)
{
536
537
538
void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
if (retval != NULL)
{
539
HANDLE h = ((WinApiFile *) retval)->handle;
540
541
DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
542
{
543
const char *err = winApiStrError();
544
CloseHandle(h);
545
allocator.Free(retval);
546
547
548
549
BAIL_MACRO(err, NULL);
} /* if */
} /* if */
550
return retval;
551
} /* __PHYSFS_platformOpenAppend */
552
553
554
/* !!! FIXME: this function fails if len > 0xFFFFFFFF. */
555
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buf, PHYSFS_uint64 len)
556
{
557
HANDLE Handle = ((WinApiFile *) opaque)->handle;
558
DWORD CountOfBytesRead = 0;
559
560
BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
561
562
563
564
if(!ReadFile(Handle, buf, (DWORD) len, &CountOfBytesRead, NULL))
BAIL_MACRO(winApiStrError(), -1);
return (PHYSFS_sint64) CountOfBytesRead;
565
566
} /* __PHYSFS_platformRead */
567
568
/* !!! FIXME: this function fails if len > 0xFFFFFFFF. */
569
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
570
PHYSFS_uint64 len)
571
{
572
HANDLE Handle = ((WinApiFile *) opaque)->handle;
573
DWORD CountOfBytesWritten = 0;
574
575
BAIL_IF_MACRO(!__PHYSFS_ui64FitsAddressSpace(len),ERR_INVALID_ARGUMENT,-1);
576
577
578
579
if(!WriteFile(Handle, buffer, (DWORD) len, &CountOfBytesWritten, NULL))
BAIL_MACRO(winApiStrError(), -1);
return (PHYSFS_sint64) CountOfBytesWritten;
580
581
} /* __PHYSFS_platformWrite */
582
583
584
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
585
HANDLE Handle = ((WinApiFile *) opaque)->handle;
586
587
LONG HighOrderPos;
PLONG pHighOrderPos;
588
DWORD rc;
589
590
591
592
/* Get the high order 32-bits of the position */
HighOrderPos = HIGHORDER_UINT64(pos);
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
* MSDN: "If you do not need the high-order 32 bits, this
* pointer must be set to NULL."
*/
pHighOrderPos = (HighOrderPos) ? &HighOrderPos : NULL;
/*
* !!! FIXME: MSDN: "Windows Me/98/95: If the pointer
* !!! FIXME: lpDistanceToMoveHigh is not NULL, then it must
* !!! FIXME: point to either 0, INVALID_SET_FILE_POINTER, or
* !!! FIXME: the sign extension of the value of lDistanceToMove.
* !!! FIXME: Any other value will be rejected."
*/
607
/* Move pointer "pos" count from start of file */
608
rc = SetFilePointer(Handle, LOWORDER_UINT64(pos),
609
pHighOrderPos, FILE_BEGIN);
610
611
612
613
if ( (rc == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
{
614
BAIL_MACRO(winApiStrError(), 0);
615
616
} /* if */
617
return 1; /* No error occured */
618
} /* __PHYSFS_platformSeek */
619
620
621
622
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
623
HANDLE Handle = ((WinApiFile *) opaque)->handle;
624
LONG HighPos = 0;
625
DWORD LowPos;
626
627
628
PHYSFS_sint64 retval;
/* Get current position */
629
LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
630
631
if ( (LowPos == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
632
{
633
BAIL_MACRO(winApiStrError(), -1);
634
} /* if */
635
636
637
else
{
/* Combine the high/low order to create the 64-bit position value */
638
639
640
641
retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
assert(retval >= 0);
} /* else */
642
return retval;
643
} /* __PHYSFS_platformTell */
644
645
646
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
647
{
648
HANDLE Handle = ((WinApiFile *) opaque)->handle;
649
650
DWORD SizeHigh;
DWORD SizeLow;
651
652
PHYSFS_sint64 retval;
653
SizeLow = GetFileSize(Handle, &SizeHigh);
654
655
if ( (SizeLow == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
656
{
657
BAIL_MACRO(winApiStrError(), -1);
658
} /* if */
659
660
661
else
{
/* Combine the high/low order to create the 64-bit position value */
662
663
retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
assert(retval >= 0);
664
} /* else */
665
666
return retval;
667
668
} /* __PHYSFS_platformFileLength */
669
670
671
int __PHYSFS_platformFlush(void *opaque)
{
672
WinApiFile *fh = ((WinApiFile *) opaque);
673
if (!fh->readonly)
674
BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), winApiStrError(), 0);
675
676
return 1;
677
} /* __PHYSFS_platformFlush */
678
679
680
void __PHYSFS_platformClose(void *opaque)
681
{
682
HANDLE Handle = ((WinApiFile *) opaque)->handle;
683
(void) CloseHandle(Handle); /* ignore errors. You should have flushed! */
684
allocator.Free(opaque);
685
} /* __PHYSFS_platformClose */
686
687
688
static int doPlatformDelete(LPWSTR wpath)
689
{
690
691
692
const int isdir = (GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY);
const BOOL rc = (isdir) ? RemoveDirectoryW(wpath) : DeleteFileW(wpath);
BAIL_IF_MACRO(!rc, winApiStrError(), 0);
693
return 1; /* if you made it here, it worked. */
694
695
696
697
698
699
} /* doPlatformDelete */
int __PHYSFS_platformDelete(const char *path)
{
int retval = 0;
700
LPWSTR wpath = NULL;
701
702
703
704
UTF8_TO_UNICODE_STACK_MACRO(wpath, path);
BAIL_IF_MACRO(wpath == NULL, ERR_OUT_OF_MEMORY, 0);
retval = doPlatformDelete(wpath);
__PHYSFS_smallFree(wpath);
705
return retval;
706
} /* __PHYSFS_platformDelete */
707
708
709
710
711
712
713
/*
* !!! FIXME: why aren't we using Critical Sections instead of Mutexes?
* !!! FIXME: mutexes on Windows are for cross-process sync. CritSects are
* !!! FIXME: mutexes for threads in a single process and are faster.
*/
714
715
void *__PHYSFS_platformCreateMutex(void)
{
716
return ((void *) CreateMutex(NULL, FALSE, NULL));
717
718
} /* __PHYSFS_platformCreateMutex */
719
720
721
void __PHYSFS_platformDestroyMutex(void *mutex)
{
722
723
724
CloseHandle((HANDLE) mutex);
} /* __PHYSFS_platformDestroyMutex */
725
726
727
int __PHYSFS_platformGrabMutex(void *mutex)
{
728
return (WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
729
} /* __PHYSFS_platformGrabMutex */
730
731
732
733
void __PHYSFS_platformReleaseMutex(void *mutex)
{
734
735
736
ReleaseMutex((HANDLE) mutex);
} /* __PHYSFS_platformReleaseMutex */
737
738
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
739
740
741
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
742
743
744
TIME_ZONE_INFORMATION tzi;
DWORD tzid;
PHYSFS_sint64 retval;
745
struct tm tm;
746
BOOL rc;
747
748
BAIL_IF_MACRO(!FileTimeToSystemTime(ft, &st_utc), winApiStrError(), -1);
749
tzid = GetTimeZoneInformation(&tzi);
750
BAIL_IF_MACRO(tzid == TIME_ZONE_ID_INVALID, winApiStrError(), -1);
751
752
rc = SystemTimeToTzSpecificLocalTime(&tzi, &st_utc, &st_localtz);
BAIL_IF_MACRO(!rc, winApiStrError(), -1);
753
754
/* Convert to a format that mktime() can grok... */
755
756
757
758
759
760
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;
761
tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/;
762
763
tm.tm_yday = -1;
tm.tm_isdst = -1;
764
765
766
767
/* Convert to a format PhysicsFS can grok... */
retval = (PHYSFS_sint64) mktime(&tm);
BAIL_IF_MACRO(retval == -1, strerror(errno), -1);
768
return retval;
769
770
} /* FileTimeToPhysfsTime */
771
int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *stat)
772
773
774
{
WIN32_FILE_ATTRIBUTE_DATA winstat;
WCHAR *wstr = NULL;
775
DWORD err = 0;
776
777
778
BOOL rc = 0;
UTF8_TO_UNICODE_STACK_MACRO(wstr, filename);
779
780
781
782
BAIL_IF_MACRO(wstr == NULL, ERR_OUT_OF_MEMORY, 0);
rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
err = (!rc) ? GetLastError() : 0;
*exists = ((err != ERROR_FILE_NOT_FOUND) && (err != ERROR_PATH_NOT_FOUND));
783
__PHYSFS_smallFree(wstr);
784
BAIL_IF_MACRO(!rc, winApiStrErrorByNum(err), 0);
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
stat->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
stat->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
stat->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
if(winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
} /* if */
else if(winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE))
{
/* !!! FIXME: what are reparse points? */
stat->filetype = PHYSFS_FILETYPE_OTHER;
/* !!! FIXME: don't rely on this */
stat->filesize = 0;
} /* else if */
/* !!! FIXME: check for symlinks on Vista. */
else
{
stat->filetype = PHYSFS_FILETYPE_REGULAR;
809
stat->filesize = (((PHYSFS_uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow;
810
811
812
813
} /* else */
stat->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
814
return 1;
815
816
817
} /* __PHYSFS_platformStat */
818
/* !!! FIXME: Don't use C runtime for allocators? */
819
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a)
820
{
821
return 0; /* just use malloc() and friends. */
822
} /* __PHYSFS_platformSetDefaultAllocator */
823
824
#endif /* PHYSFS_PLATFORM_WINDOWS */
825
826
/* end of windows.c ... */