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