physfs.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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 <fcntl.h>
  17. #include <errno.h>
  18. #include <assert.h>
  19. #include "physfs.h"
  20. #define __PHYSICSFS_INTERNAL__
  21. #include "physfs_internal.h"
  22. typedef struct __PHYSFS_ERRMSGTYPE__
  23. {
  24. PHYSFS_uint64 tid;
  25. int errorAvailable;
  26. char errorString[80];
  27. struct __PHYSFS_ERRMSGTYPE__ *next;
  28. } ErrMsg;
  29. typedef struct __PHYSFS_DIRINFO__
  30. {
  31. char *dirName;
  32. DirHandle *dirHandle;
  33. struct __PHYSFS_DIRINFO__ *next;
  34. } PhysDirInfo;
  35. typedef struct __PHYSFS_FILEHANDLELIST__
  36. {
  37. PHYSFS_file handle;
  38. struct __PHYSFS_FILEHANDLELIST__ *next;
  39. } FileHandleList;
  40. /* The various i/o drivers... */
  41. #if (defined PHYSFS_SUPPORTS_ZIP)
  42. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
  43. extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
  44. #endif
  45. #if (defined PHYSFS_SUPPORTS_GRP)
  46. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
  47. extern const DirFunctions __PHYSFS_DirFunctions_GRP;
  48. #endif
  49. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  50. static const PHYSFS_ArchiveInfo *supported_types[] =
  51. {
  52. #if (defined PHYSFS_SUPPORTS_ZIP)
  53. &__PHYSFS_ArchiveInfo_ZIP,
  54. #endif
  55. #if (defined PHYSFS_SUPPORTS_GRP)
  56. &__PHYSFS_ArchiveInfo_GRP,
  57. #endif
  58. NULL
  59. };
  60. static const DirFunctions *dirFunctions[] =
  61. {
  62. #if (defined PHYSFS_SUPPORTS_ZIP)
  63. &__PHYSFS_DirFunctions_ZIP,
  64. #endif
  65. #if (defined PHYSFS_SUPPORTS_GRP)
  66. &__PHYSFS_DirFunctions_GRP,
  67. #endif
  68. &__PHYSFS_DirFunctions_DIR,
  69. NULL
  70. };
  71. /* General PhysicsFS state ... */
  72. static int initialized = 0;
  73. static ErrMsg *errorMessages = NULL;
  74. static PhysDirInfo *searchPath = NULL;
  75. static PhysDirInfo *writeDir = NULL;
  76. static FileHandleList *openWriteList = NULL;
  77. static FileHandleList *openReadList = NULL;
  78. static char *baseDir = NULL;
  79. static char *userDir = NULL;
  80. static int allowSymLinks = 0;
  81. /* mutexes ... */
  82. static void *errorLock = NULL; /* protects error message list. */
  83. static void *stateLock = NULL; /* protects other PhysFS static state. */
  84. /* functions ... */
  85. static ErrMsg *findErrorForCurrentThread(void)
  86. {
  87. ErrMsg *i;
  88. PHYSFS_uint64 tid;
  89. if (initialized)
  90. __PHYSFS_platformGrabMutex(errorLock);
  91. if (errorMessages != NULL)
  92. {
  93. tid = __PHYSFS_platformGetThreadID();
  94. for (i = errorMessages; i != NULL; i = i->next)
  95. {
  96. if (i->tid == tid)
  97. {
  98. __PHYSFS_platformReleaseMutex(errorLock);
  99. return(i);
  100. } /* if */
  101. } /* for */
  102. } /* if */
  103. if (initialized)
  104. __PHYSFS_platformReleaseMutex(errorLock);
  105. return(NULL); /* no error available. */
  106. } /* findErrorForCurrentThread */
  107. void __PHYSFS_setError(const char *str)
  108. {
  109. ErrMsg *err;
  110. if (str == NULL)
  111. return;
  112. err = findErrorForCurrentThread();
  113. if (err == NULL)
  114. {
  115. err = (ErrMsg *) malloc(sizeof (ErrMsg));
  116. if (err == NULL)
  117. return; /* uhh...? */
  118. memset((void *) err, '\0', sizeof (ErrMsg));
  119. err->tid = __PHYSFS_platformGetThreadID();
  120. __PHYSFS_platformGrabMutex(errorLock);
  121. err->next = errorMessages;
  122. errorMessages = err;
  123. __PHYSFS_platformReleaseMutex(errorLock);
  124. } /* if */
  125. err->errorAvailable = 1;
  126. strncpy(err->errorString, str, sizeof (err->errorString));
  127. err->errorString[sizeof (err->errorString) - 1] = '\0';
  128. } /* __PHYSFS_setError */
  129. const char *PHYSFS_getLastError(void)
  130. {
  131. ErrMsg *err = findErrorForCurrentThread();
  132. if ((err == NULL) || (!err->errorAvailable))
  133. return(NULL);
  134. err->errorAvailable = 0;
  135. return(err->errorString);
  136. } /* PHYSFS_getLastError */
  137. /* MAKE SURE that errorLock is held before calling this! */
  138. static void freeErrorMessages(void)
  139. {
  140. ErrMsg *i;
  141. ErrMsg *next;
  142. for (i = errorMessages; i != NULL; i = next)
  143. {
  144. next = i->next;
  145. free(i);
  146. } /* for */
  147. } /* freeErrorMessages */
  148. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  149. {
  150. if (ver != NULL)
  151. {
  152. ver->major = PHYSFS_VER_MAJOR;
  153. ver->minor = PHYSFS_VER_MINOR;
  154. ver->patch = PHYSFS_VER_PATCH;
  155. } /* if */
  156. } /* PHYSFS_getLinkedVersion */
  157. static DirHandle *openDirectory(const char *d, int forWriting)
  158. {
  159. const DirFunctions **i;
  160. BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
  161. for (i = dirFunctions; *i != NULL; i++)
  162. {
  163. if ((*i)->isArchive(d, forWriting))
  164. return( (*i)->openArchive(d, forWriting) );
  165. } /* for */
  166. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  167. return(NULL);
  168. } /* openDirectory */
  169. static PhysDirInfo *buildDirInfo(const char *newDir, int forWriting)
  170. {
  171. DirHandle *dirHandle = NULL;
  172. PhysDirInfo *di = NULL;
  173. BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
  174. dirHandle = openDirectory(newDir, forWriting);
  175. BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
  176. di = (PhysDirInfo *) malloc(sizeof (PhysDirInfo));
  177. if (di == NULL)
  178. {
  179. dirHandle->funcs->dirClose(dirHandle);
  180. BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
  181. } /* if */
  182. di->dirName = (char *) malloc(strlen(newDir) + 1);
  183. if (di->dirName == NULL)
  184. {
  185. free(di);
  186. dirHandle->funcs->dirClose(dirHandle);
  187. BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
  188. } /* if */
  189. di->next = NULL;
  190. di->dirHandle = dirHandle;
  191. strcpy(di->dirName, newDir);
  192. return(di);
  193. } /* buildDirInfo */
  194. /* MAKE SURE you've got the stateLock held before calling this! */
  195. static int freeDirInfo(PhysDirInfo *di, FileHandleList *openList)
  196. {
  197. FileHandleList *i;
  198. if (di == NULL)
  199. return(1);
  200. for (i = openList; i != NULL; i = i->next)
  201. {
  202. const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
  203. BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
  204. } /* for */
  205. di->dirHandle->funcs->dirClose(di->dirHandle);
  206. free(di->dirName);
  207. free(di);
  208. return(1);
  209. } /* freeDirInfo */
  210. static char *calculateUserDir(void)
  211. {
  212. char *retval = NULL;
  213. const char *str = NULL;
  214. str = __PHYSFS_platformGetUserDir();
  215. if (str != NULL)
  216. retval = (char *) str;
  217. else
  218. {
  219. const char *dirsep = PHYSFS_getDirSeparator();
  220. const char *uname = __PHYSFS_platformGetUserName();
  221. str = (uname != NULL) ? uname : "default";
  222. retval = (char *) malloc(strlen(baseDir) + strlen(str) +
  223. strlen(dirsep) + 6);
  224. if (retval == NULL)
  225. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  226. else
  227. sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
  228. if (uname != NULL)
  229. free((void *) uname);
  230. } /* else */
  231. return(retval);
  232. } /* calculateUserDir */
  233. static int appendDirSep(char **dir)
  234. {
  235. const char *dirsep = PHYSFS_getDirSeparator();
  236. char *ptr;
  237. if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
  238. return(1);
  239. ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
  240. if (!ptr)
  241. {
  242. free(*dir);
  243. return(0);
  244. } /* if */
  245. strcat(ptr, dirsep);
  246. *dir = ptr;
  247. return(1);
  248. } /* appendDirSep */
  249. static char *calculateBaseDir(const char *argv0)
  250. {
  251. const char *dirsep = PHYSFS_getDirSeparator();
  252. char *retval;
  253. char *ptr;
  254. /*
  255. * See if the platform driver wants to handle this for us...
  256. */
  257. retval = __PHYSFS_platformCalcBaseDir(argv0);
  258. if (retval != NULL)
  259. return(retval);
  260. /*
  261. * Determine if there's a path on argv0. If there is, that's the base dir.
  262. */
  263. ptr = strstr(argv0, dirsep);
  264. if (ptr != NULL)
  265. {
  266. char *p = ptr;
  267. size_t size;
  268. while (p != NULL)
  269. {
  270. ptr = p;
  271. p = strstr(p + 1, dirsep);
  272. } /* while */
  273. size = (size_t) (ptr - argv0); /* !!! is this portable? */
  274. retval = (char *) malloc(size + 1);
  275. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  276. memcpy(retval, argv0, size);
  277. retval[size] = '\0';
  278. return(retval);
  279. } /* if */
  280. /*
  281. * Last ditch effort: it's the current working directory. (*shrug*)
  282. */
  283. retval = __PHYSFS_platformCurrentDir();
  284. if(retval != NULL) {
  285. return(retval);
  286. }
  287. /*
  288. * Ok, current directory doesn't exist, use the root directory.
  289. * Not a good alternative, but it only happens if the current
  290. * directory was deleted from under the program.
  291. */
  292. retval = (char *) malloc(strlen(dirsep) + 1);
  293. strcpy(retval, dirsep);
  294. return(retval);
  295. } /* calculateBaseDir */
  296. static int initializeMutexes(void)
  297. {
  298. errorLock = __PHYSFS_platformCreateMutex();
  299. if (errorLock == NULL)
  300. goto initializeMutexes_failed;
  301. stateLock = __PHYSFS_platformCreateMutex();
  302. if (stateLock == NULL)
  303. goto initializeMutexes_failed;
  304. return(1); /* success. */
  305. initializeMutexes_failed:
  306. if (errorLock != NULL)
  307. __PHYSFS_platformDestroyMutex(errorLock);
  308. if (stateLock != NULL)
  309. __PHYSFS_platformDestroyMutex(stateLock);
  310. errorLock = stateLock = NULL;
  311. return(0); /* failed. */
  312. } /* initializeMutexes */
  313. int PHYSFS_init(const char *argv0)
  314. {
  315. char *ptr;
  316. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  317. BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
  318. BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
  319. baseDir = calculateBaseDir(argv0);
  320. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  321. ptr = __PHYSFS_platformRealPath(baseDir);
  322. free(baseDir);
  323. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  324. baseDir = ptr;
  325. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  326. userDir = calculateUserDir();
  327. if (userDir != NULL)
  328. {
  329. ptr = __PHYSFS_platformRealPath(userDir);
  330. free(userDir);
  331. userDir = ptr;
  332. } /* if */
  333. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  334. {
  335. free(baseDir);
  336. baseDir = NULL;
  337. return(0);
  338. } /* if */
  339. initialized = 1;
  340. /* This makes sure that the error subsystem is initialized. */
  341. __PHYSFS_setError(PHYSFS_getLastError());
  342. return(1);
  343. } /* PHYSFS_init */
  344. /* MAKE SURE you hold stateLock before calling this! */
  345. static int closeFileHandleList(FileHandleList **list)
  346. {
  347. FileHandleList *i;
  348. FileHandleList *next = NULL;
  349. FileHandle *h;
  350. for (i = *list; i != NULL; i = next)
  351. {
  352. next = i->next;
  353. h = (FileHandle *) (i->handle.opaque);
  354. if (!h->funcs->fileClose(h))
  355. {
  356. *list = i;
  357. return(0);
  358. } /* if */
  359. free(i);
  360. } /* for */
  361. *list = NULL;
  362. return(1);
  363. } /* closeFileHandleList */
  364. /* MAKE SURE you hold the stateLock before calling this! */
  365. static void freeSearchPath(void)
  366. {
  367. PhysDirInfo *i;
  368. PhysDirInfo *next = NULL;
  369. closeFileHandleList(&openReadList);
  370. if (searchPath != NULL)
  371. {
  372. for (i = searchPath; i != NULL; i = next)
  373. {
  374. next = i->next;
  375. freeDirInfo(i, openReadList);
  376. } /* for */
  377. searchPath = NULL;
  378. } /* if */
  379. } /* freeSearchPath */
  380. int PHYSFS_deinit(void)
  381. {
  382. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  383. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
  384. closeFileHandleList(&openWriteList);
  385. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  386. freeSearchPath();
  387. freeErrorMessages();
  388. if (baseDir != NULL)
  389. {
  390. free(baseDir);
  391. baseDir = NULL;
  392. } /* if */
  393. if (userDir != NULL)
  394. {
  395. free(userDir);
  396. userDir = NULL;
  397. } /* if */
  398. allowSymLinks = 0;
  399. initialized = 0;
  400. __PHYSFS_platformDestroyMutex(errorLock);
  401. __PHYSFS_platformDestroyMutex(stateLock);
  402. errorLock = stateLock = NULL;
  403. return(1);
  404. } /* PHYSFS_deinit */
  405. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  406. {
  407. return(supported_types);
  408. } /* PHYSFS_supportedArchiveTypes */
  409. void PHYSFS_freeList(void *list)
  410. {
  411. void **i;
  412. for (i = (void **) list; *i != NULL; i++)
  413. free(*i);
  414. free(list);
  415. } /* PHYSFS_freeList */
  416. const char *PHYSFS_getDirSeparator(void)
  417. {
  418. return(__PHYSFS_platformDirSeparator);
  419. } /* PHYSFS_getDirSeparator */
  420. char **PHYSFS_getCdRomDirs(void)
  421. {
  422. return(__PHYSFS_platformDetectAvailableCDs());
  423. } /* PHYSFS_getCdRomDirs */
  424. const char *PHYSFS_getBaseDir(void)
  425. {
  426. return(baseDir); /* this is calculated in PHYSFS_init()... */
  427. } /* PHYSFS_getBaseDir */
  428. const char *PHYSFS_getUserDir(void)
  429. {
  430. return(userDir); /* this is calculated in PHYSFS_init()... */
  431. } /* PHYSFS_getUserDir */
  432. const char *PHYSFS_getWriteDir(void)
  433. {
  434. const char *retval = NULL;
  435. __PHYSFS_platformGrabMutex(stateLock);
  436. if (writeDir != NULL)
  437. retval = writeDir->dirName;
  438. __PHYSFS_platformReleaseMutex(stateLock);
  439. return(retval);
  440. } /* PHYSFS_getWriteDir */
  441. int PHYSFS_setWriteDir(const char *newDir)
  442. {
  443. int retval = 1;
  444. __PHYSFS_platformGrabMutex(stateLock);
  445. if (writeDir != NULL)
  446. {
  447. BAIL_IF_MACRO_MUTEX(!freeDirInfo(writeDir, openWriteList), NULL,
  448. stateLock, 0);
  449. writeDir = NULL;
  450. } /* if */
  451. if (newDir != NULL)
  452. {
  453. writeDir = buildDirInfo(newDir, 1);
  454. retval = (writeDir != NULL);
  455. } /* if */
  456. __PHYSFS_platformReleaseMutex(stateLock);
  457. return(retval);
  458. } /* PHYSFS_setWriteDir */
  459. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  460. {
  461. PhysDirInfo *di;
  462. PhysDirInfo *prev = NULL;
  463. PhysDirInfo *i;
  464. __PHYSFS_platformGrabMutex(stateLock);
  465. for (i = searchPath; i != NULL; i = i->next)
  466. {
  467. /* already in search path? */
  468. BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
  469. prev = i;
  470. } /* for */
  471. di = buildDirInfo(newDir, 0);
  472. BAIL_IF_MACRO_MUTEX(di == NULL, NULL, stateLock, 0);
  473. if (appendToPath)
  474. {
  475. di->next = NULL;
  476. if (prev == NULL)
  477. searchPath = di;
  478. else
  479. prev->next = di;
  480. } /* if */
  481. else
  482. {
  483. di->next = searchPath;
  484. searchPath = di;
  485. } /* else */
  486. __PHYSFS_platformReleaseMutex(stateLock);
  487. return(1);
  488. } /* PHYSFS_addToSearchPath */
  489. int PHYSFS_removeFromSearchPath(const char *oldDir)
  490. {
  491. PhysDirInfo *i;
  492. PhysDirInfo *prev = NULL;
  493. PhysDirInfo *next = NULL;
  494. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  495. __PHYSFS_platformGrabMutex(stateLock);
  496. for (i = searchPath; i != NULL; i = i->next)
  497. {
  498. if (strcmp(i->dirName, oldDir) == 0)
  499. {
  500. next = i->next;
  501. BAIL_IF_MACRO_MUTEX(!freeDirInfo(i, openReadList), NULL,
  502. stateLock, 0);
  503. if (prev == NULL)
  504. searchPath = next;
  505. else
  506. prev->next = next;
  507. BAIL_MACRO_MUTEX(NULL, stateLock, 1);
  508. } /* if */
  509. prev = i;
  510. } /* for */
  511. BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
  512. } /* PHYSFS_removeFromSearchPath */
  513. char **PHYSFS_getSearchPath(void)
  514. {
  515. int count = 1;
  516. int x;
  517. PhysDirInfo *i;
  518. char **retval;
  519. __PHYSFS_platformGrabMutex(stateLock);
  520. for (i = searchPath; i != NULL; i = i->next)
  521. count++;
  522. retval = (char **) malloc(sizeof (char *) * count);
  523. BAIL_IF_MACRO_MUTEX(!retval, ERR_OUT_OF_MEMORY, stateLock, NULL);
  524. count--;
  525. retval[count] = NULL;
  526. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  527. {
  528. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  529. if (retval[x] == NULL) /* this is friggin' ugly. */
  530. {
  531. while (x > 0)
  532. {
  533. x--;
  534. free(retval[x]);
  535. } /* while */
  536. free(retval);
  537. BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
  538. } /* if */
  539. strcpy(retval[x], i->dirName);
  540. } /* for */
  541. __PHYSFS_platformReleaseMutex(stateLock);
  542. return(retval);
  543. } /* PHYSFS_getSearchPath */
  544. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  545. const char *archiveExt, int includeCdRoms,
  546. int archivesFirst)
  547. {
  548. const char *basedir = PHYSFS_getBaseDir();
  549. const char *userdir = PHYSFS_getUserDir();
  550. const char *dirsep = PHYSFS_getDirSeparator();
  551. char *str;
  552. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  553. /* set write dir... */
  554. str = malloc(strlen(userdir) + (strlen(organization) * 2) +
  555. (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
  556. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  557. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  558. if (!PHYSFS_setWriteDir(str))
  559. {
  560. int no_write = 0;
  561. sprintf(str, ".%s/%s", organization, appName);
  562. if ( (PHYSFS_setWriteDir(userdir)) &&
  563. (PHYSFS_mkdir(str)) )
  564. {
  565. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  566. if (!PHYSFS_setWriteDir(str))
  567. no_write = 1;
  568. } /* if */
  569. else
  570. {
  571. no_write = 1;
  572. } /* else */
  573. if (no_write)
  574. {
  575. PHYSFS_setWriteDir(NULL); /* just in case. */
  576. free(str);
  577. BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
  578. } /* if */
  579. } /* if */
  580. /* Put write dir first in search path... */
  581. PHYSFS_addToSearchPath(str, 0);
  582. free(str);
  583. /* Put base path on search path... */
  584. PHYSFS_addToSearchPath(basedir, 1);
  585. /* handle CD-ROMs... */
  586. if (includeCdRoms)
  587. {
  588. char **cds = PHYSFS_getCdRomDirs();
  589. char **i;
  590. for (i = cds; *i != NULL; i++)
  591. PHYSFS_addToSearchPath(*i, 1);
  592. PHYSFS_freeList(cds);
  593. } /* if */
  594. /* Root out archives, and add them to search path... */
  595. if (archiveExt != NULL)
  596. {
  597. char **rc = PHYSFS_enumerateFiles("");
  598. char **i;
  599. size_t extlen = strlen(archiveExt);
  600. char *ext;
  601. for (i = rc; *i != NULL; i++)
  602. {
  603. size_t l = strlen(*i);
  604. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  605. {
  606. ext = (*i) + (l - extlen);
  607. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  608. {
  609. const char *d = PHYSFS_getRealDir(*i);
  610. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  611. if (str != NULL)
  612. {
  613. sprintf(str, "%s%s%s", d, dirsep, *i);
  614. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  615. free(str);
  616. } /* if */
  617. } /* if */
  618. } /* if */
  619. } /* for */
  620. PHYSFS_freeList(rc);
  621. } /* if */
  622. return(1);
  623. } /* PHYSFS_setSaneConfig */
  624. void PHYSFS_permitSymbolicLinks(int allow)
  625. {
  626. allowSymLinks = allow;
  627. } /* PHYSFS_permitSymbolicLinks */
  628. /* string manipulation in C makes my ass itch. */
  629. char * __PHYSFS_convertToDependent(const char *prepend,
  630. const char *dirName,
  631. const char *append)
  632. {
  633. const char *dirsep = __PHYSFS_platformDirSeparator;
  634. size_t sepsize = strlen(dirsep);
  635. char *str;
  636. char *i1;
  637. char *i2;
  638. size_t allocSize;
  639. while (*dirName == '/')
  640. dirName++;
  641. allocSize = strlen(dirName) + 1;
  642. if (prepend != NULL)
  643. allocSize += strlen(prepend) + sepsize;
  644. if (append != NULL)
  645. allocSize += strlen(append) + sepsize;
  646. /* make sure there's enough space if the dir separator is bigger. */
  647. if (sepsize > 1)
  648. {
  649. str = (char *) dirName;
  650. do
  651. {
  652. str = strchr(str, '/');
  653. if (str != NULL)
  654. {
  655. allocSize += (sepsize - 1);
  656. str++;
  657. } /* if */
  658. } while (str != NULL);
  659. } /* if */
  660. str = (char *) malloc(allocSize);
  661. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  662. if (prepend == NULL)
  663. *str = '\0';
  664. else
  665. {
  666. strcpy(str, prepend);
  667. strcat(str, dirsep);
  668. } /* else */
  669. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  670. {
  671. if (*i1 == '/')
  672. {
  673. strcpy(i2, dirsep);
  674. i2 += sepsize;
  675. } /* if */
  676. else
  677. {
  678. *i2 = *i1;
  679. } /* else */
  680. } /* for */
  681. *i2 = '\0';
  682. if (append)
  683. {
  684. strcat(str, dirsep);
  685. strcpy(str, append);
  686. } /* if */
  687. return(str);
  688. } /* __PHYSFS_convertToDependent */
  689. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
  690. {
  691. int retval = 1;
  692. char *start;
  693. char *end;
  694. char *str;
  695. start = str = malloc(strlen(fname) + 1);
  696. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  697. strcpy(str, fname);
  698. while (1)
  699. {
  700. end = strchr(start, '/');
  701. if (end != NULL)
  702. *end = '\0';
  703. if ( (strcmp(start, ".") == 0) ||
  704. (strcmp(start, "..") == 0) ||
  705. (strchr(start, '\\') != NULL) ||
  706. (strchr(start, ':') != NULL) )
  707. {
  708. __PHYSFS_setError(ERR_INSECURE_FNAME);
  709. retval = 0;
  710. break;
  711. } /* if */
  712. if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
  713. {
  714. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  715. retval = 0;
  716. break;
  717. } /* if */
  718. if (end == NULL)
  719. break;
  720. *end = '/';
  721. start = end + 1;
  722. } /* while */
  723. free(str);
  724. return(retval);
  725. } /* __PHYSFS_verifySecurity */
  726. int PHYSFS_mkdir(const char *dname)
  727. {
  728. DirHandle *h;
  729. char *str;
  730. char *start;
  731. char *end;
  732. int retval = 0;
  733. BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
  734. while (*dname == '/')
  735. dname++;
  736. __PHYSFS_platformGrabMutex(stateLock);
  737. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  738. h = writeDir->dirHandle;
  739. BAIL_IF_MACRO_MUTEX(!h->funcs->mkdir, ERR_NOT_SUPPORTED, stateLock, 0);
  740. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, dname), NULL, stateLock, 0);
  741. start = str = malloc(strlen(dname) + 1);
  742. BAIL_IF_MACRO_MUTEX(str == NULL, ERR_OUT_OF_MEMORY, stateLock, 0);
  743. strcpy(str, dname);
  744. while (1)
  745. {
  746. end = strchr(start, '/');
  747. if (end != NULL)
  748. *end = '\0';
  749. retval = h->funcs->mkdir(h, str);
  750. if (!retval)
  751. break;
  752. if (end == NULL)
  753. break;
  754. *end = '/';
  755. start = end + 1;
  756. } /* while */
  757. __PHYSFS_platformReleaseMutex(stateLock);
  758. free(str);
  759. return(retval);
  760. } /* PHYSFS_mkdir */
  761. int PHYSFS_delete(const char *fname)
  762. {
  763. int retval;
  764. DirHandle *h;
  765. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  766. while (*fname == '/')
  767. fname++;
  768. __PHYSFS_platformGrabMutex(stateLock);
  769. BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
  770. h = writeDir->dirHandle;
  771. BAIL_IF_MACRO_MUTEX(!h->funcs->remove, ERR_NOT_SUPPORTED, stateLock, 0);
  772. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL, stateLock, 0);
  773. retval = h->funcs->remove(h, fname);
  774. __PHYSFS_platformReleaseMutex(stateLock);
  775. return(retval);
  776. } /* PHYSFS_delete */
  777. const char *PHYSFS_getRealDir(const char *filename)
  778. {
  779. PhysDirInfo *i;
  780. while (*filename == '/')
  781. filename++;
  782. __PHYSFS_platformGrabMutex(stateLock);
  783. for (i = searchPath; i != NULL; i = i->next)
  784. {
  785. DirHandle *h = i->dirHandle;
  786. if (__PHYSFS_verifySecurity(h, filename))
  787. {
  788. if (h->funcs->exists(h, filename))
  789. {
  790. __PHYSFS_platformReleaseMutex(stateLock);
  791. return(i->dirName);
  792. } /* if */
  793. } /* if */
  794. } /* for */
  795. __PHYSFS_platformReleaseMutex(stateLock);
  796. return(NULL);
  797. } /* PHYSFS_getRealDir */
  798. static int countList(LinkedStringList *list)
  799. {
  800. int retval = 0;
  801. LinkedStringList *i;
  802. for (i = list; i != NULL; i = i->next)
  803. retval++;
  804. return(retval);
  805. } /* countList */
  806. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  807. {
  808. int i;
  809. LinkedStringList *next = NULL;
  810. int len = countList(finalList);
  811. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  812. if (retval == NULL)
  813. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  814. for (i = 0; i < len; i++)
  815. {
  816. next = finalList->next;
  817. if (retval == NULL)
  818. free(finalList->str);
  819. else
  820. retval[i] = finalList->str;
  821. free(finalList);
  822. finalList = next;
  823. } /* for */
  824. if (retval != NULL)
  825. retval[i] = NULL;
  826. return(retval);
  827. } /* convertStringListToPhysFSList */
  828. static void insertStringListItem(LinkedStringList **final,
  829. LinkedStringList *item)
  830. {
  831. LinkedStringList *i;
  832. LinkedStringList *prev = NULL;
  833. int rc;
  834. for (i = *final; i != NULL; i = i->next)
  835. {
  836. rc = strcmp(i->str, item->str);
  837. if (rc > 0) /* insertion point. */
  838. break;
  839. else if (rc == 0) /* already in list. */
  840. {
  841. free(item->str);
  842. free(item);
  843. return;
  844. } /* else if */
  845. prev = i;
  846. } /* for */
  847. /*
  848. * If we are here, we are either at the insertion point.
  849. * This may be the end of the list, or the list may be empty, too.
  850. */
  851. if (prev == NULL)
  852. *final = item;
  853. else
  854. prev->next = item;
  855. item->next = i;
  856. } /* insertStringListItem */
  857. /* if we run out of memory anywhere in here, we give back what we can. */
  858. static void interpolateStringLists(LinkedStringList **final,
  859. LinkedStringList *newList)
  860. {
  861. LinkedStringList *next = NULL;
  862. while (newList != NULL)
  863. {
  864. next = newList->next;
  865. insertStringListItem(final, newList);
  866. newList = next;
  867. } /* while */
  868. } /* interpolateStringLists */
  869. char **PHYSFS_enumerateFiles(const char *path)
  870. {
  871. PhysDirInfo *i;
  872. char **retval = NULL;
  873. LinkedStringList *rc;
  874. LinkedStringList *finalList = NULL;
  875. int omitSymLinks = !allowSymLinks;
  876. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  877. while (*path == '/')
  878. path++;
  879. __PHYSFS_platformGrabMutex(stateLock);
  880. for (i = searchPath; i != NULL; i = i->next)
  881. {
  882. DirHandle *h = i->dirHandle;
  883. if (__PHYSFS_verifySecurity(h, path))
  884. {
  885. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  886. interpolateStringLists(&finalList, rc);
  887. } /* if */
  888. } /* for */
  889. __PHYSFS_platformReleaseMutex(stateLock);
  890. retval = convertStringListToPhysFSList(finalList);
  891. return(retval);
  892. } /* PHYSFS_enumerateFiles */
  893. int PHYSFS_exists(const char *fname)
  894. {
  895. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  896. while (*fname == '/')
  897. fname++;
  898. return(PHYSFS_getRealDir(fname) != NULL);
  899. } /* PHYSFS_exists */
  900. int PHYSFS_isDirectory(const char *fname)
  901. {
  902. PhysDirInfo *i;
  903. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  904. while (*fname == '/')
  905. fname++;
  906. if (*fname == '\0')
  907. return(1);
  908. __PHYSFS_platformGrabMutex(stateLock);
  909. for (i = searchPath; i != NULL; i = i->next)
  910. {
  911. DirHandle *h = i->dirHandle;
  912. if (__PHYSFS_verifySecurity(h, fname))
  913. {
  914. if (h->funcs->exists(h, fname))
  915. {
  916. int retval = h->funcs->isDirectory(h, fname);
  917. __PHYSFS_platformReleaseMutex(stateLock);
  918. return(retval);
  919. } /* if */
  920. } /* if */
  921. } /* for */
  922. __PHYSFS_platformReleaseMutex(stateLock);
  923. return(0);
  924. } /* PHYSFS_isDirectory */
  925. int PHYSFS_isSymbolicLink(const char *fname)
  926. {
  927. PhysDirInfo *i;
  928. if (!allowSymLinks)
  929. return(0);
  930. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  931. while (*fname == '/')
  932. fname++;
  933. __PHYSFS_platformGrabMutex(stateLock);
  934. for (i = searchPath; i != NULL; i = i->next)
  935. {
  936. DirHandle *h = i->dirHandle;
  937. if (__PHYSFS_verifySecurity(h, fname))
  938. {
  939. if (h->funcs->exists(h, fname))
  940. {
  941. int retval = h->funcs->isSymLink(h, fname);
  942. __PHYSFS_platformReleaseMutex(stateLock);
  943. return(retval);
  944. } /* if */
  945. } /* if */
  946. } /* for */
  947. /* !!! FIXME: setError ERR_FILE_NOT_FOUND? */
  948. __PHYSFS_platformReleaseMutex(stateLock);
  949. return(0);
  950. } /* PHYSFS_isSymbolicLink */
  951. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  952. {
  953. PHYSFS_file *retval = NULL;
  954. FileHandle *rc = NULL;
  955. DirHandle *h;
  956. const DirFunctions *f;
  957. FileHandleList *list;
  958. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  959. while (*fname == '/')
  960. fname++;
  961. __PHYSFS_platformGrabMutex(stateLock);
  962. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  963. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  964. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL,
  965. stateLock, NULL);
  966. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  967. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  968. f = h->funcs;
  969. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  970. if (rc == NULL)
  971. free(list);
  972. else
  973. {
  974. list->handle.opaque = (void *) rc;
  975. list->next = openWriteList;
  976. openWriteList = list;
  977. retval = &(list->handle);
  978. } /* else */
  979. __PHYSFS_platformReleaseMutex(stateLock);
  980. return(retval);
  981. } /* doOpenWrite */
  982. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  983. {
  984. return(doOpenWrite(filename, 0));
  985. } /* PHYSFS_openWrite */
  986. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  987. {
  988. return(doOpenWrite(filename, 1));
  989. } /* PHYSFS_openAppend */
  990. PHYSFS_file *PHYSFS_openRead(const char *fname)
  991. {
  992. PHYSFS_file *retval;
  993. FileHandle *rc = NULL;
  994. FileHandleList *list;
  995. PhysDirInfo *i;
  996. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  997. while (*fname == '/')
  998. fname++;
  999. __PHYSFS_platformGrabMutex(stateLock);
  1000. for (i = searchPath; i != NULL; i = i->next)
  1001. {
  1002. DirHandle *h = i->dirHandle;
  1003. if (__PHYSFS_verifySecurity(h, fname))
  1004. {
  1005. rc = h->funcs->openRead(h, fname);
  1006. if (rc != NULL)
  1007. break;
  1008. } /* if */
  1009. } /* for */
  1010. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1011. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1012. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  1013. list->handle.opaque = (void *) rc;
  1014. list->next = openReadList;
  1015. openReadList = list;
  1016. retval = &(list->handle);
  1017. __PHYSFS_platformReleaseMutex(stateLock);
  1018. return(retval);
  1019. } /* PHYSFS_openRead */
  1020. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1021. {
  1022. FileHandle *h = (FileHandle *) handle->opaque;
  1023. FileHandleList *prev = NULL;
  1024. FileHandleList *i;
  1025. int rc;
  1026. for (i = *list; i != NULL; i = i->next)
  1027. {
  1028. if (&i->handle == handle) /* handle is in this list? */
  1029. {
  1030. rc = h->funcs->fileClose(h);
  1031. if (!rc)
  1032. return(-1);
  1033. if (prev == NULL)
  1034. *list = i->next;
  1035. else
  1036. prev->next = i->next;
  1037. free(i);
  1038. return(1);
  1039. } /* if */
  1040. prev = i;
  1041. } /* for */
  1042. return(0);
  1043. } /* closeHandleInOpenList */
  1044. int PHYSFS_close(PHYSFS_file *handle)
  1045. {
  1046. int rc;
  1047. __PHYSFS_platformGrabMutex(stateLock);
  1048. /* -1 == close failure. 0 == not found. 1 == success. */
  1049. rc = closeHandleInOpenList(&openReadList, handle);
  1050. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1051. if (!rc)
  1052. {
  1053. rc = closeHandleInOpenList(&openWriteList, handle);
  1054. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1055. } /* if */
  1056. __PHYSFS_platformReleaseMutex(stateLock);
  1057. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1058. return(1);
  1059. } /* PHYSFS_close */
  1060. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1061. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1062. {
  1063. FileHandle *h = (FileHandle *) handle->opaque;
  1064. assert(h != NULL);
  1065. assert(h->funcs != NULL);
  1066. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  1067. return(h->funcs->read(h, buffer, objSize, objCount));
  1068. } /* PHYSFS_read */
  1069. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1070. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1071. {
  1072. FileHandle *h = (FileHandle *) handle->opaque;
  1073. assert(h != NULL);
  1074. assert(h->funcs != NULL);
  1075. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  1076. return(h->funcs->write(h, buffer, objSize, objCount));
  1077. } /* PHYSFS_write */
  1078. int PHYSFS_eof(PHYSFS_file *handle)
  1079. {
  1080. FileHandle *h = (FileHandle *) handle->opaque;
  1081. assert(h != NULL);
  1082. assert(h->funcs != NULL);
  1083. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  1084. return(h->funcs->eof(h));
  1085. } /* PHYSFS_eof */
  1086. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1087. {
  1088. FileHandle *h = (FileHandle *) handle->opaque;
  1089. assert(h != NULL);
  1090. assert(h->funcs != NULL);
  1091. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  1092. return(h->funcs->tell(h));
  1093. } /* PHYSFS_tell */
  1094. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1095. {
  1096. FileHandle *h = (FileHandle *) handle->opaque;
  1097. assert(h != NULL);
  1098. assert(h->funcs != NULL);
  1099. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1100. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  1101. return(h->funcs->seek(h, pos));
  1102. } /* PHYSFS_seek */
  1103. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1104. {
  1105. FileHandle *h = (FileHandle *) handle->opaque;
  1106. assert(h != NULL);
  1107. assert(h->funcs != NULL);
  1108. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1109. return(h->funcs->fileLength(h));
  1110. } /* PHYSFS_filelength */
  1111. /* end of physfs.c ... */