physfs.c 48 KB

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