physfs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295
  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. int no_write = 0;
  506. sprintf(str, ".%s/%s", organization, appName);
  507. if ( (PHYSFS_setWriteDir(userdir)) &&
  508. (PHYSFS_mkdir(str)) )
  509. {
  510. sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
  511. if (!PHYSFS_setWriteDir(str))
  512. no_write = 1;
  513. } /* if */
  514. else
  515. {
  516. no_write = 1;
  517. } /* else */
  518. if (no_write)
  519. {
  520. PHYSFS_setWriteDir(NULL); /* just in case. */
  521. free(str);
  522. BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
  523. } /* if */
  524. } /* if */
  525. /* Put write dir first in search path... */
  526. PHYSFS_addToSearchPath(str, 0);
  527. free(str);
  528. /* Put base path on search path... */
  529. PHYSFS_addToSearchPath(basedir, 1);
  530. /* handle CD-ROMs... */
  531. if (includeCdRoms)
  532. {
  533. char **cds = PHYSFS_getCdRomDirs();
  534. char **i;
  535. for (i = cds; *i != NULL; i++)
  536. PHYSFS_addToSearchPath(*i, 1);
  537. PHYSFS_freeList(cds);
  538. } /* if */
  539. /* Root out archives, and add them to search path... */
  540. if (archiveExt != NULL)
  541. {
  542. char **rc = PHYSFS_enumerateFiles("");
  543. char **i;
  544. size_t extlen = strlen(archiveExt);
  545. char *ext;
  546. for (i = rc; *i != NULL; i++)
  547. {
  548. size_t l = strlen(*i);
  549. if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
  550. {
  551. ext = (*i) + (l - extlen);
  552. if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
  553. {
  554. const char *d = PHYSFS_getRealDir(*i);
  555. str = malloc(strlen(d) + strlen(dirsep) + l + 1);
  556. if (str != NULL)
  557. {
  558. sprintf(str, "%s%s%s", d, dirsep, *i);
  559. PHYSFS_addToSearchPath(str, archivesFirst == 0);
  560. free(str);
  561. } /* if */
  562. } /* if */
  563. } /* if */
  564. } /* for */
  565. PHYSFS_freeList(rc);
  566. } /* if */
  567. return(1);
  568. } /* PHYSFS_setSaneConfig */
  569. void PHYSFS_permitSymbolicLinks(int allow)
  570. {
  571. allowSymLinks = allow;
  572. } /* PHYSFS_permitSymbolicLinks */
  573. /* string manipulation in C makes my ass itch. */
  574. char * __PHYSFS_convertToDependent(const char *prepend,
  575. const char *dirName,
  576. const char *append)
  577. {
  578. const char *dirsep = PHYSFS_getDirSeparator();
  579. size_t sepsize = strlen(dirsep);
  580. char *str;
  581. char *i1;
  582. char *i2;
  583. size_t allocSize;
  584. while (*dirName == '/')
  585. dirName++;
  586. allocSize = strlen(dirName) + 1;
  587. if (prepend != NULL)
  588. allocSize += strlen(prepend) + sepsize;
  589. if (append != NULL)
  590. allocSize += strlen(append) + sepsize;
  591. /* make sure there's enough space if the dir separator is bigger. */
  592. if (sepsize > 1)
  593. {
  594. str = (char *) dirName;
  595. do
  596. {
  597. str = strchr(str, '/');
  598. if (str != NULL)
  599. {
  600. allocSize += (sepsize - 1);
  601. str++;
  602. } /* if */
  603. } while (str != NULL);
  604. } /* if */
  605. str = (char *) malloc(allocSize);
  606. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
  607. if (prepend == NULL)
  608. *str = '\0';
  609. else
  610. {
  611. strcpy(str, prepend);
  612. strcat(str, dirsep);
  613. } /* else */
  614. for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
  615. {
  616. if (*i1 == '/')
  617. {
  618. strcpy(i2, dirsep);
  619. i2 += sepsize;
  620. } /* if */
  621. else
  622. {
  623. *i2 = *i1;
  624. } /* else */
  625. } /* for */
  626. *i2 = '\0';
  627. if (append)
  628. {
  629. strcat(str, dirsep);
  630. strcpy(str, append);
  631. } /* if */
  632. return(str);
  633. } /* __PHYSFS_convertToDependentNotation */
  634. int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
  635. {
  636. int retval = 1;
  637. char *start;
  638. char *end;
  639. char *str;
  640. start = str = malloc(strlen(fname) + 1);
  641. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  642. strcpy(str, fname);
  643. while (1)
  644. {
  645. end = strchr(start, '/');
  646. if (end != NULL)
  647. *end = '\0';
  648. if ( (strcmp(start, ".") == 0) ||
  649. (strcmp(start, "..") == 0) ||
  650. (strchr(start, '\\') != NULL) ||
  651. (strchr(start, ':') != NULL) )
  652. {
  653. __PHYSFS_setError(ERR_INSECURE_FNAME);
  654. retval = 0;
  655. break;
  656. } /* if */
  657. if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
  658. {
  659. __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
  660. retval = 0;
  661. break;
  662. } /* if */
  663. if (end == NULL)
  664. break;
  665. *end = '/';
  666. start = end + 1;
  667. } /* while */
  668. free(str);
  669. return(retval);
  670. } /* __PHYSFS_verifySecurity */
  671. int PHYSFS_mkdir(const char *dirName)
  672. {
  673. DirHandle *h;
  674. char *str;
  675. char *start;
  676. char *end;
  677. int retval = 0;
  678. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  679. h = writeDir->dirHandle;
  680. while (*dirName == '/')
  681. dirName++;
  682. BAIL_IF_MACRO(h->funcs->mkdir == NULL, ERR_NOT_SUPPORTED, 0);
  683. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, dirName), NULL, 0);
  684. start = str = malloc(strlen(dirName) + 1);
  685. BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
  686. strcpy(str, dirName);
  687. while (1)
  688. {
  689. end = strchr(start, '/');
  690. if (end != NULL)
  691. *end = '\0';
  692. retval = h->funcs->mkdir(h, str);
  693. if (!retval)
  694. break;
  695. if (end == NULL)
  696. break;
  697. *end = '/';
  698. start = end + 1;
  699. } /* while */
  700. free(str);
  701. return(retval);
  702. } /* PHYSFS_mkdir */
  703. int PHYSFS_delete(const char *fname)
  704. {
  705. DirHandle *h;
  706. BAIL_IF_MACRO(writeDir == NULL, ERR_NO_WRITE_DIR, 0);
  707. h = writeDir->dirHandle;
  708. BAIL_IF_MACRO(h->funcs->remove == NULL, ERR_NOT_SUPPORTED, 0);
  709. while (*fname == '/')
  710. fname++;
  711. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, 0);
  712. return(h->funcs->remove(h, fname));
  713. } /* PHYSFS_delete */
  714. const char *PHYSFS_getRealDir(const char *filename)
  715. {
  716. DirInfo *i;
  717. while (*filename == '/')
  718. filename++;
  719. for (i = searchPath; i != NULL; i = i->next)
  720. {
  721. DirHandle *h = i->dirHandle;
  722. if (__PHYSFS_verifySecurity(h, filename))
  723. {
  724. if (h->funcs->exists(h, filename))
  725. return(i->dirName);
  726. } /* if */
  727. } /* for */
  728. return(NULL);
  729. } /* PHYSFS_getRealDir */
  730. static int countList(LinkedStringList *list)
  731. {
  732. int retval = 0;
  733. LinkedStringList *i;
  734. for (i = list; i != NULL; i = i->next)
  735. retval++;
  736. return(retval);
  737. } /* countList */
  738. static char **convertStringListToPhysFSList(LinkedStringList *finalList)
  739. {
  740. int i;
  741. LinkedStringList *next = NULL;
  742. int len = countList(finalList);
  743. char **retval = (char **) malloc((len + 1) * sizeof (char *));
  744. if (retval == NULL)
  745. __PHYSFS_setError(ERR_OUT_OF_MEMORY);
  746. for (i = 0; i < len; i++)
  747. {
  748. next = finalList->next;
  749. if (retval == NULL)
  750. free(finalList->str);
  751. else
  752. retval[i] = finalList->str;
  753. free(finalList);
  754. finalList = next;
  755. } /* for */
  756. if (retval != NULL)
  757. retval[i] = NULL;
  758. return(retval);
  759. } /* convertStringListToPhysFSList */
  760. static void insertStringListItem(LinkedStringList **final,
  761. LinkedStringList *item)
  762. {
  763. LinkedStringList *i;
  764. LinkedStringList *prev = NULL;
  765. int rc;
  766. for (i = *final; i != NULL; i = i->next)
  767. {
  768. rc = strcmp(i->str, item->str);
  769. if (rc > 0) /* insertion point. */
  770. break;
  771. else if (rc == 0) /* already in list. */
  772. {
  773. free(item->str);
  774. free(item);
  775. return;
  776. } /* else if */
  777. prev = i;
  778. } /* for */
  779. /*
  780. * If we are here, we are either at the insertion point.
  781. * This may be the end of the list, or the list may be empty, too.
  782. */
  783. if (prev == NULL)
  784. *final = item;
  785. else
  786. prev->next = item;
  787. item->next = i;
  788. } /* insertStringListItem */
  789. /* if we run out of memory anywhere in here, we give back what we can. */
  790. static void interpolateStringLists(LinkedStringList **final,
  791. LinkedStringList *newList)
  792. {
  793. LinkedStringList *next = NULL;
  794. while (newList != NULL)
  795. {
  796. next = newList->next;
  797. insertStringListItem(final, newList);
  798. newList = next;
  799. } /* while */
  800. } /* interpolateStringLists */
  801. char **PHYSFS_enumerateFiles(const char *path)
  802. {
  803. DirInfo *i;
  804. char **retval = NULL;
  805. LinkedStringList *rc;
  806. LinkedStringList *finalList = NULL;
  807. int omitSymLinks = !allowSymLinks;
  808. while (*path == '/')
  809. path++;
  810. for (i = searchPath; i != NULL; i = i->next)
  811. {
  812. DirHandle *h = i->dirHandle;
  813. if (__PHYSFS_verifySecurity(h, path))
  814. {
  815. rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
  816. interpolateStringLists(&finalList, rc);
  817. } /* if */
  818. } /* for */
  819. retval = convertStringListToPhysFSList(finalList);
  820. return(retval);
  821. } /* PHYSFS_enumerateFiles */
  822. int PHYSFS_exists(const char *fname)
  823. {
  824. while (*fname == '/')
  825. fname++;
  826. return(PHYSFS_getRealDir(fname) != NULL);
  827. } /* PHYSFS_exists */
  828. int PHYSFS_isDirectory(const char *fname)
  829. {
  830. DirInfo *i;
  831. while (*fname == '/')
  832. fname++;
  833. if (*fname == '\0')
  834. return(1);
  835. for (i = searchPath; i != NULL; i = i->next)
  836. {
  837. DirHandle *h = i->dirHandle;
  838. if (__PHYSFS_verifySecurity(h, fname))
  839. {
  840. if (h->funcs->exists(h, fname))
  841. return(h->funcs->isDirectory(h, fname));
  842. } /* if */
  843. } /* for */
  844. return(0);
  845. } /* PHYSFS_isDirectory */
  846. int PHYSFS_isSymbolicLink(const char *fname)
  847. {
  848. DirInfo *i;
  849. if (!allowSymLinks)
  850. return(0);
  851. while (*fname == '/')
  852. fname++;
  853. for (i = searchPath; i != NULL; i = i->next)
  854. {
  855. DirHandle *h = i->dirHandle;
  856. if (__PHYSFS_verifySecurity(h, fname))
  857. {
  858. if (h->funcs->exists(h, fname))
  859. return(h->funcs->isSymLink(h, fname));
  860. } /* if */
  861. } /* for */
  862. return(0);
  863. } /* PHYSFS_isSymbolicLink */
  864. static PHYSFS_file *doOpenWrite(const char *fname, int appending)
  865. {
  866. PHYSFS_file *retval = NULL;
  867. FileHandle *rc = NULL;
  868. DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
  869. const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
  870. FileHandleList *list;
  871. while (*fname == '/')
  872. fname++;
  873. BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
  874. BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
  875. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  876. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  877. rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
  878. if (rc == NULL)
  879. free(list);
  880. else
  881. {
  882. list->handle.opaque = (void *) rc;
  883. list->next = openWriteList;
  884. openWriteList = list;
  885. retval = &(list->handle);
  886. } /* else */
  887. return(retval);
  888. } /* doOpenWrite */
  889. PHYSFS_file *PHYSFS_openWrite(const char *filename)
  890. {
  891. return(doOpenWrite(filename, 0));
  892. } /* PHYSFS_openWrite */
  893. PHYSFS_file *PHYSFS_openAppend(const char *filename)
  894. {
  895. return(doOpenWrite(filename, 1));
  896. } /* PHYSFS_openAppend */
  897. PHYSFS_file *PHYSFS_openRead(const char *fname)
  898. {
  899. FileHandle *rc = NULL;
  900. FileHandleList *list;
  901. DirInfo *i;
  902. while (*fname == '/')
  903. fname++;
  904. for (i = searchPath; i != NULL; i = i->next)
  905. {
  906. DirHandle *h = i->dirHandle;
  907. if (__PHYSFS_verifySecurity(h, fname))
  908. {
  909. rc = h->funcs->openRead(h, fname);
  910. if (rc != NULL)
  911. break;
  912. } /* if */
  913. } /* for */
  914. if (rc == NULL)
  915. return(NULL);
  916. list = (FileHandleList *) malloc(sizeof (FileHandleList));
  917. BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
  918. list->handle.opaque = (void *) rc;
  919. list->next = openReadList;
  920. openReadList = list;
  921. return(&(list->handle));
  922. } /* PHYSFS_openRead */
  923. static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
  924. {
  925. FileHandle *h = (FileHandle *) handle->opaque;
  926. FileHandleList *prev = NULL;
  927. FileHandleList *i;
  928. int rc;
  929. for (i = *list; i != NULL; i = i->next)
  930. {
  931. if (&i->handle == handle) /* handle is in this list? */
  932. {
  933. rc = h->funcs->fileClose(h);
  934. if (!rc)
  935. return(-1);
  936. if (prev == NULL)
  937. *list = i->next;
  938. else
  939. prev->next = i->next;
  940. free(i);
  941. return(1);
  942. } /* if */
  943. prev = i;
  944. } /* for */
  945. return(0);
  946. } /* closeHandleInOpenList */
  947. int PHYSFS_close(PHYSFS_file *handle)
  948. {
  949. int rc;
  950. /* -1 == close failure. 0 == not found. 1 == success. */
  951. rc = closeHandleInOpenList(&openReadList, handle);
  952. BAIL_IF_MACRO(rc == -1, NULL, 0);
  953. if (!rc)
  954. {
  955. rc = closeHandleInOpenList(&openWriteList, handle);
  956. BAIL_IF_MACRO(rc == -1, NULL, 0);
  957. } /* if */
  958. if (!rc)
  959. __PHYSFS_setError(ERR_NOT_A_HANDLE);
  960. return(rc);
  961. } /* PHYSFS_close */
  962. int PHYSFS_read(PHYSFS_file *handle, void *buffer,
  963. unsigned int objSize, unsigned int objCount)
  964. {
  965. FileHandle *h = (FileHandle *) handle->opaque;
  966. assert(h != NULL);
  967. assert(h->funcs != NULL);
  968. BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
  969. return(h->funcs->read(h, buffer, objSize, objCount));
  970. } /* PHYSFS_read */
  971. int PHYSFS_write(PHYSFS_file *handle, const void *buffer,
  972. unsigned int objSize, unsigned int objCount)
  973. {
  974. FileHandle *h = (FileHandle *) handle->opaque;
  975. assert(h != NULL);
  976. assert(h->funcs != NULL);
  977. BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
  978. return(h->funcs->write(h, buffer, objSize, objCount));
  979. } /* PHYSFS_write */
  980. int PHYSFS_eof(PHYSFS_file *handle)
  981. {
  982. FileHandle *h = (FileHandle *) handle->opaque;
  983. assert(h != NULL);
  984. assert(h->funcs != NULL);
  985. BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
  986. return(h->funcs->eof(h));
  987. } /* PHYSFS_eof */
  988. int PHYSFS_tell(PHYSFS_file *handle)
  989. {
  990. FileHandle *h = (FileHandle *) handle->opaque;
  991. assert(h != NULL);
  992. assert(h->funcs != NULL);
  993. BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
  994. return(h->funcs->tell(h));
  995. } /* PHYSFS_tell */
  996. int PHYSFS_seek(PHYSFS_file *handle, int pos)
  997. {
  998. FileHandle *h = (FileHandle *) handle->opaque;
  999. assert(h != NULL);
  1000. assert(h->funcs != NULL);
  1001. BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
  1002. BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
  1003. return(h->funcs->seek(h, pos));
  1004. } /* PHYSFS_seek */
  1005. int PHYSFS_fileLength(PHYSFS_file *handle)
  1006. {
  1007. FileHandle *h = (FileHandle *) handle->opaque;
  1008. assert(h != NULL);
  1009. assert(h->funcs != NULL);
  1010. BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
  1011. return(h->funcs->fileLength(h));
  1012. } /* PHYSFS_filelength */
  1013. /* end of physfs.c ... */