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