physfs.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  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. if (*fname == '\0') /* quick rejection. */
  967. return(1);
  968. if (h->mountPoint != NULL) /* NULL mountpoint means "/". */
  969. {
  970. /* !!! FIXME: Case insensitive? */
  971. size_t mntpntlen = strlen(h->mountPoint);
  972. assert(mntpntlen > 1); /* root mount points should be NULL. */
  973. size_t len = strlen(fname);
  974. /* not under the mountpoint, so skip this archive. */
  975. BAIL_IF_MACRO(len < mntpntlen, ERR_NO_SUCH_PATH, 0);
  976. retval = strncmp(h->mountPoint, fname, mntpntlen);
  977. BAIL_IF_MACRO(retval != 0, ERR_NO_SUCH_PATH, 0);
  978. fname += mntpntlen; /* move to start of actual archive path. */
  979. retval = 1;
  980. } /* if */
  981. start = fname;
  982. if (!allowSymLinks)
  983. {
  984. while (1)
  985. {
  986. end = strchr(start, '/');
  987. if (end != NULL)
  988. *end = '\0';
  989. if (h->funcs->isSymLink(h->opaque, fname, &retval))
  990. BAIL_MACRO(ERR_SYMLINK_DISALLOWED, 0); /* insecure. */
  991. /* break out early if path element is missing. */
  992. if (!retval)
  993. {
  994. /*
  995. * We need to clear it if it's the last element of the path,
  996. * since this might be a non-existant file we're opening
  997. * for writing...
  998. */
  999. if ((end == NULL) || (allowMissing))
  1000. retval = 1;
  1001. break;
  1002. } /* if */
  1003. if (end == NULL)
  1004. break;
  1005. *end = '/';
  1006. start = end + 1;
  1007. } /* while */
  1008. } /* if */
  1009. return(retval);
  1010. } /* __PHYSFS_verifySecurity */
  1011. int PHYSFS_mkdir(const char *_dname)
  1012. {
  1013. DirHandle *h;
  1014. char *start;
  1015. char *end;
  1016. int retval = 0;
  1017. int exists = 1; /* force existance check on first path element. */
  1018. char *dname;
  1019. dname = ((_dname) ? (char *) alloca(strlen(_dname) + 1) : NULL);
  1020. BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
  1021. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_dname, dname), NULL, 0);
  1022. __PHYSFS_platformGrabMutex(stateLock);
  1023. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1024. h = writeDir;
  1025. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h,dname,1),NULL,stateLock,0);
  1026. start = dname;
  1027. while (1)
  1028. {
  1029. end = strchr(start, '/');
  1030. if (end != NULL)
  1031. *end = '\0';
  1032. /* only check for existance if all parent dirs existed, too... */
  1033. if (exists)
  1034. retval = h->funcs->isDirectory(h->opaque, dname, &exists);
  1035. if (!exists)
  1036. retval = h->funcs->mkdir(h->opaque, dname);
  1037. if (!retval)
  1038. break;
  1039. if (end == NULL)
  1040. break;
  1041. *end = '/';
  1042. start = end + 1;
  1043. } /* while */
  1044. __PHYSFS_platformReleaseMutex(stateLock);
  1045. return(retval);
  1046. } /* PHYSFS_mkdir */
  1047. int PHYSFS_delete(const char *_fname)
  1048. {
  1049. int retval;
  1050. DirHandle *h;
  1051. char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1052. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1053. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
  1054. __PHYSFS_platformGrabMutex(stateLock);
  1055. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  1056. h = writeDir;
  1057. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h,fname,0),NULL,stateLock,0);
  1058. retval = h->funcs->remove(h->opaque, fname);
  1059. __PHYSFS_platformReleaseMutex(stateLock);
  1060. return(retval);
  1061. } /* PHYSFS_delete */
  1062. const char *PHYSFS_getRealDir(const char *_fname)
  1063. {
  1064. DirHandle *i;
  1065. const char *retval = NULL;
  1066. char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1067. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1068. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, NULL);
  1069. __PHYSFS_platformGrabMutex(stateLock);
  1070. for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
  1071. {
  1072. if (__PHYSFS_verifySecurity(i, fname, 0))
  1073. {
  1074. if (i->funcs->exists(i->opaque, fname))
  1075. retval = i->dirName;
  1076. } /* if */
  1077. } /* for */
  1078. __PHYSFS_platformReleaseMutex(stateLock);
  1079. return(retval);
  1080. } /* PHYSFS_getRealDir */
  1081. static int locateInStringList(const char *str,
  1082. char **list,
  1083. PHYSFS_uint32 *pos)
  1084. {
  1085. PHYSFS_uint32 hi = *pos - 1;
  1086. PHYSFS_uint32 lo = 0;
  1087. PHYSFS_uint32 i = hi >> 1;
  1088. int cmp;
  1089. assert(*pos != 0); /* this doesn't work with empty lists! */
  1090. while (hi != lo)
  1091. {
  1092. cmp = strcmp(list[i], str);
  1093. if (cmp == 0) /* it's in the list already. */
  1094. return(1);
  1095. else if (cmp < 0)
  1096. {
  1097. hi = i;
  1098. i = lo + ((hi - lo) >> 1);
  1099. } /* else if */
  1100. else
  1101. {
  1102. lo = i + 1;
  1103. i = lo + ((1 + hi - lo) >> 1);
  1104. } /* else */
  1105. } /* while */
  1106. /* hi == lo, check it in case it's the match... */
  1107. cmp = strcmp(list[lo], str);
  1108. if (cmp == 0)
  1109. return(1);
  1110. /* not in the list, set insertion point... */
  1111. *pos = (cmp < 0) ? lo : lo + 1;
  1112. return(0);
  1113. } /* locateInStringList */
  1114. static void enumFilesCallback(void *data, const char *str)
  1115. {
  1116. PHYSFS_uint32 pos;
  1117. void *ptr;
  1118. char *newstr;
  1119. EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
  1120. /*
  1121. * See if file is in the list already, and if not, insert it in there
  1122. * alphabetically...
  1123. */
  1124. pos = pecd->size;
  1125. if (pos > 0)
  1126. {
  1127. if (locateInStringList(str, pecd->list, &pos))
  1128. return; /* already in the list. */
  1129. } /* if */
  1130. ptr = realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
  1131. newstr = malloc(strlen(str) + 1);
  1132. if (ptr != NULL)
  1133. pecd->list = (char **) ptr;
  1134. if ((ptr == NULL) || (newstr == NULL))
  1135. return; /* better luck next time. */
  1136. strcpy(newstr, str);
  1137. if (pos != pecd->size)
  1138. {
  1139. memmove(&pecd->list[pos+1], &pecd->list[pos],
  1140. sizeof (char *) * ((pecd->size) - pos));
  1141. } /* if */
  1142. pecd->list[pos] = newstr;
  1143. pecd->size++;
  1144. } /* enumFilesCallback */
  1145. char **PHYSFS_enumerateFiles(const char *path)
  1146. {
  1147. EnumStringListCallbackData ecd;
  1148. memset(&ecd, '\0', sizeof (ecd));
  1149. ecd.list = (char **) malloc(sizeof (char *));
  1150. BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
  1151. PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
  1152. ecd.list[ecd.size] = NULL;
  1153. return(ecd.list);
  1154. } /* PHYSFS_enumerateFiles */
  1155. void PHYSFS_enumerateFilesCallback(const char *_fname,
  1156. PHYSFS_StringCallback callback,
  1157. void *data)
  1158. {
  1159. DirHandle *i;
  1160. int noSyms;
  1161. char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1162. if ((fname == NULL) || (callback == NULL))
  1163. return; /* oh well. */
  1164. if (!sanitizePlatformIndependentPath(_fname, fname))
  1165. return;
  1166. __PHYSFS_platformGrabMutex(stateLock);
  1167. noSyms = !allowSymLinks;
  1168. for (i = searchPath; i != NULL; i = i->next)
  1169. {
  1170. if (__PHYSFS_verifySecurity(i, fname, 0))
  1171. i->funcs->enumerateFiles(i->opaque, fname, noSyms, callback, data);
  1172. } /* for */
  1173. __PHYSFS_platformReleaseMutex(stateLock);
  1174. } /* PHYSFS_enumerateFilesCallback */
  1175. int PHYSFS_exists(const char *fname)
  1176. {
  1177. return(PHYSFS_getRealDir(fname) != NULL);
  1178. } /* PHYSFS_exists */
  1179. PHYSFS_sint64 PHYSFS_getLastModTime(const char *_fname)
  1180. {
  1181. DirHandle *i;
  1182. PHYSFS_sint64 retval = -1;
  1183. int fileExists = 0;
  1184. char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1185. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1186. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
  1187. if (*fname == '\0') /* eh...punt if it's the root dir. */
  1188. return(1); /* !!! FIXME: Maybe this should be an error? */
  1189. __PHYSFS_platformGrabMutex(stateLock);
  1190. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1191. {
  1192. if (__PHYSFS_verifySecurity(i, fname, 0))
  1193. retval = i->funcs->getLastModTime(i->opaque, fname, &fileExists);
  1194. } /* for */
  1195. __PHYSFS_platformReleaseMutex(stateLock);
  1196. return(retval);
  1197. } /* PHYSFS_getLastModTime */
  1198. int PHYSFS_isDirectory(const char *_fname)
  1199. {
  1200. DirHandle *i;
  1201. int retval = 0;
  1202. int fileExists = 0;
  1203. char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1204. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1205. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
  1206. BAIL_IF_MACRO(*fname == '\0', NULL, 1); /* Root is always a dir. :) */
  1207. __PHYSFS_platformGrabMutex(stateLock);
  1208. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1209. {
  1210. if (__PHYSFS_verifySecurity(i, fname, 0))
  1211. retval = i->funcs->isDirectory(i->opaque, fname, &fileExists);
  1212. } /* for */
  1213. __PHYSFS_platformReleaseMutex(stateLock);
  1214. return(retval);
  1215. } /* PHYSFS_isDirectory */
  1216. int PHYSFS_isSymbolicLink(const char *_fname)
  1217. {
  1218. DirHandle *i;
  1219. int retval = 0;
  1220. int fileExists = 0;
  1221. char *fname;
  1222. BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
  1223. fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1224. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1225. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
  1226. BAIL_IF_MACRO(*fname == '\0', NULL, 0); /* Root is never a symlink */
  1227. __PHYSFS_platformGrabMutex(stateLock);
  1228. for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
  1229. {
  1230. if (__PHYSFS_verifySecurity(i, fname, 0))
  1231. retval = i->funcs->isSymLink(i->opaque, fname, &fileExists);
  1232. } /* for */
  1233. __PHYSFS_platformReleaseMutex(stateLock);
  1234. return(retval);
  1235. } /* PHYSFS_isSymbolicLink */
  1236. static PHYSFS_File *doOpenWrite(const char *_fname, int appending)
  1237. {
  1238. void *opaque = NULL;
  1239. FileHandle *fh = NULL;
  1240. DirHandle *h = NULL;
  1241. const PHYSFS_Archiver *f;
  1242. char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1243. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1244. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
  1245. __PHYSFS_platformGrabMutex(stateLock);
  1246. BAIL_IF_MACRO_MUTEX(!writeDir, ERR_NO_WRITE_DIR, stateLock, NULL);
  1247. h = writeDir;
  1248. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname, 0), NULL,
  1249. stateLock, NULL);
  1250. f = h->funcs;
  1251. if (appending)
  1252. opaque = f->openAppend(h->opaque, fname);
  1253. else
  1254. opaque = f->openWrite(h->opaque, fname);
  1255. BAIL_IF_MACRO_MUTEX(opaque == NULL, NULL, stateLock, NULL);
  1256. fh = (FileHandle *) malloc(sizeof (FileHandle));
  1257. if (fh == NULL)
  1258. {
  1259. f->fileClose(opaque);
  1260. BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
  1261. } /* if */
  1262. else
  1263. {
  1264. memset(fh, '\0', sizeof (FileHandle));
  1265. fh->opaque = opaque;
  1266. fh->dirHandle = h;
  1267. fh->funcs = h->funcs;
  1268. fh->next = openWriteList;
  1269. openWriteList = fh;
  1270. } /* else */
  1271. __PHYSFS_platformReleaseMutex(stateLock);
  1272. return((PHYSFS_File *) fh);
  1273. } /* doOpenWrite */
  1274. PHYSFS_File *PHYSFS_openWrite(const char *filename)
  1275. {
  1276. return(doOpenWrite(filename, 0));
  1277. } /* PHYSFS_openWrite */
  1278. PHYSFS_File *PHYSFS_openAppend(const char *filename)
  1279. {
  1280. return(doOpenWrite(filename, 1));
  1281. } /* PHYSFS_openAppend */
  1282. PHYSFS_File *PHYSFS_openRead(const char *_fname)
  1283. {
  1284. FileHandle *fh = NULL;
  1285. int fileExists = 0;
  1286. DirHandle *i = NULL;
  1287. fvoid *opaque = NULL;
  1288. char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
  1289. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  1290. BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
  1291. __PHYSFS_platformGrabMutex(stateLock);
  1292. BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
  1293. i = searchPath;
  1294. do
  1295. {
  1296. if (__PHYSFS_verifySecurity(i, fname, 0))
  1297. {
  1298. opaque = i->funcs->openRead(i->opaque, fname, &fileExists);
  1299. if (opaque)
  1300. break;
  1301. } /* if */
  1302. i = i->next;
  1303. } while ((i != NULL) && (!fileExists));
  1304. BAIL_IF_MACRO_MUTEX(opaque == NULL, NULL, stateLock, NULL);
  1305. fh = (FileHandle *) malloc(sizeof (FileHandle));
  1306. if (fh == NULL)
  1307. {
  1308. i->funcs->fileClose(opaque);
  1309. BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
  1310. } /* if */
  1311. memset(fh, '\0', sizeof (FileHandle));
  1312. fh->opaque = opaque;
  1313. fh->forReading = 1;
  1314. fh->dirHandle = i;
  1315. fh->funcs = i->funcs;
  1316. fh->next = openReadList;
  1317. openReadList = fh;
  1318. __PHYSFS_platformReleaseMutex(stateLock);
  1319. return((PHYSFS_File *) fh);
  1320. } /* PHYSFS_openRead */
  1321. static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
  1322. {
  1323. FileHandle *prev = NULL;
  1324. FileHandle *i;
  1325. int rc = 1;
  1326. for (i = *list; i != NULL; i = i->next)
  1327. {
  1328. if (i == handle) /* handle is in this list? */
  1329. {
  1330. PHYSFS_uint8 *tmp = handle->buffer;
  1331. rc = PHYSFS_flush((PHYSFS_File *) handle);
  1332. if (rc)
  1333. rc = handle->funcs->fileClose(handle->opaque);
  1334. if (!rc)
  1335. return(-1);
  1336. if (tmp != NULL) /* free any associated buffer. */
  1337. free(tmp);
  1338. if (prev == NULL)
  1339. *list = handle->next;
  1340. else
  1341. prev->next = handle->next;
  1342. free(handle);
  1343. return(1);
  1344. } /* if */
  1345. prev = i;
  1346. } /* for */
  1347. return(0);
  1348. } /* closeHandleInOpenList */
  1349. int PHYSFS_close(PHYSFS_File *_handle)
  1350. {
  1351. FileHandle *handle = (FileHandle *) _handle;
  1352. int rc;
  1353. __PHYSFS_platformGrabMutex(stateLock);
  1354. /* -1 == close failure. 0 == not found. 1 == success. */
  1355. rc = closeHandleInOpenList(&openReadList, handle);
  1356. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1357. if (!rc)
  1358. {
  1359. rc = closeHandleInOpenList(&openWriteList, handle);
  1360. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1361. } /* if */
  1362. __PHYSFS_platformReleaseMutex(stateLock);
  1363. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1364. return(1);
  1365. } /* PHYSFS_close */
  1366. static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *buffer,
  1367. PHYSFS_uint32 objSize,
  1368. PHYSFS_uint32 objCount)
  1369. {
  1370. PHYSFS_sint64 retval = 0;
  1371. PHYSFS_uint32 remainder = 0;
  1372. while (objCount > 0)
  1373. {
  1374. PHYSFS_uint32 buffered = fh->buffill - fh->bufpos;
  1375. PHYSFS_uint64 mustread = (objSize * objCount) - remainder;
  1376. PHYSFS_uint32 copied;
  1377. if (buffered == 0) /* need to refill buffer? */
  1378. {
  1379. PHYSFS_sint64 rc = fh->funcs->read(fh->opaque, fh->buffer,
  1380. 1, fh->bufsize);
  1381. if (rc <= 0)
  1382. {
  1383. fh->bufpos -= remainder;
  1384. return(((rc == -1) && (retval == 0)) ? -1 : retval);
  1385. } /* if */
  1386. buffered = fh->buffill = (PHYSFS_uint32) rc;
  1387. fh->bufpos = 0;
  1388. } /* if */
  1389. if (buffered > mustread)
  1390. buffered = (PHYSFS_uint32) mustread;
  1391. memcpy(buffer, fh->buffer + fh->bufpos, (size_t) buffered);
  1392. buffer = ((PHYSFS_uint8 *) buffer) + buffered;
  1393. fh->bufpos += buffered;
  1394. buffered += remainder; /* take remainder into account. */
  1395. copied = (buffered / objSize);
  1396. remainder = (buffered % objSize);
  1397. retval += copied;
  1398. objCount -= copied;
  1399. } /* while */
  1400. return(retval);
  1401. } /* doBufferedRead */
  1402. PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
  1403. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1404. {
  1405. FileHandle *fh = (FileHandle *) handle;
  1406. BAIL_IF_MACRO(!fh->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
  1407. if (fh->buffer != NULL)
  1408. return(doBufferedRead(fh, buffer, objSize, objCount));
  1409. return(fh->funcs->read(fh->opaque, buffer, objSize, objCount));
  1410. } /* PHYSFS_read */
  1411. static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
  1412. PHYSFS_uint32 objSize,
  1413. PHYSFS_uint32 objCount)
  1414. {
  1415. FileHandle *fh = (FileHandle *) handle;
  1416. /* whole thing fits in the buffer? */
  1417. if (fh->buffill + (objSize * objCount) < fh->bufsize)
  1418. {
  1419. memcpy(fh->buffer + fh->buffill, buffer, objSize * objCount);
  1420. fh->buffill += (objSize * objCount);
  1421. return(objCount);
  1422. } /* if */
  1423. /* would overflow buffer. Flush and then write the new objects, too. */
  1424. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
  1425. return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
  1426. } /* doBufferedWrite */
  1427. PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
  1428. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1429. {
  1430. FileHandle *fh = (FileHandle *) handle;
  1431. BAIL_IF_MACRO(fh->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
  1432. if (fh->buffer != NULL)
  1433. return(doBufferedWrite(handle, buffer, objSize, objCount));
  1434. return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
  1435. } /* PHYSFS_write */
  1436. int PHYSFS_eof(PHYSFS_File *handle)
  1437. {
  1438. FileHandle *fh = (FileHandle *) handle;
  1439. if (!fh->forReading) /* never EOF on files opened for write/append. */
  1440. return(0);
  1441. /* eof if buffer is empty and archiver says so. */
  1442. return((fh->bufpos == fh->buffill) && (fh->funcs->eof(fh->opaque)));
  1443. } /* PHYSFS_eof */
  1444. PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
  1445. {
  1446. FileHandle *fh = (FileHandle *) handle;
  1447. PHYSFS_sint64 pos = fh->funcs->tell(fh->opaque);
  1448. PHYSFS_sint64 retval = fh->forReading ?
  1449. (pos - fh->buffill) + fh->bufpos :
  1450. (pos + fh->buffill);
  1451. return(retval);
  1452. } /* PHYSFS_tell */
  1453. int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
  1454. {
  1455. FileHandle *fh = (FileHandle *) handle;
  1456. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1457. if (fh->buffer && fh->forReading)
  1458. {
  1459. /* avoid throwing away our precious buffer if seeking within it. */
  1460. PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
  1461. if ( /* seeking within the already-buffered range? */
  1462. ((offset >= 0) && (offset <= fh->buffill - fh->bufpos)) /* fwd */
  1463. || ((offset < 0) && (-offset <= fh->bufpos)) /* backward */ )
  1464. {
  1465. fh->bufpos += offset;
  1466. return(1); /* successful seek */
  1467. } /* if */
  1468. } /* if */
  1469. /* we have to fall back to a 'raw' seek. */
  1470. fh->buffill = fh->bufpos = 0;
  1471. return(fh->funcs->seek(fh->opaque, pos));
  1472. } /* PHYSFS_seek */
  1473. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
  1474. {
  1475. FileHandle *fh = (FileHandle *) handle;
  1476. return(fh->funcs->fileLength(fh->opaque));
  1477. } /* PHYSFS_filelength */
  1478. int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
  1479. {
  1480. FileHandle *fh = (FileHandle *) handle;
  1481. PHYSFS_uint32 bufsize;
  1482. /* !!! FIXME: Unlocalized string. */
  1483. BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
  1484. bufsize = (PHYSFS_uint32) _bufsize;
  1485. BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
  1486. /*
  1487. * For reads, we need to move the file pointer to where it would be
  1488. * if we weren't buffering, so that the next read will get the
  1489. * right chunk of stuff from the file. PHYSFS_flush() handles writes.
  1490. */
  1491. if ((fh->forReading) && (fh->buffill != fh->bufpos))
  1492. {
  1493. PHYSFS_uint64 pos;
  1494. PHYSFS_sint64 curpos = fh->funcs->tell(fh->opaque);
  1495. BAIL_IF_MACRO(curpos == -1, NULL, 0);
  1496. pos = ((curpos - fh->buffill) + fh->bufpos);
  1497. BAIL_IF_MACRO(!fh->funcs->seek(fh->opaque, pos), NULL, 0);
  1498. } /* if */
  1499. if (bufsize == 0) /* delete existing buffer. */
  1500. {
  1501. if (fh->buffer != NULL)
  1502. {
  1503. free(fh->buffer);
  1504. fh->buffer = NULL;
  1505. } /* if */
  1506. } /* if */
  1507. else
  1508. {
  1509. PHYSFS_uint8 *newbuf = realloc(fh->buffer, bufsize);
  1510. BAIL_IF_MACRO(newbuf == NULL, ERR_OUT_OF_MEMORY, 0);
  1511. fh->buffer = newbuf;
  1512. } /* else */
  1513. fh->bufsize = bufsize;
  1514. fh->buffill = fh->bufpos = 0;
  1515. return(1);
  1516. } /* PHYSFS_setBuffer */
  1517. int PHYSFS_flush(PHYSFS_File *handle)
  1518. {
  1519. FileHandle *fh = (FileHandle *) handle;
  1520. PHYSFS_sint64 rc;
  1521. if ((fh->forReading) || (fh->bufpos == fh->buffill))
  1522. return(1); /* open for read or buffer empty are successful no-ops. */
  1523. /* dump buffer to disk. */
  1524. rc = fh->funcs->write(fh->opaque, fh->buffer + fh->bufpos,
  1525. fh->buffill - fh->bufpos, 1);
  1526. BAIL_IF_MACRO(rc <= 0, NULL, 0);
  1527. fh->bufpos = fh->buffill = 0;
  1528. return(1);
  1529. } /* PHYSFS_flush */
  1530. int PHYSFS_setAllocator(PHYSFS_Allocator *a)
  1531. {
  1532. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  1533. externalAllocator = (a != NULL);
  1534. if (externalAllocator)
  1535. memcpy(&allocator, a, sizeof (PHYSFS_Allocator));
  1536. return(1);
  1537. } /* PHYSFS_setAllocator */
  1538. static void setDefaultAllocator(void)
  1539. {
  1540. assert(!externalAllocator);
  1541. allocator.init = __PHYSFS_platformAllocatorInit;
  1542. allocator.deinit = __PHYSFS_platformAllocatorDeinit;
  1543. allocator.malloc = __PHYSFS_platformAllocatorMalloc;
  1544. allocator.realloc = __PHYSFS_platformAllocatorRealloc;
  1545. allocator.free = __PHYSFS_platformAllocatorFree;
  1546. } /* setDefaultAllocator */
  1547. PHYSFS_Allocator *__PHYSFS_getAllocator(void)
  1548. {
  1549. return(&allocator);
  1550. } /* __PHYFS_getAllocator */
  1551. /* end of physfs.c ... */