physfs.c 43 KB

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