/
win32.c
1151 lines (927 loc) · 31.5 KB
1
/*
2
* Win32 support routines for PhysicsFS.
3
4
5
*
* Please see the file LICENSE in the source's root directory.
*
6
* This file written by Ryan C. Gordon, and made sane by Gregory S. Read.
7
8
*/
9
10
11
12
#if HAVE_CONFIG_H
# include <config.h>
#endif
13
14
#ifdef WIN32
15
16
#include <windows.h>
#include <stdio.h>
17
#include <stdlib.h>
18
19
#include <string.h>
#include <errno.h>
20
#include <ctype.h>
21
#include <time.h>
22
23
24
25
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
26
27
#if (defined _MSC_VER)
#define alloca(x) _alloca(x)
28
#elif (defined __MINGW32__) /* scary...hopefully this is okay. */
29
#define alloca(x) __builtin_alloca(x)
30
31
#endif
32
33
34
35
#define LOWORDER_UINT64(pos) (PHYSFS_uint32) \
(pos & 0x00000000FFFFFFFF)
#define HIGHORDER_UINT64(pos) (PHYSFS_uint32) \
(((pos & 0xFFFFFFFF00000000) >> 32) & 0x00000000FFFFFFFF)
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* GetUserProfileDirectory() is only available on >= NT4 (no 9x/ME systems!) */
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
HANDLE hToken,
LPTSTR lpProfileDir,
LPDWORD lpcchSize);
/* GetFileAttributesEx() is only available on >= Win98 or WinNT4 ... */
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETFILEATTRIBUTESEX) (
LPCTSTR lpFileName,
GET_FILEEX_INFO_LEVELS fInfoLevelId,
LPVOID lpFileInformation);
typedef struct
{
HANDLE handle;
int readonly;
} win32file;
54
55
56
57
58
const char *__PHYSFS_platformDirSeparator = "\\";
static LPFNGETFILEATTRIBUTESEX pGetFileAttributesEx = NULL;
static HANDLE libKernel32 = NULL;
static char *userDir = NULL;
59
60
61
62
63
64
/*
* 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
*/
65
#define PHYSFS_INVALID_SET_FILE_POINTER 0xFFFFFFFF
66
67
/* just in case... */
68
69
#define PHYSFS_INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Figure out what the last failing Win32 API call was, and
* 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.
*/
static const char *win32strerror(void)
{
static TCHAR msgbuf[255];
82
TCHAR *ptr = msgbuf;
83
84
85
86
87
88
89
90
91
92
93
94
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
msgbuf,
sizeof (msgbuf) / sizeof (TCHAR),
NULL
);
95
96
97
98
99
100
101
102
103
104
/* chop off newlines. */
for (ptr = msgbuf; *ptr; ptr++)
{
if ((*ptr == '\n') || (*ptr == '\r'))
{
*ptr = ' ';
break;
} /* if */
} /* for */
105
106
107
108
return((const char *) msgbuf);
} /* win32strerror */
109
static char *getExePath(const char *argv0)
110
{
111
112
113
DWORD buflen;
int success = 0;
char *ptr = NULL;
114
char *retval = (char *) allocator.Malloc(sizeof (TCHAR) * (MAX_PATH + 1));
115
116
117
118
119
120
121
122
123
124
125
126
127
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
retval[0] = '\0';
buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
if (buflen <= 0)
__PHYSFS_setError(win32strerror());
else
{
retval[buflen] = '\0'; /* does API always null-terminate this? */
/* make sure the string was not truncated. */
if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") != 0)
128
__PHYSFS_setError(ERR_GETMODFN_TRUNC);
129
130
131
132
else
{
ptr = strrchr(retval, '\\');
if (ptr == NULL)
133
__PHYSFS_setError(ERR_GETMODFN_NO_DIR);
134
135
136
137
138
139
140
141
142
143
144
145
146
else
{
*(ptr + 1) = '\0'; /* chop off filename. */
success = 1;
} /* else */
} /* else */
} /* else */
/* if any part of the previous approach failed, try SearchPath()... */
if (!success)
{
if (argv0 == NULL)
147
__PHYSFS_setError(ERR_ARGV0_IS_NULL);
148
149
150
151
152
153
else
{
buflen = SearchPath(NULL, argv0, NULL, MAX_PATH+1, retval, &ptr);
if (buflen == 0)
__PHYSFS_setError(win32strerror());
else if (buflen > MAX_PATH)
154
__PHYSFS_setError(ERR_SEARCHPATH_TRUNC);
155
156
157
158
159
160
161
else
success = 1;
} /* else */
} /* if */
if (!success)
{
162
allocator.Free(retval);
163
164
165
166
return(NULL); /* physfs error message will be set, above. */
} /* if */
/* free up the bytes we didn't actually use. */
167
ptr = (char *) allocator.Realloc(retval, strlen(retval) + 1);
168
169
170
171
172
if (ptr != NULL)
retval = ptr;
return(retval); /* w00t. */
} /* getExePath */
173
174
175
/*
176
177
178
* Try to make use of GetUserProfileDirectory(), which isn't available on
* some common variants of Win32. If we can't use this, we just punt and
* use the physfs base dir for the user dir, too.
179
*
180
181
182
183
* 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.
184
*/
185
static int determineUserDir(void)
186
{
187
DWORD psize = 0;
188
189
char dummy[1];
BOOL rc = 0;
190
191
192
193
HANDLE processHandle; /* Current process handle */
HANDLE accessToken = NULL; /* Security handle to process */
LPFNGETUSERPROFILEDIR GetUserProfileDirectory;
HMODULE lib;
194
195
assert(userDir == NULL);
196
197
198
199
200
201
202
/*
* GetUserProfileDirectory() is only available on NT 4.0 and later.
* This means Win95/98/ME (and CE?) users have to do without, so for
* them, we'll default to the base directory when we can't get the
* function pointer.
*/
203
204
205
206
207
208
lib = LoadLibrary("userenv.dll");
if (lib)
{
/* !!! FIXME: Handle Unicode? */
GetUserProfileDirectory = (LPFNGETUSERPROFILEDIR)
209
GetProcAddress(lib, "GetUserProfileDirectoryA");
210
211
212
213
214
215
216
217
218
219
220
221
222
223
if (GetUserProfileDirectory)
{
processHandle = GetCurrentProcess();
if (OpenProcessToken(processHandle, TOKEN_QUERY, &accessToken))
{
/*
* 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 = GetUserProfileDirectory(accessToken, dummy, &psize);
assert(!rc); /* success?! */
/* Allocate memory for the profile directory */
224
userDir = (char *) allocator.Malloc(psize);
225
226
227
228
if (userDir != NULL)
{
if (!GetUserProfileDirectory(accessToken, userDir, &psize))
{
229
allocator.Free(userDir);
230
231
232
233
userDir = NULL;
} /* if */
} /* else */
} /* if */
234
235
236
CloseHandle(accessToken);
} /* if */
237
238
FreeLibrary(lib);
239
240
} /* if */
241
if (userDir == NULL) /* couldn't get profile for some reason. */
242
{
243
244
245
/* Might just be a non-NT system; resort to the basedir. */
userDir = getExePath(NULL);
BAIL_IF_MACRO(userDir == NULL, NULL, 0); /* STILL failed?! */
246
} /* if */
247
248
249
return(1); /* We made it: hit the showers. */
} /* determineUserDir */
250
251
252
static BOOL mediaInDrive(const char *drive)
253
{
254
UINT oldErrorMode;
255
256
DWORD tmp;
BOOL retval;
257
258
/* Prevent windows warning message appearing when checking media size */
259
oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
260
261
/* If this function succeeds, there's media in the drive */
262
retval = GetVolumeInformation(drive, NULL, 0, NULL, NULL, &tmp, NULL, 0);
263
264
/* Revert back to old windows error handler */
265
266
SetErrorMode(oldErrorMode);
267
return(retval);
268
} /* mediaInDrive */
269
270
271
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data)
272
273
{
char drive_str[4] = "x:\\";
274
275
char ch;
for (ch = 'A'; ch <= 'Z'; ch++)
276
{
277
drive_str[0] = ch;
278
if (GetDriveType(drive_str) == DRIVE_CDROM && mediaInDrive(drive_str))
279
cb(data, drive_str);
280
} /* for */
281
} /* __PHYSFS_platformDetectAvailableCDs */
282
283
284
285
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
286
287
if ((argv0 != NULL) && (strchr(argv0, '\\') != NULL))
return(NULL); /* default behaviour can handle this. */
288
289
return(getExePath(argv0));
290
291
292
293
294
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
295
DWORD bufsize = 0;
296
297
298
299
LPTSTR retval = NULL;
if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
{
300
retval = (LPTSTR) allocator.Malloc(bufsize);
301
302
303
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (GetUserName(retval, &bufsize) == 0) /* ?! */
{
304
__PHYSFS_setError(win32strerror());
305
allocator.Free(retval);
306
307
308
309
310
311
312
313
314
315
retval = NULL;
} /* if */
} /* if */
return((char *) retval);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
316
char *retval = (char *) allocator.Malloc(strlen(userDir) + 1);
317
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
318
319
strcpy(retval, userDir); /* calculated at init time. */
return(retval);
320
321
322
} /* __PHYSFS_platformGetUserDir */
323
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
324
{
325
return((PHYSFS_uint64) GetCurrentThreadId());
326
327
328
} /* __PHYSFS_platformGetThreadID */
329
/* ...make this Cygwin AND Visual C friendly... */
330
331
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
332
333
334
#if (defined _MSC_VER)
return(stricmp(x, y));
#else
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
int ux, uy;
do
{
ux = toupper((int) *x);
uy = toupper((int) *y);
if (ux > uy)
return(1);
else if (ux < uy)
return(-1);
x++;
y++;
} while ((ux) && (uy));
return(0);
350
#endif
351
352
353
} /* __PHYSFS_platformStricmp */
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
int __PHYSFS_platformStrnicmp(const char *x, const char *y, PHYSFS_uint32 len)
{
#if (defined _MSC_VER)
return(strnicmp(x, y, (int) len));
#else
int ux, uy;
if (!len)
return(0);
do
{
ux = toupper((int) *x);
uy = toupper((int) *y);
if (ux > uy)
return(1);
else if (ux < uy)
return(-1);
x++;
y++;
len--;
} while ((ux) && (uy) && (len));
return(0);
#endif
} /* __PHYSFS_platformStricmp */
382
383
int __PHYSFS_platformExists(const char *fname)
{
384
385
386
387
388
BAIL_IF_MACRO
(
GetFileAttributes(fname) == PHYSFS_INVALID_FILE_ATTRIBUTES,
win32strerror(), 0
);
389
return(1);
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
} /* __PHYSFS_platformExists */
int __PHYSFS_platformIsSymLink(const char *fname)
{
return(0); /* no symlinks on win32. */
} /* __PHYSFS_platformIsSymlink */
int __PHYSFS_platformIsDirectory(const char *fname)
{
return((GetFileAttributes(fname) & FILE_ATTRIBUTE_DIRECTORY) != 0);
} /* __PHYSFS_platformIsDirectory */
char *__PHYSFS_platformCvtToDependent(const char *prepend,
const char *dirName,
const char *append)
{
int len = ((prepend) ? strlen(prepend) : 0) +
((append) ? strlen(append) : 0) +
strlen(dirName) + 1;
412
char *retval = (char *) allocator.Malloc(len);
413
char *p;
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (prepend)
strcpy(retval, prepend);
else
retval[0] = '\0';
strcat(retval, dirName);
if (append)
strcat(retval, append);
for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = '\\';
return(retval);
} /* __PHYSFS_platformCvtToDependent */
/* Much like my college days, try to sleep for 10 milliseconds at a time... */
void __PHYSFS_platformTimeslice(void)
{
Sleep(10);
} /* __PHYSFS_platformTimeslice */
441
442
void __PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks,
443
444
PHYSFS_EnumFilesCallback callback,
const char *origdir,
445
void *callbackdata)
446
447
448
{
HANDLE dir;
WIN32_FIND_DATA ent;
449
size_t len = strlen(dirname);
450
char *SearchPath;
451
452
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
453
SearchPath = (char *) alloca(len + 3);
454
455
if (SearchPath == NULL)
return;
456
457
458
/* Copy current dirname */
strcpy(SearchPath, dirname);
459
460
/* if there's no '\\' at the end of the path, stick one in there. */
461
if (SearchPath[len - 1] != '\\')
462
{
463
464
SearchPath[len++] = '\\';
SearchPath[len] = '\0';
465
466
} /* if */
467
468
/* Append the "*" to the end of the string */
strcat(SearchPath, "*");
469
470
dir = FindFirstFile(SearchPath, &ent);
471
472
if (dir == INVALID_HANDLE_VALUE)
return;
473
474
do
475
476
477
478
479
480
481
{
if (strcmp(ent.cFileName, ".") == 0)
continue;
if (strcmp(ent.cFileName, "..") == 0)
continue;
482
callback(callbackdata, origdir, ent.cFileName);
483
} while (FindNextFile(dir, &ent) != 0);
484
485
486
487
488
489
490
FindClose(dir);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
491
LPTSTR retval;
492
493
DWORD buflen = 0;
494
buflen = GetCurrentDirectory(buflen, NULL);
495
retval = (LPTSTR) allocator.Malloc(sizeof (TCHAR) * (buflen + 2));
496
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
497
GetCurrentDirectory(buflen, retval);
498
499
500
501
if (retval[buflen - 2] != '\\')
strcat(retval, "\\");
502
503
504
505
return((char *) retval);
} /* __PHYSFS_platformCurrentDir */
506
/* this could probably use a cleanup. */
507
508
char *__PHYSFS_platformRealPath(const char *path)
{
509
510
511
512
513
514
char *retval = NULL;
char *p = NULL;
BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
515
retval = (char *) allocator.Malloc(MAX_PATH);
516
517
518
519
520
521
522
523
524
525
526
527
528
529
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/*
* If in \\server\path format, it's already an absolute path.
* We'll need to check for "." and ".." dirs, though, just in case.
*/
if ((path[0] == '\\') && (path[1] == '\\'))
strcpy(retval, path);
else
{
char *currentDir = __PHYSFS_platformCurrentDir();
if (currentDir == NULL)
{
530
allocator.Free(retval);
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
} /* if */
if (path[1] == ':') /* drive letter specified? */
{
/*
* Apparently, "D:mypath" is the same as "D:\\mypath" if
* D: is not the current drive. However, if D: is the
* current drive, then "D:mypath" is a relative path. Ugh.
*/
if (path[2] == '\\') /* maybe an absolute path? */
strcpy(retval, path);
else /* definitely an absolute path. */
{
if (path[0] == currentDir[0]) /* current drive; relative. */
{
strcpy(retval, currentDir);
strcat(retval, path + 2);
} /* if */
else /* not current drive; absolute. */
{
retval[0] = path[0];
retval[1] = ':';
retval[2] = '\\';
strcpy(retval + 3, path + 2);
} /* else */
} /* else */
} /* if */
else /* no drive letter specified. */
{
if (path[0] == '\\') /* absolute path. */
{
retval[0] = currentDir[0];
retval[1] = ':';
strcpy(retval + 2, path);
} /* if */
else
{
strcpy(retval, currentDir);
strcat(retval, path);
} /* else */
} /* else */
576
allocator.Free(currentDir);
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
} /* else */
/* (whew.) Ok, now take out "." and ".." path entries... */
p = retval;
while ( (p = strstr(p, "\\.")) != NULL)
{
/* it's a "." entry that doesn't end the string. */
if (p[2] == '\\')
memmove(p + 1, p + 3, strlen(p + 3) + 1);
/* it's a "." entry that ends the string. */
else if (p[2] == '\0')
p[0] = '\0';
/* it's a ".." entry. */
else if (p[2] == '.')
{
char *prevEntry = p - 1;
while ((prevEntry != retval) && (*prevEntry != '\\'))
prevEntry--;
if (prevEntry == retval) /* make it look like a "." entry. */
memmove(p + 1, p + 2, strlen(p + 2) + 1);
else
{
if (p[3] != '\0') /* doesn't end string. */
*prevEntry = '\0';
else /* ends string. */
memmove(prevEntry + 1, p + 4, strlen(p + 4) + 1);
p = prevEntry;
} /* else */
} /* else if */
else
{
p++; /* look past current char. */
} /* else */
} /* while */
/* shrink the retval's memory block if possible... */
619
p = (char *) allocator.Realloc(retval, strlen(retval) + 1);
620
621
622
623
if (p != NULL)
retval = p;
return(retval);
624
625
626
627
628
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
629
DWORD rc = CreateDirectory(path, NULL);
630
631
632
633
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
return(1);
} /* __PHYSFS_platformMkDir */
634
635
636
/*
* Get OS info and save the important parts.
637
638
639
*
* Returns non-zero if successful, otherwise it returns zero on failure.
*/
640
static int getOSInfo(void)
641
{
642
643
#if 0 /* we don't actually use this at the moment, but may in the future. */
OSVERSIONINFO OSVersionInfo; /* Information about the OS */
644
OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
645
BAIL_IF_MACRO(!GetVersionEx(&OSVersionInfo), win32strerror(), 0);
646
647
/* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
648
649
650
runningNT = ((OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(OSVersionInfo.dwMajorVersion >= 4));
#endif
651
652
653
return(1);
} /* getOSInfo */
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
* Some things we want/need are in external DLLs that may or may not be
* available, based on the operating system, etc. This function loads those
* libraries and hunts down the needed pointers.
*
* Libraries that are one-shot deals, or better loaded as needed, are loaded
* elsewhere (see determineUserDir()).
*
* Returns zero if a needed library couldn't load, non-zero if we have enough
* to go on (which means some useful but non-crucial libraries may _NOT_ be
* loaded; check the related module-scope variables).
*/
static int loadLibraries(void)
669
{
670
671
/* If this get unwieldy, make it table driven. */
672
int allNeededLibrariesLoaded = 1; /* flip to zero as needed. */
673
674
675
libKernel32 = LoadLibrary("kernel32.dll");
if (libKernel32)
676
{
677
678
pGetFileAttributesEx = (LPFNGETFILEATTRIBUTESEX)
GetProcAddress(libKernel32, "GetFileAttributesExA");
679
} /* if */
680
681
682
683
684
685
/* add other DLLs here... */
/* see if there's any reason to keep kernel32.dll around... */
if (libKernel32)
686
{
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
if ((pGetFileAttributesEx == NULL) /* && (somethingElse == NULL) */ )
{
FreeLibrary(libKernel32);
libKernel32 = NULL;
} /* if */
} /* if */
return(allNeededLibrariesLoaded);
} /* loadLibraries */
int __PHYSFS_platformInit(void)
{
BAIL_IF_MACRO(!getOSInfo(), NULL, 0);
BAIL_IF_MACRO(!loadLibraries(), NULL, 0);
BAIL_IF_MACRO(!determineUserDir(), NULL, 0);
return(1); /* It's all good */
} /* __PHYSFS_platformInit */
706
707
708
709
int __PHYSFS_platformDeinit(void)
{
710
if (userDir != NULL)
711
{
712
allocator.Free(userDir);
713
userDir = NULL;
714
} /* if */
715
716
717
718
719
720
721
if (libKernel32)
{
FreeLibrary(libKernel32);
libKernel32 = NULL;
} /* if */
722
723
724
725
726
727
728
729
730
731
732
733
return(1); /* It's all good */
} /* __PHYSFS_platformDeinit */
static void *doOpen(const char *fname, DWORD mode, DWORD creation, int rdonly)
{
HANDLE fileHandle;
win32file *retval;
fileHandle = CreateFile(fname, mode, FILE_SHARE_READ, NULL,
creation, FILE_ATTRIBUTE_NORMAL, NULL);
734
735
BAIL_IF_MACRO
(
736
fileHandle == INVALID_HANDLE_VALUE,
737
738
win32strerror(), NULL
);
739
740
retval = (win32file *) allocator.Malloc(sizeof (win32file));
741
if (retval == NULL)
742
{
743
744
CloseHandle(fileHandle);
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
745
} /* if */
746
747
748
749
750
751
retval->readonly = rdonly;
retval->handle = fileHandle;
return(retval);
} /* doOpen */
752
753
754
void *__PHYSFS_platformOpenRead(const char *filename)
{
755
756
return(doOpen(filename, GENERIC_READ, OPEN_EXISTING, 1));
} /* __PHYSFS_platformOpenRead */
757
758
759
760
void *__PHYSFS_platformOpenWrite(const char *filename)
{
761
762
return(doOpen(filename, GENERIC_WRITE, CREATE_ALWAYS, 0));
} /* __PHYSFS_platformOpenWrite */
763
764
765
766
void *__PHYSFS_platformOpenAppend(const char *filename)
{
767
768
769
770
void *retval = doOpen(filename, GENERIC_WRITE, OPEN_ALWAYS, 0);
if (retval != NULL)
{
HANDLE h = ((win32file *) retval)->handle;
771
772
DWORD rc = SetFilePointer(h, 0, NULL, FILE_END);
if (rc == PHYSFS_INVALID_SET_FILE_POINTER)
773
774
775
{
const char *err = win32strerror();
CloseHandle(h);
776
allocator.Free(retval);
777
778
779
780
781
782
BAIL_MACRO(err, NULL);
} /* if */
} /* if */
return(retval);
} /* __PHYSFS_platformOpenAppend */
783
784
785
786
787
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
788
HANDLE Handle = ((win32file *) opaque)->handle;
789
790
791
792
DWORD CountOfBytesRead;
PHYSFS_sint64 retval;
/* Read data from the file */
793
/* !!! FIXME: uint32 might be a greater # than DWORD */
794
if(!ReadFile(Handle, buffer, count * size, &CountOfBytesRead, NULL))
795
{
796
797
BAIL_MACRO(win32strerror(), -1);
} /* if */
798
799
800
else
{
/* Return the number of "objects" read. */
801
/* !!! FIXME: What if not the right amount of bytes was read to make an object? */
802
retval = CountOfBytesRead / size;
803
} /* else */
804
805
806
807
return(retval);
} /* __PHYSFS_platformRead */
808
809
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
810
811
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
812
HANDLE Handle = ((win32file *) opaque)->handle;
813
814
815
816
DWORD CountOfBytesWritten;
PHYSFS_sint64 retval;
/* Read data from the file */
817
/* !!! FIXME: uint32 might be a greater # than DWORD */
818
if(!WriteFile(Handle, buffer, count * size, &CountOfBytesWritten, NULL))
819
{
820
821
BAIL_MACRO(win32strerror(), -1);
} /* if */
822
823
824
else
{
/* Return the number of "objects" read. */
825
/* !!! FIXME: What if not the right number of bytes was written? */
826
retval = CountOfBytesWritten / size;
827
} /* else */
828
829
830
831
return(retval);
} /* __PHYSFS_platformWrite */
832
833
834
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
835
HANDLE Handle = ((win32file *) opaque)->handle;
836
DWORD HighOrderPos;
837
DWORD *pHighOrderPos;
838
DWORD rc;
839
840
841
842
/* Get the high order 32-bits of the position */
HighOrderPos = HIGHORDER_UINT64(pos);
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
* 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."
*/
857
/* Move pointer "pos" count from start of file */
858
rc = SetFilePointer(Handle, LOWORDER_UINT64(pos),
859
pHighOrderPos, FILE_BEGIN);
860
861
862
863
if ( (rc == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
{
864
BAIL_MACRO(win32strerror(), 0);
865
866
} /* if */
867
868
return(1); /* No error occured */
} /* __PHYSFS_platformSeek */
869
870
871
872
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
873
HANDLE Handle = ((win32file *) opaque)->handle;
874
875
DWORD HighPos = 0;
DWORD LowPos;
876
877
878
PHYSFS_sint64 retval;
/* Get current position */
879
LowPos = SetFilePointer(Handle, 0, &HighPos, FILE_CURRENT);
880
881
if ( (LowPos == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
882
{
883
884
BAIL_MACRO(win32strerror(), 0);
} /* if */
885
886
887
else
{
/* Combine the high/low order to create the 64-bit position value */
888
889
890
891
892
893
retval = (((PHYSFS_uint64) HighPos) << 32) | LowPos;
assert(retval >= 0);
} /* else */
return(retval);
} /* __PHYSFS_platformTell */
894
895
896
PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
897
{
898
HANDLE Handle = ((win32file *) opaque)->handle;
899
900
DWORD SizeHigh;
DWORD SizeLow;
901
902
PHYSFS_sint64 retval;
903
SizeLow = GetFileSize(Handle, &SizeHigh);
904
905
if ( (SizeLow == PHYSFS_INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR) )
906
{
907
908
BAIL_MACRO(win32strerror(), -1);
} /* if */
909
910
911
else
{
/* Combine the high/low order to create the 64-bit position value */
912
913
retval = (((PHYSFS_uint64) SizeHigh) << 32) | SizeLow;
assert(retval >= 0);
914
} /* else */
915
916
917
918
return(retval);
} /* __PHYSFS_platformFileLength */
919
920
921
922
923
924
925
int __PHYSFS_platformEOF(void *opaque)
{
PHYSFS_sint64 FilePosition;
int retval = 0;
/* Get the current position in the file */
926
if ((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
927
{
928
929
/* Non-zero if EOF is equal to the file length */
retval = FilePosition == __PHYSFS_platformFileLength(opaque);
930
931
932
933
} /* if */
return(retval);
} /* __PHYSFS_platformEOF */
934
935
936
937
int __PHYSFS_platformFlush(void *opaque)
{
938
939
940
win32file *fh = ((win32file *) opaque);
if (!fh->readonly)
BAIL_IF_MACRO(!FlushFileBuffers(fh->handle), win32strerror(), 0);
941
942
943
return(1);
} /* __PHYSFS_platformFlush */
944
945
946
947
int __PHYSFS_platformClose(void *opaque)
{
948
949
HANDLE Handle = ((win32file *) opaque)->handle;
BAIL_IF_MACRO(!CloseHandle(Handle), win32strerror(), 0);
950
allocator.Free(opaque);
951
952
return(1);
} /* __PHYSFS_platformClose */
953
954
955
956
957
int __PHYSFS_platformDelete(const char *path)
{
/* If filename is a folder */
958
if (GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY)
959
{
960
961
BAIL_IF_MACRO(!RemoveDirectory(path), win32strerror(), 0);
} /* if */
962
963
else
{
964
965
BAIL_IF_MACRO(!DeleteFile(path), win32strerror(), 0);
} /* else */
966
967
968
return(1); /* if you got here, it worked. */
} /* __PHYSFS_platformDelete */
969
970
971
972
void *__PHYSFS_platformCreateMutex(void)
{
973
974
975
return((void *) CreateMutex(NULL, FALSE, NULL));
} /* __PHYSFS_platformCreateMutex */
976
977
978
void __PHYSFS_platformDestroyMutex(void *mutex)
{
979
980
981
CloseHandle((HANDLE) mutex);
} /* __PHYSFS_platformDestroyMutex */
982
983
984
int __PHYSFS_platformGrabMutex(void *mutex)
{
985
986
return(WaitForSingleObject((HANDLE) mutex, INFINITE) != WAIT_FAILED);
} /* __PHYSFS_platformGrabMutex */
987
988
989
990
void __PHYSFS_platformReleaseMutex(void *mutex)
{
991
992
993
ReleaseMutex((HANDLE) mutex);
} /* __PHYSFS_platformReleaseMutex */
994
995
static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
996
997
998
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
999
1000
TIME_ZONE_INFORMATION tzi;
DWORD tzid;