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