physfs.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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. baseDir = calculateBaseDir(argv0);
  284. BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
  285. ptr = __PHYSFS_platformRealPath(baseDir);
  286. free(baseDir);
  287. BAIL_IF_MACRO(ptr == NULL, NULL, 0);
  288. baseDir = ptr;
  289. BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
  290. userDir = calculateUserDir();
  291. if (userDir != NULL)
  292. {
  293. ptr = __PHYSFS_platformRealPath(userDir);
  294. free(userDir);
  295. userDir = ptr;
  296. } /* if */
  297. if ((userDir == NULL) || (!appendDirSep(&userDir)))
  298. {
  299. free(baseDir);
  300. baseDir = NULL;
  301. return(0);
  302. } /* if */
  303. initialized = 1;
  304. return(1);
  305. } /* PHYSFS_init */
  306. static int closeFileHandleList(FileHandleList **list)
  307. {
  308. FileHandleList *i;
  309. FileHandleList *next = NULL;
  310. FileHandle *h;
  311. for (i = *list; i != NULL; i = next)
  312. {
  313. next = i->next;
  314. h = (FileHandle *) (i->handle.opaque);
  315. if (!h->funcs->fileClose(h))
  316. {
  317. *list = i;
  318. return(0);
  319. } /* if */
  320. free(i);
  321. } /* for */
  322. *list = NULL;
  323. return(1);
  324. } /* closeFileHandleList */
  325. static void freeSearchPath(void)
  326. {
  327. DirInfo *i;
  328. DirInfo *next = NULL;
  329. closeFileHandleList(&openReadList);
  330. if (searchPath != NULL)
  331. {
  332. for (i = searchPath; i != NULL; i = next)
  333. {
  334. next = i->next;
  335. freeDirInfo(i, openReadList);
  336. } /* for */
  337. searchPath = NULL;
  338. } /* if */
  339. } /* freeSearchPath */
  340. int PHYSFS_deinit(void)
  341. {
  342. BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
  343. closeFileHandleList(&openWriteList);
  344. BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
  345. freeSearchPath();
  346. freeErrorMessages();
  347. if (baseDir != NULL)
  348. {
  349. free(baseDir);
  350. baseDir = NULL;
  351. } /* if */
  352. if (userDir != NULL)
  353. {
  354. free(userDir);
  355. userDir = NULL;
  356. } /* if */
  357. allowSymLinks = 0;
  358. initialized = 0;
  359. return(1);
  360. } /* PHYSFS_deinit */
  361. const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
  362. {
  363. return(supported_types);
  364. } /* PHYSFS_supportedArchiveTypes */
  365. void PHYSFS_freeList(void *list)
  366. {
  367. void **i;
  368. for (i = (void **) list; *i != NULL; i++)
  369. free(*i);
  370. free(list);
  371. } /* PHYSFS_freeList */
  372. const char *PHYSFS_getDirSeparator(void)
  373. {
  374. return(__PHYSFS_platformDirSeparator);
  375. } /* PHYSFS_getDirSeparator */
  376. char **PHYSFS_getCdRomDirs(void)
  377. {
  378. return(__PHYSFS_platformDetectAvailableCDs());
  379. } /* PHYSFS_getCdRomDirs */
  380. const char *PHYSFS_getBaseDir(void)
  381. {
  382. return(baseDir); /* this is calculated in PHYSFS_init()... */
  383. } /* PHYSFS_getBaseDir */
  384. const char *PHYSFS_getUserDir(void)
  385. {
  386. return(userDir); /* this is calculated in PHYSFS_init()... */
  387. } /* PHYSFS_getUserDir */
  388. const char *PHYSFS_getWriteDir(void)
  389. {
  390. if (writeDir == NULL)
  391. return(NULL);
  392. return(writeDir->dirName);
  393. } /* PHYSFS_getWriteDir */
  394. int PHYSFS_setWriteDir(const char *newDir)
  395. {
  396. if (writeDir != NULL)
  397. {
  398. BAIL_IF_MACRO(!freeDirInfo(writeDir, openWriteList), NULL, 0);
  399. writeDir = NULL;
  400. } /* if */
  401. if (newDir != NULL)
  402. {
  403. writeDir = buildDirInfo(newDir, 1);
  404. return(writeDir != NULL);
  405. } /* if */
  406. return(1);
  407. } /* PHYSFS_setWriteDir */
  408. int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
  409. {
  410. DirInfo *di;
  411. DirInfo *i = searchPath;
  412. DirInfo *prev = NULL;
  413. while (i != NULL)
  414. {
  415. if (strcmp(newDir, i->dirName) == 0) /* already in search path. */
  416. return(1);
  417. prev = i;
  418. i = i->next;
  419. } /* while */
  420. di = buildDirInfo(newDir, 0);
  421. BAIL_IF_MACRO(di == NULL, NULL, 0);
  422. if (appendToPath)
  423. {
  424. di->next = NULL;
  425. if (prev == NULL)
  426. searchPath = di;
  427. else
  428. prev->next = di;
  429. } /* if */
  430. else
  431. {
  432. di->next = searchPath;
  433. searchPath = di;
  434. } /* else */
  435. return(1);
  436. } /* PHYSFS_addToSearchPath */
  437. int PHYSFS_removeFromSearchPath(const char *oldDir)
  438. {
  439. DirInfo *i;
  440. DirInfo *prev = NULL;
  441. DirInfo *next = NULL;
  442. BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
  443. for (i = searchPath; i != NULL; i = i->next)
  444. {
  445. if (strcmp(i->dirName, oldDir) == 0)
  446. {
  447. next = i->next;
  448. BAIL_IF_MACRO(!freeDirInfo(i, openReadList), NULL, 0);
  449. if (prev == NULL)
  450. searchPath = next;
  451. else
  452. prev->next = next;
  453. return(1);
  454. } /* if */
  455. prev = i;
  456. } /* for */
  457. __PHYSFS_setError(ERR_NOT_IN_SEARCH_PATH);
  458. return(0);
  459. } /* PHYSFS_removeFromSearchPath */
  460. char **PHYSFS_getSearchPath(void)
  461. {
  462. int count = 1;
  463. int x;
  464. DirInfo *i;
  465. char **retval;
  466. for (i = searchPath; i != NULL; i = i->next)
  467. count++;
  468. retval = (char **) malloc(sizeof (char *) * count);
  469. BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
  470. count--;
  471. retval[count] = NULL;
  472. for (i = searchPath, x = 0; x < count; i = i->next, x++)
  473. {
  474. retval[x] = (char *) malloc(strlen(i->dirName) + 1);
  475. if (retval[x] == NULL) /* this is friggin' ugly. */
  476. {
  477. while (x > 0)
  478. {
  479. x--;
  480. free(retval[x]);
  481. } /* while */
  482. free(retval);
  483. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  484. return(NULL);
  485. } /* if */
  486. strcpy(retval[x], i->dirName);
  487. } /* for */
  488. return(retval);
  489. } /* PHYSFS_getSearchPath */
  490. int PHYSFS_setSaneConfig(const char *organization, const char *appName,
  491. const char *archiveExt, int includeCdRoms,
  492. int archivesFirst)
  493. {
  494. const char *basedir = PHYSFS_getBaseDir();
  495. const char *userdir = PHYSFS_getUserDir();
  496. const char *dirsep = PHYSFS_getDirSeparator();
  497. char *str;
  498. /* set write dir... */
  499. str = malloc(strlen(userdir) + (strlen(organization) * 2) +
  500. (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
  501. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  502. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  503. if (!PHYSFS_setWriteDir(str))
  504. {
  505. if ( (!PHYSFS_setWriteDir(userdir)) ||
  506. (!PHYSFS_mkdir(str + strlen(userdir))) )
  507. {
  508. PHYSFS_setWriteDir(NULL);
  509. free(str);
  510. BAIL_IF_MACRO(1, ERR_CANT_SET_WRITE_DIR, 0);
  511. } /* if */
  512. } /* if */
  513. /* Put write dir related dirs on search path... */
  514. PHYSFS_addToSearchPath(str, 1);
  515. free(str);
  516. /* Put base path on search path... */
  517. PHYSFS_addToSearchPath(basedir, 1);
  518. /* handle CD-ROMs... */
  519. if (includeCdRoms)
  520. {
  521. char **cds = PHYSFS_getCdRomDirs();
  522. char **i;
  523. for (i = cds; *i != NULL; i++)
  524. PHYSFS_addToSearchPath(*i, 1);
  525. PHYSFS_freeList(cds);
  526. } /* if */
  527. /* Root out archives, and add them to search path... */
  528. if (archiveExt != NULL)
  529. {
  530. char **rc = PHYSFS_enumerateFiles("");
  531. char **i;
  532. size_t extlen = strlen(archiveExt);
  533. char *ext;
  534. for (i = rc; *i != NULL; i++)
  535. {
  536. size_t l = strlen(*i);
  537. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  538. {
  539. ext = (*i) + (l - extlen);
  540. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  541. {
  542. const char *d = PHYSFS_getRealDir(*i);
  543. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  544. if (str != NULL)
  545. {
  546. sprintf(str, "%s%s%s", d, dirsep, *i);
  547. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  548. free(str);
  549. } /* if */
  550. } /* if */
  551. } /* if */
  552. } /* for */
  553. PHYSFS_freeList(rc);
  554. } /* if */
  555. return(1);
  556. } /* PHYSFS_setSaneConfig */
  557. void PHYSFS_permitSymbolicLinks(int allow)
  558. {
  559. allowSymLinks = allow;
  560. } /* PHYSFS_permitSymbolicLinks */
  561. /* string manipulation in C makes my ass itch. */
  562. char * __PHYSFS_convertToDependent(const char *prepend,
  563. const char *dirName,
  564. const char *append)
  565. {
  566. const char *dirsep = PHYSFS_getDirSeparator();
  567. size_t sepsize = strlen(dirsep);
  568. char *str;
  569. char *i1;
  570. char *i2;
  571. size_t allocSize;
  572. while (*dirName == '/')
  573. dirName++;
  574. allocSize = strlen(dirName) + 1;
  575. if (prepend != NULL)
  576. allocSize += strlen(prepend) + sepsize;
  577. if (append != NULL)
  578. allocSize += strlen(append) + sepsize;
  579. /* make sure there's enough space if the dir separator is bigger. */
  580. if (sepsize > 1)
  581. {
  582. str = (char *) dirName;
  583. do
  584. {
  585. str = strchr(str, '/');
  586. if (str != NULL)
  587. {
  588. allocSize += (sepsize - 1);
  589. str++;
  590. } /* if */
  591. } while (str != NULL);
  592. } /* if */
  593. str = (char *) malloc(allocSize);
  594. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  595. if (prepend == NULL)
  596. *str = '\0';
  597. else
  598. {
  599. strcpy(str, prepend);
  600. strcat(str, dirsep);
  601. } /* else */
  602. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  603. {
  604. if (*i1 == '/')
  605. {
  606. strcpy(i2, dirsep);
  607. i2 += sepsize;
  608. } /* if */
  609. else
  610. {
  611. *i2 = *i1;
  612. } /* else */
  613. } /* for */
  614. *i2 = '\0';
  615. if (append)
  616. {
  617. strcat(str, dirsep);
  618. strcpy(str, append);
  619. } /* if */
  620. return(str);
  621. } /* __PHYSFS_convertToDependentNotation */
  622. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
  623. {
  624. int retval = 1;
  625. char *start;
  626. char *end;
  627. char *str;
  628. start = str = malloc(strlen(fname) + 1);
  629. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  630. strcpy(str, fname);
  631. while (1)
  632. {
  633. end = strchr(start, '/');
  634. if (end != NULL)
  635. *end = '\0';
  636. if ( (strcmp(start, ".") == 0) ||
  637. (strcmp(start, "..") == 0) ||
  638. (strchr(start, '\\') != NULL) ||
  639. (strchr(start, ':') != NULL) )
  640. {
  641. __PHYSFS_setError(ERR_INSECURE_FNAME);
  642. retval = 0;
  643. break;
  644. } /* if */
  645. if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
  646. {
  647. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  648. retval = 0;
  649. break;
  650. } /* if */
  651. if (end == NULL)
  652. break;
  653. *end = '/';
  654. start = end + 1;
  655. } /* while */
  656. free(str);
  657. return(retval);
  658. } /* __PHYSFS_verifySecurity */
  659. int PHYSFS_mkdir(const char *dirName)
  660. {
  661. DirHandle *h;
  662. char *str;
  663. char *start;
  664. char *end;
  665. int retval = 0;
  666. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  667. h = writeDir->dirHandle;
  668. while (*dirName == '/')
  669. dirName++;
  670. BAIL_IF_MACRO(h->funcs->mkdir == NULL, ERR_NOT_SUPPORTED, 0);
  671. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, dirName), NULL, 0);
  672. start = str = malloc(strlen(dirName) + 1);
  673. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  674. strcpy(str, dirName);
  675. while (1)
  676. {
  677. end = strchr(start, '/');
  678. if (end != NULL)
  679. *end = '\0';
  680. retval = h->funcs->mkdir(h, str);
  681. if (!retval)
  682. break;
  683. if (end == NULL)
  684. break;
  685. *end = '/';
  686. start = end + 1;
  687. } /* while */
  688. free(str);
  689. return(retval);
  690. } /* PHYSFS_mkdir */
  691. int PHYSFS_delete(const char *fname)
  692. {
  693. DirHandle *h;
  694. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  695. h = writeDir->dirHandle;
  696. BAIL_IF_MACRO(h->funcs->remove == NULL, ERR_NOT_SUPPORTED, 0);
  697. while (*fname == '/')
  698. fname++;
  699. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, 0);
  700. return(h->funcs->remove(h, fname));
  701. } /* PHYSFS_delete */
  702. const char *PHYSFS_getRealDir(const char *filename)
  703. {
  704. DirInfo *i;
  705. while (*filename == '/')
  706. filename++;
  707. for (i = searchPath; i != NULL; i = i->next)
  708. {
  709. DirHandle *h = i->dirHandle;
  710. if (__PHYSFS_verifySecurity(h, filename))
  711. {
  712. if (h->funcs->exists(h, filename))
  713. return(i->dirName);
  714. } /* if */
  715. } /* for */
  716. return(NULL);
  717. } /* PHYSFS_getRealDir */
  718. static int countList(LinkedStringList *list)
  719. {
  720. int retval = 0;
  721. LinkedStringList *i;
  722. for (i = list; i != NULL; i = i->next)
  723. retval++;
  724. return(retval);
  725. } /* countList */
  726. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  727. {
  728. int i;
  729. LinkedStringList *next = NULL;
  730. int len = countList(finalList);
  731. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  732. if (retval == NULL)
  733. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  734. for (i = 0; i < len; i++)
  735. {
  736. next = finalList->next;
  737. if (retval == NULL)
  738. free(finalList->str);
  739. else
  740. retval[i] = finalList->str;
  741. free(finalList);
  742. finalList = next;
  743. } /* for */
  744. if (retval != NULL)
  745. retval[i] = NULL;
  746. return(retval);
  747. } /* convertStringListToPhysFSList */
  748. static void insertStringListItem(LinkedStringList **final,
  749. LinkedStringList *item)
  750. {
  751. LinkedStringList *i;
  752. LinkedStringList *prev = NULL;
  753. int rc;
  754. for (i = *final; i != NULL; i = i->next)
  755. {
  756. rc = strcmp(i->str, item->str);
  757. if (rc > 0) /* insertion point. */
  758. break;
  759. else if (rc == 0) /* already in list. */
  760. {
  761. free(item->str);
  762. free(item);
  763. return;
  764. } /* else if */
  765. prev = i;
  766. } /* for */
  767. /*
  768. * If we are here, we are either at the insertion point.
  769. * This may be the end of the list, or the list may be empty, too.
  770. */
  771. if (prev == NULL)
  772. *final = item;
  773. else
  774. prev->next = item;
  775. item->next = i;
  776. } /* insertStringListItem */
  777. /* if we run out of memory anywhere in here, we give back what we can. */
  778. static void interpolateStringLists(LinkedStringList **final,
  779. LinkedStringList *newList)
  780. {
  781. LinkedStringList *next = NULL;
  782. while (newList != NULL)
  783. {
  784. next = newList->next;
  785. insertStringListItem(final, newList);
  786. newList = next;
  787. } /* while */
  788. } /* interpolateStringLists */
  789. char **PHYSFS_enumerateFiles(const char *path)
  790. {
  791. DirInfo *i;
  792. char **retval = NULL;
  793. LinkedStringList *rc;
  794. LinkedStringList *finalList = NULL;
  795. int omitSymLinks = !allowSymLinks;
  796. while (*path == '/')
  797. path++;
  798. for (i = searchPath; i != NULL; i = i->next)
  799. {
  800. DirHandle *h = i->dirHandle;
  801. if (__PHYSFS_verifySecurity(h, path))
  802. {
  803. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  804. interpolateStringLists(&finalList, rc);
  805. } /* if */
  806. } /* for */
  807. retval = convertStringListToPhysFSList(finalList);
  808. return(retval);
  809. } /* PHYSFS_enumerateFiles */
  810. int PHYSFS_exists(const char *fname)
  811. {
  812. while (*fname == '/')
  813. fname++;
  814. return(PHYSFS_getRealDir(fname) != NULL);
  815. } /* PHYSFS_exists */
  816. int PHYSFS_isDirectory(const char *fname)
  817. {
  818. DirInfo *i;
  819. while (*fname == '/')
  820. fname++;
  821. if (*fname == '\0')
  822. return(1);
  823. for (i = searchPath; i != NULL; i = i->next)
  824. {
  825. DirHandle *h = i->dirHandle;
  826. if (__PHYSFS_verifySecurity(h, fname))
  827. {
  828. if (h->funcs->exists(h, fname))
  829. return(h->funcs->isDirectory(h, fname));
  830. } /* if */
  831. } /* for */
  832. return(0);
  833. } /* PHYSFS_isDirectory */
  834. int PHYSFS_isSymbolicLink(const char *fname)
  835. {
  836. DirInfo *i;
  837. if (!allowSymLinks)
  838. return(0);
  839. while (*fname == '/')
  840. fname++;
  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->isSymLink(h, fname));
  848. } /* if */
  849. } /* for */
  850. return(0);
  851. } /* PHYSFS_isSymbolicLink */
  852. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  853. {
  854. PHYSFS_file *retval = NULL;
  855. FileHandle *rc = NULL;
  856. DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  857. const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
  858. FileHandleList *list;
  859. while (*fname == '/')
  860. fname++;
  861. BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
  862. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
  863. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  864. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  865. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  866. if (rc == NULL)
  867. free(list);
  868. else
  869. {
  870. list->handle.opaque = (void *) rc;
  871. list->next = openWriteList;
  872. openWriteList = list;
  873. retval = &(list->handle);
  874. } /* else */
  875. return(retval);
  876. } /* doOpenWrite */
  877. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  878. {
  879. return(doOpenWrite(filename, 0));
  880. } /* PHYSFS_openWrite */
  881. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  882. {
  883. return(doOpenWrite(filename, 1));
  884. } /* PHYSFS_openAppend */
  885. PHYSFS_file *PHYSFS_openRead(const char *fname)
  886. {
  887. FileHandle *rc = NULL;
  888. FileHandleList *list;
  889. DirInfo *i;
  890. while (*fname == '/')
  891. fname++;
  892. for (i = searchPath; i != NULL; i = i->next)
  893. {
  894. DirHandle *h = i->dirHandle;
  895. if (__PHYSFS_verifySecurity(h, fname))
  896. {
  897. rc = h->funcs->openRead(h, fname);
  898. if (rc != NULL)
  899. break;
  900. } /* if */
  901. } /* for */
  902. if (rc == NULL)
  903. return(NULL);
  904. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  905. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  906. list->handle.opaque = (void *) rc;
  907. list->next = openReadList;
  908. openReadList = list;
  909. return(&(list->handle));
  910. } /* PHYSFS_openRead */
  911. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  912. {
  913. FileHandle *h = (FileHandle *) handle->opaque;
  914. FileHandleList *prev = NULL;
  915. FileHandleList *i;
  916. int rc;
  917. for (i = *list; i != NULL; i = i->next)
  918. {
  919. if (&i->handle == handle) /* handle is in this list? */
  920. {
  921. rc = h->funcs->fileClose(h);
  922. if (!rc)
  923. return(-1);
  924. if (prev == NULL)
  925. *list = i->next;
  926. else
  927. prev->next = i->next;
  928. free(i);
  929. return(1);
  930. } /* if */
  931. prev = i;
  932. } /* for */
  933. return(0);
  934. } /* closeHandleInOpenList */
  935. int PHYSFS_close(PHYSFS_file *handle)
  936. {
  937. int rc;
  938. /* -1 == close failure. 0 == not found. 1 == success. */
  939. rc = closeHandleInOpenList(&openReadList, handle);
  940. BAIL_IF_MACRO(rc == -1, NULL, 0);
  941. if (!rc)
  942. {
  943. rc = closeHandleInOpenList(&openWriteList, handle);
  944. BAIL_IF_MACRO(rc == -1, NULL, 0);
  945. } /* if */
  946. if (!rc)
  947. __PHYSFS_setError(ERR_NOT_A_HANDLE);
  948. return(rc);
  949. } /* PHYSFS_close */
  950. int PHYSFS_read(PHYSFS_file *handle, void *buffer,
  951. unsigned int objSize, unsigned int objCount)
  952. {
  953. FileHandle *h = (FileHandle *) handle->opaque;
  954. assert(h != NULL);
  955. assert(h->funcs != NULL);
  956. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  957. return(h->funcs->read(h, buffer, objSize, objCount));
  958. } /* PHYSFS_read */
  959. int PHYSFS_write(PHYSFS_file *handle, void *buffer,
  960. unsigned int objSize, unsigned int objCount)
  961. {
  962. FileHandle *h = (FileHandle *) handle->opaque;
  963. assert(h != NULL);
  964. assert(h->funcs != NULL);
  965. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  966. return(h->funcs->write(h, buffer, objSize, objCount));
  967. } /* PHYSFS_write */
  968. int PHYSFS_eof(PHYSFS_file *handle)
  969. {
  970. FileHandle *h = (FileHandle *) handle->opaque;
  971. assert(h != NULL);
  972. assert(h->funcs != NULL);
  973. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  974. return(h->funcs->eof(h));
  975. } /* PHYSFS_eof */
  976. int PHYSFS_tell(PHYSFS_file *handle)
  977. {
  978. FileHandle *h = (FileHandle *) handle->opaque;
  979. assert(h != NULL);
  980. assert(h->funcs != NULL);
  981. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  982. return(h->funcs->tell(h));
  983. } /* PHYSFS_tell */
  984. int PHYSFS_seek(PHYSFS_file *handle, int pos)
  985. {
  986. FileHandle *h = (FileHandle *) handle->opaque;
  987. assert(h != NULL);
  988. assert(h->funcs != NULL);
  989. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  990. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  991. return(h->funcs->seek(h, pos));
  992. } /* PHYSFS_seek */
  993. int PHYSFS_fileLength(PHYSFS_file *handle)
  994. {
  995. FileHandle *h = (FileHandle *) handle->opaque;
  996. assert(h != NULL);
  997. assert(h->funcs != NULL);
  998. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  999. return(h->funcs->fileLength(h));
  1000. } /* PHYSFS_filelength */
  1001. /* end of physfs.c ... */