physfs.c 99 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473
  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.txt in the source's root directory.
  7. *
  8. * This file written by Ryan C. Gordon.
  9. */
  10. #define __PHYSICSFS_INTERNAL__
  11. #include "physfs_internal.h"
  12. #if defined(_MSC_VER)
  13. /* this code came from https://stackoverflow.com/a/8712996 */
  14. int __PHYSFS_msvc_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
  15. {
  16. int count = -1;
  17. if (size != 0)
  18. count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
  19. if (count == -1)
  20. count = _vscprintf(format, ap);
  21. return count;
  22. }
  23. int __PHYSFS_msvc_snprintf(char *outBuf, size_t size, const char *format, ...)
  24. {
  25. int count;
  26. va_list ap;
  27. va_start(ap, format);
  28. count = __PHYSFS_msvc_vsnprintf(outBuf, size, format, ap);
  29. va_end(ap);
  30. return count;
  31. }
  32. #endif
  33. typedef struct __PHYSFS_DIRHANDLE__
  34. {
  35. void *opaque; /* Instance data unique to the archiver. */
  36. char *dirName; /* Path to archive in platform-dependent notation. */
  37. char *mountPoint; /* Mountpoint in virtual file tree. */
  38. char *root; /* subdirectory of archiver to use as root of archive (NULL for actual root) */
  39. size_t rootlen; /* subdirectory of archiver to use as root of archive (NULL for actual root) */
  40. const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
  41. struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
  42. } DirHandle;
  43. typedef struct __PHYSFS_FILEHANDLE__
  44. {
  45. PHYSFS_Io *io; /* Instance data unique to the archiver for this file. */
  46. PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
  47. const DirHandle *dirHandle; /* Archiver instance that created this */
  48. PHYSFS_uint8 *buffer; /* Buffer, if set (NULL otherwise). Don't touch! */
  49. size_t bufsize; /* Bufsize, if set (0 otherwise). Don't touch! */
  50. size_t buffill; /* Buffer fill size. Don't touch! */
  51. size_t bufpos; /* Buffer position. Don't touch! */
  52. struct __PHYSFS_FILEHANDLE__ *next; /* linked list stuff. */
  53. } FileHandle;
  54. typedef struct __PHYSFS_ERRSTATETYPE__
  55. {
  56. void *tid;
  57. PHYSFS_ErrorCode code;
  58. struct __PHYSFS_ERRSTATETYPE__ *next;
  59. } ErrState;
  60. /* General PhysicsFS state ... */
  61. static int initialized = 0;
  62. static ErrState *errorStates = NULL;
  63. static DirHandle *searchPath = NULL;
  64. static DirHandle *writeDir = NULL;
  65. static FileHandle *openWriteList = NULL;
  66. static FileHandle *openReadList = NULL;
  67. static char *baseDir = NULL;
  68. static char *userDir = NULL;
  69. static char *prefDir = NULL;
  70. static int allowSymLinks = 0;
  71. static PHYSFS_Archiver **archivers = NULL;
  72. static PHYSFS_ArchiveInfo **archiveInfo = NULL;
  73. static volatile size_t numArchivers = 0;
  74. static size_t longest_root = 0;
  75. /* mutexes ... */
  76. static void *errorLock = NULL; /* protects error message list. */
  77. static void *stateLock = NULL; /* protects other PhysFS static state. */
  78. /* allocator ... */
  79. static int externalAllocator = 0;
  80. PHYSFS_Allocator allocator;
  81. #ifdef PHYSFS_NEED_ATOMIC_OP_FALLBACK
  82. static inline int __PHYSFS_atomicAdd(int *ptrval, const int val)
  83. {
  84. int retval;
  85. __PHYSFS_platformGrabMutex(stateLock);
  86. *ptrval += val;
  87. retval = *ptrval;
  88. __PHYSFS_platformReleaseMutex(stateLock);
  89. return retval;
  90. } /* __PHYSFS_atomicAdd */
  91. int __PHYSFS_ATOMIC_INCR(int *ptrval)
  92. {
  93. return __PHYSFS_atomicAdd(ptrval, 1);
  94. } /* __PHYSFS_ATOMIC_INCR */
  95. int __PHYSFS_ATOMIC_DECR(int *ptrval)
  96. {
  97. return __PHYSFS_atomicAdd(ptrval, -1);
  98. } /* __PHYSFS_ATOMIC_DECR */
  99. #endif
  100. /* PHYSFS_Io implementation for i/o to physical filesystem... */
  101. /* !!! FIXME: maybe refcount the paths in a string pool? */
  102. typedef struct __PHYSFS_NativeIoInfo
  103. {
  104. void *handle;
  105. const char *path;
  106. int mode; /* 'r', 'w', or 'a' */
  107. } NativeIoInfo;
  108. static PHYSFS_sint64 nativeIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
  109. {
  110. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  111. return __PHYSFS_platformRead(info->handle, buf, len);
  112. } /* nativeIo_read */
  113. static PHYSFS_sint64 nativeIo_write(PHYSFS_Io *io, const void *buffer,
  114. PHYSFS_uint64 len)
  115. {
  116. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  117. return __PHYSFS_platformWrite(info->handle, buffer, len);
  118. } /* nativeIo_write */
  119. static int nativeIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  120. {
  121. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  122. return __PHYSFS_platformSeek(info->handle, offset);
  123. } /* nativeIo_seek */
  124. static PHYSFS_sint64 nativeIo_tell(PHYSFS_Io *io)
  125. {
  126. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  127. return __PHYSFS_platformTell(info->handle);
  128. } /* nativeIo_tell */
  129. static PHYSFS_sint64 nativeIo_length(PHYSFS_Io *io)
  130. {
  131. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  132. return __PHYSFS_platformFileLength(info->handle);
  133. } /* nativeIo_length */
  134. static PHYSFS_Io *nativeIo_duplicate(PHYSFS_Io *io)
  135. {
  136. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  137. return __PHYSFS_createNativeIo(info->path, info->mode);
  138. } /* nativeIo_duplicate */
  139. static int nativeIo_flush(PHYSFS_Io *io)
  140. {
  141. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  142. return __PHYSFS_platformFlush(info->handle);
  143. } /* nativeIo_flush */
  144. static void nativeIo_destroy(PHYSFS_Io *io)
  145. {
  146. NativeIoInfo *info = (NativeIoInfo *) io->opaque;
  147. __PHYSFS_platformClose(info->handle);
  148. allocator.Free((void *) info->path);
  149. allocator.Free(info);
  150. allocator.Free(io);
  151. } /* nativeIo_destroy */
  152. static const PHYSFS_Io __PHYSFS_nativeIoInterface =
  153. {
  154. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  155. nativeIo_read,
  156. nativeIo_write,
  157. nativeIo_seek,
  158. nativeIo_tell,
  159. nativeIo_length,
  160. nativeIo_duplicate,
  161. nativeIo_flush,
  162. nativeIo_destroy
  163. };
  164. PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode)
  165. {
  166. PHYSFS_Io *io = NULL;
  167. NativeIoInfo *info = NULL;
  168. void *handle = NULL;
  169. char *pathdup = NULL;
  170. assert((mode == 'r') || (mode == 'w') || (mode == 'a'));
  171. io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  172. GOTO_IF(!io, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
  173. info = (NativeIoInfo *) allocator.Malloc(sizeof (NativeIoInfo));
  174. GOTO_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
  175. pathdup = (char *) allocator.Malloc(strlen(path) + 1);
  176. GOTO_IF(!pathdup, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
  177. if (mode == 'r')
  178. handle = __PHYSFS_platformOpenRead(path);
  179. else if (mode == 'w')
  180. handle = __PHYSFS_platformOpenWrite(path);
  181. else if (mode == 'a')
  182. handle = __PHYSFS_platformOpenAppend(path);
  183. GOTO_IF_ERRPASS(!handle, createNativeIo_failed);
  184. strcpy(pathdup, path);
  185. info->handle = handle;
  186. info->path = pathdup;
  187. info->mode = mode;
  188. memcpy(io, &__PHYSFS_nativeIoInterface, sizeof (*io));
  189. io->opaque = info;
  190. return io;
  191. createNativeIo_failed:
  192. if (handle != NULL) __PHYSFS_platformClose(handle);
  193. if (pathdup != NULL) allocator.Free(pathdup);
  194. if (info != NULL) allocator.Free(info);
  195. if (io != NULL) allocator.Free(io);
  196. return NULL;
  197. } /* __PHYSFS_createNativeIo */
  198. /* PHYSFS_Io implementation for i/o to a memory buffer... */
  199. typedef struct __PHYSFS_MemoryIoInfo
  200. {
  201. const PHYSFS_uint8 *buf;
  202. PHYSFS_uint64 len;
  203. PHYSFS_uint64 pos;
  204. PHYSFS_Io *parent;
  205. int refcount;
  206. void (*destruct)(void *);
  207. } MemoryIoInfo;
  208. static PHYSFS_sint64 memoryIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
  209. {
  210. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  211. const PHYSFS_uint64 avail = info->len - info->pos;
  212. assert(avail <= info->len);
  213. if (avail == 0)
  214. return 0; /* we're at EOF; nothing to do. */
  215. if (len > avail)
  216. len = avail;
  217. memcpy(buf, info->buf + info->pos, (size_t) len);
  218. info->pos += len;
  219. return len;
  220. } /* memoryIo_read */
  221. static PHYSFS_sint64 memoryIo_write(PHYSFS_Io *io, const void *buffer,
  222. PHYSFS_uint64 len)
  223. {
  224. BAIL(PHYSFS_ERR_OPEN_FOR_READING, -1);
  225. } /* memoryIo_write */
  226. static int memoryIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  227. {
  228. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  229. BAIL_IF(offset > info->len, PHYSFS_ERR_PAST_EOF, 0);
  230. info->pos = offset;
  231. return 1;
  232. } /* memoryIo_seek */
  233. static PHYSFS_sint64 memoryIo_tell(PHYSFS_Io *io)
  234. {
  235. const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  236. return (PHYSFS_sint64) info->pos;
  237. } /* memoryIo_tell */
  238. static PHYSFS_sint64 memoryIo_length(PHYSFS_Io *io)
  239. {
  240. const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  241. return (PHYSFS_sint64) info->len;
  242. } /* memoryIo_length */
  243. static PHYSFS_Io *memoryIo_duplicate(PHYSFS_Io *io)
  244. {
  245. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  246. MemoryIoInfo *newinfo = NULL;
  247. PHYSFS_Io *parent = info->parent;
  248. PHYSFS_Io *retval = NULL;
  249. /* avoid deep copies. */
  250. assert((!parent) || (!((MemoryIoInfo *) parent->opaque)->parent) );
  251. /* share the buffer between duplicates. */
  252. if (parent != NULL) /* dup the parent, increment its refcount. */
  253. return parent->duplicate(parent);
  254. /* we're the parent. */
  255. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  256. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  257. newinfo = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
  258. if (!newinfo)
  259. {
  260. allocator.Free(retval);
  261. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  262. } /* if */
  263. (void) __PHYSFS_ATOMIC_INCR(&info->refcount);
  264. memset(newinfo, '\0', sizeof (*info));
  265. newinfo->buf = info->buf;
  266. newinfo->len = info->len;
  267. newinfo->pos = 0;
  268. newinfo->parent = io;
  269. newinfo->refcount = 0;
  270. newinfo->destruct = NULL;
  271. memcpy(retval, io, sizeof (*retval));
  272. retval->opaque = newinfo;
  273. return retval;
  274. } /* memoryIo_duplicate */
  275. static int memoryIo_flush(PHYSFS_Io *io) { return 1; /* it's read-only. */ }
  276. static void memoryIo_destroy(PHYSFS_Io *io)
  277. {
  278. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  279. PHYSFS_Io *parent = info->parent;
  280. if (parent != NULL)
  281. {
  282. assert(info->buf == ((MemoryIoInfo *) info->parent->opaque)->buf);
  283. assert(info->len == ((MemoryIoInfo *) info->parent->opaque)->len);
  284. assert(info->refcount == 0);
  285. assert(info->destruct == NULL);
  286. allocator.Free(info);
  287. allocator.Free(io);
  288. parent->destroy(parent); /* decrements refcount. */
  289. return;
  290. } /* if */
  291. /* we _are_ the parent. */
  292. assert(info->refcount > 0); /* even in a race, we hold a reference. */
  293. if (__PHYSFS_ATOMIC_DECR(&info->refcount) == 0)
  294. {
  295. void (*destruct)(void *) = info->destruct;
  296. void *buf = (void *) info->buf;
  297. io->opaque = NULL; /* kill this here in case of race. */
  298. allocator.Free(info);
  299. allocator.Free(io);
  300. if (destruct != NULL)
  301. destruct(buf);
  302. } /* if */
  303. } /* memoryIo_destroy */
  304. static const PHYSFS_Io __PHYSFS_memoryIoInterface =
  305. {
  306. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  307. memoryIo_read,
  308. memoryIo_write,
  309. memoryIo_seek,
  310. memoryIo_tell,
  311. memoryIo_length,
  312. memoryIo_duplicate,
  313. memoryIo_flush,
  314. memoryIo_destroy
  315. };
  316. PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
  317. void (*destruct)(void *))
  318. {
  319. PHYSFS_Io *io = NULL;
  320. MemoryIoInfo *info = NULL;
  321. io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  322. GOTO_IF(!io, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
  323. info = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
  324. GOTO_IF(!info, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
  325. memset(info, '\0', sizeof (*info));
  326. info->buf = (const PHYSFS_uint8 *) buf;
  327. info->len = len;
  328. info->pos = 0;
  329. info->parent = NULL;
  330. info->refcount = 1;
  331. info->destruct = destruct;
  332. memcpy(io, &__PHYSFS_memoryIoInterface, sizeof (*io));
  333. io->opaque = info;
  334. return io;
  335. createMemoryIo_failed:
  336. if (info != NULL) allocator.Free(info);
  337. if (io != NULL) allocator.Free(io);
  338. return NULL;
  339. } /* __PHYSFS_createMemoryIo */
  340. /* PHYSFS_Io implementation for i/o to a PHYSFS_File... */
  341. static PHYSFS_sint64 handleIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
  342. {
  343. return PHYSFS_readBytes((PHYSFS_File *) io->opaque, buf, len);
  344. } /* handleIo_read */
  345. static PHYSFS_sint64 handleIo_write(PHYSFS_Io *io, const void *buffer,
  346. PHYSFS_uint64 len)
  347. {
  348. return PHYSFS_writeBytes((PHYSFS_File *) io->opaque, buffer, len);
  349. } /* handleIo_write */
  350. static int handleIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
  351. {
  352. return PHYSFS_seek((PHYSFS_File *) io->opaque, offset);
  353. } /* handleIo_seek */
  354. static PHYSFS_sint64 handleIo_tell(PHYSFS_Io *io)
  355. {
  356. return PHYSFS_tell((PHYSFS_File *) io->opaque);
  357. } /* handleIo_tell */
  358. static PHYSFS_sint64 handleIo_length(PHYSFS_Io *io)
  359. {
  360. return PHYSFS_fileLength((PHYSFS_File *) io->opaque);
  361. } /* handleIo_length */
  362. static PHYSFS_Io *handleIo_duplicate(PHYSFS_Io *io)
  363. {
  364. /*
  365. * There's no duplicate at the PHYSFS_File level, so we break the
  366. * abstraction. We're allowed to: we're physfs.c!
  367. */
  368. FileHandle *origfh = (FileHandle *) io->opaque;
  369. FileHandle *newfh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  370. PHYSFS_Io *retval = NULL;
  371. GOTO_IF(!newfh, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
  372. memset(newfh, '\0', sizeof (*newfh));
  373. retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  374. GOTO_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
  375. #if 0 /* we don't buffer the duplicate, at least not at the moment. */
  376. if (origfh->buffer != NULL)
  377. {
  378. newfh->buffer = (PHYSFS_uint8 *) allocator.Malloc(origfh->bufsize);
  379. if (!newfh->buffer)
  380. GOTO(PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
  381. newfh->bufsize = origfh->bufsize;
  382. } /* if */
  383. #endif
  384. newfh->io = origfh->io->duplicate(origfh->io);
  385. GOTO_IF_ERRPASS(!newfh->io, handleIo_dupe_failed);
  386. newfh->forReading = origfh->forReading;
  387. newfh->dirHandle = origfh->dirHandle;
  388. __PHYSFS_platformGrabMutex(stateLock);
  389. if (newfh->forReading)
  390. {
  391. newfh->next = openReadList;
  392. openReadList = newfh;
  393. } /* if */
  394. else
  395. {
  396. newfh->next = openWriteList;
  397. openWriteList = newfh;
  398. } /* else */
  399. __PHYSFS_platformReleaseMutex(stateLock);
  400. memcpy(retval, io, sizeof (PHYSFS_Io));
  401. retval->opaque = newfh;
  402. return retval;
  403. handleIo_dupe_failed:
  404. if (newfh)
  405. {
  406. if (newfh->io != NULL) newfh->io->destroy(newfh->io);
  407. if (newfh->buffer != NULL) allocator.Free(newfh->buffer);
  408. allocator.Free(newfh);
  409. } /* if */
  410. return NULL;
  411. } /* handleIo_duplicate */
  412. static int handleIo_flush(PHYSFS_Io *io)
  413. {
  414. return PHYSFS_flush((PHYSFS_File *) io->opaque);
  415. } /* handleIo_flush */
  416. static void handleIo_destroy(PHYSFS_Io *io)
  417. {
  418. if (io->opaque != NULL)
  419. PHYSFS_close((PHYSFS_File *) io->opaque);
  420. allocator.Free(io);
  421. } /* handleIo_destroy */
  422. static const PHYSFS_Io __PHYSFS_handleIoInterface =
  423. {
  424. CURRENT_PHYSFS_IO_API_VERSION, NULL,
  425. handleIo_read,
  426. handleIo_write,
  427. handleIo_seek,
  428. handleIo_tell,
  429. handleIo_length,
  430. handleIo_duplicate,
  431. handleIo_flush,
  432. handleIo_destroy
  433. };
  434. static PHYSFS_Io *__PHYSFS_createHandleIo(PHYSFS_File *f)
  435. {
  436. PHYSFS_Io *io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
  437. BAIL_IF(!io, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  438. memcpy(io, &__PHYSFS_handleIoInterface, sizeof (*io));
  439. io->opaque = f;
  440. return io;
  441. } /* __PHYSFS_createHandleIo */
  442. /* functions ... */
  443. typedef struct
  444. {
  445. char **list;
  446. PHYSFS_uint32 size;
  447. PHYSFS_ErrorCode errcode;
  448. } EnumStringListCallbackData;
  449. static void enumStringListCallback(void *data, const char *str)
  450. {
  451. void *ptr;
  452. char *newstr;
  453. EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
  454. if (pecd->errcode)
  455. return;
  456. ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
  457. newstr = (char *) allocator.Malloc(strlen(str) + 1);
  458. if (ptr != NULL)
  459. pecd->list = (char **) ptr;
  460. if ((ptr == NULL) || (newstr == NULL))
  461. {
  462. pecd->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
  463. pecd->list[pecd->size] = NULL;
  464. PHYSFS_freeList(pecd->list);
  465. return;
  466. } /* if */
  467. strcpy(newstr, str);
  468. pecd->list[pecd->size] = newstr;
  469. pecd->size++;
  470. } /* enumStringListCallback */
  471. static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
  472. {
  473. EnumStringListCallbackData ecd;
  474. memset(&ecd, '\0', sizeof (ecd));
  475. ecd.list = (char **) allocator.Malloc(sizeof (char *));
  476. BAIL_IF(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  477. func(enumStringListCallback, &ecd);
  478. if (ecd.errcode)
  479. {
  480. PHYSFS_setErrorCode(ecd.errcode);
  481. return NULL;
  482. } /* if */
  483. ecd.list[ecd.size] = NULL;
  484. return ecd.list;
  485. } /* doEnumStringList */
  486. static void __PHYSFS_bubble_sort(void *a, size_t lo, size_t hi,
  487. int (*cmpfn)(void *, size_t, size_t),
  488. void (*swapfn)(void *, size_t, size_t))
  489. {
  490. size_t i;
  491. int sorted;
  492. do
  493. {
  494. sorted = 1;
  495. for (i = lo; i < hi; i++)
  496. {
  497. if (cmpfn(a, i, i + 1) > 0)
  498. {
  499. swapfn(a, i, i + 1);
  500. sorted = 0;
  501. } /* if */
  502. } /* for */
  503. } while (!sorted);
  504. } /* __PHYSFS_bubble_sort */
  505. static void __PHYSFS_quick_sort(void *a, size_t lo, size_t hi,
  506. int (*cmpfn)(void *, size_t, size_t),
  507. void (*swapfn)(void *, size_t, size_t))
  508. {
  509. size_t i;
  510. size_t j;
  511. size_t v;
  512. if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
  513. __PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
  514. else
  515. {
  516. i = (hi + lo) / 2;
  517. if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
  518. if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
  519. if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
  520. j = hi - 1;
  521. swapfn(a, i, j);
  522. i = lo;
  523. v = j;
  524. while (1)
  525. {
  526. while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
  527. while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
  528. if (j < i)
  529. break;
  530. swapfn(a, i, j);
  531. } /* while */
  532. if (i != (hi-1))
  533. swapfn(a, i, hi-1);
  534. __PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
  535. __PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
  536. } /* else */
  537. } /* __PHYSFS_quick_sort */
  538. void __PHYSFS_sort(void *entries, size_t max,
  539. int (*cmpfn)(void *, size_t, size_t),
  540. void (*swapfn)(void *, size_t, size_t))
  541. {
  542. /*
  543. * Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
  544. * https://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
  545. */
  546. if (max > 0)
  547. __PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
  548. } /* __PHYSFS_sort */
  549. static ErrState *findErrorForCurrentThread(void)
  550. {
  551. ErrState *i;
  552. void *tid;
  553. if (errorLock != NULL)
  554. __PHYSFS_platformGrabMutex(errorLock);
  555. if (errorStates != NULL)
  556. {
  557. tid = __PHYSFS_platformGetThreadID();
  558. for (i = errorStates; i != NULL; i = i->next)
  559. {
  560. if (i->tid == tid)
  561. {
  562. if (errorLock != NULL)
  563. __PHYSFS_platformReleaseMutex(errorLock);
  564. return i;
  565. } /* if */
  566. } /* for */
  567. } /* if */
  568. if (errorLock != NULL)
  569. __PHYSFS_platformReleaseMutex(errorLock);
  570. return NULL; /* no error available. */
  571. } /* findErrorForCurrentThread */
  572. /* this doesn't reset the error state. */
  573. static inline PHYSFS_ErrorCode currentErrorCode(void)
  574. {
  575. const ErrState *err = findErrorForCurrentThread();
  576. return err ? err->code : PHYSFS_ERR_OK;
  577. } /* currentErrorCode */
  578. PHYSFS_ErrorCode PHYSFS_getLastErrorCode(void)
  579. {
  580. ErrState *err = findErrorForCurrentThread();
  581. const PHYSFS_ErrorCode retval = (err) ? err->code : PHYSFS_ERR_OK;
  582. if (err)
  583. err->code = PHYSFS_ERR_OK;
  584. return retval;
  585. } /* PHYSFS_getLastErrorCode */
  586. PHYSFS_DECL const char *PHYSFS_getErrorByCode(PHYSFS_ErrorCode code)
  587. {
  588. switch (code)
  589. {
  590. case PHYSFS_ERR_OK: return "no error";
  591. case PHYSFS_ERR_OTHER_ERROR: return "unknown error";
  592. case PHYSFS_ERR_OUT_OF_MEMORY: return "out of memory";
  593. case PHYSFS_ERR_NOT_INITIALIZED: return "not initialized";
  594. case PHYSFS_ERR_IS_INITIALIZED: return "already initialized";
  595. case PHYSFS_ERR_ARGV0_IS_NULL: return "argv[0] is NULL";
  596. case PHYSFS_ERR_UNSUPPORTED: return "unsupported";
  597. case PHYSFS_ERR_PAST_EOF: return "past end of file";
  598. case PHYSFS_ERR_FILES_STILL_OPEN: return "files still open";
  599. case PHYSFS_ERR_INVALID_ARGUMENT: return "invalid argument";
  600. case PHYSFS_ERR_NOT_MOUNTED: return "not mounted";
  601. case PHYSFS_ERR_NOT_FOUND: return "not found";
  602. case PHYSFS_ERR_SYMLINK_FORBIDDEN: return "symlinks are forbidden";
  603. case PHYSFS_ERR_NO_WRITE_DIR: return "write directory is not set";
  604. case PHYSFS_ERR_OPEN_FOR_READING: return "file open for reading";
  605. case PHYSFS_ERR_OPEN_FOR_WRITING: return "file open for writing";
  606. case PHYSFS_ERR_NOT_A_FILE: return "not a file";
  607. case PHYSFS_ERR_READ_ONLY: return "read-only filesystem";
  608. case PHYSFS_ERR_CORRUPT: return "corrupted";
  609. case PHYSFS_ERR_SYMLINK_LOOP: return "infinite symbolic link loop";
  610. case PHYSFS_ERR_IO: return "i/o error";
  611. case PHYSFS_ERR_PERMISSION: return "permission denied";
  612. case PHYSFS_ERR_NO_SPACE: return "no space available for writing";
  613. case PHYSFS_ERR_BAD_FILENAME: return "filename is illegal or insecure";
  614. case PHYSFS_ERR_BUSY: return "tried to modify a file the OS needs";
  615. case PHYSFS_ERR_DIR_NOT_EMPTY: return "directory isn't empty";
  616. case PHYSFS_ERR_OS_ERROR: return "OS reported an error";
  617. case PHYSFS_ERR_DUPLICATE: return "duplicate resource";
  618. case PHYSFS_ERR_BAD_PASSWORD: return "bad password";
  619. case PHYSFS_ERR_APP_CALLBACK: return "app callback reported error";
  620. } /* switch */
  621. return NULL; /* don't know this error code. */
  622. } /* PHYSFS_getErrorByCode */
  623. void PHYSFS_setErrorCode(PHYSFS_ErrorCode errcode)
  624. {
  625. ErrState *err;
  626. if (!errcode)
  627. return;
  628. err = findErrorForCurrentThread();
  629. if (err == NULL)
  630. {
  631. err = (ErrState *) allocator.Malloc(sizeof (ErrState));
  632. if (err == NULL)
  633. return; /* uhh...? */
  634. memset(err, '\0', sizeof (ErrState));
  635. err->tid = __PHYSFS_platformGetThreadID();
  636. if (errorLock != NULL)
  637. __PHYSFS_platformGrabMutex(errorLock);
  638. err->next = errorStates;
  639. errorStates = err;
  640. if (errorLock != NULL)
  641. __PHYSFS_platformReleaseMutex(errorLock);
  642. } /* if */
  643. err->code = errcode;
  644. } /* PHYSFS_setErrorCode */
  645. const char *PHYSFS_getLastError(void)
  646. {
  647. const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
  648. return (err) ? PHYSFS_getErrorByCode(err) : NULL;
  649. } /* PHYSFS_getLastError */
  650. /* MAKE SURE that errorLock is held before calling this! */
  651. static void freeErrorStates(void)
  652. {
  653. ErrState *i;
  654. ErrState *next;
  655. for (i = errorStates; i != NULL; i = next)
  656. {
  657. next = i->next;
  658. allocator.Free(i);
  659. } /* for */
  660. errorStates = NULL;
  661. } /* freeErrorStates */
  662. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  663. {
  664. if (ver != NULL)
  665. {
  666. ver->major = PHYSFS_VER_MAJOR;
  667. ver->minor = PHYSFS_VER_MINOR;
  668. ver->patch = PHYSFS_VER_PATCH;
  669. } /* if */
  670. } /* PHYSFS_getLinkedVersion */
  671. static const char *find_filename_extension(const char *fname)
  672. {
  673. const char *retval = NULL;
  674. if (fname != NULL)
  675. {
  676. const char *p = strchr(fname, '.');
  677. retval = p;
  678. while (p != NULL)
  679. {
  680. p = strchr(p + 1, '.');
  681. if (p != NULL)
  682. retval = p;
  683. } /* while */
  684. if (retval != NULL)
  685. retval++; /* skip '.' */
  686. } /* if */
  687. return retval;
  688. } /* find_filename_extension */
  689. static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
  690. const char *d, int forWriting, int *_claimed)
  691. {
  692. DirHandle *retval = NULL;
  693. void *opaque = NULL;
  694. if (io != NULL)
  695. BAIL_IF_ERRPASS(!io->seek(io, 0), NULL);
  696. opaque = funcs->openArchive(io, d, forWriting, _claimed);
  697. if (opaque != NULL)
  698. {
  699. retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
  700. if (retval == NULL)
  701. funcs->closeArchive(opaque);
  702. else
  703. {
  704. memset(retval, '\0', sizeof (DirHandle));
  705. retval->mountPoint = NULL;
  706. retval->funcs = funcs;
  707. retval->opaque = opaque;
  708. } /* else */
  709. } /* if */
  710. return retval;
  711. } /* tryOpenDir */
  712. static DirHandle *openDirectory(PHYSFS_Io *io, const char *d, int forWriting)
  713. {
  714. DirHandle *retval = NULL;
  715. PHYSFS_Archiver **i;
  716. const char *ext;
  717. int created_io = 0;
  718. int claimed = 0;
  719. PHYSFS_ErrorCode errcode;
  720. assert((io != NULL) || (d != NULL));
  721. if (io == NULL)
  722. {
  723. /* file doesn't exist, etc? Just fail out. */
  724. PHYSFS_Stat statbuf;
  725. BAIL_IF_ERRPASS(!__PHYSFS_platformStat(d, &statbuf, 1), NULL);
  726. /* DIR gets first shot (unlike the rest, it doesn't deal with files). */
  727. if (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY)
  728. {
  729. retval = tryOpenDir(io, &__PHYSFS_Archiver_DIR, d, forWriting, &claimed);
  730. if (retval || claimed)
  731. return retval;
  732. } /* if */
  733. io = __PHYSFS_createNativeIo(d, forWriting ? 'w' : 'r');
  734. BAIL_IF_ERRPASS(!io, NULL);
  735. created_io = 1;
  736. } /* if */
  737. ext = find_filename_extension(d);
  738. if (ext != NULL)
  739. {
  740. /* Look for archivers with matching file extensions first... */
  741. for (i = archivers; (*i != NULL) && (retval == NULL) && !claimed; i++)
  742. {
  743. if (PHYSFS_utf8stricmp(ext, (*i)->info.extension) == 0)
  744. retval = tryOpenDir(io, *i, d, forWriting, &claimed);
  745. } /* for */
  746. /* failing an exact file extension match, try all the others... */
  747. for (i = archivers; (*i != NULL) && (retval == NULL) && !claimed; i++)
  748. {
  749. if (PHYSFS_utf8stricmp(ext, (*i)->info.extension) != 0)
  750. retval = tryOpenDir(io, *i, d, forWriting, &claimed);
  751. } /* for */
  752. } /* if */
  753. else /* no extension? Try them all. */
  754. {
  755. for (i = archivers; (*i != NULL) && (retval == NULL) && !claimed; i++)
  756. retval = tryOpenDir(io, *i, d, forWriting, &claimed);
  757. } /* else */
  758. errcode = claimed ? currentErrorCode() : PHYSFS_ERR_UNSUPPORTED;
  759. if ((!retval) && (created_io))
  760. io->destroy(io);
  761. BAIL_IF(!retval, errcode, NULL);
  762. return retval;
  763. } /* openDirectory */
  764. /*
  765. * Make a platform-independent path string sane. Doesn't actually check the
  766. * file hierarchy, it just cleans up the string.
  767. * (dst) must be a buffer at least as big as (src), as this is where the
  768. * cleaned up string is deposited.
  769. * If there are illegal bits in the path (".." entries, etc) then we
  770. * return zero and (dst) is undefined. Non-zero if the path was sanitized.
  771. */
  772. static int sanitizePlatformIndependentPath(const char *src, char *dst)
  773. {
  774. char *prev;
  775. char ch;
  776. while (*src == '/') /* skip initial '/' chars... */
  777. src++;
  778. /* Make sure the entire string isn't "." or ".." */
  779. if ((strcmp(src, ".") == 0) || (strcmp(src, "..") == 0))
  780. BAIL(PHYSFS_ERR_BAD_FILENAME, 0);
  781. prev = dst;
  782. do
  783. {
  784. ch = *(src++);
  785. if ((ch == ':') || (ch == '\\')) /* illegal chars in a physfs path. */
  786. BAIL(PHYSFS_ERR_BAD_FILENAME, 0);
  787. if (ch == '/') /* path separator. */
  788. {
  789. *dst = '\0'; /* "." and ".." are illegal pathnames. */
  790. if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
  791. BAIL(PHYSFS_ERR_BAD_FILENAME, 0);
  792. while (*src == '/') /* chop out doubles... */
  793. src++;
  794. if (*src == '\0') /* ends with a pathsep? */
  795. break; /* we're done, don't add final pathsep to dst. */
  796. prev = dst + 1;
  797. } /* if */
  798. *(dst++) = ch;
  799. } while (ch != '\0');
  800. return 1;
  801. } /* sanitizePlatformIndependentPath */
  802. static inline size_t dirHandleRootLen(const DirHandle *h)
  803. {
  804. return h ? h->rootlen : 0;
  805. } /* dirHandleRootLen */
  806. static inline int sanitizePlatformIndependentPathWithRoot(const DirHandle *h, const char *src, char *dst)
  807. {
  808. return sanitizePlatformIndependentPath(src, dst + dirHandleRootLen(h));
  809. } /* sanitizePlatformIndependentPathWithRoot */
  810. /*
  811. * Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
  812. * output from sanitizePlatformIndependentPath(), so that it is in a known
  813. * state.
  814. *
  815. * This only finds legitimate segments of a mountpoint. If the mountpoint is
  816. * "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
  817. * all zero. "/a/b" will succeed, though.
  818. */
  819. static int partOfMountPoint(DirHandle *h, char *fname)
  820. {
  821. int rc;
  822. size_t len, mntpntlen;
  823. if (h->mountPoint == NULL)
  824. return 0;
  825. else if (*fname == '\0')
  826. return 1;
  827. len = strlen(fname);
  828. mntpntlen = strlen(h->mountPoint);
  829. if (len > mntpntlen) /* can't be a subset of mountpoint. */
  830. return 0;
  831. /* if true, must be not a match or a complete match, but not a subset. */
  832. if ((len + 1) == mntpntlen)
  833. return 0;
  834. rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
  835. if (rc != 0)
  836. return 0; /* not a match. */
  837. /* make sure /a/b matches /a/b/ and not /a/bc ... */
  838. return h->mountPoint[len] == '/';
  839. } /* partOfMountPoint */
  840. static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
  841. const char *mountPoint, int forWriting)
  842. {
  843. DirHandle *dirHandle = NULL;
  844. char *tmpmntpnt = NULL;
  845. assert(newDir != NULL); /* should have caught this higher up. */
  846. if (mountPoint != NULL)
  847. {
  848. const size_t len = strlen(mountPoint) + 1;
  849. tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
  850. GOTO_IF(!tmpmntpnt, PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  851. if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
  852. goto badDirHandle;
  853. mountPoint = tmpmntpnt; /* sanitized version. */
  854. } /* if */
  855. dirHandle = openDirectory(io, newDir, forWriting);
  856. GOTO_IF_ERRPASS(!dirHandle, badDirHandle);
  857. dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
  858. GOTO_IF(!dirHandle->dirName, PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  859. strcpy(dirHandle->dirName, newDir);
  860. if ((mountPoint != NULL) && (*mountPoint != '\0'))
  861. {
  862. dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
  863. if (!dirHandle->mountPoint)
  864. GOTO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
  865. strcpy(dirHandle->mountPoint, mountPoint);
  866. strcat(dirHandle->mountPoint, "/");
  867. } /* if */
  868. __PHYSFS_smallFree(tmpmntpnt);
  869. return dirHandle;
  870. badDirHandle:
  871. if (dirHandle != NULL)
  872. {
  873. dirHandle->funcs->closeArchive(dirHandle->opaque);
  874. allocator.Free(dirHandle->dirName);
  875. allocator.Free(dirHandle->mountPoint);
  876. allocator.Free(dirHandle);
  877. } /* if */
  878. __PHYSFS_smallFree(tmpmntpnt);
  879. return NULL;
  880. } /* createDirHandle */
  881. /* MAKE SURE you've got the stateLock held before calling this! */
  882. static int freeDirHandle(DirHandle *dh, FileHandle *openList)
  883. {
  884. FileHandle *i;
  885. if (dh == NULL)
  886. return 1;
  887. for (i = openList; i != NULL; i = i->next)
  888. BAIL_IF(i->dirHandle == dh, PHYSFS_ERR_FILES_STILL_OPEN, 0);
  889. dh->funcs->closeArchive(dh->opaque);
  890. if (dh->root) allocator.Free(dh->root);
  891. allocator.Free(dh->dirName);
  892. allocator.Free(dh->mountPoint);
  893. allocator.Free(dh);
  894. return 1;
  895. } /* freeDirHandle */
  896. static char *calculateBaseDir(const char *argv0)
  897. {
  898. const char dirsep = __PHYSFS_platformDirSeparator;
  899. char *retval = NULL;
  900. char *ptr = NULL;
  901. /* Give the platform layer first shot at this. */
  902. retval = __PHYSFS_platformCalcBaseDir(argv0);
  903. if (retval != NULL)
  904. return retval;
  905. /* We need argv0 to go on. */
  906. BAIL_IF(argv0 == NULL, PHYSFS_ERR_ARGV0_IS_NULL, NULL);
  907. ptr = strrchr(argv0, dirsep);
  908. if (ptr != NULL)
  909. {
  910. const size_t size = ((size_t) (ptr - argv0)) + 1;
  911. retval = (char *) allocator.Malloc(size + 1);
  912. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  913. memcpy(retval, argv0, size);
  914. retval[size] = '\0';
  915. return retval;
  916. } /* if */
  917. /* argv0 wasn't helpful. */
  918. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  919. } /* calculateBaseDir */
  920. static int initializeMutexes(void)
  921. {
  922. errorLock = __PHYSFS_platformCreateMutex();
  923. if (errorLock == NULL)
  924. goto initializeMutexes_failed;
  925. stateLock = __PHYSFS_platformCreateMutex();
  926. if (stateLock == NULL)
  927. goto initializeMutexes_failed;
  928. return 1; /* success. */
  929. initializeMutexes_failed:
  930. if (errorLock != NULL)
  931. __PHYSFS_platformDestroyMutex(errorLock);
  932. if (stateLock != NULL)
  933. __PHYSFS_platformDestroyMutex(stateLock);
  934. errorLock = stateLock = NULL;
  935. return 0; /* failed. */
  936. } /* initializeMutexes */
  937. static int doRegisterArchiver(const PHYSFS_Archiver *_archiver);
  938. static int initStaticArchivers(void)
  939. {
  940. #define REGISTER_STATIC_ARCHIVER(arc) { \
  941. if (!doRegisterArchiver(&__PHYSFS_Archiver_##arc)) { \
  942. return 0; \
  943. } \
  944. }
  945. #if PHYSFS_SUPPORTS_ZIP
  946. REGISTER_STATIC_ARCHIVER(ZIP);
  947. #endif
  948. #if PHYSFS_SUPPORTS_7Z
  949. SZIP_global_init();
  950. REGISTER_STATIC_ARCHIVER(7Z);
  951. #endif
  952. #if PHYSFS_SUPPORTS_GRP
  953. REGISTER_STATIC_ARCHIVER(GRP);
  954. #endif
  955. #if PHYSFS_SUPPORTS_QPAK
  956. REGISTER_STATIC_ARCHIVER(QPAK);
  957. #endif
  958. #if PHYSFS_SUPPORTS_HOG
  959. REGISTER_STATIC_ARCHIVER(HOG);
  960. #endif
  961. #if PHYSFS_SUPPORTS_MVL
  962. REGISTER_STATIC_ARCHIVER(MVL);
  963. #endif
  964. #if PHYSFS_SUPPORTS_WAD
  965. REGISTER_STATIC_ARCHIVER(WAD);
  966. #endif
  967. #if PHYSFS_SUPPORTS_CSM
  968. REGISTER_STATIC_ARCHIVER(CSM);
  969. #endif
  970. #if PHYSFS_SUPPORTS_SLB
  971. REGISTER_STATIC_ARCHIVER(SLB);
  972. #endif
  973. #if PHYSFS_SUPPORTS_ISO9660
  974. REGISTER_STATIC_ARCHIVER(ISO9660);
  975. #endif
  976. #if PHYSFS_SUPPORTS_VDF
  977. REGISTER_STATIC_ARCHIVER(VDF)
  978. #endif
  979. #if PHYSFS_SUPPORTS_LECARCHIVES
  980. REGISTER_STATIC_ARCHIVER(GOB)
  981. REGISTER_STATIC_ARCHIVER(LFD)
  982. REGISTER_STATIC_ARCHIVER(LAB)
  983. #endif
  984. #undef REGISTER_STATIC_ARCHIVER
  985. return 1;
  986. } /* initStaticArchivers */
  987. static void setDefaultAllocator(void);
  988. static int doDeinit(void);
  989. int PHYSFS_init(const char *argv0)
  990. {
  991. BAIL_IF(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
  992. if (!externalAllocator)
  993. setDefaultAllocator();
  994. if ((allocator.Init != NULL) && (!allocator.Init())) return 0;
  995. if (!__PHYSFS_platformInit(argv0))
  996. {
  997. if (allocator.Deinit != NULL) allocator.Deinit();
  998. return 0;
  999. } /* if */
  1000. /* everything below here can be cleaned up safely by doDeinit(). */
  1001. if (!initializeMutexes()) goto initFailed;
  1002. baseDir = calculateBaseDir(argv0);
  1003. if (!baseDir) goto initFailed;
  1004. userDir = __PHYSFS_platformCalcUserDir();
  1005. if (!userDir) goto initFailed;
  1006. /* Platform layer is required to append a dirsep. */
  1007. #ifndef __ANDROID__ /* it's an APK file, not a directory, on Android. */
  1008. assert(baseDir[strlen(baseDir) - 1] == __PHYSFS_platformDirSeparator);
  1009. #endif
  1010. assert(userDir[strlen(userDir) - 1] == __PHYSFS_platformDirSeparator);
  1011. if (!initStaticArchivers()) goto initFailed;
  1012. initialized = 1;
  1013. /* This makes sure that the error subsystem is initialized. */
  1014. PHYSFS_setErrorCode(PHYSFS_getLastErrorCode());
  1015. return 1;
  1016. initFailed:
  1017. doDeinit();
  1018. return 0;
  1019. } /* PHYSFS_init */
  1020. /* MAKE SURE you hold stateLock before calling this! */
  1021. static int closeFileHandleList(FileHandle **list)
  1022. {
  1023. FileHandle *i;
  1024. FileHandle *next = NULL;
  1025. for (i = *list; i != NULL; i = next)
  1026. {
  1027. PHYSFS_Io *io = i->io;
  1028. next = i->next;
  1029. if (io->flush && !io->flush(io))
  1030. {
  1031. *list = i;
  1032. return 0;
  1033. } /* if */
  1034. io->destroy(io);
  1035. allocator.Free(i);
  1036. } /* for */
  1037. *list = NULL;
  1038. return 1;
  1039. } /* closeFileHandleList */
  1040. /* MAKE SURE you hold the stateLock before calling this! */
  1041. static void freeSearchPath(void)
  1042. {
  1043. DirHandle *i;
  1044. DirHandle *next = NULL;
  1045. closeFileHandleList(&openReadList);
  1046. if (searchPath != NULL)
  1047. {
  1048. for (i = searchPath; i != NULL; i = next)
  1049. {
  1050. next = i->next;
  1051. freeDirHandle(i, openReadList);
  1052. } /* for */
  1053. searchPath = NULL;
  1054. } /* if */
  1055. } /* freeSearchPath */
  1056. /* MAKE SURE you hold stateLock before calling this! */
  1057. static int archiverInUse(const PHYSFS_Archiver *arc, const DirHandle *list)
  1058. {
  1059. const DirHandle *i;
  1060. for (i = list; i != NULL; i = i->next)
  1061. {
  1062. if (i->funcs == arc)
  1063. return 1;
  1064. } /* for */
  1065. return 0; /* not in use */
  1066. } /* archiverInUse */
  1067. /* MAKE SURE you hold stateLock before calling this! */
  1068. static int doDeregisterArchiver(const size_t idx)
  1069. {
  1070. const size_t len = (numArchivers - idx) * sizeof (void *);
  1071. PHYSFS_ArchiveInfo *info = archiveInfo[idx];
  1072. PHYSFS_Archiver *arc = archivers[idx];
  1073. /* make sure nothing is still using this archiver */
  1074. if (archiverInUse(arc, searchPath) || archiverInUse(arc, writeDir))
  1075. BAIL(PHYSFS_ERR_FILES_STILL_OPEN, 0);
  1076. allocator.Free((void *) info->extension);
  1077. allocator.Free((void *) info->description);
  1078. allocator.Free((void *) info->author);
  1079. allocator.Free((void *) info->url);
  1080. allocator.Free((void *) arc);
  1081. memmove(&archiveInfo[idx], &archiveInfo[idx+1], len);
  1082. memmove(&archivers[idx], &archivers[idx+1], len);
  1083. assert(numArchivers > 0);
  1084. numArchivers--;
  1085. return 1;
  1086. } /* doDeregisterArchiver */
  1087. /* Does NOT hold the state lock; we're shutting down. */
  1088. static void freeArchivers(void)
  1089. {
  1090. while (numArchivers > 0)
  1091. {
  1092. if (!doDeregisterArchiver(numArchivers - 1))
  1093. assert(!"nothing should be mounted during shutdown.");
  1094. } /* while */
  1095. allocator.Free(archivers);
  1096. allocator.Free(archiveInfo);
  1097. archivers = NULL;
  1098. archiveInfo = NULL;
  1099. } /* freeArchivers */
  1100. static int doDeinit(void)
  1101. {
  1102. closeFileHandleList(&openWriteList);
  1103. BAIL_IF(!PHYSFS_setWriteDir(NULL), PHYSFS_ERR_FILES_STILL_OPEN, 0);
  1104. freeSearchPath();
  1105. freeArchivers();
  1106. freeErrorStates();
  1107. if (baseDir != NULL)
  1108. {
  1109. allocator.Free(baseDir);
  1110. baseDir = NULL;
  1111. } /* if */
  1112. if (userDir != NULL)
  1113. {
  1114. allocator.Free(userDir);
  1115. userDir = NULL;
  1116. } /* if */
  1117. if (prefDir != NULL)
  1118. {
  1119. allocator.Free(prefDir);
  1120. prefDir = NULL;
  1121. } /* if */
  1122. if (archiveInfo != NULL)
  1123. {
  1124. allocator.Free(archiveInfo);
  1125. archiveInfo = NULL;
  1126. } /* if */
  1127. if (archivers != NULL)
  1128. {
  1129. allocator.Free(archivers);
  1130. archivers = NULL;
  1131. } /* if */
  1132. longest_root = 0;
  1133. allowSymLinks = 0;
  1134. initialized = 0;
  1135. if (errorLock) __PHYSFS_platformDestroyMutex(errorLock);
  1136. if (stateLock) __PHYSFS_platformDestroyMutex(stateLock);
  1137. if (allocator.Deinit != NULL)
  1138. allocator.Deinit();
  1139. errorLock = stateLock = NULL;
  1140. __PHYSFS_platformDeinit();
  1141. return 1;
  1142. } /* doDeinit */
  1143. int PHYSFS_deinit(void)
  1144. {
  1145. BAIL_IF(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1146. return doDeinit();
  1147. } /* PHYSFS_deinit */
  1148. int PHYSFS_isInit(void)
  1149. {
  1150. return initialized;
  1151. } /* PHYSFS_isInit */
  1152. char *__PHYSFS_strdup(const char *str)
  1153. {
  1154. char *retval = (char *) allocator.Malloc(strlen(str) + 1);
  1155. if (retval)
  1156. strcpy(retval, str);
  1157. return retval;
  1158. } /* __PHYSFS_strdup */
  1159. PHYSFS_uint32 __PHYSFS_hashString(const char *str)
  1160. {
  1161. PHYSFS_uint32 hash = 5381;
  1162. while (1)
  1163. {
  1164. const char ch = *(str++);
  1165. if (ch == 0)
  1166. break;
  1167. hash = ((hash << 5) + hash) ^ ch;
  1168. } /* while */
  1169. return hash;
  1170. } /* __PHYSFS_hashString */
  1171. PHYSFS_uint32 __PHYSFS_hashStringCaseFold(const char *str)
  1172. {
  1173. PHYSFS_uint32 hash = 5381;
  1174. while (1)
  1175. {
  1176. const PHYSFS_uint32 cp = __PHYSFS_utf8codepoint(&str);
  1177. if (cp == 0)
  1178. break;
  1179. else
  1180. {
  1181. PHYSFS_uint32 folded[3];
  1182. const int numbytes = (int) (PHYSFS_caseFold(cp, folded) * sizeof (PHYSFS_uint32));
  1183. const char *bytes = (const char *) folded;
  1184. int i;
  1185. for (i = 0; i < numbytes; i++)
  1186. hash = ((hash << 5) + hash) ^ *(bytes++);
  1187. } /* else */
  1188. } /* while */
  1189. return hash;
  1190. } /* __PHYSFS_hashStringCaseFold */
  1191. PHYSFS_uint32 __PHYSFS_hashStringCaseFoldUSAscii(const char *str)
  1192. {
  1193. PHYSFS_uint32 hash = 5381;
  1194. while (1)
  1195. {
  1196. char ch = *(str++);
  1197. if (ch == 0)
  1198. break;
  1199. else if ((ch >= 'A') && (ch <= 'Z'))
  1200. ch -= ('A' - 'a');
  1201. hash = ((hash << 5) + hash) ^ ch;
  1202. } /* while */
  1203. return hash;
  1204. } /* __PHYSFS_hashStringCaseFoldUSAscii */
  1205. /* MAKE SURE you hold stateLock before calling this! */
  1206. static int doRegisterArchiver(const PHYSFS_Archiver *_archiver)
  1207. {
  1208. const PHYSFS_uint32 maxver = CURRENT_PHYSFS_ARCHIVER_API_VERSION;
  1209. const size_t len = (numArchivers + 2) * sizeof (void *);
  1210. PHYSFS_Archiver *archiver = NULL;
  1211. PHYSFS_ArchiveInfo *info = NULL;
  1212. const char *ext = NULL;
  1213. void *ptr = NULL;
  1214. size_t i;
  1215. BAIL_IF(!_archiver, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1216. BAIL_IF(_archiver->version > maxver, PHYSFS_ERR_UNSUPPORTED, 0);
  1217. BAIL_IF(!_archiver->info.extension, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1218. BAIL_IF(!_archiver->info.description, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1219. BAIL_IF(!_archiver->info.author, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1220. BAIL_IF(!_archiver->info.url, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1221. BAIL_IF(!_archiver->openArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1222. BAIL_IF(!_archiver->enumerate, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1223. BAIL_IF(!_archiver->openRead, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1224. BAIL_IF(!_archiver->openWrite, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1225. BAIL_IF(!_archiver->openAppend, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1226. BAIL_IF(!_archiver->remove, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1227. BAIL_IF(!_archiver->mkdir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1228. BAIL_IF(!_archiver->closeArchive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1229. BAIL_IF(!_archiver->stat, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1230. ext = _archiver->info.extension;
  1231. for (i = 0; i < numArchivers; i++)
  1232. {
  1233. if (PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
  1234. BAIL(PHYSFS_ERR_DUPLICATE, 0);
  1235. } /* for */
  1236. /* make a copy of the data. */
  1237. archiver = (PHYSFS_Archiver *) allocator.Malloc(sizeof (*archiver));
  1238. GOTO_IF(!archiver, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1239. /* Must copy sizeof (OLD_VERSION_OF_STRUCT) when version changes! */
  1240. memcpy(archiver, _archiver, sizeof (*archiver));
  1241. info = (PHYSFS_ArchiveInfo *) &archiver->info;
  1242. memset(info, '\0', sizeof (*info)); /* NULL in case an alloc fails. */
  1243. #define CPYSTR(item) \
  1244. info->item = __PHYSFS_strdup(_archiver->info.item); \
  1245. GOTO_IF(!info->item, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1246. CPYSTR(extension);
  1247. CPYSTR(description);
  1248. CPYSTR(author);
  1249. CPYSTR(url);
  1250. info->supportsSymlinks = _archiver->info.supportsSymlinks;
  1251. #undef CPYSTR
  1252. ptr = allocator.Realloc(archiveInfo, len);
  1253. GOTO_IF(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1254. archiveInfo = (PHYSFS_ArchiveInfo **) ptr;
  1255. ptr = allocator.Realloc(archivers, len);
  1256. GOTO_IF(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, regfailed);
  1257. archivers = (PHYSFS_Archiver **) ptr;
  1258. archiveInfo[numArchivers] = info;
  1259. archiveInfo[numArchivers + 1] = NULL;
  1260. archivers[numArchivers] = archiver;
  1261. archivers[numArchivers + 1] = NULL;
  1262. numArchivers++;
  1263. return 1;
  1264. regfailed:
  1265. if (info != NULL)
  1266. {
  1267. allocator.Free((void *) info->extension);
  1268. allocator.Free((void *) info->description);
  1269. allocator.Free((void *) info->author);
  1270. allocator.Free((void *) info->url);
  1271. } /* if */
  1272. allocator.Free(archiver);
  1273. return 0;
  1274. } /* doRegisterArchiver */
  1275. int PHYSFS_registerArchiver(const PHYSFS_Archiver *archiver)
  1276. {
  1277. int retval;
  1278. BAIL_IF(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1279. __PHYSFS_platformGrabMutex(stateLock);
  1280. retval = doRegisterArchiver(archiver);
  1281. __PHYSFS_platformReleaseMutex(stateLock);
  1282. return retval;
  1283. } /* PHYSFS_registerArchiver */
  1284. int PHYSFS_deregisterArchiver(const char *ext)
  1285. {
  1286. size_t i;
  1287. BAIL_IF(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1288. BAIL_IF(!ext, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1289. __PHYSFS_platformGrabMutex(stateLock);
  1290. for (i = 0; i < numArchivers; i++)
  1291. {
  1292. if (PHYSFS_utf8stricmp(archiveInfo[i]->extension, ext) == 0)
  1293. {
  1294. const int retval = doDeregisterArchiver(i);
  1295. __PHYSFS_platformReleaseMutex(stateLock);
  1296. return retval;
  1297. } /* if */
  1298. } /* for */
  1299. __PHYSFS_platformReleaseMutex(stateLock);
  1300. BAIL(PHYSFS_ERR_NOT_FOUND, 0);
  1301. } /* PHYSFS_deregisterArchiver */
  1302. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  1303. {
  1304. BAIL_IF(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
  1305. return (const PHYSFS_ArchiveInfo **) archiveInfo;
  1306. } /* PHYSFS_supportedArchiveTypes */
  1307. void PHYSFS_freeList(void *list)
  1308. {
  1309. void **i;
  1310. if (list != NULL)
  1311. {
  1312. for (i = (void **) list; *i != NULL; i++)
  1313. allocator.Free(*i);
  1314. allocator.Free(list);
  1315. } /* if */
  1316. } /* PHYSFS_freeList */
  1317. const char *PHYSFS_getDirSeparator(void)
  1318. {
  1319. static char retval[2] = { __PHYSFS_platformDirSeparator, '\0' };
  1320. return retval;
  1321. } /* PHYSFS_getDirSeparator */
  1322. char **PHYSFS_getCdRomDirs(void)
  1323. {
  1324. return doEnumStringList(__PHYSFS_platformDetectAvailableCDs);
  1325. } /* PHYSFS_getCdRomDirs */
  1326. void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data)
  1327. {
  1328. __PHYSFS_platformDetectAvailableCDs(callback, data);
  1329. } /* PHYSFS_getCdRomDirsCallback */
  1330. const char *PHYSFS_getPrefDir(const char *org, const char *app)
  1331. {
  1332. const char dirsep = __PHYSFS_platformDirSeparator;
  1333. PHYSFS_Stat statbuf;
  1334. char *ptr = NULL;
  1335. char *endstr = NULL;
  1336. BAIL_IF(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1337. BAIL_IF(!org, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1338. BAIL_IF(*org == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1339. BAIL_IF(!app, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1340. BAIL_IF(*app == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1341. allocator.Free(prefDir);
  1342. prefDir = __PHYSFS_platformCalcPrefDir(org, app);
  1343. BAIL_IF_ERRPASS(!prefDir, NULL);
  1344. assert(strlen(prefDir) > 0);
  1345. endstr = prefDir + (strlen(prefDir) - 1);
  1346. assert(*endstr == dirsep);
  1347. *endstr = '\0'; /* mask out the final dirsep for now. */
  1348. if (!__PHYSFS_platformStat(prefDir, &statbuf, 1))
  1349. {
  1350. for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
  1351. {
  1352. *ptr = '\0';
  1353. __PHYSFS_platformMkDir(prefDir);
  1354. *ptr = dirsep;
  1355. } /* for */
  1356. if (!__PHYSFS_platformMkDir(prefDir))
  1357. {
  1358. allocator.Free(prefDir);
  1359. prefDir = NULL;
  1360. } /* if */
  1361. } /* if */
  1362. *endstr = dirsep; /* readd the final dirsep. */
  1363. return prefDir;
  1364. } /* PHYSFS_getPrefDir */
  1365. const char *PHYSFS_getBaseDir(void)
  1366. {
  1367. return baseDir; /* this is calculated in PHYSFS_init()... */
  1368. } /* PHYSFS_getBaseDir */
  1369. const char *__PHYSFS_getUserDir(void) /* not deprecated internal version. */
  1370. {
  1371. return userDir; /* this is calculated in PHYSFS_init()... */
  1372. } /* __PHYSFS_getUserDir */
  1373. const char *PHYSFS_getUserDir(void)
  1374. {
  1375. return __PHYSFS_getUserDir();
  1376. } /* PHYSFS_getUserDir */
  1377. const char *PHYSFS_getWriteDir(void)
  1378. {
  1379. const char *retval = NULL;
  1380. __PHYSFS_platformGrabMutex(stateLock);
  1381. if (writeDir != NULL)
  1382. retval = writeDir->dirName;
  1383. __PHYSFS_platformReleaseMutex(stateLock);
  1384. return retval;
  1385. } /* PHYSFS_getWriteDir */
  1386. int PHYSFS_setWriteDir(const char *newDir)
  1387. {
  1388. int retval = 1;
  1389. __PHYSFS_platformGrabMutex(stateLock);
  1390. if (writeDir != NULL)
  1391. {
  1392. BAIL_IF_MUTEX_ERRPASS(!freeDirHandle(writeDir, openWriteList),
  1393. stateLock, 0);
  1394. writeDir = NULL;
  1395. } /* if */
  1396. if (newDir != NULL)
  1397. {
  1398. writeDir = createDirHandle(NULL, newDir, NULL, 1);
  1399. retval = (writeDir != NULL);
  1400. } /* if */
  1401. __PHYSFS_platformReleaseMutex(stateLock);
  1402. return retval;
  1403. } /* PHYSFS_setWriteDir */
  1404. int PHYSFS_setRoot(const char *archive, const char *subdir)
  1405. {
  1406. DirHandle *i;
  1407. BAIL_IF(!archive, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1408. __PHYSFS_platformGrabMutex(stateLock);
  1409. for (i = searchPath; i != NULL; i = i->next)
  1410. {
  1411. if ((i->dirName != NULL) && (strcmp(archive, i->dirName) == 0))
  1412. {
  1413. if (!subdir || (strcmp(subdir, "/") == 0))
  1414. {
  1415. if (i->root)
  1416. allocator.Free(i->root);
  1417. i->root = NULL;
  1418. i->rootlen = 0;
  1419. } /* if */
  1420. else
  1421. {
  1422. const size_t len = strlen(subdir) + 1;
  1423. char *ptr = (char *) allocator.Malloc(len);
  1424. BAIL_IF_MUTEX(!ptr, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
  1425. if (!sanitizePlatformIndependentPath(subdir, ptr))
  1426. {
  1427. allocator.Free(ptr);
  1428. BAIL_MUTEX_ERRPASS(stateLock, 0);
  1429. } /* if */
  1430. if (i->root)
  1431. allocator.Free(i->root);
  1432. i->root = ptr;
  1433. i->rootlen = strlen(i->root); /* in case sanitizePlatformIndependentPath changed subdir */
  1434. if (longest_root < i->rootlen)
  1435. longest_root = i->rootlen;
  1436. } /* else */
  1437. break;
  1438. } /* if */
  1439. } /* for */
  1440. __PHYSFS_platformReleaseMutex(stateLock);
  1441. return 1;
  1442. } /* PHYSFS_setRoot */
  1443. static int doMount(PHYSFS_Io *io, const char *fname,
  1444. const char *mountPoint, int appendToPath)
  1445. {
  1446. DirHandle *dh;
  1447. DirHandle *prev = NULL;
  1448. DirHandle *i;
  1449. BAIL_IF(!fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1450. if (mountPoint == NULL)
  1451. mountPoint = "/";
  1452. __PHYSFS_platformGrabMutex(stateLock);
  1453. for (i = searchPath; i != NULL; i = i->next)
  1454. {
  1455. /* already in search path? */
  1456. if ((i->dirName != NULL) && (strcmp(fname, i->dirName) == 0))
  1457. BAIL_MUTEX_ERRPASS(stateLock, 1);
  1458. prev = i;
  1459. } /* for */
  1460. dh = createDirHandle(io, fname, mountPoint, 0);
  1461. BAIL_IF_MUTEX_ERRPASS(!dh, stateLock, 0);
  1462. if (appendToPath)
  1463. {
  1464. if (prev == NULL)
  1465. searchPath = dh;
  1466. else
  1467. prev->next = dh;
  1468. } /* if */
  1469. else
  1470. {
  1471. dh->next = searchPath;
  1472. searchPath = dh;
  1473. } /* else */
  1474. __PHYSFS_platformReleaseMutex(stateLock);
  1475. return 1;
  1476. } /* doMount */
  1477. int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
  1478. const char *mountPoint, int appendToPath)
  1479. {
  1480. BAIL_IF(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1481. BAIL_IF(!fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1482. BAIL_IF(io->version != 0, PHYSFS_ERR_UNSUPPORTED, 0);
  1483. return doMount(io, fname, mountPoint, appendToPath);
  1484. } /* PHYSFS_mountIo */
  1485. int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
  1486. const char *fname, const char *mountPoint,
  1487. int appendToPath)
  1488. {
  1489. int retval = 0;
  1490. PHYSFS_Io *io = NULL;
  1491. BAIL_IF(!buf, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1492. BAIL_IF(!fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1493. io = __PHYSFS_createMemoryIo(buf, len, del);
  1494. BAIL_IF_ERRPASS(!io, 0);
  1495. retval = doMount(io, fname, mountPoint, appendToPath);
  1496. if (!retval)
  1497. {
  1498. /* docs say not to call (del) in case of failure, so cheat. */
  1499. MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
  1500. info->destruct = NULL;
  1501. io->destroy(io);
  1502. } /* if */
  1503. return retval;
  1504. } /* PHYSFS_mountMemory */
  1505. int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
  1506. const char *mountPoint, int appendToPath)
  1507. {
  1508. int retval = 0;
  1509. PHYSFS_Io *io = NULL;
  1510. BAIL_IF(!file, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1511. BAIL_IF(!fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1512. io = __PHYSFS_createHandleIo(file);
  1513. BAIL_IF_ERRPASS(!io, 0);
  1514. retval = doMount(io, fname, mountPoint, appendToPath);
  1515. if (!retval)
  1516. {
  1517. /* docs say not to destruct in case of failure, so cheat. */
  1518. io->opaque = NULL;
  1519. io->destroy(io);
  1520. } /* if */
  1521. return retval;
  1522. } /* PHYSFS_mountHandle */
  1523. int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
  1524. {
  1525. BAIL_IF(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1526. return doMount(NULL, newDir, mountPoint, appendToPath);
  1527. } /* PHYSFS_mount */
  1528. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  1529. {
  1530. return PHYSFS_mount(newDir, NULL, appendToPath);
  1531. } /* PHYSFS_addToSearchPath */
  1532. int PHYSFS_removeFromSearchPath(const char *oldDir)
  1533. {
  1534. return PHYSFS_unmount(oldDir);
  1535. } /* PHYSFS_removeFromSearchPath */
  1536. int PHYSFS_unmount(const char *oldDir)
  1537. {
  1538. DirHandle *i;
  1539. DirHandle *prev = NULL;
  1540. DirHandle *next = NULL;
  1541. BAIL_IF(oldDir == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1542. __PHYSFS_platformGrabMutex(stateLock);
  1543. for (i = searchPath; i != NULL; i = i->next)
  1544. {
  1545. if (strcmp(i->dirName, oldDir) == 0)
  1546. {
  1547. next = i->next;
  1548. BAIL_IF_MUTEX_ERRPASS(!freeDirHandle(i, openReadList),
  1549. stateLock, 0);
  1550. if (prev == NULL)
  1551. searchPath = next;
  1552. else
  1553. prev->next = next;
  1554. BAIL_MUTEX_ERRPASS(stateLock, 1);
  1555. } /* if */
  1556. prev = i;
  1557. } /* for */
  1558. BAIL_MUTEX(PHYSFS_ERR_NOT_MOUNTED, stateLock, 0);
  1559. } /* PHYSFS_unmount */
  1560. char **PHYSFS_getSearchPath(void)
  1561. {
  1562. return doEnumStringList(PHYSFS_getSearchPathCallback);
  1563. } /* PHYSFS_getSearchPath */
  1564. const char *PHYSFS_getMountPoint(const char *dir)
  1565. {
  1566. DirHandle *i;
  1567. __PHYSFS_platformGrabMutex(stateLock);
  1568. for (i = searchPath; i != NULL; i = i->next)
  1569. {
  1570. if (strcmp(i->dirName, dir) == 0)
  1571. {
  1572. const char *retval = ((i->mountPoint) ? i->mountPoint : "/");
  1573. __PHYSFS_platformReleaseMutex(stateLock);
  1574. return retval;
  1575. } /* if */
  1576. } /* for */
  1577. __PHYSFS_platformReleaseMutex(stateLock);
  1578. BAIL(PHYSFS_ERR_NOT_MOUNTED, NULL);
  1579. } /* PHYSFS_getMountPoint */
  1580. void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
  1581. {
  1582. DirHandle *i;
  1583. __PHYSFS_platformGrabMutex(stateLock);
  1584. for (i = searchPath; i != NULL; i = i->next)
  1585. callback(data, i->dirName);
  1586. __PHYSFS_platformReleaseMutex(stateLock);
  1587. } /* PHYSFS_getSearchPathCallback */
  1588. typedef struct setSaneCfgEnumData
  1589. {
  1590. const char *archiveExt;
  1591. size_t archiveExtLen;
  1592. int archivesFirst;
  1593. PHYSFS_ErrorCode errcode;
  1594. } setSaneCfgEnumData;
  1595. static PHYSFS_EnumerateCallbackResult setSaneCfgEnumCallback(void *_data,
  1596. const char *dir, const char *f)
  1597. {
  1598. setSaneCfgEnumData *data = (setSaneCfgEnumData *) _data;
  1599. const size_t extlen = data->archiveExtLen;
  1600. const size_t l = strlen(f);
  1601. const char *ext;
  1602. if ((l > extlen) && (f[l - extlen - 1] == '.'))
  1603. {
  1604. ext = f + (l - extlen);
  1605. if (PHYSFS_utf8stricmp(ext, data->archiveExt) == 0)
  1606. {
  1607. const char dirsep = __PHYSFS_platformDirSeparator;
  1608. const char *d = PHYSFS_getRealDir(f);
  1609. const size_t allocsize = strlen(d) + l + 2;
  1610. char *str = (char *) __PHYSFS_smallAlloc(allocsize);
  1611. if (str == NULL)
  1612. data->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
  1613. else
  1614. {
  1615. snprintf(str, allocsize, "%s%c%s", d, dirsep, f);
  1616. if (!PHYSFS_mount(str, NULL, data->archivesFirst == 0))
  1617. data->errcode = currentErrorCode();
  1618. __PHYSFS_smallFree(str);
  1619. } /* else */
  1620. } /* if */
  1621. } /* if */
  1622. /* !!! FIXME: if we want to abort on errors... */
  1623. /*return (data->errcode != PHYSFS_ERR_OK) ? PHYSFS_ENUM_ERROR : PHYSFS_ENUM_OK;*/
  1624. return PHYSFS_ENUM_OK; /* keep going */
  1625. } /* setSaneCfgEnumCallback */
  1626. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  1627. const char *archiveExt, int includeCdRoms,
  1628. int archivesFirst)
  1629. {
  1630. const char *basedir;
  1631. const char *prefdir;
  1632. BAIL_IF(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
  1633. prefdir = PHYSFS_getPrefDir(organization, appName);
  1634. BAIL_IF_ERRPASS(!prefdir, 0);
  1635. basedir = PHYSFS_getBaseDir();
  1636. BAIL_IF_ERRPASS(!basedir, 0);
  1637. BAIL_IF(!PHYSFS_setWriteDir(prefdir), PHYSFS_ERR_NO_WRITE_DIR, 0);
  1638. /* !!! FIXME: these can fail and we should report that... */
  1639. /* Put write dir first in search path... */
  1640. PHYSFS_mount(prefdir, NULL, 0);
  1641. /* Put base path on search path... */
  1642. PHYSFS_mount(basedir, NULL, 1);
  1643. /* handle CD-ROMs... */
  1644. if (includeCdRoms)
  1645. {
  1646. char **cds = PHYSFS_getCdRomDirs();
  1647. char **i;
  1648. for (i = cds; *i != NULL; i++)
  1649. PHYSFS_mount(*i, NULL, 1);
  1650. PHYSFS_freeList(cds);
  1651. } /* if */
  1652. /* Root out archives, and add them to search path... */
  1653. if (archiveExt != NULL)
  1654. {
  1655. setSaneCfgEnumData data;
  1656. memset(&data, '\0', sizeof (data));
  1657. data.archiveExt = archiveExt;
  1658. data.archiveExtLen = strlen(archiveExt);
  1659. data.archivesFirst = archivesFirst;
  1660. data.errcode = PHYSFS_ERR_OK;
  1661. if (!PHYSFS_enumerate("/", setSaneCfgEnumCallback, &data))
  1662. {
  1663. /* !!! FIXME: use this if we're reporting errors.
  1664. PHYSFS_ErrorCode errcode = currentErrorCode();
  1665. if (errcode == PHYSFS_ERR_APP_CALLBACK)
  1666. errcode = data->errcode; */
  1667. } /* if */
  1668. } /* if */
  1669. return 1;
  1670. } /* PHYSFS_setSaneConfig */
  1671. void PHYSFS_permitSymbolicLinks(int allow)
  1672. {
  1673. allowSymLinks = allow;
  1674. } /* PHYSFS_permitSymbolicLinks */
  1675. int PHYSFS_symbolicLinksPermitted(void)
  1676. {
  1677. return allowSymLinks;
  1678. } /* PHYSFS_symbolicLinksPermitted */
  1679. /*
  1680. * Verify that (fname) (in platform-independent notation), in relation
  1681. * to (h) is secure. That means that each element of fname is checked
  1682. * for symlinks (if they aren't permitted). This also allows for quick
  1683. * rejection of files that exist outside an archive's mountpoint.
  1684. *
  1685. * With some exceptions (like PHYSFS_mkdir(), which builds multiple subdirs
  1686. * at a time), you should always pass zero for "allowMissing" for efficiency.
  1687. *
  1688. * (fname) must point to an output from sanitizePlatformIndependentPath(),
  1689. * since it will make sure that path names are in the right format for
  1690. * passing certain checks. It will also do checks for "insecure" pathnames
  1691. * like ".." which should be done once instead of once per archive. This also
  1692. * gives us license to treat (fname) as scratch space in this function.
  1693. *
  1694. * (fname)'s buffer must have enough space available before it for this
  1695. * function to prepend any root directory for this DirHandle.
  1696. *
  1697. * Returns non-zero if string is safe, zero if there's a security issue.
  1698. * PHYSFS_getLastError() will specify what was wrong. (*fname) will be
  1699. * updated to point past any mount point elements so it is prepared to
  1700. * be used with the archiver directly.
  1701. */
  1702. static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
  1703. {
  1704. char *fname = *_fname;
  1705. int retval = 1;
  1706. char *start;
  1707. char *end;
  1708. if ((*fname == '\0') && (!h->root)) /* quick rejection. */
  1709. return 1;
  1710. /* !!! FIXME: This codeblock sucks. */
  1711. if (h->mountPoint != NULL) /* NULL mountpoint means "/". */
  1712. {
  1713. size_t mntpntlen = strlen(h->mountPoint);
  1714. size_t len = strlen(fname);
  1715. assert(mntpntlen > 1); /* root mount points should be NULL. */
  1716. /* not under the mountpoint, so skip this archive. */
  1717. BAIL_IF(len < mntpntlen-1, PHYSFS_ERR_NOT_FOUND, 0);
  1718. /* !!! FIXME: Case insensitive? */
  1719. retval = strncmp(h->mountPoint, fname, mntpntlen-1);
  1720. BAIL_IF(retval != 0, PHYSFS_ERR_NOT_FOUND, 0);
  1721. if (len > mntpntlen-1) /* corner case... */
  1722. BAIL_IF(fname[mntpntlen-1]!='/', PHYSFS_ERR_NOT_FOUND, 0);
  1723. fname += mntpntlen-1; /* move to start of actual archive path. */
  1724. if (*fname == '/')
  1725. fname++;
  1726. *_fname = fname; /* skip mountpoint for later use. */
  1727. retval = 1; /* may be reset, below. */
  1728. } /* if */
  1729. /* prepend the root directory, if any. */
  1730. if (h->root)
  1731. {
  1732. const int isempty = (*fname == '\0');
  1733. fname -= h->rootlen + (isempty ? 0 : 1);
  1734. strcpy(fname, h->root);
  1735. if (!isempty)
  1736. fname[h->rootlen] = '/';
  1737. *_fname = fname;
  1738. } /* if */
  1739. start = fname;
  1740. if (!allowSymLinks)
  1741. {
  1742. while (1)
  1743. {
  1744. PHYSFS_Stat statbuf;
  1745. int rc = 0;
  1746. end = strchr(start, '/');
  1747. if (end != NULL) *end = '\0';
  1748. rc = h->funcs->stat(h->opaque, fname, &statbuf);
  1749. if (rc)
  1750. rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  1751. else if (currentErrorCode() == PHYSFS_ERR_NOT_FOUND)
  1752. retval = 0;
  1753. if (end != NULL) *end = '/';
  1754. /* insecure path (has a disallowed symlink in it)? */
  1755. BAIL_IF(rc, PHYSFS_ERR_SYMLINK_FORBIDDEN, 0);
  1756. /* break out early if path element is missing. */
  1757. if (!retval)
  1758. {
  1759. /*
  1760. * We need to clear it if it's the last element of the path,
  1761. * since this might be a non-existant file we're opening
  1762. * for writing...
  1763. */
  1764. if ((end == NULL) || (allowMissing))
  1765. retval = 1;
  1766. break;
  1767. } /* if */
  1768. if (end == NULL)
  1769. break;
  1770. start = end + 1;
  1771. } /* while */
  1772. } /* if */
  1773. return retval;
  1774. } /* verifyPath */
  1775. /* This must hold the stateLock before calling. */
  1776. static int doMkdir(const char *_dname, char *dname)
  1777. {
  1778. DirHandle *h = writeDir;
  1779. char *start;
  1780. char *end;
  1781. int retval = 0;
  1782. int exists = 1; /* force existance check on first path element. */
  1783. assert(h != NULL);
  1784. BAIL_IF_ERRPASS(!sanitizePlatformIndependentPathWithRoot(h, _dname, dname), 0);
  1785. BAIL_IF_ERRPASS(!verifyPath(h, &dname, 1), 0);
  1786. start = dname;
  1787. while (1)
  1788. {
  1789. end = strchr(start, '/');
  1790. if (end != NULL)
  1791. *end = '\0';
  1792. /* only check for existance if all parent dirs existed, too... */
  1793. if (exists)
  1794. {
  1795. PHYSFS_Stat statbuf;
  1796. const int rc = h->funcs->stat(h->opaque, dname, &statbuf);
  1797. if ((!rc) && (currentErrorCode() == PHYSFS_ERR_NOT_FOUND))
  1798. exists = 0;
  1799. /* verifyPath made sure that (dname) doesn't have symlinks if they aren't
  1800. allowed, but it's possible the mounted writeDir itself has symlinks in it,
  1801. (for example "/var" on iOS is a symlink, and the prefpath will be somewhere
  1802. under that)...if we mounted that writeDir, we must allow those symlinks here
  1803. unconditionally. */
  1804. retval = ( (rc) && ((statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY) || (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK)) );
  1805. } /* if */
  1806. if (!exists)
  1807. retval = h->funcs->mkdir(h->opaque, dname);
  1808. if (!retval)
  1809. break;
  1810. if (end == NULL)
  1811. break;
  1812. *end = '/';
  1813. start = end + 1;
  1814. } /* while */
  1815. return retval;
  1816. } /* doMkdir */
  1817. int PHYSFS_mkdir(const char *_dname)
  1818. {
  1819. int retval = 0;
  1820. char *dname;
  1821. size_t len;
  1822. BAIL_IF(!_dname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  1823. __PHYSFS_platformGrabMutex(stateLock);
  1824. BAIL_IF_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
  1825. len = strlen(_dname) + dirHandleRootLen(writeDir) + 1;
  1826. dname = (char *) __PHYSFS_smallAlloc(len);
  1827. BAIL_IF_MUTEX(!dname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
  1828. retval = doMkdir(_dname, dname);
  1829. __PHYSFS_platformReleaseMutex(stateLock);
  1830. __PHYSFS_smallFree(dname);
  1831. return retval;
  1832. } /* PHYSFS_mkdir */
  1833. /* This must hold the stateLock before calling. */
  1834. static int doDelete(const char *_fname, char *fname)
  1835. {
  1836. DirHandle *h = writeDir;
  1837. BAIL_IF_ERRPASS(!sanitizePlatformIndependentPathWithRoot(h, _fname, fname), 0);
  1838. BAIL_IF_ERRPASS(!verifyPath(h, &fname, 0), 0);
  1839. return h->funcs->remove(h->opaque, fname);
  1840. } /* doDelete */
  1841. int PHYSFS_delete(const char *_fname)
  1842. {
  1843. int retval;
  1844. char *fname;
  1845. size_t len;
  1846. __PHYSFS_platformGrabMutex(stateLock);
  1847. BAIL_IF_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
  1848. len = strlen(_fname) + dirHandleRootLen(writeDir) + 1;
  1849. fname = (char *) __PHYSFS_smallAlloc(len);
  1850. BAIL_IF_MUTEX(!fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
  1851. retval = doDelete(_fname, fname);
  1852. __PHYSFS_platformReleaseMutex(stateLock);
  1853. __PHYSFS_smallFree(fname);
  1854. return retval;
  1855. } /* PHYSFS_delete */
  1856. static DirHandle *getRealDirHandle(const char *_fname)
  1857. {
  1858. DirHandle *retval = NULL;
  1859. char *allocated_fname = NULL;
  1860. char *fname = NULL;
  1861. size_t len;
  1862. BAIL_IF(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
  1863. __PHYSFS_platformGrabMutex(stateLock);
  1864. len = strlen(_fname) + longest_root + 2;
  1865. allocated_fname = __PHYSFS_smallAlloc(len);
  1866. BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, NULL);
  1867. fname = allocated_fname + longest_root + 1;
  1868. if (sanitizePlatformIndependentPath(_fname, fname))
  1869. {
  1870. DirHandle *i;
  1871. for (i = searchPath; i != NULL; i = i->next)
  1872. {
  1873. char *arcfname = fname;
  1874. if (partOfMountPoint(i, arcfname))
  1875. {
  1876. retval = i;
  1877. break;
  1878. } /* if */
  1879. else if (verifyPath(i, &arcfname, 0))
  1880. {
  1881. PHYSFS_Stat statbuf;
  1882. if (i->funcs->stat(i->opaque, arcfname, &statbuf))
  1883. {
  1884. retval = i;
  1885. break;
  1886. } /* if */
  1887. } /* if */
  1888. } /* for */
  1889. } /* if */
  1890. __PHYSFS_platformReleaseMutex(stateLock);
  1891. __PHYSFS_smallFree(allocated_fname);
  1892. return retval;
  1893. } /* getRealDirHandle */
  1894. const char *PHYSFS_getRealDir(const char *fname)
  1895. {
  1896. DirHandle *dh = getRealDirHandle(fname);
  1897. return dh ? dh->dirName : NULL;
  1898. } /* PHYSFS_getRealDir */
  1899. static int locateInStringList(const char *str,
  1900. char **list,
  1901. PHYSFS_uint32 *pos)
  1902. {
  1903. PHYSFS_uint32 len = *pos;
  1904. PHYSFS_uint32 half_len;
  1905. PHYSFS_uint32 lo = 0;
  1906. PHYSFS_uint32 middle;
  1907. int cmp;
  1908. while (len > 0)
  1909. {
  1910. half_len = len >> 1;
  1911. middle = lo + half_len;
  1912. cmp = strcmp(list[middle], str);
  1913. if (cmp == 0) /* it's in the list already. */
  1914. return 1;
  1915. else if (cmp > 0)
  1916. len = half_len;
  1917. else
  1918. {
  1919. lo = middle + 1;
  1920. len -= half_len + 1;
  1921. } /* else */
  1922. } /* while */
  1923. *pos = lo;
  1924. return 0;
  1925. } /* locateInStringList */
  1926. static PHYSFS_EnumerateCallbackResult enumFilesCallback(void *data,
  1927. const char *origdir, const char *str)
  1928. {
  1929. PHYSFS_uint32 pos;
  1930. void *ptr;
  1931. char *newstr;
  1932. EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
  1933. /*
  1934. * See if file is in the list already, and if not, insert it in there
  1935. * alphabetically...
  1936. */
  1937. pos = pecd->size;
  1938. if (locateInStringList(str, pecd->list, &pos))
  1939. return PHYSFS_ENUM_OK; /* already in the list, but keep going. */
  1940. ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
  1941. newstr = (char *) allocator.Malloc(strlen(str) + 1);
  1942. if (ptr != NULL)
  1943. pecd->list = (char **) ptr;
  1944. if ((ptr == NULL) || (newstr == NULL))
  1945. {
  1946. if (newstr)
  1947. allocator.Free(newstr);
  1948. pecd->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
  1949. return PHYSFS_ENUM_ERROR; /* better luck next time. */
  1950. } /* if */
  1951. strcpy(newstr, str);
  1952. if (pos != pecd->size)
  1953. {
  1954. memmove(&pecd->list[pos+1], &pecd->list[pos],
  1955. sizeof (char *) * ((pecd->size) - pos));
  1956. } /* if */
  1957. pecd->list[pos] = newstr;
  1958. pecd->size++;
  1959. return PHYSFS_ENUM_OK;
  1960. } /* enumFilesCallback */
  1961. char **PHYSFS_enumerateFiles(const char *path)
  1962. {
  1963. EnumStringListCallbackData ecd;
  1964. memset(&ecd, '\0', sizeof (ecd));
  1965. ecd.list = (char **) allocator.Malloc(sizeof (char *));
  1966. BAIL_IF(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  1967. if (!PHYSFS_enumerate(path, enumFilesCallback, &ecd))
  1968. {
  1969. const PHYSFS_ErrorCode errcode = currentErrorCode();
  1970. PHYSFS_uint32 i;
  1971. for (i = 0; i < ecd.size; i++)
  1972. allocator.Free(ecd.list[i]);
  1973. allocator.Free(ecd.list);
  1974. BAIL_IF(errcode == PHYSFS_ERR_APP_CALLBACK, ecd.errcode, NULL);
  1975. return NULL;
  1976. } /* if */
  1977. ecd.list[ecd.size] = NULL;
  1978. return ecd.list;
  1979. } /* PHYSFS_enumerateFiles */
  1980. /*
  1981. * Broke out to seperate function so we can use stack allocation gratuitously.
  1982. */
  1983. static PHYSFS_EnumerateCallbackResult enumerateFromMountPoint(DirHandle *i,
  1984. const char *arcfname,
  1985. PHYSFS_EnumerateCallback callback,
  1986. const char *_fname, void *data)
  1987. {
  1988. PHYSFS_EnumerateCallbackResult retval;
  1989. const size_t len = strlen(arcfname);
  1990. char *ptr = NULL;
  1991. char *end = NULL;
  1992. const size_t slen = strlen(i->mountPoint) + 1;
  1993. char *mountPoint = (char *) __PHYSFS_smallAlloc(slen);
  1994. BAIL_IF(!mountPoint, PHYSFS_ERR_OUT_OF_MEMORY, PHYSFS_ENUM_ERROR);
  1995. strcpy(mountPoint, i->mountPoint);
  1996. ptr = mountPoint + ((len) ? len + 1 : 0);
  1997. end = strchr(ptr, '/');
  1998. assert(end); /* should always find a terminating '/'. */
  1999. *end = '\0';
  2000. retval = callback(data, _fname, ptr);
  2001. __PHYSFS_smallFree(mountPoint);
  2002. BAIL_IF(retval == PHYSFS_ENUM_ERROR, PHYSFS_ERR_APP_CALLBACK, retval);
  2003. return retval;
  2004. } /* enumerateFromMountPoint */
  2005. typedef struct SymlinkFilterData
  2006. {
  2007. PHYSFS_EnumerateCallback callback;
  2008. void *callbackData;
  2009. DirHandle *dirhandle;
  2010. const char *arcfname;
  2011. PHYSFS_ErrorCode errcode;
  2012. } SymlinkFilterData;
  2013. static PHYSFS_EnumerateCallbackResult enumCallbackFilterSymLinks(void *_data,
  2014. const char *origdir, const char *fname)
  2015. {
  2016. SymlinkFilterData *data = (SymlinkFilterData *) _data;
  2017. const DirHandle *dh = data->dirhandle;
  2018. const char *arcfname = data->arcfname;
  2019. PHYSFS_Stat statbuf;
  2020. const char *trimmedDir = (*arcfname == '/') ? (arcfname + 1) : arcfname;
  2021. const size_t slen = strlen(trimmedDir) + strlen(fname) + 2;
  2022. char *path = (char *) __PHYSFS_smallAlloc(slen);
  2023. PHYSFS_EnumerateCallbackResult retval = PHYSFS_ENUM_OK;
  2024. if (path == NULL)
  2025. {
  2026. data->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
  2027. return PHYSFS_ENUM_ERROR;
  2028. } /* if */
  2029. snprintf(path, slen, "%s%s%s", trimmedDir, *trimmedDir ? "/" : "", fname);
  2030. if (!dh->funcs->stat(dh->opaque, path, &statbuf))
  2031. {
  2032. data->errcode = currentErrorCode();
  2033. retval = PHYSFS_ENUM_ERROR;
  2034. } /* if */
  2035. else
  2036. {
  2037. /* Pass it on to the application if it's not a symlink. */
  2038. if (statbuf.filetype != PHYSFS_FILETYPE_SYMLINK)
  2039. {
  2040. retval = data->callback(data->callbackData, origdir, fname);
  2041. if (retval == PHYSFS_ENUM_ERROR)
  2042. data->errcode = PHYSFS_ERR_APP_CALLBACK;
  2043. } /* if */
  2044. } /* else */
  2045. __PHYSFS_smallFree(path);
  2046. return retval;
  2047. } /* enumCallbackFilterSymLinks */
  2048. int PHYSFS_enumerate(const char *_fn, PHYSFS_EnumerateCallback cb, void *data)
  2049. {
  2050. PHYSFS_EnumerateCallbackResult retval = PHYSFS_ENUM_OK;
  2051. size_t len;
  2052. char *allocated_fname;
  2053. char *fname;
  2054. BAIL_IF(!_fn, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2055. BAIL_IF(!cb, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2056. __PHYSFS_platformGrabMutex(stateLock);
  2057. len = strlen(_fn) + longest_root + 2;
  2058. allocated_fname = (char *) __PHYSFS_smallAlloc(len);
  2059. BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
  2060. fname = allocated_fname + longest_root + 1;
  2061. if (!sanitizePlatformIndependentPath(_fn, fname))
  2062. retval = PHYSFS_ENUM_STOP;
  2063. else
  2064. {
  2065. DirHandle *i;
  2066. SymlinkFilterData filterdata;
  2067. if (!allowSymLinks)
  2068. {
  2069. memset(&filterdata, '\0', sizeof (filterdata));
  2070. filterdata.callback = cb;
  2071. filterdata.callbackData = data;
  2072. } /* if */
  2073. for (i = searchPath; (retval == PHYSFS_ENUM_OK) && i; i = i->next)
  2074. {
  2075. char *arcfname = fname;
  2076. if (partOfMountPoint(i, arcfname))
  2077. retval = enumerateFromMountPoint(i, arcfname, cb, _fn, data);
  2078. else if (verifyPath(i, &arcfname, 0))
  2079. {
  2080. PHYSFS_Stat statbuf;
  2081. if (!i->funcs->stat(i->opaque, arcfname, &statbuf))
  2082. {
  2083. if (currentErrorCode() == PHYSFS_ERR_NOT_FOUND)
  2084. continue; /* no such dir in this archive, skip it. */
  2085. } /* if */
  2086. if (statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY)
  2087. continue; /* not a directory in this archive, skip it. */
  2088. else if ((!allowSymLinks) && (i->funcs->info.supportsSymlinks))
  2089. {
  2090. filterdata.dirhandle = i;
  2091. filterdata.arcfname = arcfname;
  2092. filterdata.errcode = PHYSFS_ERR_OK;
  2093. retval = i->funcs->enumerate(i->opaque, arcfname,
  2094. enumCallbackFilterSymLinks,
  2095. _fn, &filterdata);
  2096. if (retval == PHYSFS_ENUM_ERROR)
  2097. {
  2098. if (currentErrorCode() == PHYSFS_ERR_APP_CALLBACK)
  2099. PHYSFS_setErrorCode(filterdata.errcode);
  2100. } /* if */
  2101. } /* else if */
  2102. else
  2103. {
  2104. retval = i->funcs->enumerate(i->opaque, arcfname,
  2105. cb, _fn, data);
  2106. } /* else */
  2107. } /* else if */
  2108. } /* for */
  2109. } /* if */
  2110. __PHYSFS_platformReleaseMutex(stateLock);
  2111. __PHYSFS_smallFree(allocated_fname);
  2112. return (retval == PHYSFS_ENUM_ERROR) ? 0 : 1;
  2113. } /* PHYSFS_enumerate */
  2114. typedef struct
  2115. {
  2116. PHYSFS_EnumFilesCallback callback;
  2117. void *data;
  2118. } LegacyEnumFilesCallbackData;
  2119. static PHYSFS_EnumerateCallbackResult enumFilesCallbackAlwaysSucceed(void *d,
  2120. const char *origdir, const char *fname)
  2121. {
  2122. LegacyEnumFilesCallbackData *cbdata = (LegacyEnumFilesCallbackData *) d;
  2123. cbdata->callback(cbdata->data, origdir, fname);
  2124. return PHYSFS_ENUM_OK;
  2125. } /* enumFilesCallbackAlwaysSucceed */
  2126. void PHYSFS_enumerateFilesCallback(const char *fname,
  2127. PHYSFS_EnumFilesCallback callback,
  2128. void *data)
  2129. {
  2130. LegacyEnumFilesCallbackData cbdata;
  2131. cbdata.callback = callback;
  2132. cbdata.data = data;
  2133. (void) PHYSFS_enumerate(fname, enumFilesCallbackAlwaysSucceed, &cbdata);
  2134. } /* PHYSFS_enumerateFilesCallback */
  2135. int PHYSFS_exists(const char *fname)
  2136. {
  2137. return (getRealDirHandle(fname) != NULL);
  2138. } /* PHYSFS_exists */
  2139. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  2140. {
  2141. PHYSFS_Stat statbuf;
  2142. BAIL_IF_ERRPASS(!PHYSFS_stat(fname, &statbuf), -1);
  2143. return statbuf.modtime;
  2144. } /* PHYSFS_getLastModTime */
  2145. int PHYSFS_isDirectory(const char *fname)
  2146. {
  2147. PHYSFS_Stat statbuf;
  2148. BAIL_IF_ERRPASS(!PHYSFS_stat(fname, &statbuf), 0);
  2149. return (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
  2150. } /* PHYSFS_isDirectory */
  2151. int PHYSFS_isSymbolicLink(const char *fname)
  2152. {
  2153. PHYSFS_Stat statbuf;
  2154. BAIL_IF_ERRPASS(!PHYSFS_stat(fname, &statbuf), 0);
  2155. return (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
  2156. } /* PHYSFS_isSymbolicLink */
  2157. static PHYSFS_File *doOpenWrite(const char *_fname, const int appending)
  2158. {
  2159. FileHandle *fh = NULL;
  2160. DirHandle *h;
  2161. size_t len;
  2162. char *fname;
  2163. BAIL_IF(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2164. __PHYSFS_platformGrabMutex(stateLock);
  2165. h = writeDir;
  2166. BAIL_IF_MUTEX(!h, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
  2167. len = strlen(_fname) + dirHandleRootLen(h) + 1;
  2168. fname = (char *) __PHYSFS_smallAlloc(len);
  2169. BAIL_IF_MUTEX(!fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
  2170. if (sanitizePlatformIndependentPathWithRoot(h, _fname, fname))
  2171. {
  2172. PHYSFS_Io *io = NULL;
  2173. char *arcfname = fname;
  2174. if (verifyPath(h, &arcfname, 0))
  2175. {
  2176. const PHYSFS_Archiver *f = h->funcs;
  2177. if (appending)
  2178. io = f->openAppend(h->opaque, arcfname);
  2179. else
  2180. io = f->openWrite(h->opaque, arcfname);
  2181. if (io)
  2182. {
  2183. fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  2184. if (fh == NULL)
  2185. {
  2186. io->destroy(io);
  2187. PHYSFS_setErrorCode(PHYSFS_ERR_OUT_OF_MEMORY);
  2188. } /* if */
  2189. else
  2190. {
  2191. memset(fh, '\0', sizeof (FileHandle));
  2192. fh->io = io;
  2193. fh->dirHandle = h;
  2194. fh->next = openWriteList;
  2195. openWriteList = fh;
  2196. } /* else */
  2197. } /* if */
  2198. } /* if */
  2199. } /* if */
  2200. __PHYSFS_platformReleaseMutex(stateLock);
  2201. __PHYSFS_smallFree(fname);
  2202. return ((PHYSFS_File *) fh);
  2203. } /* doOpenWrite */
  2204. PHYSFS_File *PHYSFS_openWrite(const char *filename)
  2205. {
  2206. return doOpenWrite(filename, 0);
  2207. } /* PHYSFS_openWrite */
  2208. PHYSFS_File *PHYSFS_openAppend(const char *filename)
  2209. {
  2210. return doOpenWrite(filename, 1);
  2211. } /* PHYSFS_openAppend */
  2212. PHYSFS_File *PHYSFS_openRead(const char *_fname)
  2213. {
  2214. FileHandle *fh = NULL;
  2215. char *allocated_fname;
  2216. char *fname;
  2217. size_t len;
  2218. BAIL_IF(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2219. __PHYSFS_platformGrabMutex(stateLock);
  2220. BAIL_IF_MUTEX(!searchPath, PHYSFS_ERR_NOT_FOUND, stateLock, 0);
  2221. len = strlen(_fname) + longest_root + 2;
  2222. allocated_fname = (char *) __PHYSFS_smallAlloc(len);
  2223. BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
  2224. fname = allocated_fname + longest_root + 1;
  2225. if (sanitizePlatformIndependentPath(_fname, fname))
  2226. {
  2227. PHYSFS_Io *io = NULL;
  2228. DirHandle *i;
  2229. for (i = searchPath; i != NULL; i = i->next)
  2230. {
  2231. char *arcfname = fname;
  2232. if (verifyPath(i, &arcfname, 0))
  2233. {
  2234. io = i->funcs->openRead(i->opaque, arcfname);
  2235. if (io)
  2236. break;
  2237. } /* if */
  2238. } /* for */
  2239. if (io)
  2240. {
  2241. fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
  2242. if (fh == NULL)
  2243. {
  2244. io->destroy(io);
  2245. PHYSFS_setErrorCode(PHYSFS_ERR_OUT_OF_MEMORY);
  2246. } /* if */
  2247. else
  2248. {
  2249. memset(fh, '\0', sizeof (FileHandle));
  2250. fh->io = io;
  2251. fh->forReading = 1;
  2252. fh->dirHandle = i;
  2253. fh->next = openReadList;
  2254. openReadList = fh;
  2255. } /* else */
  2256. } /* if */
  2257. } /* if */
  2258. __PHYSFS_platformReleaseMutex(stateLock);
  2259. __PHYSFS_smallFree(allocated_fname);
  2260. return ((PHYSFS_File *) fh);
  2261. } /* PHYSFS_openRead */
  2262. static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
  2263. {
  2264. FileHandle *prev = NULL;
  2265. FileHandle *i;
  2266. for (i = *list; i != NULL; i = i->next)
  2267. {
  2268. if (i == handle) /* handle is in this list? */
  2269. {
  2270. PHYSFS_Io *io = handle->io;
  2271. PHYSFS_uint8 *tmp = handle->buffer;
  2272. /* send our buffer to io... */
  2273. if (!handle->forReading)
  2274. {
  2275. if (!PHYSFS_flush((PHYSFS_File *) handle))
  2276. return -1;
  2277. /* ...then have io send it to the disk... */
  2278. else if (io->flush && !io->flush(io))
  2279. return -1;
  2280. } /* if */
  2281. /* ...then close the underlying file. */
  2282. io->destroy(io);
  2283. if (tmp != NULL) /* free any associated buffer. */
  2284. allocator.Free(tmp);
  2285. if (prev == NULL)
  2286. *list = handle->next;
  2287. else
  2288. prev->next = handle->next;
  2289. allocator.Free(handle);
  2290. return 1;
  2291. } /* if */
  2292. prev = i;
  2293. } /* for */
  2294. return 0;
  2295. } /* closeHandleInOpenList */
  2296. int PHYSFS_close(PHYSFS_File *_handle)
  2297. {
  2298. FileHandle *handle = (FileHandle *) _handle;
  2299. int rc;
  2300. __PHYSFS_platformGrabMutex(stateLock);
  2301. /* -1 == close failure. 0 == not found. 1 == success. */
  2302. rc = closeHandleInOpenList(&openReadList, handle);
  2303. BAIL_IF_MUTEX_ERRPASS(rc == -1, stateLock, 0);
  2304. if (!rc)
  2305. {
  2306. rc = closeHandleInOpenList(&openWriteList, handle);
  2307. BAIL_IF_MUTEX_ERRPASS(rc == -1, stateLock, 0);
  2308. } /* if */
  2309. __PHYSFS_platformReleaseMutex(stateLock);
  2310. BAIL_IF(!rc, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2311. return 1;
  2312. } /* PHYSFS_close */
  2313. static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *_buffer, size_t len)
  2314. {
  2315. PHYSFS_uint8 *buffer = (PHYSFS_uint8 *) _buffer;
  2316. PHYSFS_sint64 retval = 0;
  2317. while (len > 0)
  2318. {
  2319. const size_t avail = fh->buffill - fh->bufpos;
  2320. if (avail > 0) /* data available in the buffer. */
  2321. {
  2322. const size_t cpy = (len < avail) ? len : avail;
  2323. memcpy(buffer, fh->buffer + fh->bufpos, cpy);
  2324. assert(len >= cpy);
  2325. buffer += cpy;
  2326. len -= cpy;
  2327. fh->bufpos += cpy;
  2328. retval += cpy;
  2329. } /* if */
  2330. else /* buffer is empty, refill it. */
  2331. {
  2332. PHYSFS_Io *io = fh->io;
  2333. const PHYSFS_sint64 rc = io->read(io, fh->buffer, fh->bufsize);
  2334. fh->bufpos = 0;
  2335. if (rc > 0)
  2336. fh->buffill = (size_t) rc;
  2337. else
  2338. {
  2339. fh->buffill = 0;
  2340. if (retval == 0) /* report already-read data, or failure. */
  2341. retval = rc;
  2342. break;
  2343. } /* else */
  2344. } /* else */
  2345. } /* while */
  2346. return retval;
  2347. } /* doBufferedRead */
  2348. PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
  2349. PHYSFS_uint32 size, PHYSFS_uint32 count)
  2350. {
  2351. const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
  2352. const PHYSFS_sint64 retval = PHYSFS_readBytes(handle, buffer, len);
  2353. return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
  2354. } /* PHYSFS_read */
  2355. PHYSFS_sint64 PHYSFS_readBytes(PHYSFS_File *handle, void *buffer,
  2356. PHYSFS_uint64 _len)
  2357. {
  2358. const size_t len = (size_t) _len;
  2359. FileHandle *fh = (FileHandle *) handle;
  2360. #ifdef PHYSFS_NO_64BIT_SUPPORT
  2361. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
  2362. #else
  2363. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
  2364. #endif
  2365. if (!__PHYSFS_ui64FitsAddressSpace(_len))
  2366. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2367. BAIL_IF(_len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2368. BAIL_IF(!fh->forReading, PHYSFS_ERR_OPEN_FOR_WRITING, -1);
  2369. BAIL_IF_ERRPASS(len == 0, 0);
  2370. if (fh->buffer)
  2371. return doBufferedRead(fh, buffer, len);
  2372. return fh->io->read(fh->io, buffer, len);
  2373. } /* PHYSFS_readBytes */
  2374. static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
  2375. const size_t len)
  2376. {
  2377. FileHandle *fh = (FileHandle *) handle;
  2378. /* whole thing fits in the buffer? */
  2379. if ((fh->buffill + len) < fh->bufsize)
  2380. {
  2381. memcpy(fh->buffer + fh->buffill, buffer, len);
  2382. fh->buffill += len;
  2383. return (PHYSFS_sint64) len;
  2384. } /* if */
  2385. /* would overflow buffer. Flush and then write the new objects, too. */
  2386. BAIL_IF_ERRPASS(!PHYSFS_flush(handle), -1);
  2387. return fh->io->write(fh->io, buffer, len);
  2388. } /* doBufferedWrite */
  2389. PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
  2390. PHYSFS_uint32 size, PHYSFS_uint32 count)
  2391. {
  2392. const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
  2393. const PHYSFS_sint64 retval = PHYSFS_writeBytes(handle, buffer, len);
  2394. return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
  2395. } /* PHYSFS_write */
  2396. PHYSFS_sint64 PHYSFS_writeBytes(PHYSFS_File *handle, const void *buffer,
  2397. PHYSFS_uint64 _len)
  2398. {
  2399. const size_t len = (size_t) _len;
  2400. FileHandle *fh = (FileHandle *) handle;
  2401. #ifdef PHYSFS_NO_64BIT_SUPPORT
  2402. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
  2403. #else
  2404. const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
  2405. #endif
  2406. if (!__PHYSFS_ui64FitsAddressSpace(_len))
  2407. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2408. BAIL_IF(_len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
  2409. BAIL_IF(fh->forReading, PHYSFS_ERR_OPEN_FOR_READING, -1);
  2410. BAIL_IF_ERRPASS(len == 0, 0);
  2411. if (fh->buffer)
  2412. return doBufferedWrite(handle, buffer, len);
  2413. return fh->io->write(fh->io, buffer, len);
  2414. } /* PHYSFS_write */
  2415. int PHYSFS_eof(PHYSFS_File *handle)
  2416. {
  2417. FileHandle *fh = (FileHandle *) handle;
  2418. if (!fh->forReading) /* never EOF on files opened for write/append. */
  2419. return 0;
  2420. /* can't be eof if buffer isn't empty */
  2421. if (fh->bufpos == fh->buffill)
  2422. {
  2423. /* check the Io. */
  2424. PHYSFS_Io *io = fh->io;
  2425. const PHYSFS_sint64 pos = io->tell(io);
  2426. const PHYSFS_sint64 len = io->length(io);
  2427. if ((pos < 0) || (len < 0))
  2428. return 0; /* beats me. */
  2429. return (pos >= len);
  2430. } /* if */
  2431. return 0;
  2432. } /* PHYSFS_eof */
  2433. PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
  2434. {
  2435. FileHandle *fh = (FileHandle *) handle;
  2436. const PHYSFS_sint64 pos = fh->io->tell(fh->io);
  2437. const PHYSFS_sint64 retval = fh->forReading ?
  2438. (pos - fh->buffill) + fh->bufpos :
  2439. (pos + fh->buffill);
  2440. return retval;
  2441. } /* PHYSFS_tell */
  2442. int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
  2443. {
  2444. FileHandle *fh = (FileHandle *) handle;
  2445. BAIL_IF_ERRPASS(!PHYSFS_flush(handle), 0);
  2446. if (fh->buffer && fh->forReading)
  2447. {
  2448. /* avoid throwing away our precious buffer if seeking within it. */
  2449. PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
  2450. if ( /* seeking within the already-buffered range? */
  2451. /* forward? */
  2452. ((offset >= 0) && (((size_t)offset) <= fh->buffill-fh->bufpos)) ||
  2453. /* backward? */
  2454. ((offset < 0) && (((size_t) -offset) <= fh->bufpos)) )
  2455. {
  2456. fh->bufpos = (size_t) (((PHYSFS_sint64) fh->bufpos) + offset);
  2457. return 1; /* successful seek */
  2458. } /* if */
  2459. } /* if */
  2460. /* we have to fall back to a 'raw' seek. */
  2461. fh->buffill = fh->bufpos = 0;
  2462. return fh->io->seek(fh->io, pos);
  2463. } /* PHYSFS_seek */
  2464. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
  2465. {
  2466. PHYSFS_Io *io = ((FileHandle *) handle)->io;
  2467. return io->length(io);
  2468. } /* PHYSFS_filelength */
  2469. int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
  2470. {
  2471. FileHandle *fh = (FileHandle *) handle;
  2472. const size_t bufsize = (size_t) _bufsize;
  2473. if (!__PHYSFS_ui64FitsAddressSpace(_bufsize))
  2474. BAIL(PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2475. BAIL_IF_ERRPASS(!PHYSFS_flush(handle), 0);
  2476. /*
  2477. * For reads, we need to move the file pointer to where it would be
  2478. * if we weren't buffering, so that the next read will get the
  2479. * right chunk of stuff from the file. PHYSFS_flush() handles writes.
  2480. */
  2481. if ((fh->forReading) && (fh->buffill != fh->bufpos))
  2482. {
  2483. PHYSFS_uint64 pos;
  2484. const PHYSFS_sint64 curpos = fh->io->tell(fh->io);
  2485. BAIL_IF_ERRPASS(curpos == -1, 0);
  2486. pos = ((curpos - fh->buffill) + fh->bufpos);
  2487. BAIL_IF_ERRPASS(!fh->io->seek(fh->io, pos), 0);
  2488. } /* if */
  2489. if (bufsize == 0) /* delete existing buffer. */
  2490. {
  2491. if (fh->buffer)
  2492. {
  2493. allocator.Free(fh->buffer);
  2494. fh->buffer = NULL;
  2495. } /* if */
  2496. } /* if */
  2497. else
  2498. {
  2499. PHYSFS_uint8 *newbuf;
  2500. newbuf = (PHYSFS_uint8 *) allocator.Realloc(fh->buffer, bufsize);
  2501. BAIL_IF(!newbuf, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  2502. fh->buffer = newbuf;
  2503. } /* else */
  2504. fh->bufsize = bufsize;
  2505. fh->buffill = fh->bufpos = 0;
  2506. return 1;
  2507. } /* PHYSFS_setBuffer */
  2508. int PHYSFS_flush(PHYSFS_File *handle)
  2509. {
  2510. FileHandle *fh = (FileHandle *) handle;
  2511. PHYSFS_Io *io;
  2512. PHYSFS_sint64 rc;
  2513. if ((fh->forReading) || (fh->bufpos == fh->buffill))
  2514. return 1; /* open for read or buffer empty are successful no-ops. */
  2515. /* dump buffer to disk. */
  2516. io = fh->io;
  2517. rc = io->write(io, fh->buffer + fh->bufpos, fh->buffill - fh->bufpos);
  2518. BAIL_IF_ERRPASS(rc <= 0, 0);
  2519. fh->bufpos = fh->buffill = 0;
  2520. return 1;
  2521. } /* PHYSFS_flush */
  2522. int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
  2523. {
  2524. int retval = 0;
  2525. char *allocated_fname;
  2526. char *fname;
  2527. size_t len;
  2528. BAIL_IF(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2529. BAIL_IF(!stat, PHYSFS_ERR_INVALID_ARGUMENT, 0);
  2530. /* set some sane defaults... */
  2531. stat->filesize = -1;
  2532. stat->modtime = -1;
  2533. stat->createtime = -1;
  2534. stat->accesstime = -1;
  2535. stat->filetype = PHYSFS_FILETYPE_OTHER;
  2536. stat->readonly = 1;
  2537. __PHYSFS_platformGrabMutex(stateLock);
  2538. len = strlen(_fname) + longest_root + 2;
  2539. allocated_fname = (char *) __PHYSFS_smallAlloc(len);
  2540. BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0);
  2541. fname = allocated_fname + longest_root + 1;
  2542. if (sanitizePlatformIndependentPath(_fname, fname))
  2543. {
  2544. if (*fname == '\0')
  2545. {
  2546. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  2547. stat->readonly = !writeDir; /* Writeable if we have a writeDir */
  2548. retval = 1;
  2549. } /* if */
  2550. else
  2551. {
  2552. DirHandle *i;
  2553. int exists = 0;
  2554. for (i = searchPath; ((i != NULL) && (!exists)); i = i->next)
  2555. {
  2556. char *arcfname = fname;
  2557. exists = partOfMountPoint(i, arcfname);
  2558. if (exists)
  2559. {
  2560. stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
  2561. stat->readonly = 1;
  2562. retval = 1;
  2563. } /* if */
  2564. else if (verifyPath(i, &arcfname, 0))
  2565. {
  2566. retval = i->funcs->stat(i->opaque, arcfname, stat);
  2567. if ((retval) || (currentErrorCode() != PHYSFS_ERR_NOT_FOUND))
  2568. exists = 1;
  2569. } /* else if */
  2570. } /* for */
  2571. } /* else */
  2572. } /* if */
  2573. __PHYSFS_platformReleaseMutex(stateLock);
  2574. __PHYSFS_smallFree(allocated_fname);
  2575. return retval;
  2576. } /* PHYSFS_stat */
  2577. int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const size_t _len)
  2578. {
  2579. const PHYSFS_uint64 len = (PHYSFS_uint64) _len;
  2580. return (io->read(io, buf, len) == len);
  2581. } /* __PHYSFS_readAll */
  2582. void *__PHYSFS_initSmallAlloc(void *ptr, const size_t len)
  2583. {
  2584. void *useHeap = ((ptr == NULL) ? ((void *) 1) : ((void *) 0));
  2585. if (useHeap) /* too large for stack allocation or alloca() failed. */
  2586. ptr = allocator.Malloc(len+sizeof (void *));
  2587. if (ptr != NULL)
  2588. {
  2589. void **retval = (void **) ptr;
  2590. /*printf("%s alloc'd (%lld) bytes at (%p).\n",
  2591. useHeap ? "heap" : "stack", (long long) len, ptr);*/
  2592. *retval = useHeap;
  2593. return retval + 1;
  2594. } /* if */
  2595. return NULL; /* allocation failed. */
  2596. } /* __PHYSFS_initSmallAlloc */
  2597. void __PHYSFS_smallFree(void *ptr)
  2598. {
  2599. if (ptr != NULL)
  2600. {
  2601. void **block = ((void **) ptr) - 1;
  2602. const int useHeap = (*block != NULL);
  2603. if (useHeap)
  2604. allocator.Free(block);
  2605. /*printf("%s free'd (%p).\n", useHeap ? "heap" : "stack", block);*/
  2606. } /* if */
  2607. } /* __PHYSFS_smallFree */
  2608. int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
  2609. {
  2610. BAIL_IF(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
  2611. externalAllocator = (a != NULL);
  2612. if (externalAllocator)
  2613. memcpy(&allocator, a, sizeof (PHYSFS_Allocator));
  2614. return 1;
  2615. } /* PHYSFS_setAllocator */
  2616. const PHYSFS_Allocator *PHYSFS_getAllocator(void)
  2617. {
  2618. BAIL_IF(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
  2619. return &allocator;
  2620. } /* PHYSFS_getAllocator */
  2621. #ifndef PHYSFS_NO_CRUNTIME_MALLOC
  2622. static void *mallocAllocatorMalloc(PHYSFS_uint64 s)
  2623. {
  2624. if (!__PHYSFS_ui64FitsAddressSpace(s))
  2625. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  2626. #undef malloc
  2627. return malloc((size_t) s);
  2628. } /* mallocAllocatorMalloc */
  2629. static void *mallocAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
  2630. {
  2631. if (!__PHYSFS_ui64FitsAddressSpace(s))
  2632. BAIL(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  2633. #undef realloc
  2634. return realloc(ptr, (size_t) s);
  2635. } /* mallocAllocatorRealloc */
  2636. static void mallocAllocatorFree(void *ptr)
  2637. {
  2638. #undef free
  2639. free(ptr);
  2640. } /* mallocAllocatorFree */
  2641. #endif
  2642. static void setDefaultAllocator(void)
  2643. {
  2644. assert(!externalAllocator);
  2645. allocator.Init = NULL;
  2646. allocator.Deinit = NULL;
  2647. #ifndef PHYSFS_NO_CRUNTIME_MALLOC
  2648. allocator.Malloc = mallocAllocatorMalloc;
  2649. allocator.Realloc = mallocAllocatorRealloc;
  2650. allocator.Free = mallocAllocatorFree;
  2651. #endif
  2652. } /* setDefaultAllocator */
  2653. int __PHYSFS_DirTreeInit(__PHYSFS_DirTree *dt, const size_t entrylen, const int case_sensitive, const int only_usascii)
  2654. {
  2655. static char rootpath[2] = { '/', '\0' };
  2656. size_t alloclen;
  2657. assert(entrylen >= sizeof (__PHYSFS_DirTreeEntry));
  2658. memset(dt, '\0', sizeof (*dt));
  2659. dt->case_sensitive = case_sensitive;
  2660. dt->only_usascii = only_usascii;
  2661. dt->root = (__PHYSFS_DirTreeEntry *) allocator.Malloc(entrylen);
  2662. BAIL_IF(!dt->root, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  2663. memset(dt->root, '\0', entrylen);
  2664. dt->root->name = rootpath;
  2665. dt->root->isdir = 1;
  2666. dt->hashBuckets = 64;
  2667. if (!dt->hashBuckets)
  2668. dt->hashBuckets = 1;
  2669. dt->entrylen = entrylen;
  2670. alloclen = dt->hashBuckets * sizeof (__PHYSFS_DirTreeEntry *);
  2671. dt->hash = (__PHYSFS_DirTreeEntry **) allocator.Malloc(alloclen);
  2672. BAIL_IF(!dt->hash, PHYSFS_ERR_OUT_OF_MEMORY, 0);
  2673. memset(dt->hash, '\0', alloclen);
  2674. return 1;
  2675. } /* __PHYSFS_DirTreeInit */
  2676. static PHYSFS_uint32 hashPathName(__PHYSFS_DirTree *dt, const char *name)
  2677. {
  2678. const PHYSFS_uint32 hashval = dt->case_sensitive ? __PHYSFS_hashString(name) : dt->only_usascii ? __PHYSFS_hashStringCaseFoldUSAscii(name) : __PHYSFS_hashStringCaseFold(name);
  2679. return hashval % dt->hashBuckets;
  2680. } /* hashPathName */
  2681. /* Fill in missing parent directories. */
  2682. static __PHYSFS_DirTreeEntry *addAncestors(__PHYSFS_DirTree *dt, char *name)
  2683. {
  2684. __PHYSFS_DirTreeEntry *retval = dt->root;
  2685. char *sep = strrchr(name, '/');
  2686. if (sep)
  2687. {
  2688. *sep = '\0'; /* chop off last piece. */
  2689. retval = (__PHYSFS_DirTreeEntry *) __PHYSFS_DirTreeFind(dt, name);
  2690. if (retval != NULL)
  2691. {
  2692. *sep = '/';
  2693. BAIL_IF(!retval->isdir, PHYSFS_ERR_CORRUPT, NULL);
  2694. return retval; /* already hashed. */
  2695. } /* if */
  2696. /* okay, this is a new dir. Build and hash us. */
  2697. retval = (__PHYSFS_DirTreeEntry*)__PHYSFS_DirTreeAdd(dt, name, 1);
  2698. *sep = '/';
  2699. } /* if */
  2700. return retval;
  2701. } /* addAncestors */
  2702. void *__PHYSFS_DirTreeAdd(__PHYSFS_DirTree *dt, char *name, const int isdir)
  2703. {
  2704. __PHYSFS_DirTreeEntry *retval = __PHYSFS_DirTreeFind(dt, name);
  2705. if (!retval)
  2706. {
  2707. const size_t alloclen = strlen(name) + 1 + dt->entrylen;
  2708. PHYSFS_uint32 hashval;
  2709. __PHYSFS_DirTreeEntry *parent = addAncestors(dt, name);
  2710. BAIL_IF_ERRPASS(!parent, NULL);
  2711. assert(dt->entrylen >= sizeof (__PHYSFS_DirTreeEntry));
  2712. retval = (__PHYSFS_DirTreeEntry *) allocator.Malloc(alloclen);
  2713. BAIL_IF(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
  2714. memset(retval, '\0', dt->entrylen);
  2715. retval->name = ((char *) retval) + dt->entrylen;
  2716. strcpy(retval->name, name);
  2717. hashval = hashPathName(dt, name);
  2718. retval->hashnext = dt->hash[hashval];
  2719. dt->hash[hashval] = retval;
  2720. retval->sibling = parent->children;
  2721. retval->isdir = isdir;
  2722. parent->children = retval;
  2723. } /* if */
  2724. return retval;
  2725. } /* __PHYSFS_DirTreeAdd */
  2726. /* Find the __PHYSFS_DirTreeEntry for a path in platform-independent notation. */
  2727. void *__PHYSFS_DirTreeFind(__PHYSFS_DirTree *dt, const char *path)
  2728. {
  2729. const int cs = dt->case_sensitive;
  2730. PHYSFS_uint32 hashval;
  2731. __PHYSFS_DirTreeEntry *prev = NULL;
  2732. __PHYSFS_DirTreeEntry *retval;
  2733. if (*path == '\0')
  2734. return dt->root;
  2735. hashval = hashPathName(dt, path);
  2736. for (retval = dt->hash[hashval]; retval; retval = retval->hashnext)
  2737. {
  2738. const int cmp = cs ? strcmp(retval->name, path) : PHYSFS_utf8stricmp(retval->name, path);
  2739. if (cmp == 0)
  2740. {
  2741. if (prev != NULL) /* move this to the front of the list */
  2742. {
  2743. prev->hashnext = retval->hashnext;
  2744. retval->hashnext = dt->hash[hashval];
  2745. dt->hash[hashval] = retval;
  2746. } /* if */
  2747. return retval;
  2748. } /* if */
  2749. prev = retval;
  2750. } /* for */
  2751. BAIL(PHYSFS_ERR_NOT_FOUND, NULL);
  2752. } /* __PHYSFS_DirTreeFind */
  2753. PHYSFS_EnumerateCallbackResult __PHYSFS_DirTreeEnumerate(void *opaque,
  2754. const char *dname, PHYSFS_EnumerateCallback cb,
  2755. const char *origdir, void *callbackdata)
  2756. {
  2757. PHYSFS_EnumerateCallbackResult retval = PHYSFS_ENUM_OK;
  2758. __PHYSFS_DirTree *tree = (__PHYSFS_DirTree *) opaque;
  2759. const __PHYSFS_DirTreeEntry *entry = __PHYSFS_DirTreeFind(tree, dname);
  2760. BAIL_IF(!entry, PHYSFS_ERR_NOT_FOUND, PHYSFS_ENUM_ERROR);
  2761. entry = entry->children;
  2762. while (entry && (retval == PHYSFS_ENUM_OK))
  2763. {
  2764. const char *name = entry->name;
  2765. const char *ptr = strrchr(name, '/');
  2766. retval = cb(callbackdata, origdir, ptr ? ptr + 1 : name);
  2767. BAIL_IF(retval == PHYSFS_ENUM_ERROR, PHYSFS_ERR_APP_CALLBACK, retval);
  2768. entry = entry->sibling;
  2769. } /* while */
  2770. return retval;
  2771. } /* __PHYSFS_DirTreeEnumerate */
  2772. void __PHYSFS_DirTreeDeinit(__PHYSFS_DirTree *dt)
  2773. {
  2774. if (!dt)
  2775. return;
  2776. if (dt->root)
  2777. {
  2778. assert(dt->root->sibling == NULL);
  2779. assert(dt->hash || (dt->root->children == NULL));
  2780. allocator.Free(dt->root);
  2781. } /* if */
  2782. if (dt->hash)
  2783. {
  2784. size_t i;
  2785. for (i = 0; i < dt->hashBuckets; i++)
  2786. {
  2787. __PHYSFS_DirTreeEntry *entry;
  2788. __PHYSFS_DirTreeEntry *next;
  2789. for (entry = dt->hash[i]; entry; entry = next)
  2790. {
  2791. next = entry->hashnext;
  2792. allocator.Free(entry);
  2793. } /* for */
  2794. } /* for */
  2795. allocator.Free(dt->hash);
  2796. } /* if */
  2797. } /* __PHYSFS_DirTreeDeinit */
  2798. /* end of physfs.c ... */