physfs.c 33 KB

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