Skip to content

Latest commit

 

History

History
2086 lines (1646 loc) · 56.3 KB

physfs.c

File metadata and controls

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