physfs.c 48 KB

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