Skip to content

Latest commit

 

History

History
1146 lines (904 loc) · 26.1 KB

physfs.c

File metadata and controls

1146 lines (904 loc) · 26.1 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "physfs.h"
Jul 6, 2001
Jul 6, 2001
17
18
19
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
20
21
22
23
24
25
26
27
typedef struct __PHYSFS_ERRMSGTYPE__
{
int tid;
int errorAvailable;
char errorString[80];
struct __PHYSFS_ERRMSGTYPE__ *next;
} ErrMsg;
Jul 7, 2001
Jul 7, 2001
28
typedef struct __PHYSFS_DIRINFO__
29
30
{
char *dirName;
Jul 7, 2001
Jul 7, 2001
31
32
33
DirHandle *dirHandle;
struct __PHYSFS_DIRINFO__ *next;
} DirInfo;
Jul 6, 2001
Jul 6, 2001
35
36
typedef struct __PHYSFS_FILEHANDLELIST__
{
Jul 7, 2001
Jul 7, 2001
37
PHYSFS_file *handle;
Jul 6, 2001
Jul 6, 2001
38
39
struct __PHYSFS_FILEHANDLELIST__ *next;
} FileHandleList;
Jul 6, 2001
Jul 6, 2001
41
42
/* The various i/o drivers... */
Jul 6, 2001
Jul 6, 2001
43
44
#if (defined PHYSFS_SUPPORTS_ZIP)
Jul 6, 2001
Jul 6, 2001
45
46
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
Jul 6, 2001
Jul 6, 2001
47
#endif
Jul 6, 2001
Jul 6, 2001
49
extern const DirFunctions __PHYSFS_DirFunctions_DIR;
Jul 6, 2001
Jul 6, 2001
50
Jul 6, 2001
Jul 6, 2001
51
static const PHYSFS_ArchiveInfo *supported_types[] =
52
53
{
#if (defined PHYSFS_SUPPORTS_ZIP)
Jul 6, 2001
Jul 6, 2001
54
&__PHYSFS_ArchiveInfo_ZIP,
55
56
#endif
Jul 8, 2001
Jul 8, 2001
57
58
59
60
#if (defined PHYSFS_SUPPORTS_GRP)
&__PHYSFS_ArchiveInfo_GRP,
#endif
61
62
63
NULL
};
Jul 6, 2001
Jul 6, 2001
64
65
66
67
68
69
static const DirFunctions *dirFunctions[] =
{
#if (defined PHYSFS_SUPPORTS_ZIP)
&__PHYSFS_DirFunctions_ZIP,
#endif
Jul 8, 2001
Jul 8, 2001
70
71
72
73
#if (defined PHYSFS_SUPPORTS_GRP)
&__PHYSFS_DirFunctions_GRP,
#endif
Jul 6, 2001
Jul 6, 2001
74
75
76
&__PHYSFS_DirFunctions_DIR,
NULL
};
Jul 6, 2001
Jul 6, 2001
80
81
82
83
/* General PhysicsFS state ... */
static int initialized = 0;
static ErrMsg *errorMessages = NULL;
Jul 7, 2001
Jul 7, 2001
84
85
static DirInfo *searchPath = NULL;
static DirInfo *writeDir = NULL;
Jul 6, 2001
Jul 6, 2001
86
87
88
89
90
91
92
93
94
95
static FileHandleList *openWriteList = NULL;
static FileHandleList *openReadList = NULL;
static char *baseDir = NULL;
static char *userDir = NULL;
static int allowSymLinks = 0;
/* functions ... */
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
static ErrMsg *findErrorForCurrentThread(void)
{
ErrMsg *i;
int tid;
if (errorMessages != NULL)
{
tid = __PHYSFS_platformGetThreadID();
for (i = errorMessages; i != NULL; i = i->next)
{
if (i->tid == tid)
return(i);
} /* for */
} /* if */
return(NULL); /* no error available. */
} /* findErrorForCurrentThread */
Jul 6, 2001
Jul 6, 2001
116
void __PHYSFS_setError(const char *str)
Jul 7, 2001
Jul 7, 2001
118
119
120
121
122
123
ErrMsg *err;
if (str == NULL)
return;
err = findErrorForCurrentThread();
124
125
126
127
128
129
130
131
if (err == NULL)
{
err = (ErrMsg *) malloc(sizeof (ErrMsg));
if (err == NULL)
return; /* uhh...? */
err->tid = __PHYSFS_platformGetThreadID();
Jul 6, 2001
Jul 6, 2001
132
133
err->next = errorMessages;
errorMessages = err;
134
135
136
137
138
} /* if */
err->errorAvailable = 1;
strncpy(err->errorString, str, sizeof (err->errorString));
err->errorString[sizeof (err->errorString) - 1] = '\0';
Jul 6, 2001
Jul 6, 2001
139
} /* __PHYSFS_setError */
Jul 7, 2001
Jul 7, 2001
142
143
144
145
146
147
148
149
150
151
152
153
154
static void freeErrorMessages(void)
{
ErrMsg *i;
ErrMsg *next;
for (i = errorMessages; i != NULL; i = next)
{
next = i;
free(i);
} /* for */
} /* freeErrorMessages */
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const char *PHYSFS_getLastError(void)
{
ErrMsg *err = findErrorForCurrentThread();
if ((err == NULL) || (!err->errorAvailable))
return(NULL);
err->errorAvailable = 0;
return(err->errorString);
} /* PHYSFS_getLastError */
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 7, 2001
Jul 7, 2001
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
static DirHandle *openDirectory(const char *d, int forWriting)
{
const DirFunctions **i;
for (i = dirFunctions; *i != NULL; i++)
{
if ((*i)->isArchive(d, forWriting))
return( (*i)->openArchive(d, forWriting) );
} /* for */
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
return(NULL);
} /* openDirectory */
static DirInfo *buildDirInfo(const char *newDir, int forWriting)
{
DirHandle *dirHandle = NULL;
DirInfo *di = NULL;
BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
dirHandle = openDirectory(newDir, forWriting);
BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
di = (DirInfo *) malloc(sizeof (DirInfo));
if (di == NULL)
Jul 8, 2001
Jul 8, 2001
205
dirHandle->funcs->dirClose(dirHandle);
Jul 7, 2001
Jul 7, 2001
206
207
208
209
210
211
BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
di->dirName = (char *) malloc(strlen(newDir) + 1);
if (di->dirName == NULL)
{
free(di);
Jul 8, 2001
Jul 8, 2001
212
dirHandle->funcs->dirClose(dirHandle);
Jul 7, 2001
Jul 7, 2001
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
return(0);
} /* if */
di->next = NULL;
di->dirHandle = dirHandle;
strcpy(di->dirName, newDir);
return(di);
} /* buildDirInfo */
static int freeDirInfo(DirInfo *di, FileHandleList *openList)
{
FileHandleList *i;
if (di == NULL)
return(1);
for (i = openList; i != NULL; i = i->next)
{
const DirHandle *h = ((FileHandle *) i->handle->opaque)->dirHandle;
BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
} /* for */
Jul 8, 2001
Jul 8, 2001
237
di->dirHandle->funcs->dirClose(di->dirHandle);
Jul 7, 2001
Jul 7, 2001
238
239
240
241
242
243
244
free(di->dirName);
free(di);
return(1);
} /* freeDirInfo */
static char *calculateUserDir(void)
Jul 6, 2001
Jul 6, 2001
245
246
247
248
249
250
{
char *retval = NULL;
const char *str = NULL;
str = __PHYSFS_platformGetUserDir();
if (str != NULL)
Jul 7, 2001
Jul 7, 2001
251
retval = (char *) str;
Jul 6, 2001
Jul 6, 2001
252
253
254
255
256
257
else
{
const char *dirsep = PHYSFS_getDirSeparator();
const char *uname = __PHYSFS_platformGetUserName();
str = (uname != NULL) ? uname : "default";
Jul 7, 2001
Jul 7, 2001
258
259
retval = (char *) malloc(strlen(baseDir) + strlen(str) +
(strlen(dirsep) * 2) + 6);
Jul 6, 2001
Jul 6, 2001
260
261
262
263
264
265
266
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
else
sprintf(retval, "%s%susers%s%s", baseDir, dirsep, dirsep, str);
if (uname != NULL)
Jul 7, 2001
Jul 7, 2001
267
free((void *) uname);
Jul 6, 2001
Jul 6, 2001
268
269
270
271
272
273
} /* else */
return(retval);
} /* calculateUserDir */
Jul 7, 2001
Jul 7, 2001
274
275
276
277
278
279
static char *calculateBaseDir(const char *argv0)
{
assert(0); return(NULL);
} /* calculateBaseDir */
280
281
282
283
284
int PHYSFS_init(const char *argv0)
{
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
BAIL_IF_MACRO(argv0 == NULL, ERR_INVALID_ARGUMENT, 0);
Jul 6, 2001
Jul 6, 2001
285
baseDir = calculateBaseDir(argv0);
Jul 7, 2001
Jul 7, 2001
286
BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
Jul 6, 2001
Jul 6, 2001
287
288
289
290
291
292
293
294
295
userDir = calculateUserDir();
if (userDir == NULL)
{
free(baseDir);
baseDir = NULL;
return(0);
} /* if */
296
297
298
299
300
initialized = 1;
return(1);
} /* PHYSFS_init */
Jul 7, 2001
Jul 7, 2001
301
static int closeFileHandleList(FileHandleList **list)
Jul 6, 2001
Jul 6, 2001
302
303
304
{
FileHandleList *i;
FileHandleList *next = NULL;
Jul 7, 2001
Jul 7, 2001
305
FileHandle *h;
Jul 6, 2001
Jul 6, 2001
306
307
308
309
for (i = *list; i != NULL; i = next)
{
next = i->next;
Jul 7, 2001
Jul 7, 2001
310
h = (FileHandle *) (i->handle->opaque);
Jul 8, 2001
Jul 8, 2001
311
if (!h->funcs->fileClose(i->handle->opaque))
Jul 7, 2001
Jul 7, 2001
312
313
314
315
316
317
318
{
*list = i;
return(0);
} /* if */
free(i->handle);
free(i);
Jul 6, 2001
Jul 6, 2001
319
320
321
} /* for */
*list = NULL;
Jul 7, 2001
Jul 7, 2001
322
323
return(1);
} /* closeFileHandleList */
Jul 6, 2001
Jul 6, 2001
324
325
326
327
static void freeSearchPath(void)
{
Jul 7, 2001
Jul 7, 2001
328
329
DirInfo *i;
DirInfo *next = NULL;
Jul 6, 2001
Jul 6, 2001
331
332
closeFileHandleList(&openReadList);
333
334
335
336
337
if (searchPath != NULL)
{
for (i = searchPath; i != NULL; i = next)
{
next = i;
Jul 7, 2001
Jul 7, 2001
338
freeDirInfo(i, openReadList);
339
340
341
342
343
344
} /* for */
searchPath = NULL;
} /* if */
} /* freeSearchPath */
Jul 7, 2001
Jul 7, 2001
345
int PHYSFS_deinit(void)
346
347
348
{
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
Jul 6, 2001
Jul 6, 2001
349
closeFileHandleList(&openWriteList);
Jul 7, 2001
Jul 7, 2001
350
351
BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
352
353
354
freeSearchPath();
freeErrorMessages();
Jul 6, 2001
Jul 6, 2001
355
if (baseDir != NULL)
Jul 6, 2001
Jul 6, 2001
356
{
Jul 6, 2001
Jul 6, 2001
357
free(baseDir);
Jul 6, 2001
Jul 6, 2001
358
359
360
361
362
363
364
365
baseDir = NULL;
} /* if */
if (userDir != NULL)
{
free(userDir);
userDir = NULL;
} /* if */
Jul 6, 2001
Jul 6, 2001
366
367
allowSymLinks = 0;
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
initialized = 0;
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
391
return(__PHYSFS_platformDirSeparator);
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
} /* 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
409
return(userDir); /* this is calculated in PHYSFS_init()... */
410
411
412
413
414
} /* PHYSFS_getUserDir */
const char *PHYSFS_getWriteDir(void)
{
Jul 7, 2001
Jul 7, 2001
415
416
417
418
if (writeDir == NULL)
return(NULL);
return(writeDir->dirName);
419
420
421
422
423
424
425
} /* PHYSFS_getWriteDir */
int PHYSFS_setWriteDir(const char *newDir)
{
if (writeDir != NULL)
{
Jul 7, 2001
Jul 7, 2001
426
BAIL_IF_MACRO(!freeDirInfo(writeDir, openWriteList), NULL, 0);
427
428
429
writeDir = NULL;
} /* if */
Jul 6, 2001
Jul 6, 2001
430
431
if (newDir != NULL)
{
Jul 7, 2001
Jul 7, 2001
432
433
writeDir = buildDirInfo(newDir, 1);
return(writeDir != NULL);
Jul 6, 2001
Jul 6, 2001
434
} /* if */
435
436
437
438
439
440
441
return(1);
} /* PHYSFS_setWriteDir */
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
{
Jul 7, 2001
Jul 7, 2001
442
DirInfo *di = buildDirInfo(newDir, 0);
Jul 7, 2001
Jul 7, 2001
444
BAIL_IF_MACRO(di == NULL, NULL, 0);
445
446
447
if (appendToPath)
{
Jul 7, 2001
Jul 7, 2001
448
449
di->next = searchPath;
searchPath = di;
450
451
452
} /* if */
else
{
Jul 7, 2001
Jul 7, 2001
453
454
DirInfo *i = searchPath;
DirInfo *prev = NULL;
Jul 7, 2001
Jul 7, 2001
456
di->next = NULL;
457
458
459
460
while (i != NULL)
prev = i;
if (prev == NULL)
Jul 7, 2001
Jul 7, 2001
461
searchPath = di;
462
else
Jul 7, 2001
Jul 7, 2001
463
prev->next = di;
464
465
466
467
468
469
470
471
} /* else */
return(1);
} /* PHYSFS_addToSearchPath */
int PHYSFS_removeFromSearchPath(const char *oldDir)
{
Jul 7, 2001
Jul 7, 2001
472
473
474
DirInfo *i;
DirInfo *prev = NULL;
DirInfo *next = NULL;
475
476
477
478
479
480
481
BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
for (i = searchPath; i != NULL; i = i->next)
{
if (strcmp(i->dirName, oldDir) == 0)
{
Jul 6, 2001
Jul 6, 2001
482
next = i->next;
Jul 7, 2001
Jul 7, 2001
483
BAIL_IF_MACRO(!freeDirInfo(i, openReadList), NULL, 0);
Jul 6, 2001
Jul 6, 2001
484
485
if (prev == NULL)
Jul 6, 2001
Jul 6, 2001
486
searchPath = next;
487
else
Jul 6, 2001
Jul 6, 2001
488
prev->next = next;
Jul 6, 2001
Jul 6, 2001
489
490
491
492
493
494
return(1);
} /* if */
prev = i;
} /* for */
Jul 6, 2001
Jul 6, 2001
495
__PHYSFS_setError(ERR_NOT_IN_SEARCH_PATH);
496
497
498
499
500
501
502
503
return(0);
} /* PHYSFS_removeFromSearchPath */
char **PHYSFS_getSearchPath(void)
{
int count = 1;
int x;
Jul 7, 2001
Jul 7, 2001
504
DirInfo *i;
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
char **retval;
for (i = searchPath; i != NULL; i = i->next)
count++;
retval = (char **) malloc(sizeof (char *) * count);
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
count--;
retval[count] = NULL;
for (i = searchPath, x = 0; x < count; i = i->next, x++)
{
retval[x] = (char *) malloc(strlen(i->dirName) + 1);
if (retval[x] == NULL) /* this is friggin' ugly. */
{
while (x > 0)
{
x--;
free(retval[x]);
} /* while */
free(retval);
Jul 6, 2001
Jul 6, 2001
527
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
528
529
530
531
532
533
534
535
536
537
return(NULL);
} /* if */
strcpy(retval[x], i->dirName);
} /* for */
return(retval);
} /* PHYSFS_getSearchPath */
Jul 6, 2001
Jul 6, 2001
538
int PHYSFS_setSaneConfig(const char *appName, const char *archiveExt,
539
540
int includeCdRoms, int archivesFirst)
{
Jul 6, 2001
Jul 6, 2001
541
542
543
544
545
546
547
548
549
550
551
552
const char *basedir = PHYSFS_getBaseDir();
const char *userdir = PHYSFS_getUserDir();
const char *dirsep = PHYSFS_getDirSeparator();
char *str;
int rc;
/* set write dir... */
str = malloc(strlen(userdir) + (strlen(appName) * 2) +
(strlen(dirsep) * 2) + 2);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
sprintf(str, "%s%s.%s", userdir, dirsep, appName);
rc = PHYSFS_setWriteDir(str);
Jul 7, 2001
Jul 7, 2001
553
BAIL_IF_MACRO(!rc, NULL, 0);
Jul 6, 2001
Jul 6, 2001
554
555
556
557
/* Put write dir related dirs on search path... */
PHYSFS_addToSearchPath(str, 1);
PHYSFS_mkdir(appName); /* don't care if this fails. */
Jul 7, 2001
Jul 7, 2001
558
strcat(str, dirsep);
Jul 6, 2001
Jul 6, 2001
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
strcat(str, appName);
PHYSFS_addToSearchPath(str, 1);
free(str);
/* Put base path stuff on search path... */
PHYSFS_addToSearchPath(basedir, 1);
str = malloc(strlen(basedir) + (strlen(appName) * 2) +
(strlen(dirsep) * 2) + 2);
if (str != NULL)
{
sprintf(str, "%s%s.%s", basedir, dirsep, appName);
PHYSFS_addToSearchPath(str, 1);
free(str);
} /* if */
/* handle CD-ROMs... */
if (includeCdRoms)
{
char **cds = PHYSFS_getCdRomDirs();
char **i;
for (i = cds; *i != NULL; i++)
{
Jul 7, 2001
Jul 7, 2001
581
PHYSFS_addToSearchPath(*i, 1);
Jul 6, 2001
Jul 6, 2001
582
583
584
585
str = malloc(strlen(*i) + strlen(appName) + strlen(dirsep) + 1);
if (str != NULL)
{
sprintf(str, "%s%s%s", *i, dirsep, appName);
Jul 7, 2001
Jul 7, 2001
586
PHYSFS_addToSearchPath(str, 1);
Jul 6, 2001
Jul 6, 2001
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
free(str);
} /* if */
} /* for */
PHYSFS_freeList(cds);
} /* if */
/* Root out archives, and add them to search path... */
if (archiveExt != NULL)
{
char **rc = PHYSFS_enumerateFiles("");
char **i;
int extlen = strlen(archiveExt);
char *ext;
for (i = rc; *i != NULL; i++)
{
int l = strlen(*i);
if ((l > extlen) && ((*i)[l - extlen - 1] == '.'));
{
ext = (*i) + (l - extlen);
if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
{
const char *d = PHYSFS_getRealDir(*i);
str = malloc(strlen(d) + strlen(dirsep) + l + 1);
if (str != NULL)
{
sprintf(str, "%s%s%s", d, dirsep, *i);
Jul 7, 2001
Jul 7, 2001
614
PHYSFS_addToSearchPath(str, archivesFirst == 0);
Jul 6, 2001
Jul 6, 2001
615
616
617
618
619
620
621
622
623
624
free(str);
} /* if */
} /* if */
} /* if */
} /* for */
PHYSFS_freeList(rc);
} /* if */
return(1);
625
626
627
} /* PHYSFS_setSaneConfig */
Jul 7, 2001
Jul 7, 2001
628
629
630
631
632
633
void PHYSFS_permitSymbolicLinks(int allow)
{
allowSymLinks = allow;
} /* PHYSFS_permitSymbolicLinks */
Jul 6, 2001
Jul 6, 2001
634
/* string manipulation in C makes my ass itch. */
Jul 8, 2001
Jul 8, 2001
635
636
637
char *__PHYSFS_convertToDependent(const char *prepend,
const char *dirName,
const char *append)
Jul 6, 2001
Jul 6, 2001
638
639
640
641
642
643
644
645
{
const char *dirsep = PHYSFS_getDirSeparator();
int sepsize = strlen(dirsep);
char *str;
char *i1;
char *i2;
size_t allocSize;
Jul 7, 2001
Jul 7, 2001
646
allocSize = strlen(dirName) + 1;
Jul 6, 2001
Jul 6, 2001
647
648
649
650
651
652
653
654
if (prepend != NULL)
allocSize += strlen(prepend) + sepsize;
if (append != NULL)
allocSize += strlen(append) + sepsize;
/* make sure there's enough space if the dir separator is bigger. */
if (sepsize > 1)
{
Jul 7, 2001
Jul 7, 2001
655
656
str = (char *) dirName;
do
Jul 6, 2001
Jul 6, 2001
657
{
Jul 7, 2001
Jul 7, 2001
658
659
660
str = strchr(str, '/');
if (str != NULL)
{
Jul 6, 2001
Jul 6, 2001
661
allocSize += (sepsize - 1);
Jul 7, 2001
Jul 7, 2001
662
663
664
str++;
} /* if */
} while (str != NULL);
Jul 6, 2001
Jul 6, 2001
665
666
667
668
669
} /* if */
str = (char *) malloc(allocSize);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
Jul 8, 2001
Jul 8, 2001
670
671
672
if (prepend == NULL)
*str = '\0';
else
Jul 6, 2001
Jul 6, 2001
673
674
675
{
strcpy(str, prepend);
strcat(str, dirsep);
Jul 8, 2001
Jul 8, 2001
676
} /* else */
Jul 6, 2001
Jul 6, 2001
677
Jul 7, 2001
Jul 7, 2001
678
for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
Jul 6, 2001
Jul 6, 2001
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
{
if (*i1 == '/')
{
strcpy(i2, dirsep);
i2 += sepsize;
} /* if */
else
{
*i2 = *i1;
} /* else */
} /* for */
*i2 = '\0';
if (append)
{
strcat(str, dirsep);
strcpy(str, append);
} /* if */
return(str);
Jul 7, 2001
Jul 7, 2001
699
} /* __PHYSFS_convertToDependentNotation */
Jul 6, 2001
Jul 6, 2001
700
701
Jul 7, 2001
Jul 7, 2001
702
int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
Jul 7, 2001
Jul 7, 2001
704
705
706
int retval = 1;
char *start;
char *end;
Jul 6, 2001
Jul 6, 2001
707
708
char *str;
Jul 7, 2001
Jul 7, 2001
709
710
711
start = str = malloc(strlen(fname) + 1);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
strcpy(str, fname);
Jul 6, 2001
Jul 6, 2001
712
Jul 7, 2001
Jul 7, 2001
713
714
715
716
717
718
719
720
while (1)
{
end = strchr(start, '/');
if (end != NULL)
*end = '\0';
if ( (strcmp(start, ".") == 0) ||
(strcmp(start, "..") == 0) ||
Jul 8, 2001
Jul 8, 2001
721
(strchr(start, '\\') != NULL) ||
Jul 7, 2001
Jul 7, 2001
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
(strchr(start, ':') != NULL) )
{
__PHYSFS_setError(ERR_INSECURE_FNAME);
retval = 0;
break;
} /* if */
if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
{
__PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
retval = 0;
break;
} /* if */
if (end == NULL)
break;
*end = '/';
start = end + 1;
} /* while */
Jul 6, 2001
Jul 6, 2001
742
743
free(str);
Jul 7, 2001
Jul 7, 2001
744
745
return(retval);
} /* __PHYSFS_verifySecurity */
Jul 7, 2001
Jul 7, 2001
748
int PHYSFS_mkdir(const char *dirName)
Jul 7, 2001
Jul 7, 2001
750
DirHandle *h;
Jul 6, 2001
Jul 6, 2001
751
char *str;
Jul 7, 2001
Jul 7, 2001
752
753
754
char *start;
char *end;
int retval = 0;
Jul 6, 2001
Jul 6, 2001
755
Jul 7, 2001
Jul 7, 2001
756
757
758
759
BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
h = writeDir->dirHandle;
BAIL_IF_MACRO(h->funcs->mkdir == NULL, ERR_NOT_SUPPORTED, 0);
BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, dirName), NULL, 0);
Jul 6, 2001
Jul 6, 2001
760
Jul 7, 2001
Jul 7, 2001
761
762
763
start = str = malloc(strlen(dirName) + 1);
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
strcpy(str, dirName);
Jul 6, 2001
Jul 6, 2001
764
Jul 7, 2001
Jul 7, 2001
765
766
767
768
769
while (1)
{
end = strchr(start, '/');
if (end != NULL)
*end = '\0';
Jul 6, 2001
Jul 6, 2001
770
Jul 7, 2001
Jul 7, 2001
771
772
773
retval = h->funcs->mkdir(h, str);
if (!retval)
break;
Jul 6, 2001
Jul 6, 2001
774
Jul 7, 2001
Jul 7, 2001
775
776
if (end == NULL)
break;
Jul 7, 2001
Jul 7, 2001
778
779
780
*end = '/';
start = end + 1;
} /* while */
Jul 7, 2001
Jul 7, 2001
782
783
784
785
786
787
free(str);
return(retval);
} /* PHYSFS_mkdir */
int PHYSFS_delete(const char *fname)
Jul 7, 2001
Jul 7, 2001
789
790
791
792
793
794
795
DirHandle *h;
BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
h = writeDir->dirHandle;
BAIL_IF_MACRO(h->funcs->remove == NULL, ERR_NOT_SUPPORTED, 0);
BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, 0);
return(h->funcs->remove(h, fname));
} /* PHYSFS_delete */
796
797
798
799
const char *PHYSFS_getRealDir(const char *filename)
{
Jul 7, 2001
Jul 7, 2001
800
801
802
803
804
805
806
807
808
809
810
811
812
DirInfo *i;
for (i = searchPath; i != NULL; i = i->next)
{
DirHandle *h = i->dirHandle;
if (__PHYSFS_verifySecurity(h, filename))
{
if (h->funcs->exists(h, filename))
return(i->dirName);
} /* if */
} /* for */
return(NULL);
813
814
815
} /* PHYSFS_getRealDir */
Jul 7, 2001
Jul 7, 2001
816
static int countList(LinkedStringList *list)
Jul 6, 2001
Jul 6, 2001
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
{
int retval = 0;
LinkedStringList *i;
assert(list != NULL);
for (i = list; i != NULL; i = i->next)
retval++;
return(retval);
} /* countList */
static char **convertStringListToPhysFSList(LinkedStringList *finalList)
{
int i;
LinkedStringList *next = NULL;
int len = countList(finalList);
char **retval = (char **) malloc((len + 1) * sizeof (char *));
if (retval == NULL)
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
for (i = 0; i < len; i++)
{
next = finalList->next;
if (retval == NULL)
free(finalList->str);
else
retval[i] = finalList->str;
free(finalList);
finalList = next;
} /* for */
if (retval != NULL);
retval[i] = NULL;
return(retval);
} /* convertStringListToPhysFSList */
static void insertStringListItem(LinkedStringList **final,
LinkedStringList *item)
{
LinkedStringList *i;
LinkedStringList *prev = NULL;
int rc;
for (i = *final; i != NULL; i = i->next)
{
rc = strcmp(i->str, item->str);
if (rc == 0) /* already in list. */
{
free(item->str);
free(item);
return;
} /* if */
else if (rc > 0) /* insertion point. */
{
if (prev == NULL)
*final = item;
else
prev->next = item;
item->next = i;
return;
} /* else if */
prev = i;
} /* for */
} /* insertStringListItem */
/* if we run out of memory anywhere in here, we give back what we can. */
static void interpolateStringLists(LinkedStringList **final,
LinkedStringList *newList)
{
LinkedStringList *next = NULL;
while (newList != NULL)
{
next = newList->next;
insertStringListItem(final, newList);
newList = next;
} /* while */
} /* interpolateStringLists */
902
903
904
char **PHYSFS_enumerateFiles(const char *path)
{
Jul 7, 2001
Jul 7, 2001
905
DirInfo *i;
Jul 6, 2001
Jul 6, 2001
906
907
908
909
910
911
char **retval = NULL;
LinkedStringList *rc;
LinkedStringList *finalList = NULL;
for (i = searchPath; i != NULL; i = i->next)
{
Jul 7, 2001
Jul 7, 2001
912
913
914
915
916
917
DirHandle *h = i->dirHandle;
if (__PHYSFS_verifySecurity(h, path))
{
rc = h->funcs->enumerateFiles(h, path);
interpolateStringLists(&finalList, rc);
} /* if */
Jul 6, 2001
Jul 6, 2001
918
919
920
921
} /* for */
retval = convertStringListToPhysFSList(finalList);
return(retval);
922
923
924
} /* PHYSFS_enumerateFiles */
Jul 7, 2001
Jul 7, 2001
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
int PHYSFS_exists(const char *fname)
{
return(PHYSFS_getRealDir(fname) != NULL);
} /* PHYSFS_exists */
int PHYSFS_isDirectory(const char *fname)
{
DirInfo *i;
for (i = searchPath; i != NULL; i = i->next)
{
DirHandle *h = i->dirHandle;
if (__PHYSFS_verifySecurity(h, fname))
{
if (h->funcs->exists(h, fname))
return(h->funcs->isDirectory(h, fname));
} /* if */
} /* for */
return(0);
} /* PHYSFS_isDirectory */
int PHYSFS_isSymbolicLink(const char *fname)
{
DirInfo *i;
if (!allowSymLinks)
return(0);
for (i = searchPath; i != NULL; i = i->next)
{
DirHandle *h = i->dirHandle;
if (__PHYSFS_verifySecurity(h, fname))
{
if (h->funcs->exists(h, fname))
return(h->funcs->isSymLink(h, fname));
} /* if */
} /* for */
return(0);
} /* PHYSFS_isSymbolicLink */
Jul 7, 2001
Jul 7, 2001
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static PHYSFS_file *doOpenWrite(const char *fname, int appending)
{
PHYSFS_file *retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
FileHandle *rc = NULL;
DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
FileHandleList *list;
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
list = (FileHandleList *) malloc(sizeof (FileHandleList));
BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
if (rc == NULL)
{
free(list);
free(retval);
retval = NULL;
} /* if */
else
{
retval->opaque = (void *) rc;
list->handle = retval;
list->next = openWriteList;
openWriteList = list;
} /* else */
return(retval);