physfs.c 28 KB

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