physfs.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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. #if HAVE_CONFIG_H
  11. # include <config.h>
  12. #endif
  13. #if (defined PHYSFS_PROFILING)
  14. #include <sys/time.h>
  15. #include <unistd.h>
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22. #include "physfs.h"
  23. #define __PHYSICSFS_INTERNAL__
  24. #include "physfs_internal.h"
  25. typedef struct __PHYSFS_ERRMSGTYPE__
  26. {
  27. PHYSFS_uint64 tid;
  28. int errorAvailable;
  29. char errorString[80];
  30. struct __PHYSFS_ERRMSGTYPE__ *next;
  31. } ErrMsg;
  32. typedef struct __PHYSFS_DIRINFO__
  33. {
  34. char *dirName;
  35. DirHandle *dirHandle;
  36. struct __PHYSFS_DIRINFO__ *next;
  37. } PhysDirInfo;
  38. typedef struct __PHYSFS_FILEHANDLELIST__
  39. {
  40. PHYSFS_file handle;
  41. struct __PHYSFS_FILEHANDLELIST__ *next;
  42. } FileHandleList;
  43. /* The various i/o drivers... */
  44. #if (defined PHYSFS_SUPPORTS_ZIP)
  45. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
  46. extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
  47. #endif
  48. #if (defined PHYSFS_SUPPORTS_GRP)
  49. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
  50. extern const DirFunctions __PHYSFS_DirFunctions_GRP;
  51. #endif
  52. #if (defined PHYSFS_SUPPORTS_QPAK)
  53. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
  54. extern const DirFunctions __PHYSFS_DirFunctions_QPAK;
  55. #endif
  56. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  57. static const PHYSFS_ArchiveInfo *supported_types[] =
  58. {
  59. #if (defined PHYSFS_SUPPORTS_ZIP)
  60. &__PHYSFS_ArchiveInfo_ZIP,
  61. #endif
  62. #if (defined PHYSFS_SUPPORTS_GRP)
  63. &__PHYSFS_ArchiveInfo_GRP,
  64. #endif
  65. #if (defined PHYSFS_SUPPORTS_QPAK)
  66. &__PHYSFS_ArchiveInfo_QPAK,
  67. #endif
  68. NULL
  69. };
  70. static const DirFunctions *dirFunctions[] =
  71. {
  72. #if (defined PHYSFS_SUPPORTS_ZIP)
  73. &__PHYSFS_DirFunctions_ZIP,
  74. #endif
  75. #if (defined PHYSFS_SUPPORTS_GRP)
  76. &__PHYSFS_DirFunctions_GRP,
  77. #endif
  78. #if (defined PHYSFS_SUPPORTS_QPAK)
  79. &__PHYSFS_DirFunctions_QPAK,
  80. #endif
  81. &__PHYSFS_DirFunctions_DIR,
  82. NULL
  83. };
  84. /* General PhysicsFS state ... */
  85. static int initialized = 0;
  86. static ErrMsg *errorMessages = NULL;
  87. static PhysDirInfo *searchPath = NULL;
  88. static PhysDirInfo *writeDir = NULL;
  89. static FileHandleList *openWriteList = NULL;
  90. static FileHandleList *openReadList = NULL;
  91. static char *baseDir = NULL;
  92. static char *userDir = NULL;
  93. static int allowSymLinks = 0;
  94. /* mutexes ... */
  95. static void *errorLock = NULL; /* protects error message list. */
  96. static void *stateLock = NULL; /* protects other PhysFS static state. */
  97. /* functions ... */
  98. void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
  99. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  100. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  101. {
  102. PHYSFS_uint32 i;
  103. int sorted;
  104. do
  105. {
  106. sorted = 1;
  107. for (i = lo; i < hi; i++)
  108. {
  109. if (cmpfn(a, i, i + 1) > 0)
  110. {
  111. swapfn(a, i, i + 1);
  112. sorted = 0;
  113. } /* if */
  114. } /* for */
  115. } while (!sorted);
  116. } /* __PHYSFS_bubble_sort */
  117. void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
  118. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  119. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  120. {
  121. PHYSFS_uint32 i;
  122. PHYSFS_uint32 j;
  123. PHYSFS_uint32 v;
  124. if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
  125. __PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
  126. else
  127. {
  128. i = (hi + lo) / 2;
  129. if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
  130. if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
  131. if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
  132. j = hi - 1;
  133. swapfn(a, i, j);
  134. i = lo;
  135. v = j;
  136. while (1)
  137. {
  138. while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
  139. while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
  140. if (j < i)
  141. break;
  142. swapfn(a, i, j);
  143. } /* while */
  144. swapfn(a, i, hi-1);
  145. __PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
  146. __PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
  147. } /* else */
  148. } /* __PHYSFS_quick_sort */
  149. void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
  150. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  151. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  152. {
  153. /*
  154. * Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
  155. * http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
  156. */
  157. __PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
  158. } /* __PHYSFS_sort */
  159. #if (defined PHYSFS_PROFILING)
  160. #define PHYSFS_TEST_SORT_ITERATIONS 150
  161. #define PHYSFS_TEST_SORT_ELEMENTS (64 * 1024)
  162. static int __PHYSFS_test_sort_cmp(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
  163. {
  164. PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
  165. PHYSFS_sint32 one = a[x];
  166. PHYSFS_sint32 two = a[y];
  167. if (one < two)
  168. return(-1);
  169. else if (one > two)
  170. return(1);
  171. return(0);
  172. } /* __PHYSFS_test_sort_cmp */
  173. static void __PHYSFS_test_sort_swap(void *_a, PHYSFS_uint32 x, PHYSFS_uint32 y)
  174. {
  175. PHYSFS_sint32 *a = (PHYSFS_sint32 *) _a;
  176. PHYSFS_sint32 tmp;
  177. tmp = a[x];
  178. a[x] = a[y];
  179. a[y] = tmp;
  180. } /* __PHYSFS_test_sort_swap */
  181. static int __PHYSFS_test_sort_do(PHYSFS_uint32 *timer,
  182. PHYSFS_sint32 *a, PHYSFS_uint32 max,
  183. int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
  184. void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
  185. {
  186. PHYSFS_uint32 i;
  187. struct timeval starttime, endtime;
  188. gettimeofday(&starttime, NULL);
  189. __PHYSFS_sort(a, max, cmpfn, swapfn);
  190. gettimeofday(&endtime, NULL);
  191. for (i = 1; i < max; i++)
  192. {
  193. if (a[i] < a[i - 1])
  194. return(0);
  195. } /* for */
  196. if (timer != NULL)
  197. {
  198. *timer = ( ((endtime.tv_sec - starttime.tv_sec) * 1000) +
  199. ((endtime.tv_usec - starttime.tv_usec) / 1000) );
  200. } /* if */
  201. return(1);
  202. } /* __PHYSFS_test_sort_time */
  203. static void __PHYSFS_test_sort(void)
  204. {
  205. PHYSFS_uint32 elasped[PHYSFS_TEST_SORT_ITERATIONS];
  206. PHYSFS_sint32 iter;
  207. PHYSFS_sint32 a[PHYSFS_TEST_SORT_ELEMENTS];
  208. PHYSFS_sint32 i, x;
  209. int success;
  210. printf("Testing __PHYSFS_sort (linear presorted) ... ");
  211. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  212. {
  213. /* set up array to sort. */
  214. for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
  215. a[i] = i;
  216. /* sort it. */
  217. success = __PHYSFS_test_sort_do(&elasped[iter],
  218. a, PHYSFS_TEST_SORT_ELEMENTS,
  219. __PHYSFS_test_sort_cmp,
  220. __PHYSFS_test_sort_swap);
  221. if (!success)
  222. break;
  223. } /* for */
  224. if (!success)
  225. printf("Failed!\n");
  226. else
  227. {
  228. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  229. x += elasped[iter];
  230. x /= PHYSFS_TEST_SORT_ITERATIONS;
  231. printf("Average run (%lu) ms.\n", (unsigned long) x);
  232. } /* else */
  233. printf("Testing __PHYSFS_sort (linear presorted reverse) ... ");
  234. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  235. {
  236. /* set up array to sort. */
  237. for (i = 0, x = PHYSFS_TEST_SORT_ELEMENTS;
  238. i < PHYSFS_TEST_SORT_ELEMENTS;
  239. i++, x--)
  240. {
  241. a[i] = x;
  242. } /* for */
  243. /* sort it. */
  244. success = __PHYSFS_test_sort_do(&elasped[iter],
  245. a, PHYSFS_TEST_SORT_ELEMENTS,
  246. __PHYSFS_test_sort_cmp,
  247. __PHYSFS_test_sort_swap);
  248. if (!success)
  249. break;
  250. } /* for */
  251. if (!success)
  252. printf("Failed!\n");
  253. else
  254. {
  255. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  256. x += elasped[iter];
  257. x /= PHYSFS_TEST_SORT_ITERATIONS;
  258. printf("Average run (%lu) ms.\n", (unsigned long) x);
  259. } /* else */
  260. printf("Testing __PHYSFS_sort (randomized) ... ");
  261. for (iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  262. {
  263. /* set up array to sort. */
  264. for (i = 0; i < PHYSFS_TEST_SORT_ELEMENTS; i++)
  265. a[i] = (PHYSFS_uint32) rand();
  266. /* sort it. */
  267. success = __PHYSFS_test_sort_do(&elasped[iter],
  268. a, PHYSFS_TEST_SORT_ELEMENTS,
  269. __PHYSFS_test_sort_cmp,
  270. __PHYSFS_test_sort_swap);
  271. if (!success)
  272. break;
  273. } /* for */
  274. if (!success)
  275. printf("Failed!\n");
  276. else
  277. {
  278. for (x = 0, iter = 0; iter < PHYSFS_TEST_SORT_ITERATIONS; iter++)
  279. x += elasped[iter];
  280. x /= PHYSFS_TEST_SORT_ITERATIONS;
  281. printf("Average run (%lu) ms.\n", (unsigned long) x);
  282. } /* else */
  283. printf("__PHYSFS_test_sort() complete.\n\n");
  284. } /* __PHYSFS_test_sort */
  285. #endif
  286. static ErrMsg *findErrorForCurrentThread(void)
  287. {
  288. ErrMsg *i;
  289. PHYSFS_uint64 tid;
  290. if (errorLock != NULL)
  291. __PHYSFS_platformGrabMutex(errorLock);
  292. if (errorMessages != NULL)
  293. {
  294. tid = __PHYSFS_platformGetThreadID();
  295. for (i = errorMessages; i != NULL; i = i->next)
  296. {
  297. if (i->tid == tid)
  298. {
  299. if (errorLock != NULL)
  300. __PHYSFS_platformReleaseMutex(errorLock);
  301. return(i);
  302. } /* if */
  303. } /* for */
  304. } /* if */
  305. if (errorLock != NULL)
  306. __PHYSFS_platformReleaseMutex(errorLock);
  307. return(NULL); /* no error available. */
  308. } /* findErrorForCurrentThread */
  309. void __PHYSFS_setError(const char *str)
  310. {
  311. ErrMsg *err;
  312. if (str == NULL)
  313. return;
  314. err = findErrorForCurrentThread();
  315. if (err == NULL)
  316. {
  317. err = (ErrMsg *) malloc(sizeof (ErrMsg));
  318. if (err == NULL)
  319. return; /* uhh...? */
  320. memset((void *) err, '\0', sizeof (ErrMsg));
  321. err->tid = __PHYSFS_platformGetThreadID();
  322. if (errorLock != NULL)
  323. __PHYSFS_platformGrabMutex(errorLock);
  324. err->next = errorMessages;
  325. errorMessages = err;
  326. if (errorLock != NULL)
  327. __PHYSFS_platformReleaseMutex(errorLock);
  328. } /* if */
  329. err->errorAvailable = 1;
  330. strncpy(err->errorString, str, sizeof (err->errorString));
  331. err->errorString[sizeof (err->errorString) - 1] = '\0';
  332. } /* __PHYSFS_setError */
  333. const char *PHYSFS_getLastError(void)
  334. {
  335. ErrMsg *err = findErrorForCurrentThread();
  336. if ((err == NULL) || (!err->errorAvailable))
  337. return(NULL);
  338. err->errorAvailable = 0;
  339. return(err->errorString);
  340. } /* PHYSFS_getLastError */
  341. /* MAKE SURE that errorLock is held before calling this! */
  342. static void freeErrorMessages(void)
  343. {
  344. ErrMsg *i;
  345. ErrMsg *next;
  346. for (i = errorMessages; i != NULL; i = next)
  347. {
  348. next = i->next;
  349. free(i);
  350. } /* for */
  351. errorMessages = NULL;
  352. } /* freeErrorMessages */
  353. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  354. {
  355. if (ver != NULL)
  356. {
  357. ver->major = PHYSFS_VER_MAJOR;
  358. ver->minor = PHYSFS_VER_MINOR;
  359. ver->patch = PHYSFS_VER_PATCH;
  360. } /* if */
  361. } /* PHYSFS_getLinkedVersion */
  362. static const char *find_filename_extension(const char *fname)
  363. {
  364. const char *retval = strchr(fname, '.');
  365. const char *p = retval;
  366. while (p != NULL)
  367. {
  368. p = strchr(p + 1, '.');
  369. if (p != NULL)
  370. retval = p;
  371. } /* while */
  372. if (retval != NULL)
  373. retval++; /* skip '.' */
  374. return(retval);
  375. } /* find_filename_extension */
  376. static DirHandle *openDirectory(const char *d, int forWriting)
  377. {
  378. const DirFunctions **i;
  379. const char *ext;
  380. BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
  381. ext = find_filename_extension(d);
  382. if (ext != NULL)
  383. {
  384. /* Look for archivers with matching file extensions first... */
  385. for (i = dirFunctions; *i != NULL; i++)
  386. {
  387. if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) == 0)
  388. {
  389. if ((*i)->isArchive(d, forWriting))
  390. return( (*i)->openArchive(d, forWriting) );
  391. } /* if */
  392. } /* for */
  393. /* failing an exact file extension match, try all the others... */
  394. for (i = dirFunctions; *i != NULL; i++)
  395. {
  396. if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) != 0)
  397. {
  398. if ((*i)->isArchive(d, forWriting))
  399. return( (*i)->openArchive(d, forWriting) );
  400. } /* if */
  401. } /* for */
  402. } /* if */
  403. else /* no extension? Try them all. */
  404. {
  405. for (i = dirFunctions; *i != NULL; i++)
  406. {
  407. if ((*i)->isArchive(d, forWriting))
  408. return( (*i)->openArchive(d, forWriting) );
  409. } /* for */
  410. } /* else */
  411. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  412. return(NULL);
  413. } /* openDirectory */
  414. static PhysDirInfo *buildDirInfo(const char *newDir, int forWriting)
  415. {
  416. DirHandle *dirHandle = NULL;
  417. PhysDirInfo *di = NULL;
  418. BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
  419. dirHandle = openDirectory(newDir, forWriting);
  420. BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
  421. di = (PhysDirInfo *) malloc(sizeof (PhysDirInfo));
  422. if (di == NULL)
  423. {
  424. dirHandle->funcs->dirClose(dirHandle);
  425. BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
  426. } /* if */
  427. di->dirName = (char *) malloc(strlen(newDir) + 1);
  428. if (di->dirName == NULL)
  429. {
  430. free(di);
  431. dirHandle->funcs->dirClose(dirHandle);
  432. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  433. } /* if */
  434. di->next = NULL;
  435. di->dirHandle = dirHandle;
  436. strcpy(di->dirName, newDir);
  437. return(di);
  438. } /* buildDirInfo */
  439. /* MAKE SURE you've got the stateLock held before calling this! */
  440. static int freeDirInfo(PhysDirInfo *di, FileHandleList *openList)
  441. {
  442. FileHandleList *i;
  443. if (di == NULL)
  444. return(1);
  445. for (i = openList; i != NULL; i = i->next)
  446. {
  447. const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
  448. BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
  449. } /* for */
  450. di->dirHandle->funcs->dirClose(di->dirHandle);
  451. free(di->dirName);
  452. free(di);
  453. return(1);
  454. } /* freeDirInfo */
  455. static char *calculateUserDir(void)
  456. {
  457. char *retval = NULL;
  458. const char *str = NULL;
  459. str = __PHYSFS_platformGetUserDir();
  460. if (str != NULL)
  461. retval = (char *) str;
  462. else
  463. {
  464. const char *dirsep = PHYSFS_getDirSeparator();
  465. const char *uname = __PHYSFS_platformGetUserName();
  466. str = (uname != NULL) ? uname : "default";
  467. retval = (char *) malloc(strlen(baseDir) + strlen(str) +
  468. strlen(dirsep) + 6);
  469. if (retval == NULL)
  470. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  471. else
  472. sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
  473. if (uname != NULL)
  474. free((void *) uname);
  475. } /* else */
  476. return(retval);
  477. } /* calculateUserDir */
  478. static int appendDirSep(char **dir)
  479. {
  480. const char *dirsep = PHYSFS_getDirSeparator();
  481. char *ptr;
  482. if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
  483. return(1);
  484. ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
  485. if (!ptr)
  486. {
  487. free(*dir);
  488. return(0);
  489. } /* if */
  490. strcat(ptr, dirsep);
  491. *dir = ptr;
  492. return(1);
  493. } /* appendDirSep */
  494. static char *calculateBaseDir(const char *argv0)
  495. {
  496. const char *dirsep = PHYSFS_getDirSeparator();
  497. char *retval;
  498. char *ptr;
  499. /*
  500. * See if the platform driver wants to handle this for us...
  501. */
  502. retval = __PHYSFS_platformCalcBaseDir(argv0);
  503. if (retval != NULL)
  504. return(retval);
  505. /*
  506. * Determine if there's a path on argv0. If there is, that's the base dir.
  507. */
  508. ptr = strstr(argv0, dirsep);
  509. if (ptr != NULL)
  510. {
  511. char *p = ptr;
  512. size_t size;
  513. while (p != NULL)
  514. {
  515. ptr = p;
  516. p = strstr(p + 1, dirsep);
  517. } /* while */
  518. size = (size_t) (ptr - argv0);
  519. retval = (char *) malloc(size + 1);
  520. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  521. memcpy(retval, argv0, size);
  522. retval[size] = '\0';
  523. return(retval);
  524. } /* if */
  525. /*
  526. * Last ditch effort: it's the current working directory. (*shrug*)
  527. */
  528. retval = __PHYSFS_platformCurrentDir();
  529. if(retval != NULL) {
  530. return(retval);
  531. }
  532. /*
  533. * Ok, current directory doesn't exist, use the root directory.
  534. * Not a good alternative, but it only happens if the current
  535. * directory was deleted from under the program.
  536. */
  537. retval = (char *) malloc(strlen(dirsep) + 1);
  538. strcpy(retval, dirsep);
  539. return(retval);
  540. } /* calculateBaseDir */
  541. static int initializeMutexes(void)
  542. {
  543. errorLock = __PHYSFS_platformCreateMutex();
  544. if (errorLock == NULL)
  545. goto initializeMutexes_failed;
  546. stateLock = __PHYSFS_platformCreateMutex();
  547. if (stateLock == NULL)
  548. goto initializeMutexes_failed;
  549. return(1); /* success. */
  550. initializeMutexes_failed:
  551. if (errorLock != NULL)
  552. __PHYSFS_platformDestroyMutex(errorLock);
  553. if (stateLock != NULL)
  554. __PHYSFS_platformDestroyMutex(stateLock);
  555. errorLock = stateLock = NULL;
  556. return(0); /* failed. */
  557. } /* initializeMutexes */
  558. int PHYSFS_init(const char *argv0)
  559. {
  560. char *ptr;
  561. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  562. BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
  563. BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
  564. baseDir = calculateBaseDir(argv0);
  565. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  566. ptr = __PHYSFS_platformRealPath(baseDir);
  567. free(baseDir);
  568. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  569. baseDir = ptr;
  570. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  571. userDir = calculateUserDir();
  572. if (userDir != NULL)
  573. {
  574. ptr = __PHYSFS_platformRealPath(userDir);
  575. free(userDir);
  576. userDir = ptr;
  577. } /* if */
  578. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  579. {
  580. free(baseDir);
  581. baseDir = NULL;
  582. return(0);
  583. } /* if */
  584. initialized = 1;
  585. /* This makes sure that the error subsystem is initialized. */
  586. __PHYSFS_setError(PHYSFS_getLastError());
  587. #if (defined PHYSFS_PROFILING)
  588. srand(time(NULL));
  589. setbuf(stdout, NULL);
  590. printf("\n");
  591. printf("********************************************************\n");
  592. printf("Warning! Profiling is built into this copy of PhysicsFS!\n");
  593. printf("********************************************************\n");
  594. printf("\n");
  595. printf("\n");
  596. __PHYSFS_test_sort();
  597. #endif
  598. return(1);
  599. } /* PHYSFS_init */
  600. /* MAKE SURE you hold stateLock before calling this! */
  601. static int closeFileHandleList(FileHandleList **list)
  602. {
  603. FileHandleList *i;
  604. FileHandleList *next = NULL;
  605. FileHandle *h;
  606. for (i = *list; i != NULL; i = next)
  607. {
  608. next = i->next;
  609. h = (FileHandle *) (i->handle.opaque);
  610. if (!h->funcs->fileClose(h))
  611. {
  612. *list = i;
  613. return(0);
  614. } /* if */
  615. free(i);
  616. } /* for */
  617. *list = NULL;
  618. return(1);
  619. } /* closeFileHandleList */
  620. /* MAKE SURE you hold the stateLock before calling this! */
  621. static void freeSearchPath(void)
  622. {
  623. PhysDirInfo *i;
  624. PhysDirInfo *next = NULL;
  625. closeFileHandleList(&openReadList);
  626. if (searchPath != NULL)
  627. {
  628. for (i = searchPath; i != NULL; i = next)
  629. {
  630. next = i->next;
  631. freeDirInfo(i, openReadList);
  632. } /* for */
  633. searchPath = NULL;
  634. } /* if */
  635. } /* freeSearchPath */
  636. int PHYSFS_deinit(void)
  637. {
  638. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  639. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
  640. closeFileHandleList(&openWriteList);
  641. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  642. freeSearchPath();
  643. freeErrorMessages();
  644. if (baseDir != NULL)
  645. {
  646. free(baseDir);
  647. baseDir = NULL;
  648. } /* if */
  649. if (userDir != NULL)
  650. {
  651. free(userDir);
  652. userDir = NULL;
  653. } /* if */
  654. allowSymLinks = 0;
  655. initialized = 0;
  656. __PHYSFS_platformDestroyMutex(errorLock);
  657. __PHYSFS_platformDestroyMutex(stateLock);
  658. errorLock = stateLock = NULL;
  659. return(1);
  660. } /* PHYSFS_deinit */
  661. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  662. {
  663. return(supported_types);
  664. } /* PHYSFS_supportedArchiveTypes */
  665. void PHYSFS_freeList(void *list)
  666. {
  667. void **i;
  668. for (i = (void **) list; *i != NULL; i++)
  669. free(*i);
  670. free(list);
  671. } /* PHYSFS_freeList */
  672. const char *PHYSFS_getDirSeparator(void)
  673. {
  674. return(__PHYSFS_platformDirSeparator);
  675. } /* PHYSFS_getDirSeparator */
  676. char **PHYSFS_getCdRomDirs(void)
  677. {
  678. return(__PHYSFS_platformDetectAvailableCDs());
  679. } /* PHYSFS_getCdRomDirs */
  680. const char *PHYSFS_getBaseDir(void)
  681. {
  682. return(baseDir); /* this is calculated in PHYSFS_init()... */
  683. } /* PHYSFS_getBaseDir */
  684. const char *PHYSFS_getUserDir(void)
  685. {
  686. return(userDir); /* this is calculated in PHYSFS_init()... */
  687. } /* PHYSFS_getUserDir */
  688. const char *PHYSFS_getWriteDir(void)
  689. {
  690. const char *retval = NULL;
  691. __PHYSFS_platformGrabMutex(stateLock);
  692. if (writeDir != NULL)
  693. retval = writeDir->dirName;
  694. __PHYSFS_platformReleaseMutex(stateLock);
  695. return(retval);
  696. } /* PHYSFS_getWriteDir */
  697. int PHYSFS_setWriteDir(const char *newDir)
  698. {
  699. int retval = 1;
  700. __PHYSFS_platformGrabMutex(stateLock);
  701. if (writeDir != NULL)
  702. {
  703. BAIL_IF_MACRO_MUTEX(!freeDirInfo(writeDir, openWriteList), NULL,
  704. stateLock, 0);
  705. writeDir = NULL;
  706. } /* if */
  707. if (newDir != NULL)
  708. {
  709. writeDir = buildDirInfo(newDir, 1);
  710. retval = (writeDir != NULL);
  711. } /* if */
  712. __PHYSFS_platformReleaseMutex(stateLock);
  713. return(retval);
  714. } /* PHYSFS_setWriteDir */
  715. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  716. {
  717. PhysDirInfo *di;
  718. PhysDirInfo *prev = NULL;
  719. PhysDirInfo *i;
  720. __PHYSFS_platformGrabMutex(stateLock);
  721. for (i = searchPath; i != NULL; i = i->next)
  722. {
  723. /* already in search path? */
  724. BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
  725. prev = i;
  726. } /* for */
  727. di = buildDirInfo(newDir, 0);
  728. BAIL_IF_MACRO_MUTEX(di == NULL, NULL, stateLock, 0);
  729. if (appendToPath)
  730. {
  731. di->next = NULL;
  732. if (prev == NULL)
  733. searchPath = di;
  734. else
  735. prev->next = di;
  736. } /* if */
  737. else
  738. {
  739. di->next = searchPath;
  740. searchPath = di;
  741. } /* else */
  742. __PHYSFS_platformReleaseMutex(stateLock);
  743. return(1);
  744. } /* PHYSFS_addToSearchPath */
  745. int PHYSFS_removeFromSearchPath(const char *oldDir)
  746. {
  747. PhysDirInfo *i;
  748. PhysDirInfo *prev = NULL;
  749. PhysDirInfo *next = NULL;
  750. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  751. __PHYSFS_platformGrabMutex(stateLock);
  752. for (i = searchPath; i != NULL; i = i->next)
  753. {
  754. if (strcmp(i->dirName, oldDir) == 0)
  755. {
  756. next = i->next;
  757. BAIL_IF_MACRO_MUTEX(!freeDirInfo(i, openReadList), NULL,
  758. stateLock, 0);
  759. if (prev == NULL)
  760. searchPath = next;
  761. else
  762. prev->next = next;
  763. BAIL_MACRO_MUTEX(NULL, stateLock, 1);
  764. } /* if */
  765. prev = i;
  766. } /* for */
  767. BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
  768. } /* PHYSFS_removeFromSearchPath */
  769. char **PHYSFS_getSearchPath(void)
  770. {
  771. int count = 1;
  772. int x;
  773. PhysDirInfo *i;
  774. char **retval;
  775. __PHYSFS_platformGrabMutex(stateLock);
  776. for (i = searchPath; i != NULL; i = i->next)
  777. count++;
  778. retval = (char **) malloc(sizeof (char *) * count);
  779. BAIL_IF_MACRO_MUTEX(!retval, ERR_OUT_OF_MEMORY, stateLock, NULL);
  780. count--;
  781. retval[count] = NULL;
  782. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  783. {
  784. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  785. if (retval[x] == NULL) /* this is friggin' ugly. */
  786. {
  787. while (x > 0)
  788. {
  789. x--;
  790. free(retval[x]);
  791. } /* while */
  792. free(retval);
  793. BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
  794. } /* if */
  795. strcpy(retval[x], i->dirName);
  796. } /* for */
  797. __PHYSFS_platformReleaseMutex(stateLock);
  798. return(retval);
  799. } /* PHYSFS_getSearchPath */
  800. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  801. const char *archiveExt, int includeCdRoms,
  802. int archivesFirst)
  803. {
  804. const char *basedir = PHYSFS_getBaseDir();
  805. const char *userdir = PHYSFS_getUserDir();
  806. const char *dirsep = PHYSFS_getDirSeparator();
  807. char *str;
  808. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  809. /* set write dir... */
  810. str = malloc(strlen(userdir) + (strlen(organization) * 2) +
  811. (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
  812. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  813. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  814. if (!PHYSFS_setWriteDir(str))
  815. {
  816. int no_write = 0;
  817. sprintf(str, ".%s/%s", organization, appName);
  818. if ( (PHYSFS_setWriteDir(userdir)) &&
  819. (PHYSFS_mkdir(str)) )
  820. {
  821. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  822. if (!PHYSFS_setWriteDir(str))
  823. no_write = 1;
  824. } /* if */
  825. else
  826. {
  827. no_write = 1;
  828. } /* else */
  829. if (no_write)
  830. {
  831. PHYSFS_setWriteDir(NULL); /* just in case. */
  832. free(str);
  833. BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
  834. } /* if */
  835. } /* if */
  836. /* Put write dir first in search path... */
  837. PHYSFS_addToSearchPath(str, 0);
  838. free(str);
  839. /* Put base path on search path... */
  840. PHYSFS_addToSearchPath(basedir, 1);
  841. /* handle CD-ROMs... */
  842. if (includeCdRoms)
  843. {
  844. char **cds = PHYSFS_getCdRomDirs();
  845. char **i;
  846. for (i = cds; *i != NULL; i++)
  847. PHYSFS_addToSearchPath(*i, 1);
  848. PHYSFS_freeList(cds);
  849. } /* if */
  850. /* Root out archives, and add them to search path... */
  851. if (archiveExt != NULL)
  852. {
  853. char **rc = PHYSFS_enumerateFiles("/");
  854. char **i;
  855. size_t extlen = strlen(archiveExt);
  856. char *ext;
  857. for (i = rc; *i != NULL; i++)
  858. {
  859. size_t l = strlen(*i);
  860. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  861. {
  862. ext = (*i) + (l - extlen);
  863. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  864. {
  865. const char *d = PHYSFS_getRealDir(*i);
  866. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  867. if (str != NULL)
  868. {
  869. sprintf(str, "%s%s%s", d, dirsep, *i);
  870. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  871. free(str);
  872. } /* if */
  873. } /* if */
  874. } /* if */
  875. } /* for */
  876. PHYSFS_freeList(rc);
  877. } /* if */
  878. return(1);
  879. } /* PHYSFS_setSaneConfig */
  880. void PHYSFS_permitSymbolicLinks(int allow)
  881. {
  882. allowSymLinks = allow;
  883. } /* PHYSFS_permitSymbolicLinks */
  884. /* string manipulation in C makes my ass itch. */
  885. char * __PHYSFS_convertToDependent(const char *prepend,
  886. const char *dirName,
  887. const char *append)
  888. {
  889. const char *dirsep = __PHYSFS_platformDirSeparator;
  890. size_t sepsize = strlen(dirsep);
  891. char *str;
  892. char *i1;
  893. char *i2;
  894. size_t allocSize;
  895. while (*dirName == '/')
  896. dirName++;
  897. allocSize = strlen(dirName) + 1;
  898. if (prepend != NULL)
  899. allocSize += strlen(prepend) + sepsize;
  900. if (append != NULL)
  901. allocSize += strlen(append) + sepsize;
  902. /* make sure there's enough space if the dir separator is bigger. */
  903. if (sepsize > 1)
  904. {
  905. str = (char *) dirName;
  906. do
  907. {
  908. str = strchr(str, '/');
  909. if (str != NULL)
  910. {
  911. allocSize += (sepsize - 1);
  912. str++;
  913. } /* if */
  914. } while (str != NULL);
  915. } /* if */
  916. str = (char *) malloc(allocSize);
  917. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  918. if (prepend == NULL)
  919. *str = '\0';
  920. else
  921. {
  922. strcpy(str, prepend);
  923. strcat(str, dirsep);
  924. } /* else */
  925. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  926. {
  927. if (*i1 == '/')
  928. {
  929. strcpy(i2, dirsep);
  930. i2 += sepsize;
  931. } /* if */
  932. else
  933. {
  934. *i2 = *i1;
  935. } /* else */
  936. } /* for */
  937. *i2 = '\0';
  938. if (append)
  939. {
  940. strcat(str, dirsep);
  941. strcpy(str, append);
  942. } /* if */
  943. return(str);
  944. } /* __PHYSFS_convertToDependent */
  945. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
  946. {
  947. int retval = 1;
  948. char *start;
  949. char *end;
  950. char *str;
  951. /* !!! FIXME: Can we ditch this malloc()? */
  952. start = str = malloc(strlen(fname) + 1);
  953. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  954. strcpy(str, fname);
  955. while (1)
  956. {
  957. end = strchr(start, '/');
  958. if (end != NULL)
  959. *end = '\0';
  960. if ( (strcmp(start, ".") == 0) ||
  961. (strcmp(start, "..") == 0) ||
  962. (strchr(start, '\\') != NULL) ||
  963. (strchr(start, ':') != NULL) )
  964. {
  965. __PHYSFS_setError(ERR_INSECURE_FNAME);
  966. retval = 0;
  967. break;
  968. } /* if */
  969. if (!allowSymLinks)
  970. {
  971. if (h->funcs->isSymLink(h, str, &retval))
  972. {
  973. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  974. retval = 0;
  975. break;
  976. } /* if */
  977. /* break out early if path element is missing. */
  978. if (!retval)
  979. break;
  980. } /* if */
  981. if (end == NULL)
  982. break;
  983. *end = '/';
  984. start = end + 1;
  985. } /* while */
  986. free(str);
  987. return(retval);
  988. } /* __PHYSFS_verifySecurity */
  989. int PHYSFS_mkdir(const char *dname)
  990. {
  991. DirHandle *h;
  992. char *str;
  993. char *start;
  994. char *end;
  995. int retval = 0;
  996. BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
  997. while (*dname == '/')
  998. dname++;
  999. __PHYSFS_platformGrabMutex(stateLock);
  1000. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1001. h = writeDir->dirHandle;
  1002. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, dname), NULL, stateLock, 0);
  1003. start = str = malloc(strlen(dname) + 1);
  1004. BAIL_IF_MACRO_MUTEX(str == NULL, ERR_OUT_OF_MEMORY, stateLock, 0);
  1005. strcpy(str, dname);
  1006. while (1)
  1007. {
  1008. end = strchr(start, '/');
  1009. if (end != NULL)
  1010. *end = '\0';
  1011. retval = h->funcs->mkdir(h, str);
  1012. if (!retval)
  1013. break;
  1014. if (end == NULL)
  1015. break;
  1016. *end = '/';
  1017. start = end + 1;
  1018. } /* while */
  1019. __PHYSFS_platformReleaseMutex(stateLock);
  1020. free(str);
  1021. return(retval);
  1022. } /* PHYSFS_mkdir */
  1023. int PHYSFS_delete(const char *fname)
  1024. {
  1025. int retval;
  1026. DirHandle *h;
  1027. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1028. while (*fname == '/')
  1029. fname++;
  1030. __PHYSFS_platformGrabMutex(stateLock);
  1031. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1032. h = writeDir->dirHandle;
  1033. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL, stateLock, 0);
  1034. retval = h->funcs->remove(h, fname);
  1035. __PHYSFS_platformReleaseMutex(stateLock);
  1036. return(retval);
  1037. } /* PHYSFS_delete */
  1038. const char *PHYSFS_getRealDir(const char *filename)
  1039. {
  1040. PhysDirInfo *i;
  1041. const char *retval = NULL;
  1042. while (*filename == '/')
  1043. filename++;
  1044. __PHYSFS_platformGrabMutex(stateLock);
  1045. for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
  1046. {
  1047. DirHandle *h = i->dirHandle;
  1048. if (__PHYSFS_verifySecurity(h, filename))
  1049. {
  1050. if (h->funcs->exists(h, filename))
  1051. retval = i->dirName;
  1052. } /* if */
  1053. } /* for */
  1054. __PHYSFS_platformReleaseMutex(stateLock);
  1055. return(retval);
  1056. } /* PHYSFS_getRealDir */
  1057. static int countList(LinkedStringList *list)
  1058. {
  1059. int retval = 0;
  1060. LinkedStringList *i;
  1061. for (i = list; i != NULL; i = i->next)
  1062. retval++;
  1063. return(retval);
  1064. } /* countList */
  1065. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  1066. {
  1067. int i;
  1068. LinkedStringList *next = NULL;
  1069. int len = countList(finalList);
  1070. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  1071. if (retval == NULL)
  1072. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  1073. for (i = 0; i < len; i++)
  1074. {
  1075. next = finalList->next;
  1076. if (retval == NULL)
  1077. free(finalList->str);
  1078. else
  1079. retval[i] = finalList->str;
  1080. free(finalList);
  1081. finalList = next;
  1082. } /* for */
  1083. if (retval != NULL)
  1084. retval[i] = NULL;
  1085. return(retval);
  1086. } /* convertStringListToPhysFSList */
  1087. static void insertStringListItem(LinkedStringList **final,
  1088. LinkedStringList *item)
  1089. {
  1090. LinkedStringList *i;
  1091. LinkedStringList *prev = NULL;
  1092. int rc;
  1093. for (i = *final; i != NULL; i = i->next)
  1094. {
  1095. rc = strcmp(i->str, item->str);
  1096. if (rc > 0) /* insertion point. */
  1097. break;
  1098. else if (rc == 0) /* already in list. */
  1099. {
  1100. free(item->str);
  1101. free(item);
  1102. return;
  1103. } /* else if */
  1104. prev = i;
  1105. } /* for */
  1106. /*
  1107. * If we are here, we are either at the insertion point.
  1108. * This may be the end of the list, or the list may be empty, too.
  1109. */
  1110. if (prev == NULL)
  1111. *final = item;
  1112. else
  1113. prev->next = item;
  1114. item->next = i;
  1115. } /* insertStringListItem */
  1116. /* if we run out of memory anywhere in here, we give back what we can. */
  1117. static void interpolateStringLists(LinkedStringList **final,
  1118. LinkedStringList *newList)
  1119. {
  1120. LinkedStringList *next = NULL;
  1121. while (newList != NULL)
  1122. {
  1123. next = newList->next;
  1124. insertStringListItem(final, newList);
  1125. newList = next;
  1126. } /* while */
  1127. } /* interpolateStringLists */
  1128. char **PHYSFS_enumerateFiles(const char *path)
  1129. {
  1130. PhysDirInfo *i;
  1131. char **retval = NULL;
  1132. LinkedStringList *rc;
  1133. LinkedStringList *finalList = NULL;
  1134. int omitSymLinks = !allowSymLinks;
  1135. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  1136. while (*path == '/')
  1137. path++;
  1138. __PHYSFS_platformGrabMutex(stateLock);
  1139. for (i = searchPath; i != NULL; i = i->next)
  1140. {
  1141. DirHandle *h = i->dirHandle;
  1142. if (__PHYSFS_verifySecurity(h, path))
  1143. {
  1144. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  1145. interpolateStringLists(&finalList, rc);
  1146. } /* if */
  1147. } /* for */
  1148. __PHYSFS_platformReleaseMutex(stateLock);
  1149. retval = convertStringListToPhysFSList(finalList);
  1150. return(retval);
  1151. } /* PHYSFS_enumerateFiles */
  1152. int PHYSFS_exists(const char *fname)
  1153. {
  1154. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1155. while (*fname == '/')
  1156. fname++;
  1157. return(PHYSFS_getRealDir(fname) != NULL);
  1158. } /* PHYSFS_exists */
  1159. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  1160. {
  1161. PhysDirInfo *i;
  1162. PHYSFS_sint64 retval = -1;
  1163. int fileExists = 0;
  1164. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1165. while (*fname == '/')
  1166. fname++;
  1167. if (*fname == '\0') /* eh...punt if it's the root dir. */
  1168. return(1);
  1169. __PHYSFS_platformGrabMutex(stateLock);
  1170. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1171. {
  1172. DirHandle *h = i->dirHandle;
  1173. if (__PHYSFS_verifySecurity(h, fname))
  1174. retval = h->funcs->getLastModTime(h, fname, &fileExists);
  1175. } /* for */
  1176. __PHYSFS_platformReleaseMutex(stateLock);
  1177. return(retval);
  1178. } /* PHYSFS_getLastModTime */
  1179. int PHYSFS_isDirectory(const char *fname)
  1180. {
  1181. PhysDirInfo *i;
  1182. int retval = 0;
  1183. int fileExists = 0;
  1184. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1185. while (*fname == '/')
  1186. fname++;
  1187. BAIL_IF_MACRO(*fname == '\0', NULL, 1); /* Root is always a dir. :) */
  1188. __PHYSFS_platformGrabMutex(stateLock);
  1189. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1190. {
  1191. DirHandle *h = i->dirHandle;
  1192. if (__PHYSFS_verifySecurity(h, fname))
  1193. retval = h->funcs->isDirectory(h, fname, &fileExists);
  1194. } /* for */
  1195. __PHYSFS_platformReleaseMutex(stateLock);
  1196. return(retval);
  1197. } /* PHYSFS_isDirectory */
  1198. int PHYSFS_isSymbolicLink(const char *fname)
  1199. {
  1200. PhysDirInfo *i;
  1201. int retval = 0;
  1202. int fileExists = 0;
  1203. BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
  1204. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1205. while (*fname == '/')
  1206. fname++;
  1207. BAIL_IF_MACRO(*fname == '\0', NULL, 0); /* Root is never a symlink */
  1208. __PHYSFS_platformGrabMutex(stateLock);
  1209. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1210. {
  1211. DirHandle *h = i->dirHandle;
  1212. if (__PHYSFS_verifySecurity(h, fname))
  1213. retval = h->funcs->isSymLink(h, fname, &fileExists);
  1214. } /* for */
  1215. __PHYSFS_platformReleaseMutex(stateLock);
  1216. return(retval);
  1217. } /* PHYSFS_isSymbolicLink */
  1218. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  1219. {
  1220. PHYSFS_file *retval = NULL;
  1221. FileHandle *rc = NULL;
  1222. DirHandle *h;
  1223. const DirFunctions *f;
  1224. FileHandleList *list;
  1225. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1226. while (*fname == '/')
  1227. fname++;
  1228. __PHYSFS_platformGrabMutex(stateLock);
  1229. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  1230. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  1231. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL,
  1232. stateLock, NULL);
  1233. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1234. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1235. f = h->funcs;
  1236. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  1237. if (rc == NULL)
  1238. free(list);
  1239. else
  1240. {
  1241. list->handle.opaque = (void *) rc;
  1242. list->next = openWriteList;
  1243. openWriteList = list;
  1244. retval = &(list->handle);
  1245. } /* else */
  1246. __PHYSFS_platformReleaseMutex(stateLock);
  1247. return(retval);
  1248. } /* doOpenWrite */
  1249. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  1250. {
  1251. return(doOpenWrite(filename, 0));
  1252. } /* PHYSFS_openWrite */
  1253. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  1254. {
  1255. return(doOpenWrite(filename, 1));
  1256. } /* PHYSFS_openAppend */
  1257. PHYSFS_file *PHYSFS_openRead(const char *fname)
  1258. {
  1259. PHYSFS_file *retval = NULL;
  1260. FileHandle *rc = NULL;
  1261. FileHandleList *list;
  1262. int fileExists = 0;
  1263. PhysDirInfo *i;
  1264. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1265. while (*fname == '/')
  1266. fname++;
  1267. __PHYSFS_platformGrabMutex(stateLock);
  1268. BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
  1269. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1270. {
  1271. DirHandle *h = i->dirHandle;
  1272. if (__PHYSFS_verifySecurity(h, fname))
  1273. rc = h->funcs->openRead(h, fname, &fileExists);
  1274. } /* for */
  1275. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1276. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1277. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  1278. list->handle.opaque = (void *) rc;
  1279. list->next = openReadList;
  1280. openReadList = list;
  1281. retval = &(list->handle);
  1282. __PHYSFS_platformReleaseMutex(stateLock);
  1283. return(retval);
  1284. } /* PHYSFS_openRead */
  1285. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1286. {
  1287. FileHandle *h = (FileHandle *) handle->opaque;
  1288. FileHandleList *prev = NULL;
  1289. FileHandleList *i;
  1290. int rc;
  1291. for (i = *list; i != NULL; i = i->next)
  1292. {
  1293. if (&i->handle == handle) /* handle is in this list? */
  1294. {
  1295. rc = h->funcs->fileClose(h);
  1296. if (!rc)
  1297. return(-1);
  1298. if (prev == NULL)
  1299. *list = i->next;
  1300. else
  1301. prev->next = i->next;
  1302. free(i);
  1303. return(1);
  1304. } /* if */
  1305. prev = i;
  1306. } /* for */
  1307. return(0);
  1308. } /* closeHandleInOpenList */
  1309. int PHYSFS_close(PHYSFS_file *handle)
  1310. {
  1311. int rc;
  1312. __PHYSFS_platformGrabMutex(stateLock);
  1313. /* -1 == close failure. 0 == not found. 1 == success. */
  1314. rc = closeHandleInOpenList(&openReadList, handle);
  1315. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1316. if (!rc)
  1317. {
  1318. rc = closeHandleInOpenList(&openWriteList, handle);
  1319. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1320. } /* if */
  1321. __PHYSFS_platformReleaseMutex(stateLock);
  1322. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1323. return(1);
  1324. } /* PHYSFS_close */
  1325. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1326. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1327. {
  1328. FileHandle *h = (FileHandle *) handle->opaque;
  1329. return(h->funcs->read(h, buffer, objSize, objCount));
  1330. } /* PHYSFS_read */
  1331. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1332. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1333. {
  1334. FileHandle *h = (FileHandle *) handle->opaque;
  1335. return(h->funcs->write(h, buffer, objSize, objCount));
  1336. } /* PHYSFS_write */
  1337. int PHYSFS_eof(PHYSFS_file *handle)
  1338. {
  1339. FileHandle *h = (FileHandle *) handle->opaque;
  1340. return(h->funcs->eof(h));
  1341. } /* PHYSFS_eof */
  1342. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1343. {
  1344. FileHandle *h = (FileHandle *) handle->opaque;
  1345. return(h->funcs->tell(h));
  1346. } /* PHYSFS_tell */
  1347. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1348. {
  1349. FileHandle *h = (FileHandle *) handle->opaque;
  1350. return(h->funcs->seek(h, pos));
  1351. } /* PHYSFS_seek */
  1352. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1353. {
  1354. FileHandle *h = (FileHandle *) handle->opaque;
  1355. return(h->funcs->fileLength(h));
  1356. } /* PHYSFS_filelength */
  1357. LinkedStringList *__PHYSFS_addToLinkedStringList(LinkedStringList *retval,
  1358. LinkedStringList **prev,
  1359. const char *str,
  1360. PHYSFS_sint32 len)
  1361. {
  1362. LinkedStringList *l;
  1363. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  1364. BAIL_IF_MACRO(l == NULL, ERR_OUT_OF_MEMORY, retval);
  1365. if (len < 0)
  1366. len = strlen(str);
  1367. l->str = (char *) malloc(len + 1);
  1368. if (l->str == NULL)
  1369. {
  1370. free(l);
  1371. BAIL_MACRO(ERR_OUT_OF_MEMORY, retval);
  1372. } /* if */
  1373. strncpy(l->str, str, len);
  1374. l->str[len] = '\0';
  1375. if (retval == NULL)
  1376. retval = l;
  1377. else
  1378. (*prev)->next = l;
  1379. *prev = l;
  1380. l->next = NULL;
  1381. return(retval);
  1382. } /* __PHYSFS_addToLinkedStringList */
  1383. /* end of physfs.c ... */