physfs.c 49 KB

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