physfs.c 52 KB

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