/
physfs.c
2082 lines (1634 loc) · 52.9 KB
1
2
3
4
5
6
7
8
9
10
/**
* PhysicsFS; a portable, flexible file i/o abstraction.
*
* Documentation is in physfs.h. It's verbose, honest. :)
*
* Please see the file LICENSE in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
11
12
13
14
#if HAVE_CONFIG_H
# include <config.h>
#endif
15
16
17
18
19
#if (defined PHYSFS_PROFILING)
#include <sys/time.h>
#include <unistd.h>
#endif
20
21
22
23
24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "physfs.h"
25
26
27
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
28
29
30
typedef struct __PHYSFS_DIRHANDLE__
{
31
32
33
34
void *opaque; /* Instance data unique to the archiver. */
char *dirName; /* Path to archive in platform-dependent notation. */
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
35
36
37
} DirHandle;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
typedef struct __PHYSFS_FILEHANDLE__
{
void *opaque; /* Instance data unique to the archiver for this file. */
PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
const DirHandle *dirHandle; /* Archiver instance that created this */
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
PHYSFS_uint8 *buffer; /* Buffer, if set (NULL otherwise). Don't touch! */
PHYSFS_uint32 bufsize; /* Bufsize, if set (0 otherwise). Don't touch! */
PHYSFS_uint32 buffill; /* Buffer fill size. Don't touch! */
PHYSFS_uint32 bufpos; /* Buffer position. Don't touch! */
struct __PHYSFS_FILEHANDLE__ *next; /* linked list stuff. */
} FileHandle;
52
53
typedef struct __PHYSFS_ERRMSGTYPE__
{
54
PHYSFS_uint64 tid;
55
56
57
58
59
int errorAvailable;
char errorString[80];
struct __PHYSFS_ERRMSGTYPE__ *next;
} ErrMsg;
60
61
/* The various i/o drivers... */
62
63
#if (defined PHYSFS_SUPPORTS_ZIP)
64
65
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
66
#endif
67
68
#if (defined PHYSFS_SUPPORTS_GRP)
69
70
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
71
72
#endif
73
#if (defined PHYSFS_SUPPORTS_QPAK)
74
75
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
76
77
#endif
78
#if (defined PHYSFS_SUPPORTS_HOG)
79
80
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
81
82
83
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
84
85
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
86
87
#endif
88
#if (defined PHYSFS_SUPPORTS_WAD)
89
90
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
91
92
#endif
93
#if (defined PHYSFS_SUPPORTS_MIX)
94
95
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX;
extern const PHYSFS_Archiver __PHYSFS_Archiver_MIX;
96
97
#endif
98
extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
99
100
101
static const PHYSFS_ArchiveInfo *supported_types[] =
102
103
{
#if (defined PHYSFS_SUPPORTS_ZIP)
104
&__PHYSFS_ArchiveInfo_ZIP,
105
106
#endif
107
108
109
110
#if (defined PHYSFS_SUPPORTS_GRP)
&__PHYSFS_ArchiveInfo_GRP,
#endif
111
112
113
114
#if (defined PHYSFS_SUPPORTS_QPAK)
&__PHYSFS_ArchiveInfo_QPAK,
#endif
115
116
117
118
119
120
121
122
#if (defined PHYSFS_SUPPORTS_HOG)
&__PHYSFS_ArchiveInfo_HOG,
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
&__PHYSFS_ArchiveInfo_MVL,
#endif
123
124
125
126
#if (defined PHYSFS_SUPPORTS_WAD)
&__PHYSFS_ArchiveInfo_WAD,
#endif
127
128
129
#if (defined PHYSFS_SUPPORTS_MIX)
&__PHYSFS_ArchiveInfo_MIX,
#endif
130
131
132
133
NULL
};
134
static const PHYSFS_Archiver *archivers[] =
135
136
{
#if (defined PHYSFS_SUPPORTS_ZIP)
137
&__PHYSFS_Archiver_ZIP,
138
139
#endif
140
#if (defined PHYSFS_SUPPORTS_GRP)
141
&__PHYSFS_Archiver_GRP,
142
143
#endif
144
#if (defined PHYSFS_SUPPORTS_QPAK)
145
&__PHYSFS_Archiver_QPAK,
146
147
#endif
148
#if (defined PHYSFS_SUPPORTS_HOG)
149
&__PHYSFS_Archiver_HOG,
150
151
152
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
153
&__PHYSFS_Archiver_MVL,
154
155
#endif
156
#if (defined PHYSFS_SUPPORTS_WAD)
157
&__PHYSFS_Archiver_WAD,
158
159
#endif
160
#if (defined PHYSFS_SUPPORTS_MIX)
161
&__PHYSFS_Archiver_MIX,
162
163
#endif
164
&__PHYSFS_Archiver_DIR,
165
166
NULL
};
167
168
169
170
171
172
/* General PhysicsFS state ... */
static int initialized = 0;
static ErrMsg *errorMessages = NULL;
173
174
175
176
static DirHandle *searchPath = NULL;
static DirHandle *writeDir = NULL;
static FileHandle *openWriteList = NULL;
static FileHandle *openReadList = NULL;
177
178
179
180
static char *baseDir = NULL;
static char *userDir = NULL;
static int allowSymLinks = 0;
181
182
183
/* mutexes ... */
static void *errorLock = NULL; /* protects error message list. */
static void *stateLock = NULL; /* protects other PhysFS static state. */
184
185
186
/* allocator ... */
static int externalAllocator = 0;
187
static PHYSFS_Allocator allocator;
188
189
190
191
/* functions ... */
192
static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
PHYSFS_uint32 i;
int sorted;
do
{
sorted = 1;
for (i = lo; i < hi; i++)
{
if (cmpfn(a, i, i + 1) > 0)
{
swapfn(a, i, i + 1);
sorted = 0;
} /* if */
} /* for */
} while (!sorted);
} /* __PHYSFS_bubble_sort */
214
static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
215
216
217
218
219
220
221
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
PHYSFS_uint32 i;
PHYSFS_uint32 j;
PHYSFS_uint32 v;
222
if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
223
224
__PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
else
225
226
{
i = (hi + lo) / 2;
227
228
if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
229
230
231
232
233
234
235
236
237
238
239
240
if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
j = hi - 1;
swapfn(a, i, j);
i = lo;
v = j;
while (1)
{
while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
if (j < i)
241
break;
242
243
244
245
246
247
swapfn(a, i, j);
} /* while */
swapfn(a, i, hi-1);
__PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
__PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
} /* else */
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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
346
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
} /* __PHYSFS_quick_sort */
void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
/*
* Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
* http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
*/
__PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
} /* __PHYSFS_sort */
#if (defined PHYSFS_PROFILING)
#define PHYSFS_TEST_SORT_ITERATIONS 150
#define PHYSFS_TEST_SORT_ELEMENTS (64 * 1024)
static int __PHYSFS_test_sort_cmp(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
{
PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
PHYSFS_sint32 one = a[x];
PHYSFS_sint32 two = a[y];
if (one < two)
return(-1);
else if (one > two)
return(1);
return(0);
} /* __PHYSFS_test_sort_cmp */
static void __PHYSFS_test_sort_swap(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
{
PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
PHYSFS_sint32 tmp;
tmp = a[x];
a[x] = a[y];
a[y] = tmp;
} /* __PHYSFS_test_sort_swap */
static int __PHYSFS_test_sort_do(PHYSFS_uint32 *timer,
PHYSFS_sint32 *a, PHYSFS_uint32 max,
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
PHYSFS_uint32 i;
struct timeval starttime, endtime;
gettimeofday(&starttime, NULL);
__PHYSFS_sort(a, max, cmpfn, swapfn);
gettimeofday(&endtime, NULL);
for (i = 1; i < max; i++)
{
if (a[i] < a[i - 1])
return(0);
} /* for */
if (timer != NULL)
{
*timer = ( ((endtime.tv_sec - starttime.tv_sec) * 1000) +
((endtime.tv_usec - starttime.tv_usec) / 1000) );
} /* if */
return(1);
} /* __PHYSFS_test_sort_time */
static void __PHYSFS_test_sort(void)
{
PHYSFS_uint32 elasped[PHYSFS_TEST_SORT_ITERATIONS];
PHYSFS_sint32 iter;
PHYSFS_sint32 a[PHYSFS_TEST_SORT_ELEMENTS];
PHYSFS_sint32 i, x;
int success;
printf("Testing __PHYSFS_sort (linear presorted) ... ");
for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
{
/* set up array to sort. */
for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
a[i] = i;
/* sort it. */
success = __PHYSFS_test_sort_do(&elasped[iter],
a, PHYSFS_TEST_SORT_ELEMENTS,
__PHYSFS_test_sort_cmp,
__PHYSFS_test_sort_swap);
if (!success)
break;
} /* for */
if (!success)
printf("Failed!\n");
else
{
for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
x += elasped[iter];
x /= PHYSFS_TEST_SORT_ITERATIONS;
printf("Average run (%lu) ms.\n", (unsigned long) x);
} /* else */
printf("Testing __PHYSFS_sort (linear presorted reverse) ... ");
for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
{
/* set up array to sort. */
for (i = 0, x = PHYSFS_TEST_SORT_ELEMENTS;
i < PHYSFS_TEST_SORT_ELEMENTS;
i++, x--)
{
a[i] = x;
} /* for */
/* sort it. */
success = __PHYSFS_test_sort_do(&elasped[iter],
a, PHYSFS_TEST_SORT_ELEMENTS,
__PHYSFS_test_sort_cmp,
__PHYSFS_test_sort_swap);
if (!success)
break;
} /* for */
if (!success)
printf("Failed!\n");
else
{
for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
x += elasped[iter];
x /= PHYSFS_TEST_SORT_ITERATIONS;
printf("Average run (%lu) ms.\n", (unsigned long) x);
} /* else */
printf("Testing __PHYSFS_sort (randomized) ... ");
for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
{
/* set up array to sort. */
for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
a[i] = (PHYSFS_uint32) rand();
/* sort it. */
success = __PHYSFS_test_sort_do(&elasped[iter],
a, PHYSFS_TEST_SORT_ELEMENTS,
__PHYSFS_test_sort_cmp,
__PHYSFS_test_sort_swap);
if (!success)
break;
} /* for */
if (!success)
printf("Failed!\n");
else
{
for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
x += elasped[iter];
x /= PHYSFS_TEST_SORT_ITERATIONS;
printf("Average run (%lu) ms.\n", (unsigned long) x);
} /* else */
printf("__PHYSFS_test_sort() complete.\n\n");
} /* __PHYSFS_test_sort */
#endif
421
422
423
static ErrMsg *findErrorForCurrentThread(void)
{
ErrMsg *i;
424
PHYSFS_uint64 tid;
425
426
if (errorLock != NULL)
427
428
__PHYSFS_platformGrabMutex(errorLock);
429
430
if (errorMessages != NULL)
{
431
tid = __PHYSFS_platformGetThreadID();
432
433
434
435
for (i = errorMessages; i != NULL; i = i->next)
{
if (i->tid == tid)
436
{
437
438
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
439
return(i);
440
} /* if */
441
442
} /* for */
} /* if */
443
444
if (errorLock != NULL)
445
__PHYSFS_platformReleaseMutex(errorLock);
446
447
448
449
450
return(NULL); /* no error available. */
} /* findErrorForCurrentThread */
451
void __PHYSFS_setError(const char *str)
452
{
453
454
455
456
457
458
ErrMsg *err;
if (str == NULL)
return;
err = findErrorForCurrentThread();
459
460
461
462
463
464
465
if (err == NULL)
{
err = (ErrMsg *) malloc(sizeof (ErrMsg));
if (err == NULL)
return; /* uhh...? */
466
memset((void *) err, '\0', sizeof (ErrMsg));
467
err->tid = __PHYSFS_platformGetThreadID();
468
469
470
471
if (errorLock != NULL)
__PHYSFS_platformGrabMutex(errorLock);
472
473
err->next = errorMessages;
errorMessages = err;
474
475
476
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
477
478
479
480
481
} /* if */
err->errorAvailable = 1;
strncpy(err->errorString, str, sizeof (err->errorString));
err->errorString[sizeof (err->errorString) - 1] = '\0';
482
} /* __PHYSFS_setError */
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
const char *PHYSFS_getLastError(void)
{
ErrMsg *err = findErrorForCurrentThread();
if ((err == NULL) || (!err->errorAvailable))
return(NULL);
err->errorAvailable = 0;
return(err->errorString);
} /* PHYSFS_getLastError */
/* MAKE SURE that errorLock is held before calling this! */
498
499
500
501
502
503
504
static void freeErrorMessages(void)
{
ErrMsg *i;
ErrMsg *next;
for (i = errorMessages; i != NULL; i = next)
{
505
next = i->next;
506
507
free(i);
} /* for */
508
509
errorMessages = NULL;
510
511
512
} /* freeErrorMessages */
513
514
515
516
517
518
519
520
521
522
523
void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
{
if (ver != NULL)
{
ver->major = PHYSFS_VER_MAJOR;
ver->minor = PHYSFS_VER_MINOR;
ver->patch = PHYSFS_VER_PATCH;
} /* if */
} /* PHYSFS_getLinkedVersion */
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
static const char *find_filename_extension(const char *fname)
{
const char *retval = strchr(fname, '.');
const char *p = retval;
while (p != NULL)
{
p = strchr(p + 1, '.');
if (p != NULL)
retval = p;
} /* while */
if (retval != NULL)
retval++; /* skip '.' */
return(retval);
} /* find_filename_extension */
543
544
static DirHandle *tryOpenDir(const PHYSFS_Archiver *funcs,
const char *d, int forWriting)
545
546
{
DirHandle *retval = NULL;
547
if (funcs->isArchive(d, forWriting))
548
{
549
void *opaque = funcs->openArchive(d, forWriting);
550
551
552
553
if (opaque != NULL)
{
retval = (DirHandle *) allocator.malloc(sizeof (DirHandle));
if (retval == NULL)
554
funcs->dirClose(opaque);
555
556
else
{
557
558
memset(retval, '\0', sizeof (DirHandle));
retval->funcs = funcs;
559
560
561
562
563
564
565
566
567
retval->opaque = opaque;
} /* else */
} /* if */
} /* if */
return(retval);
} /* tryOpenDir */
568
569
static DirHandle *openDirectory(const char *d, int forWriting)
{
570
DirHandle *retval = NULL;
571
const PHYSFS_Archiver **i;
572
const char *ext;
573
574
575
BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
576
577
ext = find_filename_extension(d);
if (ext != NULL)
578
{
579
/* Look for archivers with matching file extensions first... */
580
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
581
582
{
if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) == 0)
583
retval = tryOpenDir(*i, d, forWriting);
584
585
586
} /* for */
/* failing an exact file extension match, try all the others... */
587
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
588
589
{
if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) != 0)
590
retval = tryOpenDir(*i, d, forWriting);
591
592
593
594
595
} /* for */
} /* if */
else /* no extension? Try them all. */
{
596
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
597
retval = tryOpenDir(*i, d, forWriting);
598
} /* else */
599
600
601
BAIL_IF_MACRO(retval == NULL, ERR_UNSUPPORTED_ARCHIVE, NULL);
return(retval);
602
603
604
} /* openDirectory */
605
static DirHandle *createDirHandle(const char *newDir, int forWriting)
606
607
608
{
DirHandle *dirHandle = NULL;
609
BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, NULL);
610
611
dirHandle = openDirectory(newDir, forWriting);
612
BAIL_IF_MACRO(dirHandle == NULL, NULL, NULL);
613
614
615
dirHandle->dirName = (char *) malloc(strlen(newDir) + 1);
if (dirHandle->dirName == NULL)
616
{
617
618
dirHandle->funcs->dirClose(dirHandle->opaque);
free(dirHandle);
619
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
620
} /* if */
621
622
623
624
strcpy(dirHandle->dirName, newDir);
return(dirHandle);
} /* createDirHandle */
625
626
627
/* MAKE SURE you've got the stateLock held before calling this! */
628
static int freeDirHandle(DirHandle *dh, FileHandle *openList)
629
{
630
FileHandle *i;
631
632
if (dh == NULL)
633
634
635
return(1);
for (i = openList; i != NULL; i = i->next)
636
BAIL_IF_MACRO(i->dirHandle == dh, ERR_FILES_STILL_OPEN, 0);
637
638
639
640
dh->funcs->dirClose(dh->opaque);
free(dh->dirName);
free(dh);
641
return(1);
642
} /* freeDirHandle */
643
644
645
static char *calculateUserDir(void)
646
647
648
649
650
651
{
char *retval = NULL;
const char *str = NULL;
str = __PHYSFS_platformGetUserDir();
if (str != NULL)
652
retval = (char *) str;
653
654
655
656
657
658
else
{
const char *dirsep = PHYSFS_getDirSeparator();
const char *uname = __PHYSFS_platformGetUserName();
str = (uname != NULL) ? uname : "default";
659
retval = (char *) malloc(strlen(baseDir) + strlen(str) +
660
strlen(dirsep) + 6);
661
662
663
664
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
else
665
sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
666
667
if (uname != NULL)
668
free((void *) uname);
669
670
671
672
673
674
} /* else */
return(retval);
} /* calculateUserDir */
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
static int appendDirSep(char **dir)
{
const char *dirsep = PHYSFS_getDirSeparator();
char *ptr;
if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
return(1);
ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
if (!ptr)
{
free(*dir);
return(0);
} /* if */
strcat(ptr, dirsep);
*dir = ptr;
return(1);
} /* appendDirSep */
696
697
static char *calculateBaseDir(const char *argv0)
{
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
const char *dirsep = PHYSFS_getDirSeparator();
char *retval;
char *ptr;
/*
* See if the platform driver wants to handle this for us...
*/
retval = __PHYSFS_platformCalcBaseDir(argv0);
if (retval != NULL)
return(retval);
/*
* Determine if there's a path on argv0. If there is, that's the base dir.
*/
ptr = strstr(argv0, dirsep);
if (ptr != NULL)
{
char *p = ptr;
size_t size;
while (p != NULL)
{
ptr = p;
p = strstr(p + 1, dirsep);
} /* while */
723
size = (size_t) (ptr - argv0);
724
725
726
727
728
729
730
731
732
733
retval = (char *) malloc(size + 1);
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
memcpy(retval, argv0, size);
retval[size] = '\0';
return(retval);
} /* if */
/*
* Last ditch effort: it's the current working directory. (*shrug*)
*/
734
retval = __PHYSFS_platformCurrentDir();
735
736
if (retval != NULL)
return(retval);
737
738
739
740
741
742
743
744
745
/*
* Ok, current directory doesn't exist, use the root directory.
* Not a good alternative, but it only happens if the current
* directory was deleted from under the program.
*/
retval = (char *) malloc(strlen(dirsep) + 1);
strcpy(retval, dirsep);
return(retval);
746
747
748
} /* calculateBaseDir */
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
static int initializeMutexes(void)
{
errorLock = __PHYSFS_platformCreateMutex();
if (errorLock == NULL)
goto initializeMutexes_failed;
stateLock = __PHYSFS_platformCreateMutex();
if (stateLock == NULL)
goto initializeMutexes_failed;
return(1); /* success. */
initializeMutexes_failed:
if (errorLock != NULL)
__PHYSFS_platformDestroyMutex(errorLock);
if (stateLock != NULL)
__PHYSFS_platformDestroyMutex(stateLock);
errorLock = stateLock = NULL;
return(0); /* failed. */
} /* initializeMutexes */
773
774
static void setDefaultAllocator(void);
775
776
int PHYSFS_init(const char *argv0)
{
777
778
char *ptr;
779
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
780
781
782
783
if (!externalAllocator)
setDefaultAllocator();
784
785
BAIL_IF_MACRO(!allocator.init(), NULL, 0);
786
BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
787
788
789
BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
790
baseDir = calculateBaseDir(argv0);
791
BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
792
793
794
795
796
797
ptr = __PHYSFS_platformRealPath(baseDir);
free(baseDir);
BAIL_IF_MACRO(ptr == NULL, NULL, 0);
baseDir = ptr;
798
BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
799
800
userDir = calculateUserDir();
801
802
803
804
805
806
807
if (userDir != NULL)
{
ptr = __PHYSFS_platformRealPath(userDir);
free(userDir);
userDir = ptr;
} /* if */
808
if ((userDir == NULL) || (!appendDirSep(&userDir)))
809
810
811
812
813
814
{
free(baseDir);
baseDir = NULL;
return(0);
} /* if */
815
initialized = 1;
816
817
818
/* This makes sure that the error subsystem is initialized. */
__PHYSFS_setError(PHYSFS_getLastError());
819
820
821
822
823
824
825
826
827
828
829
830
831
#if (defined PHYSFS_PROFILING)
srand(time(NULL));
setbuf(stdout, NULL);
printf("\n");
printf("********************************************************\n");
printf("Warning! Profiling is built into this copy of PhysicsFS!\n");
printf("********************************************************\n");
printf("\n");
printf("\n");
__PHYSFS_test_sort();
#endif
832
833
834
835
return(1);
} /* PHYSFS_init */
836
/* MAKE SURE you hold stateLock before calling this! */
837
static int closeFileHandleList(FileHandle **list)
838
{
839
840
FileHandle *i;
FileHandle *next = NULL;
841
FileHandle *h;
842
843
844
845
for (i = *list; i != NULL; i = next)
{
next = i->next;
846
if (!i->funcs->fileClose(i->opaque))
847
848
849
850
851
852
{
*list = i;
return(0);
} /* if */
free(i);
853
854
855
} /* for */
*list = NULL;
856
857
return(1);
} /* closeFileHandleList */
858
859
860
/* MAKE SURE you hold the stateLock before calling this! */
861
862
static void freeSearchPath(void)
{
863
864
DirHandle *i;
DirHandle *next = NULL;
865
866
867
closeFileHandleList(&openReadList);
868
869
870
871
if (searchPath != NULL)
{
for (i = searchPath; i != NULL; i = next)
{
872
next = i->next;
873
freeDirHandle(i, openReadList);
874
875
876
877
878
879
} /* for */
searchPath = NULL;
} /* if */
} /* freeSearchPath */
880
int PHYSFS_deinit(void)
881
882
{
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
883
BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
884
885
closeFileHandleList(&openWriteList);
886
887
BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
888
889
890
freeSearchPath();
freeErrorMessages();
891
if (baseDir != NULL)
892
{
893
free(baseDir);
894
895
896
897
898
899
900
901
baseDir = NULL;
} /* if */
if (userDir != NULL)
{
free(userDir);
userDir = NULL;
} /* if */
902
903
allowSymLinks = 0;
904
initialized = 0;
905
906
907
908
__PHYSFS_platformDestroyMutex(errorLock);
__PHYSFS_platformDestroyMutex(stateLock);
909
910
allocator.deinit();
911
errorLock = stateLock = NULL;
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
return(1);
} /* PHYSFS_deinit */
const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
{
return(supported_types);
} /* PHYSFS_supportedArchiveTypes */
void PHYSFS_freeList(void *list)
{
void **i;
for (i = (void **) list; *i != NULL; i++)
free(*i);
free(list);
} /* PHYSFS_freeList */
const char *PHYSFS_getDirSeparator(void)
{
934
return(__PHYSFS_platformDirSeparator);
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
} /* PHYSFS_getDirSeparator */
char **PHYSFS_getCdRomDirs(void)
{
return(__PHYSFS_platformDetectAvailableCDs());
} /* PHYSFS_getCdRomDirs */
const char *PHYSFS_getBaseDir(void)
{
return(baseDir); /* this is calculated in PHYSFS_init()... */
} /* PHYSFS_getBaseDir */
const char *PHYSFS_getUserDir(void)
{
952
return(userDir); /* this is calculated in PHYSFS_init()... */
953
954
955
956
957
} /* PHYSFS_getUserDir */
const char *PHYSFS_getWriteDir(void)
{
958
959
960
961
962
963
const char *retval = NULL;
__PHYSFS_platformGrabMutex(stateLock);
if (writeDir != NULL)
retval = writeDir->dirName;
__PHYSFS_platformReleaseMutex(stateLock);
964
965
return(retval);
966
967
968
969
970
} /* PHYSFS_getWriteDir */
int PHYSFS_setWriteDir(const char *newDir)
{
971
972
973
974
int retval = 1;
__PHYSFS_platformGrabMutex(stateLock);
975
976
if (writeDir != NULL)
{
977
BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), NULL,
978
stateLock, 0);
979
980
981
writeDir = NULL;
} /* if */
982
983
if (newDir != NULL)
{
984
writeDir = createDirHandle(newDir, 1);
985
retval = (writeDir != NULL);
986
} /* if */
987
988
989
990
__PHYSFS_platformReleaseMutex(stateLock);
return(retval);
991
992
993
994
995
} /* PHYSFS_setWriteDir */
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
{
996
997
998
DirHandle *dh;
DirHandle *prev = NULL;
DirHandle *i;
999
1000
__PHYSFS_platformGrabMutex(stateLock);