physfs.c 35 KB

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