physfs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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. return(__PHYSFS_platformCurrentDir());
  266. } /* calculateBaseDir */
  267. int PHYSFS_init(const char *argv0)
  268. {
  269. char *ptr;
  270. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  271. BAIL_IF_MACRO(argv0 == NULL, ERR_INVALID_ARGUMENT, 0);
  272. baseDir = calculateBaseDir(argv0);
  273. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  274. ptr = __PHYSFS_platformRealPath(baseDir);
  275. free(baseDir);
  276. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  277. baseDir = ptr;
  278. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  279. userDir = calculateUserDir();
  280. if (userDir != NULL)
  281. {
  282. ptr = __PHYSFS_platformRealPath(userDir);
  283. free(userDir);
  284. userDir = ptr;
  285. } /* if */
  286. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  287. {
  288. free(baseDir);
  289. baseDir = NULL;
  290. return(0);
  291. } /* if */
  292. initialized = 1;
  293. return(1);
  294. } /* PHYSFS_init */
  295. static int closeFileHandleList(FileHandleList **list)
  296. {
  297. FileHandleList *i;
  298. FileHandleList *next = NULL;
  299. FileHandle *h;
  300. for (i = *list; i != NULL; i = next)
  301. {
  302. next = i->next;
  303. h = (FileHandle *) (i->handle.opaque);
  304. if (!h->funcs->fileClose(h))
  305. {
  306. *list = i;
  307. return(0);
  308. } /* if */
  309. free(i);
  310. } /* for */
  311. *list = NULL;
  312. return(1);
  313. } /* closeFileHandleList */
  314. static void freeSearchPath(void)
  315. {
  316. DirInfo *i;
  317. DirInfo *next = NULL;
  318. closeFileHandleList(&openReadList);
  319. if (searchPath != NULL)
  320. {
  321. for (i = searchPath; i != NULL; i = next)
  322. {
  323. next = i->next;
  324. freeDirInfo(i, openReadList);
  325. } /* for */
  326. searchPath = NULL;
  327. } /* if */
  328. } /* freeSearchPath */
  329. int PHYSFS_deinit(void)
  330. {
  331. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  332. closeFileHandleList(&openWriteList);
  333. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  334. freeSearchPath();
  335. freeErrorMessages();
  336. if (baseDir != NULL)
  337. {
  338. free(baseDir);
  339. baseDir = NULL;
  340. } /* if */
  341. if (userDir != NULL)
  342. {
  343. free(userDir);
  344. userDir = NULL;
  345. } /* if */
  346. allowSymLinks = 0;
  347. initialized = 0;
  348. return(1);
  349. } /* PHYSFS_deinit */
  350. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  351. {
  352. return(supported_types);
  353. } /* PHYSFS_supportedArchiveTypes */
  354. void PHYSFS_freeList(void *list)
  355. {
  356. void **i;
  357. for (i = (void **) list; *i != NULL; i++)
  358. free(*i);
  359. free(list);
  360. } /* PHYSFS_freeList */
  361. const char *PHYSFS_getDirSeparator(void)
  362. {
  363. return(__PHYSFS_platformDirSeparator);
  364. } /* PHYSFS_getDirSeparator */
  365. char **PHYSFS_getCdRomDirs(void)
  366. {
  367. return(__PHYSFS_platformDetectAvailableCDs());
  368. } /* PHYSFS_getCdRomDirs */
  369. const char *PHYSFS_getBaseDir(void)
  370. {
  371. return(baseDir); /* this is calculated in PHYSFS_init()... */
  372. } /* PHYSFS_getBaseDir */
  373. const char *PHYSFS_getUserDir(void)
  374. {
  375. return(userDir); /* this is calculated in PHYSFS_init()... */
  376. } /* PHYSFS_getUserDir */
  377. const char *PHYSFS_getWriteDir(void)
  378. {
  379. if (writeDir == NULL)
  380. return(NULL);
  381. return(writeDir->dirName);
  382. } /* PHYSFS_getWriteDir */
  383. int PHYSFS_setWriteDir(const char *newDir)
  384. {
  385. if (writeDir != NULL)
  386. {
  387. BAIL_IF_MACRO(!freeDirInfo(writeDir, openWriteList), NULL, 0);
  388. writeDir = NULL;
  389. } /* if */
  390. if (newDir != NULL)
  391. {
  392. writeDir = buildDirInfo(newDir, 1);
  393. return(writeDir != NULL);
  394. } /* if */
  395. return(1);
  396. } /* PHYSFS_setWriteDir */
  397. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  398. {
  399. DirInfo *di;
  400. DirInfo *i = searchPath;
  401. DirInfo *prev = NULL;
  402. while (i != NULL)
  403. {
  404. if (strcmp(newDir, i->dirName) == 0) /* already in search path. */
  405. return(1);
  406. prev = i;
  407. i = i->next;
  408. } /* while */
  409. di = buildDirInfo(newDir, 0);
  410. BAIL_IF_MACRO(di == NULL, NULL, 0);
  411. if (appendToPath)
  412. {
  413. di->next = NULL;
  414. if (prev == NULL)
  415. searchPath = di;
  416. else
  417. prev->next = di;
  418. } /* if */
  419. else
  420. {
  421. di->next = searchPath;
  422. searchPath = di;
  423. } /* else */
  424. return(1);
  425. } /* PHYSFS_addToSearchPath */
  426. int PHYSFS_removeFromSearchPath(const char *oldDir)
  427. {
  428. DirInfo *i;
  429. DirInfo *prev = NULL;
  430. DirInfo *next = NULL;
  431. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  432. for (i = searchPath; i != NULL; i = i->next)
  433. {
  434. if (strcmp(i->dirName, oldDir) == 0)
  435. {
  436. next = i->next;
  437. BAIL_IF_MACRO(!freeDirInfo(i, openReadList), NULL, 0);
  438. if (prev == NULL)
  439. searchPath = next;
  440. else
  441. prev->next = next;
  442. return(1);
  443. } /* if */
  444. prev = i;
  445. } /* for */
  446. __PHYSFS_setError(ERR_NOT_IN_SEARCH_PATH);
  447. return(0);
  448. } /* PHYSFS_removeFromSearchPath */
  449. char **PHYSFS_getSearchPath(void)
  450. {
  451. int count = 1;
  452. int x;
  453. DirInfo *i;
  454. char **retval;
  455. for (i = searchPath; i != NULL; i = i->next)
  456. count++;
  457. retval = (char **) malloc(sizeof (char *) * count);
  458. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  459. count--;
  460. retval[count] = NULL;
  461. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  462. {
  463. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  464. if (retval[x] == NULL) /* this is friggin' ugly. */
  465. {
  466. while (x > 0)
  467. {
  468. x--;
  469. free(retval[x]);
  470. } /* while */
  471. free(retval);
  472. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  473. return(NULL);
  474. } /* if */
  475. strcpy(retval[x], i->dirName);
  476. } /* for */
  477. return(retval);
  478. } /* PHYSFS_getSearchPath */
  479. int PHYSFS_setSaneConfig(const char *appName, const char *archiveExt,
  480. int includeCdRoms, int archivesFirst)
  481. {
  482. const char *basedir = PHYSFS_getBaseDir();
  483. const char *userdir = PHYSFS_getUserDir();
  484. const char *dirsep = PHYSFS_getDirSeparator();
  485. char *str;
  486. /* set write dir... */
  487. str = malloc(strlen(userdir) + (strlen(appName) * 2) +
  488. (strlen(dirsep) * 2) + 2);
  489. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  490. sprintf(str, "%s.%s", userdir, appName);
  491. if (!PHYSFS_setWriteDir(str))
  492. {
  493. if ( (!PHYSFS_setWriteDir(userdir)) ||
  494. (!PHYSFS_mkdir(str + strlen(userdir))) )
  495. {
  496. PHYSFS_setWriteDir(NULL);
  497. free(str);
  498. BAIL_IF_MACRO(1, ERR_CANT_SET_WRITE_DIR, 0);
  499. } /* if */
  500. } /* if */
  501. if (!PHYSFS_setWriteDir(str))
  502. {
  503. PHYSFS_setWriteDir(NULL);
  504. free(str);
  505. BAIL_IF_MACRO(1, ERR_CANT_SET_WRITE_DIR, 0);
  506. } /* if */
  507. /* Put write dir related dirs on search path... */
  508. PHYSFS_addToSearchPath(str, 1);
  509. PHYSFS_mkdir(appName); /* don't care if this fails. */
  510. strcat(str, dirsep);
  511. strcat(str, appName);
  512. PHYSFS_addToSearchPath(str, 1);
  513. free(str);
  514. /* Put base path stuff on search path... */
  515. PHYSFS_addToSearchPath(basedir, 1);
  516. str = malloc(strlen(basedir) + (strlen(appName) * 2) +
  517. (strlen(dirsep) * 2) + 2);
  518. if (str != NULL)
  519. {
  520. sprintf(str, "%s.%s", basedir, appName);
  521. PHYSFS_addToSearchPath(str, 1);
  522. free(str);
  523. } /* if */
  524. /* handle CD-ROMs... */
  525. if (includeCdRoms)
  526. {
  527. char **cds = PHYSFS_getCdRomDirs();
  528. char **i;
  529. for (i = cds; *i != NULL; i++)
  530. {
  531. PHYSFS_addToSearchPath(*i, 1);
  532. str = malloc(strlen(*i) + strlen(appName) + strlen(dirsep) + 1);
  533. if (str != NULL)
  534. {
  535. sprintf(str, "%s%s%s", *i, dirsep, appName);
  536. PHYSFS_addToSearchPath(str, 1);
  537. free(str);
  538. } /* if */
  539. } /* for */
  540. PHYSFS_freeList(cds);
  541. } /* if */
  542. /* Root out archives, and add them to search path... */
  543. if (archiveExt != NULL)
  544. {
  545. char **rc = PHYSFS_enumerateFiles("");
  546. char **i;
  547. size_t extlen = strlen(archiveExt);
  548. char *ext;
  549. for (i = rc; *i != NULL; i++)
  550. {
  551. size_t l = strlen(*i);
  552. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  553. {
  554. ext = (*i) + (l - extlen);
  555. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  556. {
  557. const char *d = PHYSFS_getRealDir(*i);
  558. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  559. if (str != NULL)
  560. {
  561. sprintf(str, "%s%s%s", d, dirsep, *i);
  562. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  563. free(str);
  564. } /* if */
  565. } /* if */
  566. } /* if */
  567. } /* for */
  568. PHYSFS_freeList(rc);
  569. } /* if */
  570. return(1);
  571. } /* PHYSFS_setSaneConfig */
  572. void PHYSFS_permitSymbolicLinks(int allow)
  573. {
  574. allowSymLinks = allow;
  575. } /* PHYSFS_permitSymbolicLinks */
  576. /* string manipulation in C makes my ass itch. */
  577. char * __PHYSFS_convertToDependent(const char *prepend,
  578. const char *dirName,
  579. const char *append)
  580. {
  581. const char *dirsep = PHYSFS_getDirSeparator();
  582. size_t sepsize = strlen(dirsep);
  583. char *str;
  584. char *i1;
  585. char *i2;
  586. size_t allocSize;
  587. while (*dirName == '/')
  588. dirName++;
  589. allocSize = strlen(dirName) + 1;
  590. if (prepend != NULL)
  591. allocSize += strlen(prepend) + sepsize;
  592. if (append != NULL)
  593. allocSize += strlen(append) + sepsize;
  594. /* make sure there's enough space if the dir separator is bigger. */
  595. if (sepsize > 1)
  596. {
  597. str = (char *) dirName;
  598. do
  599. {
  600. str = strchr(str, '/');
  601. if (str != NULL)
  602. {
  603. allocSize += (sepsize - 1);
  604. str++;
  605. } /* if */
  606. } while (str != NULL);
  607. } /* if */
  608. str = (char *) malloc(allocSize);
  609. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  610. if (prepend == NULL)
  611. *str = '\0';
  612. else
  613. {
  614. strcpy(str, prepend);
  615. strcat(str, dirsep);
  616. } /* else */
  617. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  618. {
  619. if (*i1 == '/')
  620. {
  621. strcpy(i2, dirsep);
  622. i2 += sepsize;
  623. } /* if */
  624. else
  625. {
  626. *i2 = *i1;
  627. } /* else */
  628. } /* for */
  629. *i2 = '\0';
  630. if (append)
  631. {
  632. strcat(str, dirsep);
  633. strcpy(str, append);
  634. } /* if */
  635. return(str);
  636. } /* __PHYSFS_convertToDependentNotation */
  637. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
  638. {
  639. int retval = 1;
  640. char *start;
  641. char *end;
  642. char *str;
  643. start = str = malloc(strlen(fname) + 1);
  644. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  645. strcpy(str, fname);
  646. while (1)
  647. {
  648. end = strchr(start, '/');
  649. if (end != NULL)
  650. *end = '\0';
  651. if ( (strcmp(start, ".") == 0) ||
  652. (strcmp(start, "..") == 0) ||
  653. (strchr(start, '\\') != NULL) ||
  654. (strchr(start, ':') != NULL) )
  655. {
  656. __PHYSFS_setError(ERR_INSECURE_FNAME);
  657. retval = 0;
  658. break;
  659. } /* if */
  660. if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
  661. {
  662. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  663. retval = 0;
  664. break;
  665. } /* if */
  666. if (end == NULL)
  667. break;
  668. *end = '/';
  669. start = end + 1;
  670. } /* while */
  671. free(str);
  672. return(retval);
  673. } /* __PHYSFS_verifySecurity */
  674. int PHYSFS_mkdir(const char *dirName)
  675. {
  676. DirHandle *h;
  677. char *str;
  678. char *start;
  679. char *end;
  680. int retval = 0;
  681. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  682. h = writeDir->dirHandle;
  683. while (*dirName == '/')
  684. dirName++;
  685. BAIL_IF_MACRO(h->funcs->mkdir == NULL, ERR_NOT_SUPPORTED, 0);
  686. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, dirName), NULL, 0);
  687. start = str = malloc(strlen(dirName) + 1);
  688. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  689. strcpy(str, dirName);
  690. while (1)
  691. {
  692. end = strchr(start, '/');
  693. if (end != NULL)
  694. *end = '\0';
  695. retval = h->funcs->mkdir(h, str);
  696. if (!retval)
  697. break;
  698. if (end == NULL)
  699. break;
  700. *end = '/';
  701. start = end + 1;
  702. } /* while */
  703. free(str);
  704. return(retval);
  705. } /* PHYSFS_mkdir */
  706. int PHYSFS_delete(const char *fname)
  707. {
  708. DirHandle *h;
  709. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  710. h = writeDir->dirHandle;
  711. BAIL_IF_MACRO(h->funcs->remove == NULL, ERR_NOT_SUPPORTED, 0);
  712. while (*fname == '/')
  713. fname++;
  714. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, 0);
  715. return(h->funcs->remove(h, fname));
  716. } /* PHYSFS_delete */
  717. const char *PHYSFS_getRealDir(const char *filename)
  718. {
  719. DirInfo *i;
  720. while (*filename == '/')
  721. filename++;
  722. for (i = searchPath; i != NULL; i = i->next)
  723. {
  724. DirHandle *h = i->dirHandle;
  725. if (__PHYSFS_verifySecurity(h, filename))
  726. {
  727. if (h->funcs->exists(h, filename))
  728. return(i->dirName);
  729. } /* if */
  730. } /* for */
  731. return(NULL);
  732. } /* PHYSFS_getRealDir */
  733. static int countList(LinkedStringList *list)
  734. {
  735. int retval = 0;
  736. LinkedStringList *i;
  737. for (i = list; i != NULL; i = i->next)
  738. retval++;
  739. return(retval);
  740. } /* countList */
  741. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  742. {
  743. int i;
  744. LinkedStringList *next = NULL;
  745. int len = countList(finalList);
  746. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  747. if (retval == NULL)
  748. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  749. for (i = 0; i < len; i++)
  750. {
  751. next = finalList->next;
  752. if (retval == NULL)
  753. free(finalList->str);
  754. else
  755. retval[i] = finalList->str;
  756. free(finalList);
  757. finalList = next;
  758. } /* for */
  759. if (retval != NULL)
  760. retval[i] = NULL;
  761. return(retval);
  762. } /* convertStringListToPhysFSList */
  763. static void insertStringListItem(LinkedStringList **final,
  764. LinkedStringList *item)
  765. {
  766. LinkedStringList *i;
  767. LinkedStringList *prev = NULL;
  768. int rc;
  769. for (i = *final; i != NULL; i = i->next)
  770. {
  771. rc = strcmp(i->str, item->str);
  772. if (rc > 0) /* insertion point. */
  773. break;
  774. else if (rc == 0) /* already in list. */
  775. {
  776. free(item->str);
  777. free(item);
  778. return;
  779. } /* else if */
  780. prev = i;
  781. } /* for */
  782. /*
  783. * If we are here, we are either at the insertion point.
  784. * This may be the end of the list, or the list may be empty, too.
  785. */
  786. if (prev == NULL)
  787. *final = item;
  788. else
  789. prev->next = item;
  790. item->next = i;
  791. } /* insertStringListItem */
  792. /* if we run out of memory anywhere in here, we give back what we can. */
  793. static void interpolateStringLists(LinkedStringList **final,
  794. LinkedStringList *newList)
  795. {
  796. LinkedStringList *next = NULL;
  797. while (newList != NULL)
  798. {
  799. next = newList->next;
  800. insertStringListItem(final, newList);
  801. newList = next;
  802. } /* while */
  803. } /* interpolateStringLists */
  804. char **PHYSFS_enumerateFiles(const char *path)
  805. {
  806. DirInfo *i;
  807. char **retval = NULL;
  808. LinkedStringList *rc;
  809. LinkedStringList *finalList = NULL;
  810. int omitSymLinks = !allowSymLinks;
  811. while (*path == '/')
  812. path++;
  813. for (i = searchPath; i != NULL; i = i->next)
  814. {
  815. DirHandle *h = i->dirHandle;
  816. if (__PHYSFS_verifySecurity(h, path))
  817. {
  818. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  819. interpolateStringLists(&finalList, rc);
  820. } /* if */
  821. } /* for */
  822. retval = convertStringListToPhysFSList(finalList);
  823. return(retval);
  824. } /* PHYSFS_enumerateFiles */
  825. int PHYSFS_exists(const char *fname)
  826. {
  827. while (*fname == '/')
  828. fname++;
  829. return(PHYSFS_getRealDir(fname) != NULL);
  830. } /* PHYSFS_exists */
  831. int PHYSFS_isDirectory(const char *fname)
  832. {
  833. DirInfo *i;
  834. while (*fname == '/')
  835. fname++;
  836. if (*fname == '\0')
  837. return(1);
  838. for (i = searchPath; i != NULL; i = i->next)
  839. {
  840. DirHandle *h = i->dirHandle;
  841. if (__PHYSFS_verifySecurity(h, fname))
  842. {
  843. if (h->funcs->exists(h, fname))
  844. return(h->funcs->isDirectory(h, fname));
  845. } /* if */
  846. } /* for */
  847. return(0);
  848. } /* PHYSFS_isDirectory */
  849. int PHYSFS_isSymbolicLink(const char *fname)
  850. {
  851. DirInfo *i;
  852. if (!allowSymLinks)
  853. return(0);
  854. while (*fname == '/')
  855. fname++;
  856. for (i = searchPath; i != NULL; i = i->next)
  857. {
  858. DirHandle *h = i->dirHandle;
  859. if (__PHYSFS_verifySecurity(h, fname))
  860. {
  861. if (h->funcs->exists(h, fname))
  862. return(h->funcs->isSymLink(h, fname));
  863. } /* if */
  864. } /* for */
  865. return(0);
  866. } /* PHYSFS_isSymbolicLink */
  867. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  868. {
  869. PHYSFS_file *retval = NULL;
  870. FileHandle *rc = NULL;
  871. DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  872. const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
  873. FileHandleList *list;
  874. while (*fname == '/')
  875. fname++;
  876. BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
  877. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
  878. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  879. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  880. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  881. if (rc == NULL)
  882. free(list);
  883. else
  884. {
  885. list->handle.opaque = (void *) rc;
  886. list->next = openWriteList;
  887. openWriteList = list;
  888. retval = &(list->handle);
  889. } /* else */
  890. return(retval);
  891. } /* doOpenWrite */
  892. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  893. {
  894. return(doOpenWrite(filename, 0));
  895. } /* PHYSFS_openWrite */
  896. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  897. {
  898. return(doOpenWrite(filename, 1));
  899. } /* PHYSFS_openAppend */
  900. PHYSFS_file *PHYSFS_openRead(const char *fname)
  901. {
  902. FileHandle *rc = NULL;
  903. FileHandleList *list;
  904. DirInfo *i;
  905. while (*fname == '/')
  906. fname++;
  907. for (i = searchPath; i != NULL; i = i->next)
  908. {
  909. DirHandle *h = i->dirHandle;
  910. if (__PHYSFS_verifySecurity(h, fname))
  911. {
  912. rc = h->funcs->openRead(h, fname);
  913. if (rc != NULL)
  914. break;
  915. } /* if */
  916. } /* for */
  917. if (rc == NULL)
  918. return(NULL);
  919. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  920. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  921. list->handle.opaque = (void *) rc;
  922. list->next = openReadList;
  923. openReadList = list;
  924. return(&(list->handle));
  925. } /* PHYSFS_openRead */
  926. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  927. {
  928. FileHandle *h = (FileHandle *) handle->opaque;
  929. FileHandleList *prev = NULL;
  930. FileHandleList *i;
  931. int rc;
  932. for (i = *list; i != NULL; i = i->next)
  933. {
  934. if (&i->handle == handle) /* handle is in this list? */
  935. {
  936. rc = h->funcs->fileClose(h);
  937. if (!rc)
  938. return(-1);
  939. if (prev == NULL)
  940. *list = i->next;
  941. else
  942. prev->next = i->next;
  943. free(i);
  944. return(1);
  945. } /* if */
  946. prev = i;
  947. } /* for */
  948. return(0);
  949. } /* closeHandleInOpenList */
  950. int PHYSFS_close(PHYSFS_file *handle)
  951. {
  952. int rc;
  953. /* -1 == close failure. 0 == not found. 1 == success. */
  954. rc = closeHandleInOpenList(&openReadList, handle);
  955. BAIL_IF_MACRO(rc == -1, NULL, 0);
  956. if (!rc)
  957. {
  958. rc = closeHandleInOpenList(&openWriteList, handle);
  959. BAIL_IF_MACRO(rc == -1, NULL, 0);
  960. } /* if */
  961. if (!rc)
  962. __PHYSFS_setError(ERR_NOT_A_HANDLE);
  963. return(rc);
  964. } /* PHYSFS_close */
  965. int PHYSFS_read(PHYSFS_file *handle, void *buffer,
  966. unsigned int objSize, unsigned int objCount)
  967. {
  968. FileHandle *h = (FileHandle *) handle->opaque;
  969. assert(h != NULL);
  970. assert(h->funcs != NULL);
  971. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  972. return(h->funcs->read(h, buffer, objSize, objCount));
  973. } /* PHYSFS_read */
  974. int PHYSFS_write(PHYSFS_file *handle, void *buffer,
  975. unsigned int objSize, unsigned int objCount)
  976. {
  977. FileHandle *h = (FileHandle *) handle->opaque;
  978. assert(h != NULL);
  979. assert(h->funcs != NULL);
  980. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  981. return(h->funcs->write(h, buffer, objSize, objCount));
  982. } /* PHYSFS_write */
  983. int PHYSFS_eof(PHYSFS_file *handle)
  984. {
  985. FileHandle *h = (FileHandle *) handle->opaque;
  986. assert(h != NULL);
  987. assert(h->funcs != NULL);
  988. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  989. return(h->funcs->eof(h));
  990. } /* PHYSFS_eof */
  991. int PHYSFS_tell(PHYSFS_file *handle)
  992. {
  993. FileHandle *h = (FileHandle *) handle->opaque;
  994. assert(h != NULL);
  995. assert(h->funcs != NULL);
  996. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  997. return(h->funcs->tell(h));
  998. } /* PHYSFS_tell */
  999. int PHYSFS_seek(PHYSFS_file *handle, int pos)
  1000. {
  1001. FileHandle *h = (FileHandle *) handle->opaque;
  1002. assert(h != NULL);
  1003. assert(h->funcs != NULL);
  1004. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1005. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  1006. return(h->funcs->seek(h, pos));
  1007. } /* PHYSFS_seek */
  1008. int PHYSFS_fileLength(PHYSFS_file *handle)
  1009. {
  1010. FileHandle *h = (FileHandle *) handle->opaque;
  1011. assert(h != NULL);
  1012. assert(h->funcs != NULL);
  1013. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1014. return(h->funcs->fileLength(h));
  1015. } /* PHYSFS_filelength */
  1016. /* end of physfs.c ... */