Skip to content

Latest commit

 

History

History
2300 lines (1834 loc) · 62.5 KB

physfs.c

File metadata and controls

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