Skip to content

Latest commit

 

History

History
1993 lines (1572 loc) · 50.4 KB

physfs.c

File metadata and controls

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