physfs.c 29 KB

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