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