physfs.c 48 KB

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