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