physfs.c 29 KB

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