physfs.c 44 KB

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