physfs.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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 *appName, const char *archiveExt,
  491. int includeCdRoms, int archivesFirst)
  492. {
  493. const char *basedir = PHYSFS_getBaseDir();
  494. const char *userdir = PHYSFS_getUserDir();
  495. const char *dirsep = PHYSFS_getDirSeparator();
  496. char *str;
  497. /* set write dir... */
  498. str = malloc(strlen(userdir) + (strlen(appName) * 2) +
  499. (strlen(dirsep) * 2) + 2);
  500. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  501. sprintf(str, "%s.%s", userdir, appName);
  502. if (!PHYSFS_setWriteDir(str))
  503. {
  504. if ( (!PHYSFS_setWriteDir(userdir)) ||
  505. (!PHYSFS_mkdir(str + strlen(userdir))) )
  506. {
  507. PHYSFS_setWriteDir(NULL);
  508. free(str);
  509. BAIL_IF_MACRO(1, ERR_CANT_SET_WRITE_DIR, 0);
  510. } /* if */
  511. } /* if */
  512. if (!PHYSFS_setWriteDir(str))
  513. {
  514. PHYSFS_setWriteDir(NULL);
  515. free(str);
  516. BAIL_IF_MACRO(1, ERR_CANT_SET_WRITE_DIR, 0);
  517. } /* if */
  518. /* Put write dir related dirs on search path... */
  519. PHYSFS_addToSearchPath(str, 1);
  520. PHYSFS_mkdir(appName); /* don't care if this fails. */
  521. strcat(str, dirsep);
  522. strcat(str, appName);
  523. PHYSFS_addToSearchPath(str, 1);
  524. free(str);
  525. /* Put base path stuff on search path... */
  526. PHYSFS_addToSearchPath(basedir, 1);
  527. str = malloc(strlen(basedir) + (strlen(appName) * 2) +
  528. (strlen(dirsep) * 2) + 2);
  529. if (str != NULL)
  530. {
  531. sprintf(str, "%s.%s", basedir, appName);
  532. PHYSFS_addToSearchPath(str, 1);
  533. free(str);
  534. } /* if */
  535. /* handle CD-ROMs... */
  536. if (includeCdRoms)
  537. {
  538. char **cds = PHYSFS_getCdRomDirs();
  539. char **i;
  540. for (i = cds; *i != NULL; i++)
  541. {
  542. PHYSFS_addToSearchPath(*i, 1);
  543. str = malloc(strlen(*i) + strlen(appName) + strlen(dirsep) + 1);
  544. if (str != NULL)
  545. {
  546. sprintf(str, "%s%s%s", *i, dirsep, appName);
  547. PHYSFS_addToSearchPath(str, 1);
  548. free(str);
  549. } /* if */
  550. } /* for */
  551. PHYSFS_freeList(cds);
  552. } /* if */
  553. /* Root out archives, and add them to search path... */
  554. if (archiveExt != NULL)
  555. {
  556. char **rc = PHYSFS_enumerateFiles("");
  557. char **i;
  558. size_t extlen = strlen(archiveExt);
  559. char *ext;
  560. for (i = rc; *i != NULL; i++)
  561. {
  562. size_t l = strlen(*i);
  563. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  564. {
  565. ext = (*i) + (l - extlen);
  566. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  567. {
  568. const char *d = PHYSFS_getRealDir(*i);
  569. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  570. if (str != NULL)
  571. {
  572. sprintf(str, "%s%s%s", d, dirsep, *i);
  573. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  574. free(str);
  575. } /* if */
  576. } /* if */
  577. } /* if */
  578. } /* for */
  579. PHYSFS_freeList(rc);
  580. } /* if */
  581. return(1);
  582. } /* PHYSFS_setSaneConfig */
  583. void PHYSFS_permitSymbolicLinks(int allow)
  584. {
  585. allowSymLinks = allow;
  586. } /* PHYSFS_permitSymbolicLinks */
  587. /* string manipulation in C makes my ass itch. */
  588. char * __PHYSFS_convertToDependent(const char *prepend,
  589. const char *dirName,
  590. const char *append)
  591. {
  592. const char *dirsep = PHYSFS_getDirSeparator();
  593. size_t sepsize = strlen(dirsep);
  594. char *str;
  595. char *i1;
  596. char *i2;
  597. size_t allocSize;
  598. while (*dirName == '/')
  599. dirName++;
  600. allocSize = strlen(dirName) + 1;
  601. if (prepend != NULL)
  602. allocSize += strlen(prepend) + sepsize;
  603. if (append != NULL)
  604. allocSize += strlen(append) + sepsize;
  605. /* make sure there's enough space if the dir separator is bigger. */
  606. if (sepsize > 1)
  607. {
  608. str = (char *) dirName;
  609. do
  610. {
  611. str = strchr(str, '/');
  612. if (str != NULL)
  613. {
  614. allocSize += (sepsize - 1);
  615. str++;
  616. } /* if */
  617. } while (str != NULL);
  618. } /* if */
  619. str = (char *) malloc(allocSize);
  620. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  621. if (prepend == NULL)
  622. *str = '\0';
  623. else
  624. {
  625. strcpy(str, prepend);
  626. strcat(str, dirsep);
  627. } /* else */
  628. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  629. {
  630. if (*i1 == '/')
  631. {
  632. strcpy(i2, dirsep);
  633. i2 += sepsize;
  634. } /* if */
  635. else
  636. {
  637. *i2 = *i1;
  638. } /* else */
  639. } /* for */
  640. *i2 = '\0';
  641. if (append)
  642. {
  643. strcat(str, dirsep);
  644. strcpy(str, append);
  645. } /* if */
  646. return(str);
  647. } /* __PHYSFS_convertToDependentNotation */
  648. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
  649. {
  650. int retval = 1;
  651. char *start;
  652. char *end;
  653. char *str;
  654. start = str = malloc(strlen(fname) + 1);
  655. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  656. strcpy(str, fname);
  657. while (1)
  658. {
  659. end = strchr(start, '/');
  660. if (end != NULL)
  661. *end = '\0';
  662. if ( (strcmp(start, ".") == 0) ||
  663. (strcmp(start, "..") == 0) ||
  664. (strchr(start, '\\') != NULL) ||
  665. (strchr(start, ':') != NULL) )
  666. {
  667. __PHYSFS_setError(ERR_INSECURE_FNAME);
  668. retval = 0;
  669. break;
  670. } /* if */
  671. if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
  672. {
  673. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  674. retval = 0;
  675. break;
  676. } /* if */
  677. if (end == NULL)
  678. break;
  679. *end = '/';
  680. start = end + 1;
  681. } /* while */
  682. free(str);
  683. return(retval);
  684. } /* __PHYSFS_verifySecurity */
  685. int PHYSFS_mkdir(const char *dirName)
  686. {
  687. DirHandle *h;
  688. char *str;
  689. char *start;
  690. char *end;
  691. int retval = 0;
  692. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  693. h = writeDir->dirHandle;
  694. while (*dirName == '/')
  695. dirName++;
  696. BAIL_IF_MACRO(h->funcs->mkdir == NULL, ERR_NOT_SUPPORTED, 0);
  697. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, dirName), NULL, 0);
  698. start = str = malloc(strlen(dirName) + 1);
  699. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  700. strcpy(str, dirName);
  701. while (1)
  702. {
  703. end = strchr(start, '/');
  704. if (end != NULL)
  705. *end = '\0';
  706. retval = h->funcs->mkdir(h, str);
  707. if (!retval)
  708. break;
  709. if (end == NULL)
  710. break;
  711. *end = '/';
  712. start = end + 1;
  713. } /* while */
  714. free(str);
  715. return(retval);
  716. } /* PHYSFS_mkdir */
  717. int PHYSFS_delete(const char *fname)
  718. {
  719. DirHandle *h;
  720. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  721. h = writeDir->dirHandle;
  722. BAIL_IF_MACRO(h->funcs->remove == NULL, ERR_NOT_SUPPORTED, 0);
  723. while (*fname == '/')
  724. fname++;
  725. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, 0);
  726. return(h->funcs->remove(h, fname));
  727. } /* PHYSFS_delete */
  728. const char *PHYSFS_getRealDir(const char *filename)
  729. {
  730. DirInfo *i;
  731. while (*filename == '/')
  732. filename++;
  733. for (i = searchPath; i != NULL; i = i->next)
  734. {
  735. DirHandle *h = i->dirHandle;
  736. if (__PHYSFS_verifySecurity(h, filename))
  737. {
  738. if (h->funcs->exists(h, filename))
  739. return(i->dirName);
  740. } /* if */
  741. } /* for */
  742. return(NULL);
  743. } /* PHYSFS_getRealDir */
  744. static int countList(LinkedStringList *list)
  745. {
  746. int retval = 0;
  747. LinkedStringList *i;
  748. for (i = list; i != NULL; i = i->next)
  749. retval++;
  750. return(retval);
  751. } /* countList */
  752. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  753. {
  754. int i;
  755. LinkedStringList *next = NULL;
  756. int len = countList(finalList);
  757. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  758. if (retval == NULL)
  759. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  760. for (i = 0; i < len; i++)
  761. {
  762. next = finalList->next;
  763. if (retval == NULL)
  764. free(finalList->str);
  765. else
  766. retval[i] = finalList->str;
  767. free(finalList);
  768. finalList = next;
  769. } /* for */
  770. if (retval != NULL)
  771. retval[i] = NULL;
  772. return(retval);
  773. } /* convertStringListToPhysFSList */
  774. static void insertStringListItem(LinkedStringList **final,
  775. LinkedStringList *item)
  776. {
  777. LinkedStringList *i;
  778. LinkedStringList *prev = NULL;
  779. int rc;
  780. for (i = *final; i != NULL; i = i->next)
  781. {
  782. rc = strcmp(i->str, item->str);
  783. if (rc > 0) /* insertion point. */
  784. break;
  785. else if (rc == 0) /* already in list. */
  786. {
  787. free(item->str);
  788. free(item);
  789. return;
  790. } /* else if */
  791. prev = i;
  792. } /* for */
  793. /*
  794. * If we are here, we are either at the insertion point.
  795. * This may be the end of the list, or the list may be empty, too.
  796. */
  797. if (prev == NULL)
  798. *final = item;
  799. else
  800. prev->next = item;
  801. item->next = i;
  802. } /* insertStringListItem */
  803. /* if we run out of memory anywhere in here, we give back what we can. */
  804. static void interpolateStringLists(LinkedStringList **final,
  805. LinkedStringList *newList)
  806. {
  807. LinkedStringList *next = NULL;
  808. while (newList != NULL)
  809. {
  810. next = newList->next;
  811. insertStringListItem(final, newList);
  812. newList = next;
  813. } /* while */
  814. } /* interpolateStringLists */
  815. char **PHYSFS_enumerateFiles(const char *path)
  816. {
  817. DirInfo *i;
  818. char **retval = NULL;
  819. LinkedStringList *rc;
  820. LinkedStringList *finalList = NULL;
  821. int omitSymLinks = !allowSymLinks;
  822. while (*path == '/')
  823. path++;
  824. for (i = searchPath; i != NULL; i = i->next)
  825. {
  826. DirHandle *h = i->dirHandle;
  827. if (__PHYSFS_verifySecurity(h, path))
  828. {
  829. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  830. interpolateStringLists(&finalList, rc);
  831. } /* if */
  832. } /* for */
  833. retval = convertStringListToPhysFSList(finalList);
  834. return(retval);
  835. } /* PHYSFS_enumerateFiles */
  836. int PHYSFS_exists(const char *fname)
  837. {
  838. while (*fname == '/')
  839. fname++;
  840. return(PHYSFS_getRealDir(fname) != NULL);
  841. } /* PHYSFS_exists */
  842. int PHYSFS_isDirectory(const char *fname)
  843. {
  844. DirInfo *i;
  845. while (*fname == '/')
  846. fname++;
  847. if (*fname == '\0')
  848. return(1);
  849. for (i = searchPath; i != NULL; i = i->next)
  850. {
  851. DirHandle *h = i->dirHandle;
  852. if (__PHYSFS_verifySecurity(h, fname))
  853. {
  854. if (h->funcs->exists(h, fname))
  855. return(h->funcs->isDirectory(h, fname));
  856. } /* if */
  857. } /* for */
  858. return(0);
  859. } /* PHYSFS_isDirectory */
  860. int PHYSFS_isSymbolicLink(const char *fname)
  861. {
  862. DirInfo *i;
  863. if (!allowSymLinks)
  864. return(0);
  865. while (*fname == '/')
  866. fname++;
  867. for (i = searchPath; i != NULL; i = i->next)
  868. {
  869. DirHandle *h = i->dirHandle;
  870. if (__PHYSFS_verifySecurity(h, fname))
  871. {
  872. if (h->funcs->exists(h, fname))
  873. return(h->funcs->isSymLink(h, fname));
  874. } /* if */
  875. } /* for */
  876. return(0);
  877. } /* PHYSFS_isSymbolicLink */
  878. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  879. {
  880. PHYSFS_file *retval = NULL;
  881. FileHandle *rc = NULL;
  882. DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  883. const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
  884. FileHandleList *list;
  885. while (*fname == '/')
  886. fname++;
  887. BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
  888. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
  889. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  890. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  891. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  892. if (rc == NULL)
  893. free(list);
  894. else
  895. {
  896. list->handle.opaque = (void *) rc;
  897. list->next = openWriteList;
  898. openWriteList = list;
  899. retval = &(list->handle);
  900. } /* else */
  901. return(retval);
  902. } /* doOpenWrite */
  903. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  904. {
  905. return(doOpenWrite(filename, 0));
  906. } /* PHYSFS_openWrite */
  907. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  908. {
  909. return(doOpenWrite(filename, 1));
  910. } /* PHYSFS_openAppend */
  911. PHYSFS_file *PHYSFS_openRead(const char *fname)
  912. {
  913. FileHandle *rc = NULL;
  914. FileHandleList *list;
  915. DirInfo *i;
  916. while (*fname == '/')
  917. fname++;
  918. for (i = searchPath; i != NULL; i = i->next)
  919. {
  920. DirHandle *h = i->dirHandle;
  921. if (__PHYSFS_verifySecurity(h, fname))
  922. {
  923. rc = h->funcs->openRead(h, fname);
  924. if (rc != NULL)
  925. break;
  926. } /* if */
  927. } /* for */
  928. if (rc == NULL)
  929. return(NULL);
  930. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  931. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  932. list->handle.opaque = (void *) rc;
  933. list->next = openReadList;
  934. openReadList = list;
  935. return(&(list->handle));
  936. } /* PHYSFS_openRead */
  937. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  938. {
  939. FileHandle *h = (FileHandle *) handle->opaque;
  940. FileHandleList *prev = NULL;
  941. FileHandleList *i;
  942. int rc;
  943. for (i = *list; i != NULL; i = i->next)
  944. {
  945. if (&i->handle == handle) /* handle is in this list? */
  946. {
  947. rc = h->funcs->fileClose(h);
  948. if (!rc)
  949. return(-1);
  950. if (prev == NULL)
  951. *list = i->next;
  952. else
  953. prev->next = i->next;
  954. free(i);
  955. return(1);
  956. } /* if */
  957. prev = i;
  958. } /* for */
  959. return(0);
  960. } /* closeHandleInOpenList */
  961. int PHYSFS_close(PHYSFS_file *handle)
  962. {
  963. int rc;
  964. /* -1 == close failure. 0 == not found. 1 == success. */
  965. rc = closeHandleInOpenList(&openReadList, handle);
  966. BAIL_IF_MACRO(rc == -1, NULL, 0);
  967. if (!rc)
  968. {
  969. rc = closeHandleInOpenList(&openWriteList, handle);
  970. BAIL_IF_MACRO(rc == -1, NULL, 0);
  971. } /* if */
  972. if (!rc)
  973. __PHYSFS_setError(ERR_NOT_A_HANDLE);
  974. return(rc);
  975. } /* PHYSFS_close */
  976. int PHYSFS_read(PHYSFS_file *handle, void *buffer,
  977. unsigned int objSize, unsigned int objCount)
  978. {
  979. FileHandle *h = (FileHandle *) handle->opaque;
  980. assert(h != NULL);
  981. assert(h->funcs != NULL);
  982. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  983. return(h->funcs->read(h, buffer, objSize, objCount));
  984. } /* PHYSFS_read */
  985. int PHYSFS_write(PHYSFS_file *handle, void *buffer,
  986. unsigned int objSize, unsigned int objCount)
  987. {
  988. FileHandle *h = (FileHandle *) handle->opaque;
  989. assert(h != NULL);
  990. assert(h->funcs != NULL);
  991. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  992. return(h->funcs->write(h, buffer, objSize, objCount));
  993. } /* PHYSFS_write */
  994. int PHYSFS_eof(PHYSFS_file *handle)
  995. {
  996. FileHandle *h = (FileHandle *) handle->opaque;
  997. assert(h != NULL);
  998. assert(h->funcs != NULL);
  999. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  1000. return(h->funcs->eof(h));
  1001. } /* PHYSFS_eof */
  1002. int PHYSFS_tell(PHYSFS_file *handle)
  1003. {
  1004. FileHandle *h = (FileHandle *) handle->opaque;
  1005. assert(h != NULL);
  1006. assert(h->funcs != NULL);
  1007. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  1008. return(h->funcs->tell(h));
  1009. } /* PHYSFS_tell */
  1010. int PHYSFS_seek(PHYSFS_file *handle, int pos)
  1011. {
  1012. FileHandle *h = (FileHandle *) handle->opaque;
  1013. assert(h != NULL);
  1014. assert(h->funcs != NULL);
  1015. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1016. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  1017. return(h->funcs->seek(h, pos));
  1018. } /* PHYSFS_seek */
  1019. int PHYSFS_fileLength(PHYSFS_file *handle)
  1020. {
  1021. FileHandle *h = (FileHandle *) handle->opaque;
  1022. assert(h != NULL);
  1023. assert(h->funcs != NULL);
  1024. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1025. return(h->funcs->fileLength(h));
  1026. } /* PHYSFS_filelength */
  1027. /* end of physfs.c ... */