physfs.c 48 KB

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