/
win32.c
972 lines (782 loc) · 25.8 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
#include <windows.h>
#include <stdio.h>
15
16
#include <stdlib.h>
#include <ctype.h>
17
#include <time.h>
18
#include <assert.h>
19
20
21
22
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
23
24
25
#define LOWORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0x00000000FFFFFFFF)
#define HIGHORDER_UINT64(pos) (PHYSFS_uint32)(pos & 0xFFFFFFFF00000000)
26
27
const char *__PHYSFS_platformDirSeparator = "\\";
28
static int runningNT = 0; /* TRUE if NT derived OS */
29
30
static OSVERSIONINFO OSVersionInfo; /* Information about the OS */
static char *ProfileDirectory = NULL; /* User profile folder */
31
32
33
34
35
36
37
38
/* Users without the platform SDK don't have this defined. The original docs
for SetFilePointer() just said to compare with 0xFFFFFFF, so this should
work as desired */
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER 0xFFFFFFFF
#endif
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* 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];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
msgbuf,
sizeof (msgbuf) / sizeof (TCHAR),
NULL
);
return((const char *) msgbuf);
} /* win32strerror */
65
66
67
68
69
70
71
/*
* Uninitialize any NT specific stuff done in doNTInit().
*
* Return zero if there was a catastrophic failure and non-zero otherwise.
*/
static int doNTDeinit(void)
{
72
73
74
/* nothing NT-specific to deinit at this point. */
return 1; /* It's all good */
} /* doNTDeinit */
75
76
77
78
79
80
81
typedef BOOL (STDMETHODCALLTYPE FAR * LPFNGETUSERPROFILEDIR) (
HANDLE hToken,
LPTSTR lpProfileDir,
LPDWORD lpcchSize);
82
83
84
85
86
87
88
89
/*
* Initialize any NT specific stuff. This includes any OS based on NT.
*
* Return zero if there was a catastrophic failure and non-zero otherwise.
*/
static int doNTInit(void)
{
DWORD pathsize = 0;
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
char dummy[1];
BOOL rc = 0;
HANDLE ProcessHandle = NULL; /* Current process handle */
HANDLE AccessTokenHandle = NULL; /* Security handle to process */
LPFNGETUSERPROFILEDIR GetUserProfileDirectory = NULL;
HMODULE lib = NULL;
const char *err = NULL;
/* Hooray for spaghetti code! */
lib = LoadLibrary("userenv.dll");
if (!lib)
goto ntinit_failed;
/* !!! FIXME: Handle Unicode? */
GetUserProfileDirectory = (LPFNGETUSERPROFILEDIR)
GetProcAddress(lib, "GetUserProfileDirectoryA");
if (!GetUserProfileDirectory)
goto ntinit_failed;
/* Create a process handle associated with the current process ID */
ProcessHandle = GetCurrentProcess();
112
113
114
/* Create a process access token handle */
if(!OpenProcessToken(ProcessHandle, TOKEN_QUERY, &AccessTokenHandle))
115
116
117
118
119
120
121
122
123
124
goto ntinit_failed; /* we need that token to get the profile dir. */
/* Should fail. Will write the size of the profile path in pathsize */
/* Second parameter can't be NULL or the function fails. */
rc = GetUserProfileDirectory(AccessTokenHandle, dummy, &pathsize);
assert(!rc); /* success?! */
/* Allocate memory for the profile directory */
ProfileDirectory = (char *) malloc(pathsize);
if (ProfileDirectory == NULL)
125
{
126
127
128
129
130
131
132
133
134
err = ERR_OUT_OF_MEMORY;
goto ntinit_failed;
} /* if */
/* Try to get the profile directory */
if(!GetUserProfileDirectory(AccessTokenHandle, ProfileDirectory, &pathsize))
goto ntinit_failed;
goto ntinit_succeeded; /* We made it: hit the showers. */
135
136
137
138
139
140
ntinit_failed:
if (err == NULL) /* set an error string if we haven't yet. */
__PHYSFS_setError(win32strerror());
if (ProfileDirectory != NULL)
141
{
142
143
144
free(ProfileDirectory);
ProfileDirectory = NULL;
} /* if */
145
146
147
148
149
150
151
152
153
154
155
156
/* drop through and clean up the rest of the stuff... */
ntinit_succeeded:
if (lib != NULL)
FreeLibrary(lib);
if (AccessTokenHandle != NULL)
CloseHandle(AccessTokenHandle);
return ((err == NULL) ? 1 : 0);
} /* doNTInit */
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
static BOOL MediaInDrive(const char *DriveLetter)
{
UINT OldErrorMode;
DWORD DummyValue;
BOOL ReturnValue;
/* Prevent windows warning message to appear when checking media size */
OldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
/* If this function succeeds, there's media in the drive */
ReturnValue = GetDiskFreeSpace(DriveLetter, &DummyValue, &DummyValue, &DummyValue, &DummyValue);
/* Revert back to old windows error handler */
SetErrorMode(OldErrorMode);
return ReturnValue;
174
} /* MediaInDrive */
175
176
177
178
179
180
181
182
183
char **__PHYSFS_platformDetectAvailableCDs(void)
{
char **retval = (char **) malloc(sizeof (char *));
int cd_count = 1; /* We count the NULL entry. */
char drive_str[4] = "x:\\";
for (drive_str[0] = 'A'; drive_str[0] <= 'Z'; drive_str[0]++)
{
184
if (GetDriveType(drive_str) == DRIVE_CDROM && MediaInDrive(drive_str))
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
{
char **tmp = realloc(retval, sizeof (char *) * cd_count + 1);
if (tmp)
{
retval = tmp;
retval[cd_count-1] = (char *) malloc(4);
if (retval[cd_count-1])
{
strcpy(retval[cd_count-1], drive_str);
cd_count++;
} /* if */
} /* if */
} /* if */
} /* for */
retval[cd_count - 1] = NULL;
return(retval);
} /* __PHYSFS_detectAvailableCDs */
205
static char *getExePath(const char *argv0)
206
{
207
char *filepart = NULL;
208
209
210
211
212
213
214
215
216
217
218
219
220
char *retval;
DWORD buflen;
retval = (char *) malloc(sizeof (TCHAR) * (MAX_PATH + 1));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
buflen = GetModuleFileName(NULL, retval, MAX_PATH + 1);
if (buflen == 0)
{
const char *err = win32strerror();
free(retval);
BAIL_MACRO(err, NULL);
} /* if */
221
222
223
224
225
226
227
228
229
230
231
retval[buflen] = '\0'; /* does API always null-terminate the string? */
/* make sure the string was not truncated. */
if (__PHYSFS_platformStricmp(&retval[buflen - 4], ".exe") == 0)
{
char *ptr = strrchr(retval, '\\');
if (ptr != NULL)
{
*(ptr + 1) = '\0'; /* chop off filename. */
/* free up the bytes we didn't actually use. */
232
233
234
235
236
ptr = (char *) realloc(retval, strlen(retval) + 1);
if (ptr != NULL)
retval = ptr;
return(retval);
237
238
239
240
} /* if */
} /* if */
/* if any part of the previous approach failed, try SearchPath()... */
241
buflen = SearchPath(NULL, argv0, NULL, buflen, NULL, NULL);
242
retval = (char *) realloc(retval, buflen);
243
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
244
SearchPath(NULL, argv0, NULL, buflen, retval, &filepart);
245
246
return(retval);
247
248
249
250
251
252
253
254
} /* getExePath */
char *__PHYSFS_platformCalcBaseDir(const char *argv0)
{
if (strchr(argv0, '\\') != NULL) /* default behaviour can handle this. */
return(NULL);
255
return(getExePath(argv0));
256
257
258
259
260
} /* __PHYSFS_platformCalcBaseDir */
char *__PHYSFS_platformGetUserName(void)
{
261
DWORD bufsize = 0;
262
263
264
265
266
267
268
269
LPTSTR retval = NULL;
if (GetUserName(NULL, &bufsize) == 0) /* This SHOULD fail. */
{
retval = (LPTSTR) malloc(bufsize);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
if (GetUserName(retval, &bufsize) == 0) /* ?! */
{
270
__PHYSFS_setError(win32strerror());
271
272
273
274
275
276
277
278
279
280
281
free(retval);
retval = NULL;
} /* if */
} /* if */
return((char *) retval);
} /* __PHYSFS_platformGetUserName */
char *__PHYSFS_platformGetUserDir(void)
{
282
283
284
285
char *retval = (char *) malloc(strlen(ProfileDirectory) + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
strcpy(retval, ProfileDirectory); /* calculated at init time. */
return retval;
286
287
288
} /* __PHYSFS_platformGetUserDir */
289
PHYSFS_uint64 __PHYSFS_platformGetThreadID(void)
290
{
291
return((PHYSFS_uint64)GetCurrentThreadId());
292
293
294
} /* __PHYSFS_platformGetThreadID */
295
/* ...make this Cygwin AND Visual C friendly... */
296
297
int __PHYSFS_platformStricmp(const char *x, const char *y)
{
298
299
300
#if (defined _MSC_VER)
return(stricmp(x, y));
#else
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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);
316
#endif
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
} /* __PHYSFS_platformStricmp */
int __PHYSFS_platformExists(const char *fname)
{
return(GetFileAttributes(fname) != 0xffffffff);
} /* __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;
char *retval = malloc(len);
346
char *p;
347
348
349
350
351
352
353
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
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 */
LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname,
int omitSymLinks)
{
LinkedStringList *retval = NULL;
LinkedStringList *l = NULL;
LinkedStringList *prev = NULL;
HANDLE dir;
WIN32_FIND_DATA ent;
382
char *SearchPath;
383
size_t len = strlen(dirname);
384
385
/* Allocate a new string for path, maybe '\\', "*", and NULL terminator */
386
387
388
SearchPath = (char *) _alloca(len + 3);
BAIL_IF_MACRO(SearchPath == NULL, ERR_OUT_OF_MEMORY, NULL);
389
390
/* Copy current dirname */
strcpy(SearchPath, dirname);
391
392
/* if there's no '\\' at the end of the path, stick one in there. */
393
if (SearchPath[len - 1] != '\\')
394
{
395
396
SearchPath[len++] = '\\';
SearchPath[len] = '\0';
397
398
} /* if */
399
400
/* Append the "*" to the end of the string */
strcat(SearchPath, "*");
401
402
dir = FindFirstFile(SearchPath, &ent);
403
404
BAIL_IF_MACRO(dir == INVALID_HANDLE_VALUE, win32strerror(), NULL);
405
do
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
{
if (strcmp(ent.cFileName, ".") == 0)
continue;
if (strcmp(ent.cFileName, "..") == 0)
continue;
l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
if (l == NULL)
break;
l->str = (char *) malloc(strlen(ent.cFileName) + 1);
if (l->str == NULL)
{
free(l);
break;
} /* if */
strcpy(l->str, ent.cFileName);
if (retval == NULL)
retval = l;
else
prev->next = l;
prev = l;
l->next = NULL;
433
} while (FindNextFile(dir, &ent) != 0);
434
435
436
437
438
439
440
441
FindClose(dir);
return(retval);
} /* __PHYSFS_platformEnumerateFiles */
char *__PHYSFS_platformCurrentDir(void)
{
442
LPTSTR retval;
443
444
DWORD buflen = 0;
445
buflen = GetCurrentDirectory(buflen, NULL);
446
447
retval = (LPTSTR) malloc(sizeof (TCHAR) * (buflen + 2));
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
448
GetCurrentDirectory(buflen, retval);
449
450
451
452
if (retval[buflen - 2] != '\\')
strcat(retval, "\\");
453
454
455
456
return((char *) retval);
} /* __PHYSFS_platformCurrentDir */
457
/* this could probably use a cleanup. */
458
459
char *__PHYSFS_platformRealPath(const char *path)
{
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
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
char *retval = NULL;
char *p = NULL;
BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
BAIL_IF_MACRO(*path == '\0', ERR_INVALID_ARGUMENT, NULL);
retval = (char *) malloc(MAX_PATH);
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)
{
free(retval);
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 */
free(currentDir);
} /* 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... */
p = (char *) realloc(retval, strlen(retval) + 1);
if (p != NULL)
retval = p;
return(retval);
575
576
577
578
579
} /* __PHYSFS_platformRealPath */
int __PHYSFS_platformMkDir(const char *path)
{
580
DWORD rc = CreateDirectory(path, NULL);
581
582
583
584
BAIL_IF_MACRO(rc == 0, win32strerror(), 0);
return(1);
} /* __PHYSFS_platformMkDir */
585
586
587
588
589
590
591
592
593
594
/*
* Get OS info and save it.
*
* Returns non-zero if successful, otherwise it returns zero on failure.
*/
int getOSInfo(void)
{
/* Get OS info */
OSVersionInfo.dwOSVersionInfoSize = sizeof(OSVersionInfo);
595
BAIL_IF_MACRO(!GetVersionEx(&OSVersionInfo), win32strerror(), 0);
596
597
598
599
600
601
602
603
604
605
/* Set to TRUE if we are runnign a WinNT based OS 4.0 or greater */
runningNT = (OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(OSVersionInfo.dwMajorVersion > 3);
return 1;
}
int __PHYSFS_platformInit(void)
{
606
BAIL_IF_MACRO(!getOSInfo(), NULL, 0);
607
608
609
610
/* If running an NT system (NT/Win2k/XP, etc...) */
if(runningNT)
{
611
612
613
614
615
616
617
618
BAIL_IF_MACRO(!doNTInit(), NULL, 0);
} /* if */
else
{
/* Profile directory is the exe path on 95/98/ME systems. */
ProfileDirectory = getExePath(NULL);
BAIL_IF_MACRO(ProfileDirectory == NULL, win32strerror(), 0);
} /* else */
619
620
return 1; /* It's all good */
621
622
623
624
}
int __PHYSFS_platformDeinit(void)
{
625
626
627
628
if (runningNT)
{
BAIL_IF_MACRO(!doNTDeinit(), NULL, 0);
} /* if */
629
630
631
632
633
634
if (ProfileDirectory != NULL)
{
free(ProfileDirectory);
ProfileDirectory = NULL;
} /* if */
635
636
return 1; /* It's all good */
637
638
}
639
640
641
642
643
644
645
646
647
void *__PHYSFS_platformOpenRead(const char *filename)
{
HANDLE FileHandle;
/* Open an existing file for read only. File can be opened by others
who request read access on the file only. */
FileHandle = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
648
BAIL_IF_MACRO(FileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
649
650
651
652
653
654
655
656
657
658
659
660
return (void *)FileHandle;
}
void *__PHYSFS_platformOpenWrite(const char *filename)
{
HANDLE FileHandle;
/* Open an existing file for write only. File can be opened by others
who request read access to the file only */
FileHandle = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
661
BAIL_IF_MACRO(FileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
662
663
664
665
666
667
668
669
670
671
return (void *)FileHandle;
}
void *__PHYSFS_platformOpenAppend(const char *filename)
{
HANDLE FileHandle;
/* Open an existing file for appending only. File can be opened by others
who request read access to the file only. */
FileHandle = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
672
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
673
674
BAIL_IF_MACRO(FileHandle == INVALID_HANDLE_VALUE, win32strerror(), NULL);
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
return (void *)FileHandle;
}
PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
HANDLE FileHandle;
DWORD CountOfBytesRead;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Read data from the file */
/*!!! - uint32 might be a greater # than DWORD */
if(!ReadFile(FileHandle, buffer, count * size, &CountOfBytesRead, NULL))
{
692
693
BAIL_MACRO(win32strerror(), -1);
} /* if */
694
695
696
697
698
else
{
/* Return the number of "objects" read. */
/* !!! - What if not the right amount of bytes was read to make an object? */
retval = CountOfBytesRead / size;
699
} /* else */
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
return retval;
}
PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, void *buffer,
PHYSFS_uint32 size, PHYSFS_uint32 count)
{
HANDLE FileHandle;
DWORD CountOfBytesWritten;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Read data from the file */
/*!!! - uint32 might be a greater # than DWORD */
if(!WriteFile(FileHandle, buffer, count * size, &CountOfBytesWritten, NULL))
{
718
719
BAIL_MACRO(win32strerror(), -1);
} /* if */
720
721
722
723
724
else
{
/* Return the number of "objects" read. */
/*!!! - What if not the right number of bytes was written? */
retval = CountOfBytesWritten / size;
725
} /* else */
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
return retval;
}
int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
{
HANDLE FileHandle;
int retval;
DWORD HighOrderPos;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Get the high order 32-bits of the position */
HighOrderPos = HIGHORDER_UINT64(pos);
/*!!! SetFilePointer needs a signed 64-bit value. */
/* Move pointer "pos" count from start of file */
if((SetFilePointer(FileHandle, LOWORDER_UINT64(pos), &HighOrderPos, FILE_BEGIN)
== INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
/* An error occured. Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
retval = 0;
}
else
{
/* No error occured */
retval = 1;
}
return retval;
}
PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
{
HANDLE FileHandle;
DWORD HighOrderPos = 0;
DWORD LowOrderPos;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Get current position */
if(((LowOrderPos = SetFilePointer(FileHandle, 0, &HighOrderPos, FILE_CURRENT))
== INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
/* We errored out */
retval = 0;
}
780
781
782
783
784
785
786
else
{
/* Combine the high/low order to create the 64-bit position value */
retval = HighOrderPos;
retval = retval << 32;
retval |= LowOrderPos;
}
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*!!! Can't find a file pointer routine?!?!?!!?!?*/
return retval;
}
PHYSFS_sint64 __PHYSFS_platformFileLength(void *handle)
{
HANDLE FileHandle;
DWORD FileSizeHigh;
DWORD FileSizeLow;
PHYSFS_sint64 retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)handle;
801
802
/* Get the file size. Condition evaluates to TRUE if an error occured */
803
804
805
if(((FileSizeLow = GetFileSize(FileHandle, &FileSizeHigh))
== INVALID_SET_FILE_POINTER) && (GetLastError() != NO_ERROR))
{
806
807
BAIL_MACRO(win32strerror(), -1);
} /* if */
808
809
810
811
812
813
else
{
/* Combine the high/low order to create the 64-bit position value */
retval = FileSizeHigh;
retval = retval << 32;
retval |= FileSizeLow;
814
} /* else */
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
return retval;
}
int __PHYSFS_platformEOF(void *opaque)
{
HANDLE FileHandle;
PHYSFS_sint64 FilePosition;
int retval = 0;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Get the current position in the file */
if((FilePosition = __PHYSFS_platformTell(opaque)) != 0)
{
831
832
/* Non-zero if EOF is equal to the file length */
retval = FilePosition == __PHYSFS_platformFileLength(opaque);
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
}
return retval;
}
int __PHYSFS_platformFlush(void *opaque)
{
HANDLE FileHandle;
int retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Close the file */
if(!(retval = FlushFileBuffers(FileHandle)))
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
}
return retval;
}
int __PHYSFS_platformClose(void *opaque)
{
HANDLE FileHandle;
int retval;
/* Cast the generic handle to a Win32 handle */
FileHandle = (HANDLE)opaque;
/* Close the file */
if(!(retval = CloseHandle(FileHandle)))
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
}
return retval;
}
int __PHYSFS_platformDelete(const char *path)
{
int retval;
/* If filename is a folder */
if(GetFileAttributes(path) == FILE_ATTRIBUTE_DIRECTORY)
{
retval = RemoveDirectory(path);
}
else
{
retval = DeleteFile(path);
}
if(!retval)
{
/* Set the error to GetLastError */
__PHYSFS_setError(win32strerror());
}
return retval;
}
void *__PHYSFS_platformCreateMutex(void)
{
return (void *)CreateMutex(NULL, FALSE, NULL);
}
void __PHYSFS_platformDestroyMutex(void *mutex)
{
CloseHandle((HANDLE)mutex);
}
int __PHYSFS_platformGrabMutex(void *mutex)
{
int retval;
if(WaitForSingleObject((HANDLE)mutex, INFINITE) == WAIT_FAILED)
{
/* Our wait failed for some unknown reason */
914
retval = 0;
915
916
917
918
}
else
{
/* Good to go */
919
retval = 1;
920
921
922
923
924
925
926
927
928
929
}
return retval;
}
void __PHYSFS_platformReleaseMutex(void *mutex)
{
ReleaseMutex((HANDLE)mutex);
}
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
static time_t FileTimeToTimeT(FILETIME *ft)
{
SYSTEMTIME st_utc;
SYSTEMTIME st_localtz;
TIME_ZONE_INFORMATION TimeZoneInfo;
struct tm tm;
FileTimeToSystemTime(ft, &st_utc);
GetTimeZoneInformation(&TimeZoneInfo);
SystemTimeToTzSpecificLocalTime(&TimeZoneInfo, &st_utc, &st_localtz);
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;
tm.tm_wday = st_localtz.wDayOfWeek;
tm.tm_yday = -1;
tm.tm_isdst = -1;
return mktime(&tm);
} /* FileTimeToTimeT */
953
PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname)
954
{
955
WIN32_FILE_ATTRIBUTE_DATA AttributeData;
956
957
958
959
960
GetFileAttributesEx(fname, GetFileExInfoStandard, &AttributeData);
/* 0 return value indicates an error or not supported */
if(AttributeData.ftLastWriteTime.dwHighDateTime == 0 &&
AttributeData.ftLastWriteTime.dwLowDateTime == 0)
961
{
962
/* Return error */
963
BAIL_MACRO(win32strerror(), -1);
964
}
965
966
967
968
/* Return UNIX time_t version of last write time */
return (PHYSFS_sint64)FileTimeToTimeT(&AttributeData.ftLastWriteTime);
/*return (PHYSFS_sint64)FileTimeToTimeT(&AttributeData.ftCreationTime);*/
969
970
} /* __PHYSFS_platformGetLastModTime */
971
/* end of win32.c ... */