physfs.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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. /* !!! FIXME: Can we ditch this malloc()? */
  952. start = str = malloc(strlen(fname) + 1);
  953. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  954. strcpy(str, fname);
  955. while (1)
  956. {
  957. end = strchr(start, '/');
  958. if (end != NULL)
  959. *end = '\0';
  960. if ( (strcmp(start, ".") == 0) ||
  961. (strcmp(start, "..") == 0) ||
  962. (strchr(start, '\\') != NULL) ||
  963. (strchr(start, ':') != NULL) )
  964. {
  965. __PHYSFS_setError(ERR_INSECURE_FNAME);
  966. retval = 0;
  967. break;
  968. } /* if */
  969. if (!allowSymLinks)
  970. {
  971. if (h->funcs->isSymLink(h, str, &retval))
  972. {
  973. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  974. retval = 0;
  975. } /* if */
  976. /* break out early if path element is missing or it's a symlink. */
  977. if (!retval)
  978. break;
  979. } /* if */
  980. if (end == NULL)
  981. break;
  982. *end = '/';
  983. start = end + 1;
  984. } /* while */
  985. free(str);
  986. return(retval);
  987. } /* __PHYSFS_verifySecurity */
  988. int PHYSFS_mkdir(const char *dname)
  989. {
  990. DirHandle *h;
  991. char *str;
  992. char *start;
  993. char *end;
  994. int retval = 0;
  995. BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
  996. while (*dname == '/')
  997. dname++;
  998. __PHYSFS_platformGrabMutex(stateLock);
  999. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1000. h = writeDir->dirHandle;
  1001. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, dname), NULL, stateLock, 0);
  1002. start = str = malloc(strlen(dname) + 1);
  1003. BAIL_IF_MACRO_MUTEX(str == NULL, ERR_OUT_OF_MEMORY, stateLock, 0);
  1004. strcpy(str, dname);
  1005. while (1)
  1006. {
  1007. end = strchr(start, '/');
  1008. if (end != NULL)
  1009. *end = '\0';
  1010. retval = h->funcs->mkdir(h, str);
  1011. if (!retval)
  1012. break;
  1013. if (end == NULL)
  1014. break;
  1015. *end = '/';
  1016. start = end + 1;
  1017. } /* while */
  1018. __PHYSFS_platformReleaseMutex(stateLock);
  1019. free(str);
  1020. return(retval);
  1021. } /* PHYSFS_mkdir */
  1022. int PHYSFS_delete(const char *fname)
  1023. {
  1024. int retval;
  1025. DirHandle *h;
  1026. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1027. while (*fname == '/')
  1028. fname++;
  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, fname), NULL, stateLock, 0);
  1033. retval = h->funcs->remove(h, fname);
  1034. __PHYSFS_platformReleaseMutex(stateLock);
  1035. return(retval);
  1036. } /* PHYSFS_delete */
  1037. const char *PHYSFS_getRealDir(const char *filename)
  1038. {
  1039. PhysDirInfo *i;
  1040. const char *retval = NULL;
  1041. while (*filename == '/')
  1042. filename++;
  1043. __PHYSFS_platformGrabMutex(stateLock);
  1044. for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
  1045. {
  1046. DirHandle *h = i->dirHandle;
  1047. if (__PHYSFS_verifySecurity(h, filename))
  1048. {
  1049. if (h->funcs->exists(h, filename))
  1050. retval = i->dirName;
  1051. } /* if */
  1052. } /* for */
  1053. __PHYSFS_platformReleaseMutex(stateLock);
  1054. return(retval);
  1055. } /* PHYSFS_getRealDir */
  1056. static int countList(LinkedStringList *list)
  1057. {
  1058. int retval = 0;
  1059. LinkedStringList *i;
  1060. for (i = list; i != NULL; i = i->next)
  1061. retval++;
  1062. return(retval);
  1063. } /* countList */
  1064. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  1065. {
  1066. int i;
  1067. LinkedStringList *next = NULL;
  1068. int len = countList(finalList);
  1069. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  1070. if (retval == NULL)
  1071. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  1072. for (i = 0; i < len; i++)
  1073. {
  1074. next = finalList->next;
  1075. if (retval == NULL)
  1076. free(finalList->str);
  1077. else
  1078. retval[i] = finalList->str;
  1079. free(finalList);
  1080. finalList = next;
  1081. } /* for */
  1082. if (retval != NULL)
  1083. retval[i] = NULL;
  1084. return(retval);
  1085. } /* convertStringListToPhysFSList */
  1086. static void insertStringListItem(LinkedStringList **final,
  1087. LinkedStringList *item)
  1088. {
  1089. LinkedStringList *i;
  1090. LinkedStringList *prev = NULL;
  1091. int rc;
  1092. for (i = *final; i != NULL; i = i->next)
  1093. {
  1094. rc = strcmp(i->str, item->str);
  1095. if (rc > 0) /* insertion point. */
  1096. break;
  1097. else if (rc == 0) /* already in list. */
  1098. {
  1099. free(item->str);
  1100. free(item);
  1101. return;
  1102. } /* else if */
  1103. prev = i;
  1104. } /* for */
  1105. /*
  1106. * If we are here, we are either at the insertion point.
  1107. * This may be the end of the list, or the list may be empty, too.
  1108. */
  1109. if (prev == NULL)
  1110. *final = item;
  1111. else
  1112. prev->next = item;
  1113. item->next = i;
  1114. } /* insertStringListItem */
  1115. /* if we run out of memory anywhere in here, we give back what we can. */
  1116. static void interpolateStringLists(LinkedStringList **final,
  1117. LinkedStringList *newList)
  1118. {
  1119. LinkedStringList *next = NULL;
  1120. while (newList != NULL)
  1121. {
  1122. next = newList->next;
  1123. insertStringListItem(final, newList);
  1124. newList = next;
  1125. } /* while */
  1126. } /* interpolateStringLists */
  1127. char **PHYSFS_enumerateFiles(const char *path)
  1128. {
  1129. PhysDirInfo *i;
  1130. char **retval = NULL;
  1131. LinkedStringList *rc;
  1132. LinkedStringList *finalList = NULL;
  1133. int omitSymLinks = !allowSymLinks;
  1134. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  1135. while (*path == '/')
  1136. path++;
  1137. __PHYSFS_platformGrabMutex(stateLock);
  1138. for (i = searchPath; i != NULL; i = i->next)
  1139. {
  1140. DirHandle *h = i->dirHandle;
  1141. if (__PHYSFS_verifySecurity(h, path))
  1142. {
  1143. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  1144. interpolateStringLists(&finalList, rc);
  1145. } /* if */
  1146. } /* for */
  1147. __PHYSFS_platformReleaseMutex(stateLock);
  1148. retval = convertStringListToPhysFSList(finalList);
  1149. return(retval);
  1150. } /* PHYSFS_enumerateFiles */
  1151. int PHYSFS_exists(const char *fname)
  1152. {
  1153. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1154. while (*fname == '/')
  1155. fname++;
  1156. return(PHYSFS_getRealDir(fname) != NULL);
  1157. } /* PHYSFS_exists */
  1158. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  1159. {
  1160. PhysDirInfo *i;
  1161. PHYSFS_sint64 retval = -1;
  1162. int fileExists = 0;
  1163. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1164. while (*fname == '/')
  1165. fname++;
  1166. if (*fname == '\0') /* eh...punt if it's the root dir. */
  1167. return(1);
  1168. __PHYSFS_platformGrabMutex(stateLock);
  1169. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1170. {
  1171. DirHandle *h = i->dirHandle;
  1172. if (__PHYSFS_verifySecurity(h, fname))
  1173. retval = h->funcs->getLastModTime(h, fname, &fileExists);
  1174. } /* for */
  1175. __PHYSFS_platformReleaseMutex(stateLock);
  1176. return(retval);
  1177. } /* PHYSFS_getLastModTime */
  1178. int PHYSFS_isDirectory(const char *fname)
  1179. {
  1180. PhysDirInfo *i;
  1181. int retval = 0;
  1182. int fileExists = 0;
  1183. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1184. while (*fname == '/')
  1185. fname++;
  1186. BAIL_IF_MACRO(*fname == '\0', NULL, 1); /* Root is always a dir. :) */
  1187. __PHYSFS_platformGrabMutex(stateLock);
  1188. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1189. {
  1190. DirHandle *h = i->dirHandle;
  1191. if (__PHYSFS_verifySecurity(h, fname))
  1192. retval = h->funcs->isDirectory(h, fname, &fileExists);
  1193. } /* for */
  1194. __PHYSFS_platformReleaseMutex(stateLock);
  1195. return(retval);
  1196. } /* PHYSFS_isDirectory */
  1197. int PHYSFS_isSymbolicLink(const char *fname)
  1198. {
  1199. PhysDirInfo *i;
  1200. int retval = 0;
  1201. int fileExists = 0;
  1202. BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
  1203. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1204. while (*fname == '/')
  1205. fname++;
  1206. BAIL_IF_MACRO(*fname == '\0', NULL, 0); /* Root is never a symlink */
  1207. __PHYSFS_platformGrabMutex(stateLock);
  1208. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1209. {
  1210. DirHandle *h = i->dirHandle;
  1211. if (__PHYSFS_verifySecurity(h, fname))
  1212. retval = h->funcs->isSymLink(h, fname, &fileExists);
  1213. } /* for */
  1214. __PHYSFS_platformReleaseMutex(stateLock);
  1215. return(retval);
  1216. } /* PHYSFS_isSymbolicLink */
  1217. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  1218. {
  1219. PHYSFS_file *retval = NULL;
  1220. FileHandle *rc = NULL;
  1221. DirHandle *h;
  1222. const DirFunctions *f;
  1223. FileHandleList *list;
  1224. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1225. while (*fname == '/')
  1226. fname++;
  1227. __PHYSFS_platformGrabMutex(stateLock);
  1228. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  1229. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  1230. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL,
  1231. stateLock, NULL);
  1232. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1233. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1234. f = h->funcs;
  1235. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  1236. if (rc == NULL)
  1237. free(list);
  1238. else
  1239. {
  1240. list->handle.opaque = (void *) rc;
  1241. list->next = openWriteList;
  1242. openWriteList = list;
  1243. retval = &(list->handle);
  1244. } /* else */
  1245. __PHYSFS_platformReleaseMutex(stateLock);
  1246. return(retval);
  1247. } /* doOpenWrite */
  1248. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  1249. {
  1250. return(doOpenWrite(filename, 0));
  1251. } /* PHYSFS_openWrite */
  1252. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  1253. {
  1254. return(doOpenWrite(filename, 1));
  1255. } /* PHYSFS_openAppend */
  1256. PHYSFS_file *PHYSFS_openRead(const char *fname)
  1257. {
  1258. PHYSFS_file *retval = NULL;
  1259. FileHandle *rc = NULL;
  1260. FileHandleList *list;
  1261. int fileExists = 0;
  1262. PhysDirInfo *i;
  1263. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1264. while (*fname == '/')
  1265. fname++;
  1266. __PHYSFS_platformGrabMutex(stateLock);
  1267. BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
  1268. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1269. {
  1270. DirHandle *h = i->dirHandle;
  1271. if (__PHYSFS_verifySecurity(h, fname))
  1272. rc = h->funcs->openRead(h, fname, &fileExists);
  1273. } /* for */
  1274. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1275. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1276. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  1277. list->handle.opaque = (void *) rc;
  1278. list->next = openReadList;
  1279. openReadList = list;
  1280. retval = &(list->handle);
  1281. __PHYSFS_platformReleaseMutex(stateLock);
  1282. return(retval);
  1283. } /* PHYSFS_openRead */
  1284. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1285. {
  1286. FileHandle *h = (FileHandle *) handle->opaque;
  1287. FileHandleList *prev = NULL;
  1288. FileHandleList *i;
  1289. int rc;
  1290. for (i = *list; i != NULL; i = i->next)
  1291. {
  1292. if (&i->handle == handle) /* handle is in this list? */
  1293. {
  1294. rc = h->funcs->fileClose(h);
  1295. if (!rc)
  1296. return(-1);
  1297. if (prev == NULL)
  1298. *list = i->next;
  1299. else
  1300. prev->next = i->next;
  1301. free(i);
  1302. return(1);
  1303. } /* if */
  1304. prev = i;
  1305. } /* for */
  1306. return(0);
  1307. } /* closeHandleInOpenList */
  1308. int PHYSFS_close(PHYSFS_file *handle)
  1309. {
  1310. int rc;
  1311. __PHYSFS_platformGrabMutex(stateLock);
  1312. /* -1 == close failure. 0 == not found. 1 == success. */
  1313. rc = closeHandleInOpenList(&openReadList, handle);
  1314. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1315. if (!rc)
  1316. {
  1317. rc = closeHandleInOpenList(&openWriteList, handle);
  1318. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1319. } /* if */
  1320. __PHYSFS_platformReleaseMutex(stateLock);
  1321. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1322. return(1);
  1323. } /* PHYSFS_close */
  1324. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1325. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1326. {
  1327. FileHandle *h = (FileHandle *) handle->opaque;
  1328. return(h->funcs->read(h, buffer, objSize, objCount));
  1329. } /* PHYSFS_read */
  1330. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1331. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1332. {
  1333. FileHandle *h = (FileHandle *) handle->opaque;
  1334. return(h->funcs->write(h, buffer, objSize, objCount));
  1335. } /* PHYSFS_write */
  1336. int PHYSFS_eof(PHYSFS_file *handle)
  1337. {
  1338. FileHandle *h = (FileHandle *) handle->opaque;
  1339. return(h->funcs->eof(h));
  1340. } /* PHYSFS_eof */
  1341. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1342. {
  1343. FileHandle *h = (FileHandle *) handle->opaque;
  1344. return(h->funcs->tell(h));
  1345. } /* PHYSFS_tell */
  1346. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1347. {
  1348. FileHandle *h = (FileHandle *) handle->opaque;
  1349. return(h->funcs->seek(h, pos));
  1350. } /* PHYSFS_seek */
  1351. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1352. {
  1353. FileHandle *h = (FileHandle *) handle->opaque;
  1354. return(h->funcs->fileLength(h));
  1355. } /* PHYSFS_filelength */
  1356. LinkedStringList *__PHYSFS_addToLinkedStringList(LinkedStringList *retval,
  1357. LinkedStringList **prev,
  1358. const char *str,
  1359. PHYSFS_sint32 len)
  1360. {
  1361. LinkedStringList *l;
  1362. l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
  1363. BAIL_IF_MACRO(l == NULL, ERR_OUT_OF_MEMORY, retval);
  1364. if (len < 0)
  1365. len = strlen(str);
  1366. l->str = (char *) malloc(len + 1);
  1367. if (l->str == NULL)
  1368. {
  1369. free(l);
  1370. BAIL_MACRO(ERR_OUT_OF_MEMORY, retval);
  1371. } /* if */
  1372. strncpy(l->str, str, len);
  1373. l->str[len] = '\0';
  1374. if (retval == NULL)
  1375. retval = l;
  1376. else
  1377. (*prev)->next = l;
  1378. *prev = l;
  1379. l->next = NULL;
  1380. return(retval);
  1381. } /* __PHYSFS_addToLinkedStringList */
  1382. /* end of physfs.c ... */