physfs.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457
  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);
  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. __PHYSFS_setError(ERR_NO_SUCH_FILE);
  790. else
  791. {
  792. __PHYSFS_platformReleaseMutex(stateLock);
  793. return(i->dirName);
  794. } /* else */
  795. } /* if */
  796. } /* for */
  797. __PHYSFS_platformReleaseMutex(stateLock);
  798. return(NULL);
  799. } /* PHYSFS_getRealDir */
  800. static int countList(LinkedStringList *list)
  801. {
  802. int retval = 0;
  803. LinkedStringList *i;
  804. for (i = list; i != NULL; i = i->next)
  805. retval++;
  806. return(retval);
  807. } /* countList */
  808. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  809. {
  810. int i;
  811. LinkedStringList *next = NULL;
  812. int len = countList(finalList);
  813. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  814. if (retval == NULL)
  815. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  816. for (i = 0; i < len; i++)
  817. {
  818. next = finalList->next;
  819. if (retval == NULL)
  820. free(finalList->str);
  821. else
  822. retval[i] = finalList->str;
  823. free(finalList);
  824. finalList = next;
  825. } /* for */
  826. if (retval != NULL)
  827. retval[i] = NULL;
  828. return(retval);
  829. } /* convertStringListToPhysFSList */
  830. static void insertStringListItem(LinkedStringList **final,
  831. LinkedStringList *item)
  832. {
  833. LinkedStringList *i;
  834. LinkedStringList *prev = NULL;
  835. int rc;
  836. for (i = *final; i != NULL; i = i->next)
  837. {
  838. rc = strcmp(i->str, item->str);
  839. if (rc > 0) /* insertion point. */
  840. break;
  841. else if (rc == 0) /* already in list. */
  842. {
  843. free(item->str);
  844. free(item);
  845. return;
  846. } /* else if */
  847. prev = i;
  848. } /* for */
  849. /*
  850. * If we are here, we are either at the insertion point.
  851. * This may be the end of the list, or the list may be empty, too.
  852. */
  853. if (prev == NULL)
  854. *final = item;
  855. else
  856. prev->next = item;
  857. item->next = i;
  858. } /* insertStringListItem */
  859. /* if we run out of memory anywhere in here, we give back what we can. */
  860. static void interpolateStringLists(LinkedStringList **final,
  861. LinkedStringList *newList)
  862. {
  863. LinkedStringList *next = NULL;
  864. while (newList != NULL)
  865. {
  866. next = newList->next;
  867. insertStringListItem(final, newList);
  868. newList = next;
  869. } /* while */
  870. } /* interpolateStringLists */
  871. char **PHYSFS_enumerateFiles(const char *path)
  872. {
  873. PhysDirInfo *i;
  874. char **retval = NULL;
  875. LinkedStringList *rc;
  876. LinkedStringList *finalList = NULL;
  877. int omitSymLinks = !allowSymLinks;
  878. BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
  879. while (*path == '/')
  880. path++;
  881. __PHYSFS_platformGrabMutex(stateLock);
  882. for (i = searchPath; i != NULL; i = i->next)
  883. {
  884. DirHandle *h = i->dirHandle;
  885. if (__PHYSFS_verifySecurity(h, path))
  886. {
  887. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  888. interpolateStringLists(&finalList, rc);
  889. } /* if */
  890. } /* for */
  891. __PHYSFS_platformReleaseMutex(stateLock);
  892. retval = convertStringListToPhysFSList(finalList);
  893. return(retval);
  894. } /* PHYSFS_enumerateFiles */
  895. int PHYSFS_exists(const char *fname)
  896. {
  897. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  898. while (*fname == '/')
  899. fname++;
  900. return(PHYSFS_getRealDir(fname) != NULL);
  901. } /* PHYSFS_exists */
  902. PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
  903. {
  904. PhysDirInfo *i;
  905. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  906. while (*fname == '/')
  907. fname++;
  908. if (*fname == '\0') /* eh...punt if it's the root dir. */
  909. return(1);
  910. __PHYSFS_platformGrabMutex(stateLock);
  911. for (i = searchPath; i != NULL; i = i->next)
  912. {
  913. DirHandle *h = i->dirHandle;
  914. if (__PHYSFS_verifySecurity(h, fname))
  915. {
  916. if (!h->funcs->exists(h, fname))
  917. __PHYSFS_setError(ERR_NO_SUCH_FILE);
  918. else
  919. {
  920. PHYSFS_sint64 retval = -1;
  921. if (h->funcs->getLastModTime == NULL)
  922. __PHYSFS_setError(ERR_NOT_SUPPORTED);
  923. else
  924. retval = h->funcs->getLastModTime(h, fname);
  925. __PHYSFS_platformReleaseMutex(stateLock);
  926. return(retval);
  927. } /* else */
  928. } /* if */
  929. } /* for */
  930. __PHYSFS_platformReleaseMutex(stateLock);
  931. return(-1); /* error set in verifysecurity/exists */
  932. } /* PHYSFS_getLastModTime */
  933. int PHYSFS_isDirectory(const char *fname)
  934. {
  935. PhysDirInfo *i;
  936. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  937. while (*fname == '/')
  938. fname++;
  939. if (*fname == '\0')
  940. return(1);
  941. __PHYSFS_platformGrabMutex(stateLock);
  942. for (i = searchPath; i != NULL; i = i->next)
  943. {
  944. DirHandle *h = i->dirHandle;
  945. if (__PHYSFS_verifySecurity(h, fname))
  946. {
  947. if (!h->funcs->exists(h, fname))
  948. __PHYSFS_setError(ERR_NO_SUCH_FILE);
  949. else
  950. {
  951. int retval = h->funcs->isDirectory(h, fname);
  952. __PHYSFS_platformReleaseMutex(stateLock);
  953. return(retval);
  954. } /* else */
  955. } /* if */
  956. } /* for */
  957. __PHYSFS_platformReleaseMutex(stateLock);
  958. return(0);
  959. } /* PHYSFS_isDirectory */
  960. int PHYSFS_isSymbolicLink(const char *fname)
  961. {
  962. PhysDirInfo *i;
  963. if (!allowSymLinks)
  964. return(0);
  965. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
  966. while (*fname == '/')
  967. fname++;
  968. __PHYSFS_platformGrabMutex(stateLock);
  969. for (i = searchPath; i != NULL; i = i->next)
  970. {
  971. DirHandle *h = i->dirHandle;
  972. if (__PHYSFS_verifySecurity(h, fname))
  973. {
  974. if (!h->funcs->exists(h, fname))
  975. __PHYSFS_setError(ERR_NO_SUCH_FILE);
  976. else
  977. {
  978. int retval = h->funcs->isSymLink(h, fname);
  979. __PHYSFS_platformReleaseMutex(stateLock);
  980. return(retval);
  981. } /* else */
  982. } /* if */
  983. } /* for */
  984. __PHYSFS_platformReleaseMutex(stateLock);
  985. return(0);
  986. } /* PHYSFS_isSymbolicLink */
  987. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  988. {
  989. PHYSFS_file *retval = NULL;
  990. FileHandle *rc = NULL;
  991. DirHandle *h;
  992. const DirFunctions *f;
  993. FileHandleList *list;
  994. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  995. while (*fname == '/')
  996. fname++;
  997. __PHYSFS_platformGrabMutex(stateLock);
  998. h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  999. BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
  1000. BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL,
  1001. stateLock, NULL);
  1002. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1003. BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
  1004. f = h->funcs;
  1005. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  1006. if (rc == NULL)
  1007. free(list);
  1008. else
  1009. {
  1010. list->handle.opaque = (void *) rc;
  1011. list->next = openWriteList;
  1012. openWriteList = list;
  1013. retval = &(list->handle);
  1014. } /* else */
  1015. __PHYSFS_platformReleaseMutex(stateLock);
  1016. return(retval);
  1017. } /* doOpenWrite */
  1018. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  1019. {
  1020. return(doOpenWrite(filename, 0));
  1021. } /* PHYSFS_openWrite */
  1022. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  1023. {
  1024. return(doOpenWrite(filename, 1));
  1025. } /* PHYSFS_openAppend */
  1026. PHYSFS_file *PHYSFS_openRead(const char *fname)
  1027. {
  1028. PHYSFS_file *retval;
  1029. FileHandle *rc = NULL;
  1030. FileHandleList *list;
  1031. PhysDirInfo *i;
  1032. BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
  1033. while (*fname == '/')
  1034. fname++;
  1035. __PHYSFS_platformGrabMutex(stateLock);
  1036. for (i = searchPath; i != NULL; i = i->next)
  1037. {
  1038. DirHandle *h = i->dirHandle;
  1039. if (__PHYSFS_verifySecurity(h, fname))
  1040. {
  1041. rc = h->funcs->openRead(h, fname);
  1042. if (rc != NULL)
  1043. break;
  1044. } /* if */
  1045. } /* for */
  1046. BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
  1047. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  1048. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  1049. list->handle.opaque = (void *) rc;
  1050. list->next = openReadList;
  1051. openReadList = list;
  1052. retval = &(list->handle);
  1053. __PHYSFS_platformReleaseMutex(stateLock);
  1054. return(retval);
  1055. } /* PHYSFS_openRead */
  1056. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  1057. {
  1058. FileHandle *h = (FileHandle *) handle->opaque;
  1059. FileHandleList *prev = NULL;
  1060. FileHandleList *i;
  1061. int rc;
  1062. for (i = *list; i != NULL; i = i->next)
  1063. {
  1064. if (&i->handle == handle) /* handle is in this list? */
  1065. {
  1066. rc = h->funcs->fileClose(h);
  1067. if (!rc)
  1068. return(-1);
  1069. if (prev == NULL)
  1070. *list = i->next;
  1071. else
  1072. prev->next = i->next;
  1073. free(i);
  1074. return(1);
  1075. } /* if */
  1076. prev = i;
  1077. } /* for */
  1078. return(0);
  1079. } /* closeHandleInOpenList */
  1080. int PHYSFS_close(PHYSFS_file *handle)
  1081. {
  1082. int rc;
  1083. __PHYSFS_platformGrabMutex(stateLock);
  1084. /* -1 == close failure. 0 == not found. 1 == success. */
  1085. rc = closeHandleInOpenList(&openReadList, handle);
  1086. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1087. if (!rc)
  1088. {
  1089. rc = closeHandleInOpenList(&openWriteList, handle);
  1090. BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
  1091. } /* if */
  1092. __PHYSFS_platformReleaseMutex(stateLock);
  1093. BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
  1094. return(1);
  1095. } /* PHYSFS_close */
  1096. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  1097. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1098. {
  1099. FileHandle *h = (FileHandle *) handle->opaque;
  1100. assert(h != NULL);
  1101. assert(h->funcs != NULL);
  1102. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  1103. return(h->funcs->read(h, buffer, objSize, objCount));
  1104. } /* PHYSFS_read */
  1105. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  1106. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  1107. {
  1108. FileHandle *h = (FileHandle *) handle->opaque;
  1109. assert(h != NULL);
  1110. assert(h->funcs != NULL);
  1111. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  1112. return(h->funcs->write(h, buffer, objSize, objCount));
  1113. } /* PHYSFS_write */
  1114. int PHYSFS_eof(PHYSFS_file *handle)
  1115. {
  1116. FileHandle *h = (FileHandle *) handle->opaque;
  1117. assert(h != NULL);
  1118. assert(h->funcs != NULL);
  1119. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  1120. return(h->funcs->eof(h));
  1121. } /* PHYSFS_eof */
  1122. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  1123. {
  1124. FileHandle *h = (FileHandle *) handle->opaque;
  1125. assert(h != NULL);
  1126. assert(h->funcs != NULL);
  1127. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  1128. return(h->funcs->tell(h));
  1129. } /* PHYSFS_tell */
  1130. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  1131. {
  1132. FileHandle *h = (FileHandle *) handle->opaque;
  1133. assert(h != NULL);
  1134. assert(h->funcs != NULL);
  1135. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1136. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  1137. return(h->funcs->seek(h, pos));
  1138. } /* PHYSFS_seek */
  1139. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1140. {
  1141. FileHandle *h = (FileHandle *) handle->opaque;
  1142. assert(h != NULL);
  1143. assert(h->funcs != NULL);
  1144. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1145. return(h->funcs->fileLength(h));
  1146. } /* PHYSFS_filelength */
  1147. /* end of physfs.c ... */