physfs.c 29 KB

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