/
physfs.c
2284 lines (1823 loc) · 61.8 KB
1
2
3
4
5
/**
* PhysicsFS; a portable, flexible file i/o abstraction.
*
* Documentation is in physfs.h. It's verbose, honest. :)
*
6
* Please see the file LICENSE.txt in the source's root directory.
7
8
9
10
11
12
13
14
15
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "physfs.h"
16
17
18
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
19
20
21
typedef struct __PHYSFS_DIRHANDLE__
{
22
23
void *opaque; /* Instance data unique to the archiver. */
char *dirName; /* Path to archive in platform-dependent notation. */
24
char *mountPoint; /* Mountpoint in virtual file tree. */
25
26
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
27
28
29
} DirHandle;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
44
45
typedef struct __PHYSFS_ERRMSGTYPE__
{
46
void *tid;
47
48
49
50
51
int errorAvailable;
char errorString[80];
struct __PHYSFS_ERRMSGTYPE__ *next;
} ErrMsg;
52
53
/* The various i/o drivers...some of these may not be compiled in. */
54
55
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
56
57
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_LZMA;
extern const PHYSFS_Archiver __PHYSFS_Archiver_LZMA;
58
59
60
61
62
63
64
65
66
67
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
68
extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
69
70
71
static const PHYSFS_ArchiveInfo *supported_types[] =
72
73
{
#if (defined PHYSFS_SUPPORTS_ZIP)
74
&__PHYSFS_ArchiveInfo_ZIP,
75
#endif
76
#if (defined PHYSFS_SUPPORTS_7Z)
77
78
&__PHYSFS_ArchiveInfo_LZMA,
#endif
79
80
81
#if (defined PHYSFS_SUPPORTS_GRP)
&__PHYSFS_ArchiveInfo_GRP,
#endif
82
83
84
#if (defined PHYSFS_SUPPORTS_QPAK)
&__PHYSFS_ArchiveInfo_QPAK,
#endif
85
86
87
88
89
90
#if (defined PHYSFS_SUPPORTS_HOG)
&__PHYSFS_ArchiveInfo_HOG,
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
&__PHYSFS_ArchiveInfo_MVL,
#endif
91
92
#if (defined PHYSFS_SUPPORTS_WAD)
&__PHYSFS_ArchiveInfo_WAD,
93
#endif
94
95
96
NULL
};
97
static const PHYSFS_Archiver *archivers[] =
98
{
99
&__PHYSFS_Archiver_DIR,
100
#if (defined PHYSFS_SUPPORTS_ZIP)
101
&__PHYSFS_Archiver_ZIP,
102
#endif
103
#if (defined PHYSFS_SUPPORTS_7Z)
104
105
&__PHYSFS_Archiver_LZMA,
#endif
106
#if (defined PHYSFS_SUPPORTS_GRP)
107
&__PHYSFS_Archiver_GRP,
108
#endif
109
#if (defined PHYSFS_SUPPORTS_QPAK)
110
&__PHYSFS_Archiver_QPAK,
111
#endif
112
#if (defined PHYSFS_SUPPORTS_HOG)
113
&__PHYSFS_Archiver_HOG,
114
115
#endif
#if (defined PHYSFS_SUPPORTS_MVL)
116
&__PHYSFS_Archiver_MVL,
117
#endif
118
#if (defined PHYSFS_SUPPORTS_WAD)
119
&__PHYSFS_Archiver_WAD,
120
#endif
121
122
NULL
};
123
124
125
126
127
128
/* General PhysicsFS state ... */
static int initialized = 0;
static ErrMsg *errorMessages = NULL;
129
130
131
132
static DirHandle *searchPath = NULL;
static DirHandle *writeDir = NULL;
static FileHandle *openWriteList = NULL;
static FileHandle *openReadList = NULL;
133
134
135
136
static char *baseDir = NULL;
static char *userDir = NULL;
static int allowSymLinks = 0;
137
138
139
/* mutexes ... */
static void *errorLock = NULL; /* protects error message list. */
static void *stateLock = NULL; /* protects other PhysFS static state. */
140
141
142
/* allocator ... */
static int externalAllocator = 0;
143
PHYSFS_Allocator allocator;
144
145
146
147
/* functions ... */
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
typedef struct
{
char **list;
PHYSFS_uint32 size;
const char *errorstr;
} EnumStringListCallbackData;
static void enumStringListCallback(void *data, const char *str)
{
void *ptr;
char *newstr;
EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
if (pecd->errorstr)
return;
164
165
ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
newstr = (char *) allocator.Malloc(strlen(str) + 1);
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
if (ptr != NULL)
pecd->list = (char **) ptr;
if ((ptr == NULL) || (newstr == NULL))
{
pecd->errorstr = ERR_OUT_OF_MEMORY;
pecd->list[pecd->size] = NULL;
PHYSFS_freeList(pecd->list);
return;
} /* if */
strcpy(newstr, str);
pecd->list[pecd->size] = newstr;
pecd->size++;
} /* enumStringListCallback */
static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
{
EnumStringListCallbackData ecd;
memset(&ecd, '\0', sizeof (ecd));
187
ecd.list = (char **) allocator.Malloc(sizeof (char *));
188
189
190
191
BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
func(enumStringListCallback, &ecd);
BAIL_IF_MACRO(ecd.errorstr != NULL, ecd.errorstr, NULL);
ecd.list[ecd.size] = NULL;
192
return ecd.list;
193
194
195
} /* doEnumStringList */
196
static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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 */
218
static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
219
220
221
222
223
224
225
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
{
PHYSFS_uint32 i;
PHYSFS_uint32 j;
PHYSFS_uint32 v;
226
if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
227
228
__PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
else
229
230
{
i = (hi + lo) / 2;
231
232
if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
233
234
235
236
237
238
239
240
241
242
243
244
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)
245
break;
246
247
swapfn(a, i, j);
} /* while */
248
249
if (i != (hi-1))
swapfn(a, i, hi-1);
250
251
252
__PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
__PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
} /* else */
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
} /* __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 */
268
269
270
static ErrMsg *findErrorForCurrentThread(void)
{
ErrMsg *i;
271
void *tid;
272
273
if (errorLock != NULL)
274
275
__PHYSFS_platformGrabMutex(errorLock);
276
277
if (errorMessages != NULL)
{
278
tid = __PHYSFS_platformGetThreadID();
279
280
281
282
for (i = errorMessages; i != NULL; i = i->next)
{
if (i->tid == tid)
283
{
284
285
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
286
return i;
287
} /* if */
288
289
} /* for */
} /* if */
290
291
if (errorLock != NULL)
292
__PHYSFS_platformReleaseMutex(errorLock);
293
294
return NULL; /* no error available. */
295
296
297
} /* findErrorForCurrentThread */
298
void __PHYSFS_setError(const char *str)
299
{
300
301
302
303
304
305
ErrMsg *err;
if (str == NULL)
return;
err = findErrorForCurrentThread();
306
307
308
if (err == NULL)
{
309
err = (ErrMsg *) allocator.Malloc(sizeof (ErrMsg));
310
311
312
if (err == NULL)
return; /* uhh...? */
313
memset((void *) err, '\0', sizeof (ErrMsg));
314
err->tid = __PHYSFS_platformGetThreadID();
315
316
317
318
if (errorLock != NULL)
__PHYSFS_platformGrabMutex(errorLock);
319
320
err->next = errorMessages;
errorMessages = err;
321
322
323
if (errorLock != NULL)
__PHYSFS_platformReleaseMutex(errorLock);
324
325
326
327
328
} /* if */
err->errorAvailable = 1;
strncpy(err->errorString, str, sizeof (err->errorString));
err->errorString[sizeof (err->errorString) - 1] = '\0';
329
} /* __PHYSFS_setError */
330
331
332
333
334
335
336
const char *PHYSFS_getLastError(void)
{
ErrMsg *err = findErrorForCurrentThread();
if ((err == NULL) || (!err->errorAvailable))
337
return NULL;
338
339
err->errorAvailable = 0;
340
return err->errorString;
341
342
343
344
} /* PHYSFS_getLastError */
/* MAKE SURE that errorLock is held before calling this! */
345
346
347
348
349
350
351
static void freeErrorMessages(void)
{
ErrMsg *i;
ErrMsg *next;
for (i = errorMessages; i != NULL; i = next)
{
352
next = i->next;
353
allocator.Free(i);
354
} /* for */
355
356
errorMessages = NULL;
357
358
359
} /* freeErrorMessages */
360
361
362
363
364
365
366
367
368
369
370
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 */
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
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 '.' */
386
return retval;
387
388
389
} /* find_filename_extension */
390
391
static DirHandle *tryOpenDir(const PHYSFS_Archiver *funcs,
const char *d, int forWriting)
392
393
{
DirHandle *retval = NULL;
394
if (funcs->isArchive(d, forWriting))
395
{
396
void *opaque = funcs->openArchive(d, forWriting);
397
398
if (opaque != NULL)
{
399
retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
400
if (retval == NULL)
401
funcs->dirClose(opaque);
402
403
else
{
404
memset(retval, '\0', sizeof (DirHandle));
405
retval->mountPoint = NULL;
406
retval->funcs = funcs;
407
408
409
410
411
retval->opaque = opaque;
} /* else */
} /* if */
} /* if */
412
return retval;
413
414
415
} /* tryOpenDir */
416
417
static DirHandle *openDirectory(const char *d, int forWriting)
{
418
DirHandle *retval = NULL;
419
const PHYSFS_Archiver **i;
420
const char *ext;
421
422
423
BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
424
425
ext = find_filename_extension(d);
if (ext != NULL)
426
{
427
/* Look for archivers with matching file extensions first... */
428
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
429
{
430
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) == 0)
431
retval = tryOpenDir(*i, d, forWriting);
432
433
434
} /* for */
/* failing an exact file extension match, try all the others... */
435
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
436
{
437
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) != 0)
438
retval = tryOpenDir(*i, d, forWriting);
439
440
441
442
443
} /* for */
} /* if */
else /* no extension? Try them all. */
{
444
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
445
retval = tryOpenDir(*i, d, forWriting);
446
} /* else */
447
448
BAIL_IF_MACRO(retval == NULL, ERR_UNSUPPORTED_ARCHIVE, NULL);
449
return retval;
450
451
452
} /* openDirectory */
453
454
455
456
457
458
459
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
/*
* Make a platform-independent path string sane. Doesn't actually check the
* file hierarchy, it just cleans up the string.
* (dst) must be a buffer at least as big as (src), as this is where the
* cleaned up string is deposited.
* If there are illegal bits in the path (".." entries, etc) then we
* return zero and (dst) is undefined. Non-zero if the path was sanitized.
*/
static int sanitizePlatformIndependentPath(const char *src, char *dst)
{
char *prev;
char ch;
while (*src == '/') /* skip initial '/' chars... */
src++;
prev = dst;
do
{
ch = *(src++);
if ((ch == ':') || (ch == '\\')) /* illegal chars in a physfs path. */
BAIL_MACRO(ERR_INSECURE_FNAME, 0);
if (ch == '/') /* path separator. */
{
*dst = '\0'; /* "." and ".." are illegal pathnames. */
if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
BAIL_MACRO(ERR_INSECURE_FNAME, 0);
while (*src == '/') /* chop out doubles... */
src++;
if (*src == '\0') /* ends with a pathsep? */
break; /* we're done, don't add final pathsep to dst. */
prev = dst + 1;
} /* if */
*(dst++) = ch;
} while (ch != '\0');
495
return 1;
496
497
498
} /* sanitizePlatformIndependentPath */
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
* output from sanitizePlatformIndependentPath(), so that it is in a known
* state.
*
* This only finds legitimate segments of a mountpoint. If the mountpoint is
* "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
* all zero. "/a/b" will succeed, though.
*/
static int partOfMountPoint(DirHandle *h, char *fname)
{
/* !!! FIXME: This code feels gross. */
int rc;
size_t len, mntpntlen;
if (h->mountPoint == NULL)
515
return 0;
516
else if (*fname == '\0')
517
return 1;
518
519
520
521
len = strlen(fname);
mntpntlen = strlen(h->mountPoint);
if (len > mntpntlen) /* can't be a subset of mountpoint. */
522
return 0;
523
524
525
/* if true, must be not a match or a complete match, but not a subset. */
if ((len + 1) == mntpntlen)
526
return 0;
527
528
529
rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
if (rc != 0)
530
return 0; /* not a match. */
531
532
/* make sure /a/b matches /a/b/ and not /a/bc ... */
533
return h->mountPoint[len] == '/';
534
535
536
} /* partOfMountPoint */
537
538
539
static DirHandle *createDirHandle(const char *newDir,
const char *mountPoint,
int forWriting)
540
541
{
DirHandle *dirHandle = NULL;
542
char *tmpmntpnt = NULL;
543
544
GOTO_IF_MACRO(!newDir, ERR_INVALID_ARGUMENT, badDirHandle);
545
546
if (mountPoint != NULL)
{
547
548
549
550
const size_t len = strlen(mountPoint) + 1;
tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
GOTO_IF_MACRO(!tmpmntpnt, ERR_OUT_OF_MEMORY, badDirHandle);
if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
551
goto badDirHandle;
552
mountPoint = tmpmntpnt; /* sanitized version. */
553
} /* if */
554
555
dirHandle = openDirectory(newDir, forWriting);
556
GOTO_IF_MACRO(!dirHandle, NULL, badDirHandle);
557
558
dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
559
560
561
562
563
GOTO_IF_MACRO(!dirHandle->dirName, ERR_OUT_OF_MEMORY, badDirHandle);
strcpy(dirHandle->dirName, newDir);
if ((mountPoint != NULL) && (*mountPoint != '\0'))
{
564
dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
565
566
567
568
569
GOTO_IF_MACRO(!dirHandle->mountPoint, ERR_OUT_OF_MEMORY, badDirHandle);
strcpy(dirHandle->mountPoint, mountPoint);
strcat(dirHandle->mountPoint, "/");
} /* if */
570
__PHYSFS_smallFree(tmpmntpnt);
571
return dirHandle;
572
573
574
badDirHandle:
if (dirHandle != NULL)
575
{
576
dirHandle->funcs->dirClose(dirHandle->opaque);
577
578
579
allocator.Free(dirHandle->dirName);
allocator.Free(dirHandle->mountPoint);
allocator.Free(dirHandle);
580
} /* if */
581
582
__PHYSFS_smallFree(tmpmntpnt);
583
return NULL;
584
} /* createDirHandle */
585
586
587
/* MAKE SURE you've got the stateLock held before calling this! */
588
static int freeDirHandle(DirHandle *dh, FileHandle *openList)
589
{
590
FileHandle *i;
591
592
if (dh == NULL)
593
return 1;
594
595
for (i = openList; i != NULL; i = i->next)
596
BAIL_IF_MACRO(i->dirHandle == dh, ERR_FILES_STILL_OPEN, 0);
597
598
dh->funcs->dirClose(dh->opaque);
599
600
601
allocator.Free(dh->dirName);
allocator.Free(dh->mountPoint);
allocator.Free(dh);
602
return 1;
603
} /* freeDirHandle */
604
605
606
static char *calculateUserDir(void)
607
608
609
610
611
612
{
char *retval = NULL;
const char *str = NULL;
str = __PHYSFS_platformGetUserDir();
if (str != NULL)
613
retval = (char *) str;
614
615
616
617
618
619
else
{
const char *dirsep = PHYSFS_getDirSeparator();
const char *uname = __PHYSFS_platformGetUserName();
str = (uname != NULL) ? uname : "default";
620
621
retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
strlen(dirsep) + 6);
622
623
624
625
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
else
626
sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
627
628
allocator.Free((void *) uname);
629
630
} /* else */
631
return retval;
632
633
634
} /* calculateUserDir */
635
636
637
638
639
640
static int appendDirSep(char **dir)
{
const char *dirsep = PHYSFS_getDirSeparator();
char *ptr;
if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
641
return 1;
642
643
ptr = (char *) allocator.Realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
644
645
if (!ptr)
{
646
allocator.Free(*dir);
647
return 0;
648
649
650
651
} /* if */
strcat(ptr, dirsep);
*dir = ptr;
652
return 1;
653
654
655
} /* appendDirSep */
656
657
static char *calculateBaseDir(const char *argv0)
{
658
659
660
char *retval = NULL;
const char *dirsep = NULL;
char *ptr = NULL;
661
662
/* Give the platform layer first shot at this. */
663
664
retval = __PHYSFS_platformCalcBaseDir(argv0);
if (retval != NULL)
665
return retval;
666
667
668
/* We need argv0 to go on. */
BAIL_IF_MACRO(argv0 == NULL, ERR_ARGV0_IS_NULL, NULL);
669
670
671
672
673
dirsep = PHYSFS_getDirSeparator();
if (strlen(dirsep) == 1) /* fast path. */
ptr = strrchr(argv0, *dirsep);
else
674
{
675
676
ptr = strstr(argv0, dirsep);
if (ptr != NULL)
677
{
678
679
680
681
682
683
684
685
char *p = ptr;
while (p != NULL)
{
ptr = p;
p = strstr(p + 1, dirsep);
} /* while */
} /* if */
} /* else */
686
687
688
689
if (ptr != NULL)
{
size_t size = (size_t) (ptr - argv0);
690
retval = (char *) allocator.Malloc(size + 1);
691
692
693
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
memcpy(retval, argv0, size);
retval[size] = '\0';
694
return retval;
695
696
} /* if */
697
698
/* argv0 wasn't helpful. */
BAIL_MACRO(ERR_INVALID_ARGUMENT, NULL);
699
700
701
} /* calculateBaseDir */
702
703
704
705
706
707
708
709
710
711
static int initializeMutexes(void)
{
errorLock = __PHYSFS_platformCreateMutex();
if (errorLock == NULL)
goto initializeMutexes_failed;
stateLock = __PHYSFS_platformCreateMutex();
if (stateLock == NULL)
goto initializeMutexes_failed;
712
return 1; /* success. */
713
714
715
716
717
718
719
720
721
initializeMutexes_failed:
if (errorLock != NULL)
__PHYSFS_platformDestroyMutex(errorLock);
if (stateLock != NULL)
__PHYSFS_platformDestroyMutex(stateLock);
errorLock = stateLock = NULL;
722
return 0; /* failed. */
723
724
725
} /* initializeMutexes */
726
727
static void setDefaultAllocator(void);
728
729
int PHYSFS_init(const char *argv0)
{
730
731
char *ptr;
732
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
733
734
735
736
if (!externalAllocator)
setDefaultAllocator();
737
738
if (allocator.Init != NULL)
BAIL_IF_MACRO(!allocator.Init(), NULL, 0);
739
740
BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
741
742
743
BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
744
baseDir = calculateBaseDir(argv0);
745
BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
746
747
/* !!! FIXME: only call this if we got this from argv0 (unreliable). */
748
ptr = __PHYSFS_platformRealPath(baseDir);
749
allocator.Free(baseDir);
750
751
752
BAIL_IF_MACRO(ptr == NULL, NULL, 0);
baseDir = ptr;
753
BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
754
755
userDir = calculateUserDir();
756
757
758
if (userDir != NULL)
{
ptr = __PHYSFS_platformRealPath(userDir);
759
allocator.Free(userDir);
760
761
762
userDir = ptr;
} /* if */
763
if ((userDir == NULL) || (!appendDirSep(&userDir)))
764
{
765
allocator.Free(baseDir);
766
baseDir = NULL;
767
return 0;
768
769
} /* if */
770
initialized = 1;
771
772
773
/* This makes sure that the error subsystem is initialized. */
__PHYSFS_setError(PHYSFS_getLastError());
774
775
return 1;
776
777
778
} /* PHYSFS_init */
779
/* MAKE SURE you hold stateLock before calling this! */
780
static int closeFileHandleList(FileHandle **list)
781
{
782
783
FileHandle *i;
FileHandle *next = NULL;
784
785
786
787
for (i = *list; i != NULL; i = next)
{
next = i->next;
788
if (!i->funcs->fileClose(i->opaque))
789
790
{
*list = i;
791
return 0;
792
793
} /* if */
794
allocator.Free(i);
795
796
797
} /* for */
*list = NULL;
798
return 1;
799
} /* closeFileHandleList */
800
801
802
/* MAKE SURE you hold the stateLock before calling this! */
803
804
static void freeSearchPath(void)
{
805
806
DirHandle *i;
DirHandle *next = NULL;
807
808
809
closeFileHandleList(&openReadList);
810
811
812
813
if (searchPath != NULL)
{
for (i = searchPath; i != NULL; i = next)
{
814
next = i->next;
815
freeDirHandle(i, openReadList);
816
817
818
819
820
821
} /* for */
searchPath = NULL;
} /* if */
} /* freeSearchPath */
822
int PHYSFS_deinit(void)
823
824
{
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
825
BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
826
827
closeFileHandleList(&openWriteList);
828
829
BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
830
831
832
freeSearchPath();
freeErrorMessages();
833
if (baseDir != NULL)
834
{
835
allocator.Free(baseDir);
836
837
838
839
840
baseDir = NULL;
} /* if */
if (userDir != NULL)
{
841
allocator.Free(userDir);
842
843
userDir = NULL;
} /* if */
844
845
allowSymLinks = 0;
846
initialized = 0;
847
848
849
850
__PHYSFS_platformDestroyMutex(errorLock);
__PHYSFS_platformDestroyMutex(stateLock);
851
852
if (allocator.Deinit != NULL)
allocator.Deinit();
853
854
errorLock = stateLock = NULL;
855
return 1;
856
857
858
} /* PHYSFS_deinit */
859
860
int PHYSFS_isInit(void)
{
861
return initialized;
862
863
864
} /* PHYSFS_isInit */
865
866
const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
{
867
return supported_types;
868
869
870
871
872
873
} /* PHYSFS_supportedArchiveTypes */
void PHYSFS_freeList(void *list)
{
void **i;
874
875
876
877
if (list != NULL)
{
for (i = (void **) list; *i != NULL; i++)
allocator.Free(*i);
878
879
880
allocator.Free(list);
} /* if */
881
882
883
884
885
} /* PHYSFS_freeList */
const char *PHYSFS_getDirSeparator(void)
{
886
return __PHYSFS_platformDirSeparator;
887
888
889
890
891
} /* PHYSFS_getDirSeparator */
char **PHYSFS_getCdRomDirs(void)
{
892
return doEnumStringList(__PHYSFS_platformDetectAvailableCDs);
893
894
895
} /* PHYSFS_getCdRomDirs */
896
897
898
899
900
901
void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data)
{
__PHYSFS_platformDetectAvailableCDs(callback, data);
} /* PHYSFS_getCdRomDirsCallback */
902
903
const char *PHYSFS_getBaseDir(void)
{
904
return baseDir; /* this is calculated in PHYSFS_init()... */
905
906
907
908
909
} /* PHYSFS_getBaseDir */
const char *PHYSFS_getUserDir(void)
{
910
return userDir; /* this is calculated in PHYSFS_init()... */
911
912
913
914
915
} /* PHYSFS_getUserDir */
const char *PHYSFS_getWriteDir(void)
{
916
917
918
919
920
921
const char *retval = NULL;
__PHYSFS_platformGrabMutex(stateLock);
if (writeDir != NULL)
retval = writeDir->dirName;
__PHYSFS_platformReleaseMutex(stateLock);
922
923
return retval;
924
925
926
927
928
} /* PHYSFS_getWriteDir */
int PHYSFS_setWriteDir(const char *newDir)
{
929
930
931
932
int retval = 1;
__PHYSFS_platformGrabMutex(stateLock);
933
934
if (writeDir != NULL)
{
935
BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), NULL,
936
stateLock, 0);
937
938
939
writeDir = NULL;
} /* if */
940
941
if (newDir != NULL)
{
942
writeDir = createDirHandle(newDir, NULL, 1);
943
retval = (writeDir != NULL);
944
} /* if */
945
946
947
__PHYSFS_platformReleaseMutex(stateLock);
948
return retval;
949
950
951
} /* PHYSFS_setWriteDir */
952
int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
953
{
954
955
956
DirHandle *dh;
DirHandle *prev = NULL;
DirHandle *i;
957
958
BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
959
960
961
if (mountPoint == NULL)
mountPoint = "/";
962
963
__PHYSFS_platformGrabMutex(stateLock);
964
965
966
967
968
for (i = searchPath; i != NULL; i = i->next)
{
/* already in search path? */
BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
969
prev = i;
970
} /* for */
971
972
dh = createDirHandle(newDir, mountPoint, 0);
973
BAIL_IF_MACRO_MUTEX(dh == NULL, NULL, stateLock, 0);
974
975
976
977
if (appendToPath)
{
if (prev == NULL)
978
searchPath = dh;
979
else
980
prev->next = dh;
981
982
983
} /* if */
else
{
984
985
dh->next = searchPath;
searchPath = dh;
986
987
} /* else */
988
__PHYSFS_platformReleaseMutex(stateLock);
989
return 1;
990
991
992
993
994
} /* PHYSFS_mount */
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
{
995
return PHYSFS_mount(newDir, NULL, appendToPath);
996
997
998
999
1000
} /* PHYSFS_addToSearchPath */
int PHYSFS_removeFromSearchPath(const char *oldDir)
{