physfs.c 28 KB

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