physfs.c 29 KB

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