physfs.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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 <fcntl.h>
  14. #include <errno.h>
  15. #include <assert.h>
  16. #include "physfs.h"
  17. #define __PHYSICSFS_INTERNAL__
  18. #include "physfs_internal.h"
  19. typedef struct __PHYSFS_ERRMSGTYPE__
  20. {
  21. int tid;
  22. int errorAvailable;
  23. char errorString[80];
  24. struct __PHYSFS_ERRMSGTYPE__ *next;
  25. } ErrMsg;
  26. typedef struct __PHYSFS_DIRINFO__
  27. {
  28. char *dirName;
  29. DirHandle *dirHandle;
  30. struct __PHYSFS_DIRINFO__ *next;
  31. } DirInfo;
  32. typedef struct __PHYSFS_FILEHANDLELIST__
  33. {
  34. PHYSFS_file handle;
  35. struct __PHYSFS_FILEHANDLELIST__ *next;
  36. } FileHandleList;
  37. /* The various i/o drivers... */
  38. #if (defined PHYSFS_SUPPORTS_ZIP)
  39. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
  40. extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
  41. #endif
  42. #if (defined PHYSFS_SUPPORTS_GRP)
  43. extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
  44. extern const DirFunctions __PHYSFS_DirFunctions_GRP;
  45. #endif
  46. extern const DirFunctions __PHYSFS_DirFunctions_DIR;
  47. static const PHYSFS_ArchiveInfo *supported_types[] =
  48. {
  49. #if (defined PHYSFS_SUPPORTS_ZIP)
  50. &__PHYSFS_ArchiveInfo_ZIP,
  51. #endif
  52. #if (defined PHYSFS_SUPPORTS_GRP)
  53. &__PHYSFS_ArchiveInfo_GRP,
  54. #endif
  55. NULL
  56. };
  57. static const DirFunctions *dirFunctions[] =
  58. {
  59. #if (defined PHYSFS_SUPPORTS_ZIP)
  60. &__PHYSFS_DirFunctions_ZIP,
  61. #endif
  62. #if (defined PHYSFS_SUPPORTS_GRP)
  63. &__PHYSFS_DirFunctions_GRP,
  64. #endif
  65. &__PHYSFS_DirFunctions_DIR,
  66. NULL
  67. };
  68. /* General PhysicsFS state ... */
  69. static int initialized = 0;
  70. static ErrMsg *errorMessages = NULL;
  71. static DirInfo *searchPath = NULL;
  72. static DirInfo *writeDir = NULL;
  73. static FileHandleList *openWriteList = NULL;
  74. static FileHandleList *openReadList = NULL;
  75. static char *baseDir = NULL;
  76. static char *userDir = NULL;
  77. static int allowSymLinks = 0;
  78. /* functions ... */
  79. static ErrMsg *findErrorForCurrentThread(void)
  80. {
  81. ErrMsg *i;
  82. int tid;
  83. if (errorMessages != NULL)
  84. {
  85. tid = __PHYSFS_platformGetThreadID();
  86. for (i = errorMessages; i != NULL; i = i->next)
  87. {
  88. if (i->tid == tid)
  89. return(i);
  90. } /* for */
  91. } /* if */
  92. return(NULL); /* no error available. */
  93. } /* findErrorForCurrentThread */
  94. void __PHYSFS_setError(const char *str)
  95. {
  96. ErrMsg *err;
  97. if (str == NULL)
  98. return;
  99. err = findErrorForCurrentThread();
  100. if (err == NULL)
  101. {
  102. err = (ErrMsg *) malloc(sizeof (ErrMsg));
  103. if (err == NULL)
  104. return; /* uhh...? */
  105. memset((void *) err, '\0', sizeof (ErrMsg));
  106. err->tid = __PHYSFS_platformGetThreadID();
  107. err->next = errorMessages;
  108. errorMessages = err;
  109. } /* if */
  110. err->errorAvailable = 1;
  111. strncpy(err->errorString, str, sizeof (err->errorString));
  112. err->errorString[sizeof (err->errorString) - 1] = '\0';
  113. } /* __PHYSFS_setError */
  114. static void freeErrorMessages(void)
  115. {
  116. ErrMsg *i;
  117. ErrMsg *next;
  118. for (i = errorMessages; i != NULL; i = next)
  119. {
  120. next = i->next;
  121. free(i);
  122. } /* for */
  123. } /* freeErrorMessages */
  124. const char *PHYSFS_getLastError(void)
  125. {
  126. ErrMsg *err = findErrorForCurrentThread();
  127. if ((err == NULL) || (!err->errorAvailable))
  128. return(NULL);
  129. err->errorAvailable = 0;
  130. return(err->errorString);
  131. } /* PHYSFS_getLastError */
  132. void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
  133. {
  134. if (ver != NULL)
  135. {
  136. ver->major = PHYSFS_VER_MAJOR;
  137. ver->minor = PHYSFS_VER_MINOR;
  138. ver->patch = PHYSFS_VER_PATCH;
  139. } /* if */
  140. } /* PHYSFS_getLinkedVersion */
  141. static DirHandle *openDirectory(const char *d, int forWriting)
  142. {
  143. const DirFunctions **i;
  144. BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
  145. for (i = dirFunctions; *i != NULL; i++)
  146. {
  147. if ((*i)->isArchive(d, forWriting))
  148. return( (*i)->openArchive(d, forWriting) );
  149. } /* for */
  150. __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
  151. return(NULL);
  152. } /* openDirectory */
  153. static DirInfo *buildDirInfo(const char *newDir, int forWriting)
  154. {
  155. DirHandle *dirHandle = NULL;
  156. DirInfo *di = NULL;
  157. BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
  158. dirHandle = openDirectory(newDir, forWriting);
  159. BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
  160. di = (DirInfo *) malloc(sizeof (DirInfo));
  161. if (di == NULL)
  162. dirHandle->funcs->dirClose(dirHandle);
  163. BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
  164. di->dirName = (char *) malloc(strlen(newDir) + 1);
  165. if (di->dirName == NULL)
  166. {
  167. free(di);
  168. dirHandle->funcs->dirClose(dirHandle);
  169. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  170. return(0);
  171. } /* if */
  172. di->next = NULL;
  173. di->dirHandle = dirHandle;
  174. strcpy(di->dirName, newDir);
  175. return(di);
  176. } /* buildDirInfo */
  177. static int freeDirInfo(DirInfo *di, FileHandleList *openList)
  178. {
  179. FileHandleList *i;
  180. if (di == NULL)
  181. return(1);
  182. for (i = openList; i != NULL; i = i->next)
  183. {
  184. const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
  185. BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
  186. } /* for */
  187. di->dirHandle->funcs->dirClose(di->dirHandle);
  188. free(di->dirName);
  189. free(di);
  190. return(1);
  191. } /* freeDirInfo */
  192. static char *calculateUserDir(void)
  193. {
  194. char *retval = NULL;
  195. const char *str = NULL;
  196. str = __PHYSFS_platformGetUserDir();
  197. if (str != NULL)
  198. retval = (char *) str;
  199. else
  200. {
  201. const char *dirsep = PHYSFS_getDirSeparator();
  202. const char *uname = __PHYSFS_platformGetUserName();
  203. str = (uname != NULL) ? uname : "default";
  204. retval = (char *) malloc(strlen(baseDir) + strlen(str) +
  205. (strlen(dirsep) * 2) + 6);
  206. if (retval == NULL)
  207. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  208. else
  209. sprintf(retval, "%s%susers%s%s", baseDir, dirsep, dirsep, str);
  210. if (uname != NULL)
  211. free((void *) uname);
  212. } /* else */
  213. return(retval);
  214. } /* calculateUserDir */
  215. static int appendDirSep(char **dir)
  216. {
  217. const char *dirsep = PHYSFS_getDirSeparator();
  218. char *ptr;
  219. if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
  220. return(1);
  221. ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
  222. if (!ptr)
  223. {
  224. free(*dir);
  225. return(0);
  226. } /* if */
  227. strcat(ptr, dirsep);
  228. *dir = ptr;
  229. return(1);
  230. } /* appendDirSep */
  231. static char *calculateBaseDir(const char *argv0)
  232. {
  233. const char *dirsep = PHYSFS_getDirSeparator();
  234. char *retval;
  235. char *ptr;
  236. /*
  237. * See if the platform driver wants to handle this for us...
  238. */
  239. retval = __PHYSFS_platformCalcBaseDir(argv0);
  240. if (retval != NULL)
  241. return(retval);
  242. /*
  243. * Determine if there's a path on argv0. If there is, that's the base dir.
  244. */
  245. ptr = strstr(argv0, dirsep);
  246. if (ptr != NULL)
  247. {
  248. char *p = ptr;
  249. size_t size;
  250. while (p != NULL)
  251. {
  252. ptr = p;
  253. p = strstr(p + 1, dirsep);
  254. } /* while */
  255. size = (size_t) (ptr - argv0); /* !!! is this portable? */
  256. retval = (char *) malloc(size + 1);
  257. BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
  258. memcpy(retval, argv0, size);
  259. retval[size] = '\0';
  260. return(retval);
  261. } /* if */
  262. /*
  263. * Last ditch effort: it's the current working directory. (*shrug*)
  264. */
  265. retval = __PHYSFS_platformCurrentDir();
  266. if(retval != NULL) {
  267. return(retval);
  268. }
  269. /*
  270. * Ok, current directory doesn't exist, use the root directory.
  271. * Not a good alternative, but it only happens if the current
  272. * directory was deleted from under the program.
  273. */
  274. retval = (char *) malloc(strlen(dirsep) + 1);
  275. strcpy(retval, dirsep);
  276. return(retval);
  277. } /* calculateBaseDir */
  278. int PHYSFS_init(const char *argv0)
  279. {
  280. char *ptr;
  281. BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
  282. BAIL_IF_MACRO(argv0 == NULL, ERR_INVALID_ARGUMENT, 0);
  283. BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
  284. baseDir = calculateBaseDir(argv0);
  285. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  286. ptr = __PHYSFS_platformRealPath(baseDir);
  287. free(baseDir);
  288. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  289. baseDir = ptr;
  290. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  291. userDir = calculateUserDir();
  292. if (userDir != NULL)
  293. {
  294. ptr = __PHYSFS_platformRealPath(userDir);
  295. free(userDir);
  296. userDir = ptr;
  297. } /* if */
  298. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  299. {
  300. free(baseDir);
  301. baseDir = NULL;
  302. return(0);
  303. } /* if */
  304. initialized = 1;
  305. return(1);
  306. } /* PHYSFS_init */
  307. static int closeFileHandleList(FileHandleList **list)
  308. {
  309. FileHandleList *i;
  310. FileHandleList *next = NULL;
  311. FileHandle *h;
  312. for (i = *list; i != NULL; i = next)
  313. {
  314. next = i->next;
  315. h = (FileHandle *) (i->handle.opaque);
  316. if (!h->funcs->fileClose(h))
  317. {
  318. *list = i;
  319. return(0);
  320. } /* if */
  321. free(i);
  322. } /* for */
  323. *list = NULL;
  324. return(1);
  325. } /* closeFileHandleList */
  326. static void freeSearchPath(void)
  327. {
  328. DirInfo *i;
  329. DirInfo *next = NULL;
  330. closeFileHandleList(&openReadList);
  331. if (searchPath != NULL)
  332. {
  333. for (i = searchPath; i != NULL; i = next)
  334. {
  335. next = i->next;
  336. freeDirInfo(i, openReadList);
  337. } /* for */
  338. searchPath = NULL;
  339. } /* if */
  340. } /* freeSearchPath */
  341. int PHYSFS_deinit(void)
  342. {
  343. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  344. BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
  345. closeFileHandleList(&openWriteList);
  346. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  347. freeSearchPath();
  348. freeErrorMessages();
  349. if (baseDir != NULL)
  350. {
  351. free(baseDir);
  352. baseDir = NULL;
  353. } /* if */
  354. if (userDir != NULL)
  355. {
  356. free(userDir);
  357. userDir = NULL;
  358. } /* if */
  359. allowSymLinks = 0;
  360. initialized = 0;
  361. return(1);
  362. } /* PHYSFS_deinit */
  363. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  364. {
  365. return(supported_types);
  366. } /* PHYSFS_supportedArchiveTypes */
  367. void PHYSFS_freeList(void *list)
  368. {
  369. void **i;
  370. for (i = (void **) list; *i != NULL; i++)
  371. free(*i);
  372. free(list);
  373. } /* PHYSFS_freeList */
  374. const char *PHYSFS_getDirSeparator(void)
  375. {
  376. return(__PHYSFS_platformDirSeparator);
  377. } /* PHYSFS_getDirSeparator */
  378. char **PHYSFS_getCdRomDirs(void)
  379. {
  380. return(__PHYSFS_platformDetectAvailableCDs());
  381. } /* PHYSFS_getCdRomDirs */
  382. const char *PHYSFS_getBaseDir(void)
  383. {
  384. return(baseDir); /* this is calculated in PHYSFS_init()... */
  385. } /* PHYSFS_getBaseDir */
  386. const char *PHYSFS_getUserDir(void)
  387. {
  388. return(userDir); /* this is calculated in PHYSFS_init()... */
  389. } /* PHYSFS_getUserDir */
  390. const char *PHYSFS_getWriteDir(void)
  391. {
  392. if (writeDir == NULL)
  393. return(NULL);
  394. return(writeDir->dirName);
  395. } /* PHYSFS_getWriteDir */
  396. int PHYSFS_setWriteDir(const char *newDir)
  397. {
  398. if (writeDir != NULL)
  399. {
  400. BAIL_IF_MACRO(!freeDirInfo(writeDir, openWriteList), NULL, 0);
  401. writeDir = NULL;
  402. } /* if */
  403. if (newDir != NULL)
  404. {
  405. writeDir = buildDirInfo(newDir, 1);
  406. return(writeDir != NULL);
  407. } /* if */
  408. return(1);
  409. } /* PHYSFS_setWriteDir */
  410. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  411. {
  412. DirInfo *di;
  413. DirInfo *i = searchPath;
  414. DirInfo *prev = NULL;
  415. while (i != NULL)
  416. {
  417. if (strcmp(newDir, i->dirName) == 0) /* already in search path. */
  418. return(1);
  419. prev = i;
  420. i = i->next;
  421. } /* while */
  422. di = buildDirInfo(newDir, 0);
  423. BAIL_IF_MACRO(di == NULL, NULL, 0);
  424. if (appendToPath)
  425. {
  426. di->next = NULL;
  427. if (prev == NULL)
  428. searchPath = di;
  429. else
  430. prev->next = di;
  431. } /* if */
  432. else
  433. {
  434. di->next = searchPath;
  435. searchPath = di;
  436. } /* else */
  437. return(1);
  438. } /* PHYSFS_addToSearchPath */
  439. int PHYSFS_removeFromSearchPath(const char *oldDir)
  440. {
  441. DirInfo *i;
  442. DirInfo *prev = NULL;
  443. DirInfo *next = NULL;
  444. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  445. for (i = searchPath; i != NULL; i = i->next)
  446. {
  447. if (strcmp(i->dirName, oldDir) == 0)
  448. {
  449. next = i->next;
  450. BAIL_IF_MACRO(!freeDirInfo(i, openReadList), NULL, 0);
  451. if (prev == NULL)
  452. searchPath = next;
  453. else
  454. prev->next = next;
  455. return(1);
  456. } /* if */
  457. prev = i;
  458. } /* for */
  459. __PHYSFS_setError(ERR_NOT_IN_SEARCH_PATH);
  460. return(0);
  461. } /* PHYSFS_removeFromSearchPath */
  462. char **PHYSFS_getSearchPath(void)
  463. {
  464. int count = 1;
  465. int x;
  466. DirInfo *i;
  467. char **retval;
  468. for (i = searchPath; i != NULL; i = i->next)
  469. count++;
  470. retval = (char **) malloc(sizeof (char *) * count);
  471. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  472. count--;
  473. retval[count] = NULL;
  474. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  475. {
  476. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  477. if (retval[x] == NULL) /* this is friggin' ugly. */
  478. {
  479. while (x > 0)
  480. {
  481. x--;
  482. free(retval[x]);
  483. } /* while */
  484. free(retval);
  485. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  486. return(NULL);
  487. } /* if */
  488. strcpy(retval[x], i->dirName);
  489. } /* for */
  490. return(retval);
  491. } /* PHYSFS_getSearchPath */
  492. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  493. const char *archiveExt, int includeCdRoms,
  494. int archivesFirst)
  495. {
  496. const char *basedir = PHYSFS_getBaseDir();
  497. const char *userdir = PHYSFS_getUserDir();
  498. const char *dirsep = PHYSFS_getDirSeparator();
  499. char *str;
  500. /* set write dir... */
  501. str = malloc(strlen(userdir) + (strlen(organization) * 2) +
  502. (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
  503. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  504. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  505. if (!PHYSFS_setWriteDir(str))
  506. {
  507. int no_write = 0;
  508. sprintf(str, ".%s/%s", organization, appName);
  509. if ( (PHYSFS_setWriteDir(userdir)) &&
  510. (PHYSFS_mkdir(str)) )
  511. {
  512. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  513. if (!PHYSFS_setWriteDir(str))
  514. no_write = 1;
  515. } /* if */
  516. else
  517. {
  518. no_write = 1;
  519. } /* else */
  520. if (no_write)
  521. {
  522. PHYSFS_setWriteDir(NULL); /* just in case. */
  523. free(str);
  524. BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
  525. } /* if */
  526. } /* if */
  527. /* Put write dir first in search path... */
  528. PHYSFS_addToSearchPath(str, 0);
  529. free(str);
  530. /* Put base path on search path... */
  531. PHYSFS_addToSearchPath(basedir, 1);
  532. /* handle CD-ROMs... */
  533. if (includeCdRoms)
  534. {
  535. char **cds = PHYSFS_getCdRomDirs();
  536. char **i;
  537. for (i = cds; *i != NULL; i++)
  538. PHYSFS_addToSearchPath(*i, 1);
  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. size_t extlen = strlen(archiveExt);
  547. char *ext;
  548. for (i = rc; *i != NULL; i++)
  549. {
  550. size_t 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. size_t 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_convertToDependent */
  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. FileHandle *rc = NULL;
  902. FileHandleList *list;
  903. DirInfo *i;
  904. while (*fname == '/')
  905. fname++;
  906. for (i = searchPath; i != NULL; i = i->next)
  907. {
  908. DirHandle *h = i->dirHandle;
  909. if (__PHYSFS_verifySecurity(h, fname))
  910. {
  911. rc = h->funcs->openRead(h, fname);
  912. if (rc != NULL)
  913. break;
  914. } /* if */
  915. } /* for */
  916. if (rc == NULL)
  917. return(NULL);
  918. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  919. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  920. list->handle.opaque = (void *) rc;
  921. list->next = openReadList;
  922. openReadList = list;
  923. return(&(list->handle));
  924. } /* PHYSFS_openRead */
  925. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  926. {
  927. FileHandle *h = (FileHandle *) handle->opaque;
  928. FileHandleList *prev = NULL;
  929. FileHandleList *i;
  930. int rc;
  931. for (i = *list; i != NULL; i = i->next)
  932. {
  933. if (&i->handle == handle) /* handle is in this list? */
  934. {
  935. rc = h->funcs->fileClose(h);
  936. if (!rc)
  937. return(-1);
  938. if (prev == NULL)
  939. *list = i->next;
  940. else
  941. prev->next = i->next;
  942. free(i);
  943. return(1);
  944. } /* if */
  945. prev = i;
  946. } /* for */
  947. return(0);
  948. } /* closeHandleInOpenList */
  949. int PHYSFS_close(PHYSFS_file *handle)
  950. {
  951. int rc;
  952. /* -1 == close failure. 0 == not found. 1 == success. */
  953. rc = closeHandleInOpenList(&openReadList, handle);
  954. BAIL_IF_MACRO(rc == -1, NULL, 0);
  955. if (!rc)
  956. {
  957. rc = closeHandleInOpenList(&openWriteList, handle);
  958. BAIL_IF_MACRO(rc == -1, NULL, 0);
  959. } /* if */
  960. if (!rc)
  961. __PHYSFS_setError(ERR_NOT_A_HANDLE);
  962. return(rc);
  963. } /* PHYSFS_close */
  964. PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
  965. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  966. {
  967. FileHandle *h = (FileHandle *) handle->opaque;
  968. assert(h != NULL);
  969. assert(h->funcs != NULL);
  970. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  971. return(h->funcs->read(h, buffer, objSize, objCount));
  972. } /* PHYSFS_read */
  973. PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  974. PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
  975. {
  976. FileHandle *h = (FileHandle *) handle->opaque;
  977. assert(h != NULL);
  978. assert(h->funcs != NULL);
  979. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  980. return(h->funcs->write(h, buffer, objSize, objCount));
  981. } /* PHYSFS_write */
  982. int PHYSFS_eof(PHYSFS_file *handle)
  983. {
  984. FileHandle *h = (FileHandle *) handle->opaque;
  985. assert(h != NULL);
  986. assert(h->funcs != NULL);
  987. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  988. return(h->funcs->eof(h));
  989. } /* PHYSFS_eof */
  990. PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
  991. {
  992. FileHandle *h = (FileHandle *) handle->opaque;
  993. assert(h != NULL);
  994. assert(h->funcs != NULL);
  995. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  996. return(h->funcs->tell(h));
  997. } /* PHYSFS_tell */
  998. int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
  999. {
  1000. FileHandle *h = (FileHandle *) handle->opaque;
  1001. assert(h != NULL);
  1002. assert(h->funcs != NULL);
  1003. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1004. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  1005. return(h->funcs->seek(h, pos));
  1006. } /* PHYSFS_seek */
  1007. PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
  1008. {
  1009. FileHandle *h = (FileHandle *) handle->opaque;
  1010. assert(h != NULL);
  1011. assert(h->funcs != NULL);
  1012. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1013. return(h->funcs->fileLength(h));
  1014. } /* PHYSFS_filelength */
  1015. /* end of physfs.c ... */