physfs.c 84 KB

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