physfs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  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. while (*path == '/')
  809. path++;
  810. for (i = searchPath; i != NULL; i = i->next)
  811. {
  812. DirHandle *h = i->dirHandle;
  813. if (__PHYSFS_verifySecurity(h, path))
  814. {
  815. rc = h->funcs->enumerateFiles(h, path);
  816. interpolateStringLists(&finalList, rc);
  817. } /* if */
  818. } /* for */
  819. retval = convertStringListToPhysFSList(finalList);
  820. return(retval);
  821. } /* PHYSFS_enumerateFiles */
  822. int PHYSFS_exists(const char *fname)
  823. {
  824. while (*fname == '/')
  825. fname++;
  826. return(PHYSFS_getRealDir(fname) != NULL);
  827. } /* PHYSFS_exists */
  828. int PHYSFS_isDirectory(const char *fname)
  829. {
  830. DirInfo *i;
  831. while (*fname == '/')
  832. fname++;
  833. for (i = searchPath; i != NULL; i = i->next)
  834. {
  835. DirHandle *h = i->dirHandle;
  836. if (__PHYSFS_verifySecurity(h, fname))
  837. {
  838. if (h->funcs->exists(h, fname))
  839. return(h->funcs->isDirectory(h, fname));
  840. } /* if */
  841. } /* for */
  842. return(0);
  843. } /* PHYSFS_isDirectory */
  844. int PHYSFS_isSymbolicLink(const char *fname)
  845. {
  846. DirInfo *i;
  847. if (!allowSymLinks)
  848. return(0);
  849. while (*fname == '/')
  850. fname++;
  851. for (i = searchPath; i != NULL; i = i->next)
  852. {
  853. DirHandle *h = i->dirHandle;
  854. if (__PHYSFS_verifySecurity(h, fname))
  855. {
  856. if (h->funcs->exists(h, fname))
  857. return(h->funcs->isSymLink(h, fname));
  858. } /* if */
  859. } /* for */
  860. return(0);
  861. } /* PHYSFS_isSymbolicLink */
  862. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  863. {
  864. PHYSFS_file *retval = NULL;
  865. FileHandle *rc = NULL;
  866. DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  867. const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
  868. FileHandleList *list;
  869. while (*fname == '/')
  870. fname++;
  871. BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
  872. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
  873. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  874. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  875. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  876. if (rc == NULL)
  877. free(list);
  878. else
  879. {
  880. list->handle.opaque = (void *) rc;
  881. list->next = openWriteList;
  882. openWriteList = list;
  883. retval = &(list->handle);
  884. } /* else */
  885. return(retval);
  886. } /* doOpenWrite */
  887. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  888. {
  889. return(doOpenWrite(filename, 0));
  890. } /* PHYSFS_openWrite */
  891. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  892. {
  893. return(doOpenWrite(filename, 1));
  894. } /* PHYSFS_openAppend */
  895. PHYSFS_file *PHYSFS_openRead(const char *fname)
  896. {
  897. PHYSFS_file *retval = NULL;
  898. FileHandle *rc = NULL;
  899. FileHandleList *list;
  900. DirInfo *i;
  901. while (*fname == '/')
  902. fname++;
  903. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  904. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  905. for (i = searchPath; i != NULL; i = i->next)
  906. {
  907. DirHandle *h = i->dirHandle;
  908. if (__PHYSFS_verifySecurity(h, fname))
  909. {
  910. rc = h->funcs->openRead(h, fname);
  911. if (rc != NULL)
  912. break;
  913. } /* if */
  914. } /* for */
  915. if (rc == NULL)
  916. free(list);
  917. else
  918. {
  919. list->handle.opaque = (void *) rc;
  920. list->next = openReadList;
  921. openReadList = list;
  922. retval = &(list->handle);
  923. } /* else */
  924. return(retval);
  925. } /* PHYSFS_openRead */
  926. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  927. {
  928. FileHandle *h = (FileHandle *) handle->opaque;
  929. FileHandleList *prev = NULL;
  930. FileHandleList *i;
  931. int rc;
  932. for (i = *list; i != NULL; i = i->next)
  933. {
  934. if (&i->handle == handle) /* handle is in this list? */
  935. {
  936. rc = h->funcs->fileClose(h);
  937. if (!rc)
  938. return(-1);
  939. if (prev == NULL)
  940. *list = i->next;
  941. else
  942. prev->next = i->next;
  943. free(i);
  944. return(1);
  945. } /* if */
  946. prev = i;
  947. } /* for */
  948. return(0);
  949. } /* closeHandleInOpenList */
  950. int PHYSFS_close(PHYSFS_file *handle)
  951. {
  952. int rc;
  953. /* -1 == close failure. 0 == not found. 1 == success. */
  954. rc = closeHandleInOpenList(&openReadList, handle);
  955. BAIL_IF_MACRO(rc == -1, NULL, 0);
  956. if (!rc)
  957. {
  958. rc = closeHandleInOpenList(&openWriteList, handle);
  959. BAIL_IF_MACRO(rc == -1, NULL, 0);
  960. } /* if */
  961. if (!rc)
  962. __PHYSFS_setError(ERR_NOT_A_HANDLE);
  963. return(rc);
  964. } /* PHYSFS_close */
  965. int PHYSFS_read(PHYSFS_file *handle, void *buffer,
  966. unsigned int objSize, unsigned int objCount)
  967. {
  968. FileHandle *h = (FileHandle *) handle->opaque;
  969. assert(h != NULL);
  970. assert(h->funcs != NULL);
  971. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  972. return(h->funcs->read(h, buffer, objSize, objCount));
  973. } /* PHYSFS_read */
  974. int PHYSFS_write(PHYSFS_file *handle, void *buffer,
  975. unsigned int objSize, unsigned int objCount)
  976. {
  977. FileHandle *h = (FileHandle *) handle->opaque;
  978. assert(h != NULL);
  979. assert(h->funcs != NULL);
  980. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  981. return(h->funcs->write(h, buffer, objSize, objCount));
  982. } /* PHYSFS_write */
  983. int PHYSFS_eof(PHYSFS_file *handle)
  984. {
  985. FileHandle *h = (FileHandle *) handle->opaque;
  986. assert(h != NULL);
  987. assert(h->funcs != NULL);
  988. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  989. return(h->funcs->eof(h));
  990. } /* PHYSFS_eof */
  991. int PHYSFS_tell(PHYSFS_file *handle)
  992. {
  993. FileHandle *h = (FileHandle *) handle->opaque;
  994. assert(h != NULL);
  995. assert(h->funcs != NULL);
  996. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  997. return(h->funcs->tell(h));
  998. } /* PHYSFS_tell */
  999. int PHYSFS_seek(PHYSFS_file *handle, int pos)
  1000. {
  1001. FileHandle *h = (FileHandle *) handle->opaque;
  1002. assert(h != NULL);
  1003. assert(h->funcs != NULL);
  1004. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1005. return(h->funcs->seek(h, pos));
  1006. } /* PHYSFS_seek */
  1007. int PHYSFS_fileLength(PHYSFS_file *handle)
  1008. {
  1009. FileHandle *h = (FileHandle *) handle->opaque;
  1010. assert(h != NULL);
  1011. assert(h->funcs != NULL);
  1012. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1013. return(h->funcs->fileLength(h));
  1014. } /* PHYSFS_filelength */
  1015. /* end of physfs.c ... */