physfs.c 49 KB

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