physfs.c 26 KB

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