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