physfs.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930
  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. static 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. static 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, int allowMissing)
  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) && (!allowMissing))
  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,1),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. int already_exists;
  1018. end = strchr(start, '/');
  1019. if (end != NULL)
  1020. *end = '\0';
  1021. retval = h->funcs->isDirectory(h, str, &already_exists);
  1022. if ((!retval) && (!already_exists))
  1023. retval = h->funcs->mkdir(h, str);
  1024. if (!retval)
  1025. break;
  1026. if (end == NULL)
  1027. break;
  1028. *end = '/';
  1029. start = end + 1;
  1030. } /* while */
  1031. __PHYSFS_platformReleaseMutex(stateLock);
  1032. free(str);
  1033. return(retval);
  1034. } /* PHYSFS_mkdir */
  1035. int PHYSFS_delete(const char *fname)
  1036. {
  1037. int retval;
  1038. DirHandle *h;
  1039. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1040. while (*fname == '/')
  1041. fname++;
  1042. __PHYSFS_platformGrabMutex(stateLock);
  1043. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1044. h = writeDir->dirHandle;
  1045. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h,fname,0),NULL,stateLock,0);
  1046. retval = h->funcs->remove(h, fname);
  1047. __PHYSFS_platformReleaseMutex(stateLock);
  1048. return(retval);
  1049. } /* PHYSFS_delete */
  1050. const char *PHYSFS_getRealDir(const char *filename)
  1051. {
  1052. PhysDirInfo *i;
  1053. const char *retval = NULL;
  1054. while (*filename == '/')
  1055. filename++;
  1056. __PHYSFS_platformGrabMutex(stateLock);
  1057. for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
  1058. {
  1059. DirHandle *h = i->dirHandle;
  1060. if (__PHYSFS_verifySecurity(h, filename, 0))
  1061. {
  1062. if (h->funcs->exists(h, filename))
  1063. retval = i->dirName;
  1064. } /* if */
  1065. } /* for */
  1066. __PHYSFS_platformReleaseMutex(stateLock);
  1067. return(retval);
  1068. } /* PHYSFS_getRealDir */
  1069. static int countList(LinkedStringList *list)
  1070. {
  1071. int retval = 0;
  1072. LinkedStringList *i;
  1073. for (i = list; i != NULL; i = i->next)
  1074. retval++;
  1075. return(retval);
  1076. } /* countList */
  1077. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  1078. {
  1079. int i;
  1080. LinkedStringList *next = NULL;
  1081. int len = countList(finalList);
  1082. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  1083. if (retval == NULL)
  1084. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  1085. for (i = 0; i < len; i++)
  1086. {
  1087. next = finalList->next;
  1088. if (retval == NULL)
  1089. free(finalList->str);
  1090. else
  1091. retval[i] = finalList->str;
  1092. free(finalList);
  1093. finalList = next;
  1094. } /* for */
  1095. if (retval != NULL)
  1096. retval[i] = NULL;
  1097. return(retval);
  1098. } /* convertStringListToPhysFSList */
  1099. static void insertStringListItem(LinkedStringList **final,
  1100. LinkedStringList *item)
  1101. {
  1102. LinkedStringList *i;
  1103. LinkedStringList *prev = NULL;
  1104. int rc;
  1105. for (i = *final; i != NULL; i = i->next)
  1106. {
  1107. rc = strcmp(i->str, item->str);
  1108. if (rc > 0) /* insertion point. */
  1109. break;
  1110. else if (rc == 0) /* already in list. */
  1111. {
  1112. free(item->str);
  1113. free(item);
  1114. return;
  1115. } /* else if */
  1116. prev = i;
  1117. } /* for */
  1118. /*
  1119. * If we are here, we are either at the insertion point.
  1120. * This may be the end of the list, or the list may be empty, too.
  1121. */
  1122. if (prev == NULL)
  1123. *final = item;
  1124. else
  1125. prev->next = item;
  1126. item->next = i;
  1127. } /* insertStringListItem */
  1128. /* if we run out of memory anywhere in here, we give back what we can. */
  1129. static void interpolateStringLists(LinkedStringList **final,
  1130. LinkedStringList *newList)
  1131. {
  1132. LinkedStringList *next = NULL;
  1133. while (newList != NULL)
  1134. {
  1135. next = newList->next;
  1136. insertStringListItem(final, newList);
  1137. newList = next;
  1138. } /* while */
  1139. } /* interpolateStringLists */
  1140. char **PHYSFS_enumerateFiles(const char *path)
  1141. {
  1142. PhysDirInfo *i;
  1143. char **retval = NULL;
  1144. LinkedStringList *rc;
  1145. LinkedStringList *finalList = NULL;
  1146. int omitSymLinks = !allowSymLinks;
  1147. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  1148. while (*path == '/')
  1149. path++;
  1150. __PHYSFS_platformGrabMutex(stateLock);
  1151. for (i = searchPath; i != NULL; i = i->next)
  1152. {
  1153. DirHandle *h = i->dirHandle;
  1154. if (__PHYSFS_verifySecurity(h, path, 0))
  1155. {
  1156. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  1157. interpolateStringLists(&finalList, rc);
  1158. } /* if */
  1159. } /* for */
  1160. __PHYSFS_platformReleaseMutex(stateLock);
  1161. retval = convertStringListToPhysFSList(finalList);
  1162. return(retval);
  1163. } /* PHYSFS_enumerateFiles */
  1164. int PHYSFS_exists(const char *fname)
  1165. {
  1166. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1167. while (*fname == '/')
  1168. fname++;
  1169. return(PHYSFS_getRealDir(fname) != NULL);
  1170. } /* PHYSFS_exists */
  1171. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  1172. {
  1173. PhysDirInfo *i;
  1174. PHYSFS_sint64 retval = -1;
  1175. int fileExists = 0;
  1176. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1177. while (*fname == '/')
  1178. fname++;
  1179. if (*fname == '\0') /* eh...punt if it's the root dir. */
  1180. return(1);
  1181. __PHYSFS_platformGrabMutex(stateLock);
  1182. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1183. {
  1184. DirHandle *h = i->dirHandle;
  1185. if (__PHYSFS_verifySecurity(h, fname, 0))
  1186. retval = h->funcs->getLastModTime(h, fname, &fileExists);
  1187. } /* for */
  1188. __PHYSFS_platformReleaseMutex(stateLock);
  1189. return(retval);
  1190. } /* PHYSFS_getLastModTime */
  1191. int PHYSFS_isDirectory(const char *fname)
  1192. {
  1193. PhysDirInfo *i;
  1194. int retval = 0;
  1195. int fileExists = 0;
  1196. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1197. while (*fname == '/')
  1198. fname++;
  1199. BAIL_IF_MACRO(*fname == '\0', NULL, 1); /* Root is always a dir. :) */
  1200. __PHYSFS_platformGrabMutex(stateLock);
  1201. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1202. {
  1203. DirHandle *h = i->dirHandle;
  1204. if (__PHYSFS_verifySecurity(h, fname, 0))
  1205. retval = h->funcs->isDirectory(h, fname, &fileExists);
  1206. } /* for */
  1207. __PHYSFS_platformReleaseMutex(stateLock);
  1208. return(retval);
  1209. } /* PHYSFS_isDirectory */
  1210. int PHYSFS_isSymbolicLink(const char *fname)
  1211. {
  1212. PhysDirInfo *i;
  1213. int retval = 0;
  1214. int fileExists = 0;
  1215. BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
  1216. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1217. while (*fname == '/')
  1218. fname++;
  1219. BAIL_IF_MACRO(*fname == '\0', NULL, 0); /* Root is never a symlink */
  1220. __PHYSFS_platformGrabMutex(stateLock);
  1221. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1222. {
  1223. DirHandle *h = i->dirHandle;
  1224. if (__PHYSFS_verifySecurity(h, fname, 0))
  1225. retval = h->funcs->isSymLink(h, fname, &fileExists);
  1226. } /* for */
  1227. __PHYSFS_platformReleaseMutex(stateLock);
  1228. return(retval);
  1229. } /* PHYSFS_isSymbolicLink */
  1230. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  1231. {
  1232. PHYSFS_file *retval = NULL;
  1233. FileHandle *rc = NULL;
  1234. DirHandle *h;
  1235. const DirFunctions *f;
  1236. FileHandleList *list;
  1237. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1238. while (*fname == '/')
  1239. fname++;
  1240. __PHYSFS_platformGrabMutex(stateLock);
  1241. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  1242. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  1243. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname, 0), NULL,
  1244. stateLock, NULL);
  1245. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1246. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1247. f = h->funcs;
  1248. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  1249. if (rc == NULL)
  1250. free(list);
  1251. else
  1252. {
  1253. rc->buffer = NULL; /* just in case. */
  1254. rc->buffill = rc->bufpos = rc->bufsize = 0; /* just in case. */
  1255. rc->forReading = 0;
  1256. list->handle.opaque = (void *) rc;
  1257. list->next = openWriteList;
  1258. openWriteList = list;
  1259. retval = &(list->handle);
  1260. } /* else */
  1261. __PHYSFS_platformReleaseMutex(stateLock);
  1262. return(retval);
  1263. } /* doOpenWrite */
  1264. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  1265. {
  1266. return(doOpenWrite(filename, 0));
  1267. } /* PHYSFS_openWrite */
  1268. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  1269. {
  1270. return(doOpenWrite(filename, 1));
  1271. } /* PHYSFS_openAppend */
  1272. PHYSFS_file *PHYSFS_openRead(const char *fname)
  1273. {
  1274. PHYSFS_file *retval = NULL;
  1275. FileHandle *rc = NULL;
  1276. FileHandleList *list;
  1277. int fileExists = 0;
  1278. PhysDirInfo *i;
  1279. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1280. while (*fname == '/')
  1281. fname++;
  1282. __PHYSFS_platformGrabMutex(stateLock);
  1283. BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
  1284. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1285. {
  1286. DirHandle *h = i->dirHandle;
  1287. if (__PHYSFS_verifySecurity(h, fname, 0))
  1288. rc = h->funcs->openRead(h, fname, &fileExists);
  1289. } /* for */
  1290. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1291. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1292. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1293. list->handle.opaque = (void *) rc;
  1294. list->next = openReadList;
  1295. openReadList = list;
  1296. retval = &(list->handle);
  1297. __PHYSFS_platformReleaseMutex(stateLock);
  1298. rc->buffer = NULL; /* just in case. */
  1299. rc->buffill = rc->bufpos = rc->bufsize = 0; /* just in case. */
  1300. rc->forReading = 1;
  1301. return(retval);
  1302. } /* PHYSFS_openRead */
  1303. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1304. {
  1305. FileHandle *h = (FileHandle *) handle->opaque;
  1306. FileHandleList *prev = NULL;
  1307. FileHandleList *i;
  1308. int rc = 1;
  1309. for (i = *list; i != NULL; i = i->next)
  1310. {
  1311. if (&i->handle == handle) /* handle is in this list? */
  1312. {
  1313. PHYSFS_uint8 *tmp = h->buffer;
  1314. rc = PHYSFS_flush(handle);
  1315. if (rc)
  1316. rc = h->funcs->fileClose(h);
  1317. if (!rc)
  1318. return(-1);
  1319. if (tmp != NULL) /* free any associated buffer. */
  1320. free(tmp);
  1321. if (prev == NULL)
  1322. *list = i->next;
  1323. else
  1324. prev->next = i->next;
  1325. free(i);
  1326. return(1);
  1327. } /* if */
  1328. prev = i;
  1329. } /* for */
  1330. return(0);
  1331. } /* closeHandleInOpenList */
  1332. int PHYSFS_close(PHYSFS_file *handle)
  1333. {
  1334. int rc;
  1335. __PHYSFS_platformGrabMutex(stateLock);
  1336. /* -1 == close failure. 0 == not found. 1 == success. */
  1337. rc = closeHandleInOpenList(&openReadList, handle);
  1338. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1339. if (!rc)
  1340. {
  1341. rc = closeHandleInOpenList(&openWriteList, handle);
  1342. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1343. } /* if */
  1344. __PHYSFS_platformReleaseMutex(stateLock);
  1345. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1346. return(1);
  1347. } /* PHYSFS_close */
  1348. static PHYSFS_sint64 doBufferedRead(PHYSFS_file *handle, void *buffer,
  1349. PHYSFS_uint32 objSize,
  1350. PHYSFS_uint32 objCount)
  1351. {
  1352. FileHandle *h = (FileHandle *) handle->opaque;
  1353. PHYSFS_sint64 retval = 0;
  1354. PHYSFS_uint32 remainder = 0;
  1355. while (objCount > 0)
  1356. {
  1357. PHYSFS_uint32 buffered = h->buffill - h->bufpos;
  1358. PHYSFS_uint64 mustread = (objSize * objCount) - remainder;
  1359. PHYSFS_uint32 copied;
  1360. if (buffered == 0) /* need to refill buffer? */
  1361. {
  1362. PHYSFS_sint64 rc = h->funcs->read(h, h->buffer, 1, h->bufsize);
  1363. if (rc <= 0)
  1364. {
  1365. h->bufpos -= remainder;
  1366. return(((rc == -1) && (retval == 0)) ? -1 : retval);
  1367. } /* if */
  1368. buffered = h->buffill = (PHYSFS_uint32) rc;
  1369. h->bufpos = 0;
  1370. } /* if */
  1371. if (buffered > mustread)
  1372. buffered = (PHYSFS_uint32) mustread;
  1373. memcpy(buffer, h->buffer + h->bufpos, (size_t) buffered);
  1374. buffer = ((PHYSFS_uint8 *) buffer) + buffered;
  1375. h->bufpos += buffered;
  1376. buffered += remainder; /* take remainder into account. */
  1377. copied = (buffered / objSize);
  1378. remainder = (buffered % objSize);
  1379. retval += copied;
  1380. objCount -= copied;
  1381. } /* while */
  1382. return(retval);
  1383. } /* doBufferedRead */
  1384. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1385. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1386. {
  1387. FileHandle *h = (FileHandle *) handle->opaque;
  1388. BAIL_IF_MACRO(!h->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
  1389. if (h->buffer != NULL)
  1390. return(doBufferedRead(handle, buffer, objSize, objCount));
  1391. return(h->funcs->read(h, buffer, objSize, objCount));
  1392. } /* PHYSFS_read */
  1393. static PHYSFS_sint64 doBufferedWrite(PHYSFS_file *handle, const void *buffer,
  1394. PHYSFS_uint32 objSize,
  1395. PHYSFS_uint32 objCount)
  1396. {
  1397. FileHandle *h = (FileHandle *) handle->opaque;
  1398. /* whole thing fits in the buffer? */
  1399. if (h->buffill + (objSize * objCount) < h->bufsize)
  1400. {
  1401. memcpy(h->buffer + h->buffill, buffer, objSize * objCount);
  1402. h->buffill += (objSize * objCount);
  1403. return(objCount);
  1404. } /* if */
  1405. /* would overflow buffer. Flush and then write the new objects, too. */
  1406. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
  1407. return(h->funcs->write(h, buffer, objSize, objCount));
  1408. } /* doBufferedWrite */
  1409. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1410. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1411. {
  1412. FileHandle *h = (FileHandle *) handle->opaque;
  1413. BAIL_IF_MACRO(h->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
  1414. if (h->buffer != NULL)
  1415. return(doBufferedWrite(handle, buffer, objSize, objCount));
  1416. return(h->funcs->write(h, buffer, objSize, objCount));
  1417. } /* PHYSFS_write */
  1418. int PHYSFS_eof(PHYSFS_file *handle)
  1419. {
  1420. FileHandle *h = (FileHandle *) handle->opaque;
  1421. if (!h->forReading) /* never EOF on files opened for write/append. */
  1422. return(0);
  1423. /* eof if buffer is empty and archiver says so. */
  1424. return((h->bufpos == h->buffill) && (h->funcs->eof(h)));
  1425. } /* PHYSFS_eof */
  1426. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1427. {
  1428. FileHandle *h = (FileHandle *) handle->opaque;
  1429. PHYSFS_sint64 retval = h->forReading ?
  1430. (h->funcs->tell(h) - h->buffill) + h->bufpos :
  1431. (h->funcs->tell(h) + h->buffill);
  1432. return(retval);
  1433. } /* PHYSFS_tell */
  1434. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1435. {
  1436. FileHandle *h = (FileHandle *) handle->opaque;
  1437. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1438. h->buffill = h->bufpos = 0; /* just in case. */
  1439. return(h->funcs->seek(h, pos));
  1440. } /* PHYSFS_seek */
  1441. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1442. {
  1443. FileHandle *h = (FileHandle *) handle->opaque;
  1444. return(h->funcs->fileLength(h));
  1445. } /* PHYSFS_filelength */
  1446. int PHYSFS_setBuffer(PHYSFS_file *handle, PHYSFS_uint64 _bufsize)
  1447. {
  1448. FileHandle *h = (FileHandle *) handle->opaque;
  1449. PHYSFS_uint32 bufsize = (PHYSFS_uint32) _bufsize;
  1450. BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
  1451. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1452. /*
  1453. * For reads, we need to move the file pointer to where it would be
  1454. * if we weren't buffering, so that the next read will get the
  1455. * right chunk of stuff from the file. PHYSFS_flush() handles writes.
  1456. */
  1457. if ((h->forReading) && (h->buffill != h->bufpos))
  1458. {
  1459. PHYSFS_uint64 pos;
  1460. PHYSFS_sint64 curpos = h->funcs->tell(h);
  1461. BAIL_IF_MACRO(curpos == -1, NULL, 0);
  1462. pos = ((curpos - h->buffill) + h->bufpos);
  1463. BAIL_IF_MACRO(!h->funcs->seek(h, pos), NULL, 0);
  1464. } /* if */
  1465. if (bufsize == 0) /* delete existing buffer. */
  1466. {
  1467. if (h->buffer != NULL)
  1468. {
  1469. free(h->buffer);
  1470. h->buffer = NULL;
  1471. } /* if */
  1472. } /* if */
  1473. else
  1474. {
  1475. PHYSFS_uint8 *newbuf = realloc(h->buffer, bufsize);
  1476. BAIL_IF_MACRO(newbuf == NULL, ERR_OUT_OF_MEMORY, 0);
  1477. h->buffer = newbuf;
  1478. } /* else */
  1479. h->bufsize = bufsize;
  1480. h->buffill = h->bufpos = 0;
  1481. return(1);
  1482. } /* PHYSFS_setBuffer */
  1483. int PHYSFS_flush(PHYSFS_file *handle)
  1484. {
  1485. FileHandle *h = (FileHandle *) handle->opaque;
  1486. PHYSFS_sint64 rc;
  1487. if ((h->forReading) || (h->bufpos == h->buffill))
  1488. return(1); /* open for read or buffer empty are successful no-ops. */
  1489. /* dump buffer to disk. */
  1490. rc = h->funcs->write(h, h->buffer + h->bufpos, h->buffill - h->bufpos, 1);
  1491. BAIL_IF_MACRO(rc <= 0, NULL, 0);
  1492. h->bufpos = h->buffill = 0;
  1493. return(1);
  1494. } /* PHYSFS_flush */
  1495. LinkedStringList *__PHYSFS_addToLinkedStringList(LinkedStringList *retval,
  1496. LinkedStringList **prev,
  1497. const char *str,
  1498. PHYSFS_sint32 len)
  1499. {
  1500. LinkedStringList *l;
  1501. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  1502. BAIL_IF_MACRO(l == NULL, ERR_OUT_OF_MEMORY, retval);
  1503. if (len < 0)
  1504. len = strlen(str);
  1505. l->str = (char *) malloc(len + 1);
  1506. if (l->str == NULL)
  1507. {
  1508. free(l);
  1509. BAIL_MACRO(ERR_OUT_OF_MEMORY, retval);
  1510. } /* if */
  1511. strncpy(l->str, str, len);
  1512. l->str[len] = '\0';
  1513. if (retval == NULL)
  1514. retval = l;
  1515. else
  1516. (*prev)->next = l;
  1517. *prev = l;
  1518. l->next = NULL;
  1519. return(retval);
  1520. } /* __PHYSFS_addToLinkedStringList */
  1521. /* end of physfs.c ... */