| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548 |
- /**
- * PhysicsFS; a portable, flexible file i/o abstraction.
- *
- * Documentation is in physfs.h. It's verbose, honest. :)
- *
- * Please see the file LICENSE in the source's root directory.
- *
- * This file written by Ryan C. Gordon.
- */
- #if HAVE_CONFIG_H
- # include <config.h>
- #endif
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <assert.h>
- #include "physfs.h"
- #define __PHYSICSFS_INTERNAL__
- #include "physfs_internal.h"
- typedef struct __PHYSFS_ERRMSGTYPE__
- {
- PHYSFS_uint64 tid;
- int errorAvailable;
- char errorString[80];
- struct __PHYSFS_ERRMSGTYPE__ *next;
- } ErrMsg;
- typedef struct __PHYSFS_DIRINFO__
- {
- char *dirName;
- DirHandle *dirHandle;
- struct __PHYSFS_DIRINFO__ *next;
- } PhysDirInfo;
- typedef struct __PHYSFS_FILEHANDLELIST__
- {
- PHYSFS_file handle;
- struct __PHYSFS_FILEHANDLELIST__ *next;
- } FileHandleList;
- /* The various i/o drivers... */
- #if (defined PHYSFS_SUPPORTS_ZIP)
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
- extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
- #endif
- #if (defined PHYSFS_SUPPORTS_GRP)
- extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
- extern const DirFunctions __PHYSFS_DirFunctions_GRP;
- #endif
- extern const DirFunctions __PHYSFS_DirFunctions_DIR;
- static const PHYSFS_ArchiveInfo *supported_types[] =
- {
- #if (defined PHYSFS_SUPPORTS_ZIP)
- &__PHYSFS_ArchiveInfo_ZIP,
- #endif
- #if (defined PHYSFS_SUPPORTS_GRP)
- &__PHYSFS_ArchiveInfo_GRP,
- #endif
- NULL
- };
- static const DirFunctions *dirFunctions[] =
- {
- #if (defined PHYSFS_SUPPORTS_ZIP)
- &__PHYSFS_DirFunctions_ZIP,
- #endif
- #if (defined PHYSFS_SUPPORTS_GRP)
- &__PHYSFS_DirFunctions_GRP,
- #endif
- &__PHYSFS_DirFunctions_DIR,
- NULL
- };
- /* General PhysicsFS state ... */
- static int initialized = 0;
- static ErrMsg *errorMessages = NULL;
- static PhysDirInfo *searchPath = NULL;
- static PhysDirInfo *writeDir = NULL;
- static FileHandleList *openWriteList = NULL;
- static FileHandleList *openReadList = NULL;
- static char *baseDir = NULL;
- static char *userDir = NULL;
- static int allowSymLinks = 0;
- /* mutexes ... */
- static void *errorLock = NULL; /* protects error message list. */
- static void *stateLock = NULL; /* protects other PhysFS static state. */
- /* functions ... */
- static ErrMsg *findErrorForCurrentThread(void)
- {
- ErrMsg *i;
- PHYSFS_uint64 tid;
- if (errorLock != NULL)
- __PHYSFS_platformGrabMutex(errorLock);
- if (errorMessages != NULL)
- {
- tid = __PHYSFS_platformGetThreadID();
- for (i = errorMessages; i != NULL; i = i->next)
- {
- if (i->tid == tid)
- {
- if (errorLock != NULL)
- __PHYSFS_platformReleaseMutex(errorLock);
- return(i);
- } /* if */
- } /* for */
- } /* if */
- if (errorLock != NULL)
- __PHYSFS_platformReleaseMutex(errorLock);
- return(NULL); /* no error available. */
- } /* findErrorForCurrentThread */
- void __PHYSFS_setError(const char *str)
- {
- ErrMsg *err;
- if (str == NULL)
- return;
- err = findErrorForCurrentThread();
- if (err == NULL)
- {
- err = (ErrMsg *) malloc(sizeof (ErrMsg));
- if (err == NULL)
- return; /* uhh...? */
- memset((void *) err, '\0', sizeof (ErrMsg));
- err->tid = __PHYSFS_platformGetThreadID();
- if (errorLock != NULL)
- __PHYSFS_platformGrabMutex(errorLock);
- err->next = errorMessages;
- errorMessages = err;
- if (errorLock != NULL)
- __PHYSFS_platformReleaseMutex(errorLock);
- } /* if */
- err->errorAvailable = 1;
- strncpy(err->errorString, str, sizeof (err->errorString));
- err->errorString[sizeof (err->errorString) - 1] = '\0';
- } /* __PHYSFS_setError */
- const char *PHYSFS_getLastError(void)
- {
- ErrMsg *err = findErrorForCurrentThread();
- if ((err == NULL) || (!err->errorAvailable))
- return(NULL);
- err->errorAvailable = 0;
- return(err->errorString);
- } /* PHYSFS_getLastError */
- /* MAKE SURE that errorLock is held before calling this! */
- static void freeErrorMessages(void)
- {
- ErrMsg *i;
- ErrMsg *next;
- for (i = errorMessages; i != NULL; i = next)
- {
- next = i->next;
- free(i);
- } /* for */
- errorMessages = NULL;
- } /* freeErrorMessages */
- void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
- {
- if (ver != NULL)
- {
- ver->major = PHYSFS_VER_MAJOR;
- ver->minor = PHYSFS_VER_MINOR;
- ver->patch = PHYSFS_VER_PATCH;
- } /* if */
- } /* PHYSFS_getLinkedVersion */
- static const char *find_filename_extension(const char *fname)
- {
- const char *retval = strchr(fname, '.');
- const char *p = retval;
- while (p != NULL)
- {
- p = strchr(p + 1, '.');
- if (p != NULL)
- retval = p;
- } /* while */
- if (retval != NULL)
- retval++; /* skip '.' */
- return(retval);
- } /* find_filename_extension */
- static DirHandle *openDirectory(const char *d, int forWriting)
- {
- const DirFunctions **i;
- const char *ext;
- BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
- ext = find_filename_extension(d);
- if (ext != NULL)
- {
- /* Look for archivers with matching file extensions first... */
- for (i = dirFunctions; *i != NULL; i++)
- {
- if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) == 0)
- {
- if ((*i)->isArchive(d, forWriting))
- return( (*i)->openArchive(d, forWriting) );
- } /* if */
- } /* for */
- /* failing an exact file extension match, try all the others... */
- for (i = dirFunctions; *i != NULL; i++)
- {
- if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) != 0)
- {
- if ((*i)->isArchive(d, forWriting))
- return( (*i)->openArchive(d, forWriting) );
- } /* if */
- } /* for */
- } /* if */
- else /* no extension? Try them all. */
- {
- for (i = dirFunctions; *i != NULL; i++)
- {
- if ((*i)->isArchive(d, forWriting))
- return( (*i)->openArchive(d, forWriting) );
- } /* for */
- } /* else */
- __PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
- return(NULL);
- } /* openDirectory */
- static PhysDirInfo *buildDirInfo(const char *newDir, int forWriting)
- {
- DirHandle *dirHandle = NULL;
- PhysDirInfo *di = NULL;
- BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
- dirHandle = openDirectory(newDir, forWriting);
- BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
- di = (PhysDirInfo *) malloc(sizeof (PhysDirInfo));
- if (di == NULL)
- {
- dirHandle->funcs->dirClose(dirHandle);
- BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
- } /* if */
- di->dirName = (char *) malloc(strlen(newDir) + 1);
- if (di->dirName == NULL)
- {
- free(di);
- dirHandle->funcs->dirClose(dirHandle);
- BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
- } /* if */
- di->next = NULL;
- di->dirHandle = dirHandle;
- strcpy(di->dirName, newDir);
- return(di);
- } /* buildDirInfo */
- /* MAKE SURE you've got the stateLock held before calling this! */
- static int freeDirInfo(PhysDirInfo *di, FileHandleList *openList)
- {
- FileHandleList *i;
- if (di == NULL)
- return(1);
- for (i = openList; i != NULL; i = i->next)
- {
- const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
- BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
- } /* for */
-
- di->dirHandle->funcs->dirClose(di->dirHandle);
- free(di->dirName);
- free(di);
- return(1);
- } /* freeDirInfo */
- static char *calculateUserDir(void)
- {
- char *retval = NULL;
- const char *str = NULL;
- str = __PHYSFS_platformGetUserDir();
- if (str != NULL)
- retval = (char *) str;
- else
- {
- const char *dirsep = PHYSFS_getDirSeparator();
- const char *uname = __PHYSFS_platformGetUserName();
- str = (uname != NULL) ? uname : "default";
- retval = (char *) malloc(strlen(baseDir) + strlen(str) +
- strlen(dirsep) + 6);
- if (retval == NULL)
- __PHYSFS_setError(ERR_OUT_OF_MEMORY);
- else
- sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
- if (uname != NULL)
- free((void *) uname);
- } /* else */
- return(retval);
- } /* calculateUserDir */
- static int appendDirSep(char **dir)
- {
- const char *dirsep = PHYSFS_getDirSeparator();
- char *ptr;
- if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
- return(1);
- ptr = realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
- if (!ptr)
- {
- free(*dir);
- return(0);
- } /* if */
- strcat(ptr, dirsep);
- *dir = ptr;
- return(1);
- } /* appendDirSep */
- static char *calculateBaseDir(const char *argv0)
- {
- const char *dirsep = PHYSFS_getDirSeparator();
- char *retval;
- char *ptr;
- /*
- * See if the platform driver wants to handle this for us...
- */
- retval = __PHYSFS_platformCalcBaseDir(argv0);
- if (retval != NULL)
- return(retval);
- /*
- * Determine if there's a path on argv0. If there is, that's the base dir.
- */
- ptr = strstr(argv0, dirsep);
- if (ptr != NULL)
- {
- char *p = ptr;
- size_t size;
- while (p != NULL)
- {
- ptr = p;
- p = strstr(p + 1, dirsep);
- } /* while */
- size = (size_t) (ptr - argv0);
- retval = (char *) malloc(size + 1);
- BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
- memcpy(retval, argv0, size);
- retval[size] = '\0';
- return(retval);
- } /* if */
- /*
- * Last ditch effort: it's the current working directory. (*shrug*)
- */
- retval = __PHYSFS_platformCurrentDir();
- if(retval != NULL) {
- return(retval);
- }
- /*
- * Ok, current directory doesn't exist, use the root directory.
- * Not a good alternative, but it only happens if the current
- * directory was deleted from under the program.
- */
- retval = (char *) malloc(strlen(dirsep) + 1);
- strcpy(retval, dirsep);
- return(retval);
- } /* calculateBaseDir */
- static int initializeMutexes(void)
- {
- errorLock = __PHYSFS_platformCreateMutex();
- if (errorLock == NULL)
- goto initializeMutexes_failed;
- stateLock = __PHYSFS_platformCreateMutex();
- if (stateLock == NULL)
- goto initializeMutexes_failed;
- return(1); /* success. */
- initializeMutexes_failed:
- if (errorLock != NULL)
- __PHYSFS_platformDestroyMutex(errorLock);
- if (stateLock != NULL)
- __PHYSFS_platformDestroyMutex(stateLock);
- errorLock = stateLock = NULL;
- return(0); /* failed. */
- } /* initializeMutexes */
- int PHYSFS_init(const char *argv0)
- {
- char *ptr;
- BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
- BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
- BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
- baseDir = calculateBaseDir(argv0);
- BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
- ptr = __PHYSFS_platformRealPath(baseDir);
- free(baseDir);
- BAIL_IF_MACRO(ptr == NULL, NULL, 0);
- baseDir = ptr;
- BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
- userDir = calculateUserDir();
- if (userDir != NULL)
- {
- ptr = __PHYSFS_platformRealPath(userDir);
- free(userDir);
- userDir = ptr;
- } /* if */
- if ((userDir == NULL) || (!appendDirSep(&userDir)))
- {
- free(baseDir);
- baseDir = NULL;
- return(0);
- } /* if */
- initialized = 1;
- /* This makes sure that the error subsystem is initialized. */
- __PHYSFS_setError(PHYSFS_getLastError());
- return(1);
- } /* PHYSFS_init */
- /* MAKE SURE you hold stateLock before calling this! */
- static int closeFileHandleList(FileHandleList **list)
- {
- FileHandleList *i;
- FileHandleList *next = NULL;
- FileHandle *h;
- for (i = *list; i != NULL; i = next)
- {
- next = i->next;
- h = (FileHandle *) (i->handle.opaque);
- if (!h->funcs->fileClose(h))
- {
- *list = i;
- return(0);
- } /* if */
- free(i);
- } /* for */
- *list = NULL;
- return(1);
- } /* closeFileHandleList */
- /* MAKE SURE you hold the stateLock before calling this! */
- static void freeSearchPath(void)
- {
- PhysDirInfo *i;
- PhysDirInfo *next = NULL;
- closeFileHandleList(&openReadList);
- if (searchPath != NULL)
- {
- for (i = searchPath; i != NULL; i = next)
- {
- next = i->next;
- freeDirInfo(i, openReadList);
- } /* for */
- searchPath = NULL;
- } /* if */
- } /* freeSearchPath */
- int PHYSFS_deinit(void)
- {
- BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
- BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
- closeFileHandleList(&openWriteList);
- BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
- freeSearchPath();
- freeErrorMessages();
- if (baseDir != NULL)
- {
- free(baseDir);
- baseDir = NULL;
- } /* if */
- if (userDir != NULL)
- {
- free(userDir);
- userDir = NULL;
- } /* if */
- allowSymLinks = 0;
- initialized = 0;
- __PHYSFS_platformDestroyMutex(errorLock);
- __PHYSFS_platformDestroyMutex(stateLock);
- errorLock = stateLock = NULL;
- return(1);
- } /* PHYSFS_deinit */
- const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
- {
- return(supported_types);
- } /* PHYSFS_supportedArchiveTypes */
- void PHYSFS_freeList(void *list)
- {
- void **i;
- for (i = (void **) list; *i != NULL; i++)
- free(*i);
- free(list);
- } /* PHYSFS_freeList */
- const char *PHYSFS_getDirSeparator(void)
- {
- return(__PHYSFS_platformDirSeparator);
- } /* PHYSFS_getDirSeparator */
- char **PHYSFS_getCdRomDirs(void)
- {
- return(__PHYSFS_platformDetectAvailableCDs());
- } /* PHYSFS_getCdRomDirs */
- const char *PHYSFS_getBaseDir(void)
- {
- return(baseDir); /* this is calculated in PHYSFS_init()... */
- } /* PHYSFS_getBaseDir */
- const char *PHYSFS_getUserDir(void)
- {
- return(userDir); /* this is calculated in PHYSFS_init()... */
- } /* PHYSFS_getUserDir */
- const char *PHYSFS_getWriteDir(void)
- {
- const char *retval = NULL;
- __PHYSFS_platformGrabMutex(stateLock);
- if (writeDir != NULL)
- retval = writeDir->dirName;
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* PHYSFS_getWriteDir */
- int PHYSFS_setWriteDir(const char *newDir)
- {
- int retval = 1;
- __PHYSFS_platformGrabMutex(stateLock);
- if (writeDir != NULL)
- {
- BAIL_IF_MACRO_MUTEX(!freeDirInfo(writeDir, openWriteList), NULL,
- stateLock, 0);
- writeDir = NULL;
- } /* if */
- if (newDir != NULL)
- {
- writeDir = buildDirInfo(newDir, 1);
- retval = (writeDir != NULL);
- } /* if */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* PHYSFS_setWriteDir */
- int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
- {
- PhysDirInfo *di;
- PhysDirInfo *prev = NULL;
- PhysDirInfo *i;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- /* already in search path? */
- BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
- prev = i;
- } /* for */
- di = buildDirInfo(newDir, 0);
- BAIL_IF_MACRO_MUTEX(di == NULL, NULL, stateLock, 0);
- if (appendToPath)
- {
- di->next = NULL;
- if (prev == NULL)
- searchPath = di;
- else
- prev->next = di;
- } /* if */
- else
- {
- di->next = searchPath;
- searchPath = di;
- } /* else */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(1);
- } /* PHYSFS_addToSearchPath */
- int PHYSFS_removeFromSearchPath(const char *oldDir)
- {
- PhysDirInfo *i;
- PhysDirInfo *prev = NULL;
- PhysDirInfo *next = NULL;
- BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- if (strcmp(i->dirName, oldDir) == 0)
- {
- next = i->next;
- BAIL_IF_MACRO_MUTEX(!freeDirInfo(i, openReadList), NULL,
- stateLock, 0);
- if (prev == NULL)
- searchPath = next;
- else
- prev->next = next;
- BAIL_MACRO_MUTEX(NULL, stateLock, 1);
- } /* if */
- prev = i;
- } /* for */
- BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
- } /* PHYSFS_removeFromSearchPath */
- char **PHYSFS_getSearchPath(void)
- {
- int count = 1;
- int x;
- PhysDirInfo *i;
- char **retval;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- count++;
- retval = (char **) malloc(sizeof (char *) * count);
- BAIL_IF_MACRO_MUTEX(!retval, ERR_OUT_OF_MEMORY, stateLock, NULL);
- count--;
- retval[count] = NULL;
- for (i = searchPath, x = 0; x < count; i = i->next, x++)
- {
- retval[x] = (char *) malloc(strlen(i->dirName) + 1);
- if (retval[x] == NULL) /* this is friggin' ugly. */
- {
- while (x > 0)
- {
- x--;
- free(retval[x]);
- } /* while */
- free(retval);
- BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
- } /* if */
- strcpy(retval[x], i->dirName);
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* PHYSFS_getSearchPath */
- int PHYSFS_setSaneConfig(const char *organization, const char *appName,
- const char *archiveExt, int includeCdRoms,
- int archivesFirst)
- {
- const char *basedir = PHYSFS_getBaseDir();
- const char *userdir = PHYSFS_getUserDir();
- const char *dirsep = PHYSFS_getDirSeparator();
- char *str;
- BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
- /* set write dir... */
- str = malloc(strlen(userdir) + (strlen(organization) * 2) +
- (strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
- BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
- sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
- if (!PHYSFS_setWriteDir(str))
- {
- int no_write = 0;
- sprintf(str, ".%s/%s", organization, appName);
- if ( (PHYSFS_setWriteDir(userdir)) &&
- (PHYSFS_mkdir(str)) )
- {
- sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
- if (!PHYSFS_setWriteDir(str))
- no_write = 1;
- } /* if */
- else
- {
- no_write = 1;
- } /* else */
- if (no_write)
- {
- PHYSFS_setWriteDir(NULL); /* just in case. */
- free(str);
- BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
- } /* if */
- } /* if */
- /* Put write dir first in search path... */
- PHYSFS_addToSearchPath(str, 0);
- free(str);
- /* Put base path on search path... */
- PHYSFS_addToSearchPath(basedir, 1);
- /* handle CD-ROMs... */
- if (includeCdRoms)
- {
- char **cds = PHYSFS_getCdRomDirs();
- char **i;
- for (i = cds; *i != NULL; i++)
- PHYSFS_addToSearchPath(*i, 1);
- PHYSFS_freeList(cds);
- } /* if */
- /* Root out archives, and add them to search path... */
- if (archiveExt != NULL)
- {
- char **rc = PHYSFS_enumerateFiles("");
- char **i;
- size_t extlen = strlen(archiveExt);
- char *ext;
- for (i = rc; *i != NULL; i++)
- {
- size_t l = strlen(*i);
- if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
- {
- ext = (*i) + (l - extlen);
- if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
- {
- const char *d = PHYSFS_getRealDir(*i);
- str = malloc(strlen(d) + strlen(dirsep) + l + 1);
- if (str != NULL)
- {
- sprintf(str, "%s%s%s", d, dirsep, *i);
- PHYSFS_addToSearchPath(str, archivesFirst == 0);
- free(str);
- } /* if */
- } /* if */
- } /* if */
- } /* for */
- PHYSFS_freeList(rc);
- } /* if */
- return(1);
- } /* PHYSFS_setSaneConfig */
- void PHYSFS_permitSymbolicLinks(int allow)
- {
- allowSymLinks = allow;
- } /* PHYSFS_permitSymbolicLinks */
- /* string manipulation in C makes my ass itch. */
- char * __PHYSFS_convertToDependent(const char *prepend,
- const char *dirName,
- const char *append)
- {
- const char *dirsep = __PHYSFS_platformDirSeparator;
- size_t sepsize = strlen(dirsep);
- char *str;
- char *i1;
- char *i2;
- size_t allocSize;
- while (*dirName == '/')
- dirName++;
- allocSize = strlen(dirName) + 1;
- if (prepend != NULL)
- allocSize += strlen(prepend) + sepsize;
- if (append != NULL)
- allocSize += strlen(append) + sepsize;
- /* make sure there's enough space if the dir separator is bigger. */
- if (sepsize > 1)
- {
- str = (char *) dirName;
- do
- {
- str = strchr(str, '/');
- if (str != NULL)
- {
- allocSize += (sepsize - 1);
- str++;
- } /* if */
- } while (str != NULL);
- } /* if */
- str = (char *) malloc(allocSize);
- BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
- if (prepend == NULL)
- *str = '\0';
- else
- {
- strcpy(str, prepend);
- strcat(str, dirsep);
- } /* else */
- for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
- {
- if (*i1 == '/')
- {
- strcpy(i2, dirsep);
- i2 += sepsize;
- } /* if */
- else
- {
- *i2 = *i1;
- } /* else */
- } /* for */
- *i2 = '\0';
- if (append)
- {
- strcat(str, dirsep);
- strcpy(str, append);
- } /* if */
- return(str);
- } /* __PHYSFS_convertToDependent */
- int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
- {
- int retval = 1;
- char *start;
- char *end;
- char *str;
- start = str = malloc(strlen(fname) + 1);
- BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
- strcpy(str, fname);
- while (1)
- {
- end = strchr(start, '/');
- if (end != NULL)
- *end = '\0';
- if ( (strcmp(start, ".") == 0) ||
- (strcmp(start, "..") == 0) ||
- (strchr(start, '\\') != NULL) ||
- (strchr(start, ':') != NULL) )
- {
- __PHYSFS_setError(ERR_INSECURE_FNAME);
- retval = 0;
- break;
- } /* if */
- if ((!allowSymLinks) && (h->funcs->isSymLink(h, str)))
- {
- __PHYSFS_setError(ERR_SYMLINK_DISALLOWED);
- retval = 0;
- break;
- } /* if */
- if (end == NULL)
- break;
- *end = '/';
- start = end + 1;
- } /* while */
- free(str);
- return(retval);
- } /* __PHYSFS_verifySecurity */
- int PHYSFS_mkdir(const char *dname)
- {
- DirHandle *h;
- char *str;
- char *start;
- char *end;
- int retval = 0;
- BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
- while (*dname == '/')
- dname++;
- __PHYSFS_platformGrabMutex(stateLock);
- BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
- h = writeDir->dirHandle;
- BAIL_IF_MACRO_MUTEX(!h->funcs->mkdir, ERR_NOT_SUPPORTED, stateLock, 0);
- BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, dname), NULL, stateLock, 0);
- start = str = malloc(strlen(dname) + 1);
- BAIL_IF_MACRO_MUTEX(str == NULL, ERR_OUT_OF_MEMORY, stateLock, 0);
- strcpy(str, dname);
- while (1)
- {
- end = strchr(start, '/');
- if (end != NULL)
- *end = '\0';
- retval = h->funcs->mkdir(h, str);
- if (!retval)
- break;
- if (end == NULL)
- break;
- *end = '/';
- start = end + 1;
- } /* while */
- __PHYSFS_platformReleaseMutex(stateLock);
- free(str);
- return(retval);
- } /* PHYSFS_mkdir */
- int PHYSFS_delete(const char *fname)
- {
- int retval;
- DirHandle *h;
- BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
- while (*fname == '/')
- fname++;
- __PHYSFS_platformGrabMutex(stateLock);
- BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
- h = writeDir->dirHandle;
- BAIL_IF_MACRO_MUTEX(!h->funcs->remove, ERR_NOT_SUPPORTED, stateLock, 0);
- BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL, stateLock, 0);
- retval = h->funcs->remove(h, fname);
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* PHYSFS_delete */
- const char *PHYSFS_getRealDir(const char *filename)
- {
- PhysDirInfo *i;
- while (*filename == '/')
- filename++;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- DirHandle *h = i->dirHandle;
- if (__PHYSFS_verifySecurity(h, filename))
- {
- if (!h->funcs->exists(h, filename))
- __PHYSFS_setError(ERR_NO_SUCH_FILE);
- else
- {
- __PHYSFS_platformReleaseMutex(stateLock);
- return(i->dirName);
- } /* else */
- } /* if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(NULL);
- } /* PHYSFS_getRealDir */
- static int countList(LinkedStringList *list)
- {
- int retval = 0;
- LinkedStringList *i;
- for (i = list; i != NULL; i = i->next)
- retval++;
- return(retval);
- } /* countList */
- static char **convertStringListToPhysFSList(LinkedStringList *finalList)
- {
- int i;
- LinkedStringList *next = NULL;
- int len = countList(finalList);
- char **retval = (char **) malloc((len + 1) * sizeof (char *));
- if (retval == NULL)
- __PHYSFS_setError(ERR_OUT_OF_MEMORY);
- for (i = 0; i < len; i++)
- {
- next = finalList->next;
- if (retval == NULL)
- free(finalList->str);
- else
- retval[i] = finalList->str;
- free(finalList);
- finalList = next;
- } /* for */
- if (retval != NULL)
- retval[i] = NULL;
- return(retval);
- } /* convertStringListToPhysFSList */
- static void insertStringListItem(LinkedStringList **final,
- LinkedStringList *item)
- {
- LinkedStringList *i;
- LinkedStringList *prev = NULL;
- int rc;
- for (i = *final; i != NULL; i = i->next)
- {
- rc = strcmp(i->str, item->str);
- if (rc > 0) /* insertion point. */
- break;
- else if (rc == 0) /* already in list. */
- {
- free(item->str);
- free(item);
- return;
- } /* else if */
- prev = i;
- } /* for */
- /*
- * If we are here, we are either at the insertion point.
- * This may be the end of the list, or the list may be empty, too.
- */
- if (prev == NULL)
- *final = item;
- else
- prev->next = item;
- item->next = i;
- } /* insertStringListItem */
- /* if we run out of memory anywhere in here, we give back what we can. */
- static void interpolateStringLists(LinkedStringList **final,
- LinkedStringList *newList)
- {
- LinkedStringList *next = NULL;
- while (newList != NULL)
- {
- next = newList->next;
- insertStringListItem(final, newList);
- newList = next;
- } /* while */
- } /* interpolateStringLists */
- char **PHYSFS_enumerateFiles(const char *path)
- {
- PhysDirInfo *i;
- char **retval = NULL;
- LinkedStringList *rc;
- LinkedStringList *finalList = NULL;
- int omitSymLinks = !allowSymLinks;
- BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
- while (*path == '/')
- path++;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- DirHandle *h = i->dirHandle;
- if (__PHYSFS_verifySecurity(h, path))
- {
- rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
- interpolateStringLists(&finalList, rc);
- } /* if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- retval = convertStringListToPhysFSList(finalList);
- return(retval);
- } /* PHYSFS_enumerateFiles */
- int PHYSFS_exists(const char *fname)
- {
- BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
- while (*fname == '/')
- fname++;
- return(PHYSFS_getRealDir(fname) != NULL);
- } /* PHYSFS_exists */
- PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
- {
- PhysDirInfo *i;
- BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
- while (*fname == '/')
- fname++;
- if (*fname == '\0') /* eh...punt if it's the root dir. */
- return(1);
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- DirHandle *h = i->dirHandle;
- if (__PHYSFS_verifySecurity(h, fname))
- {
- if (!h->funcs->exists(h, fname))
- __PHYSFS_setError(ERR_NO_SUCH_FILE);
- else
- {
- PHYSFS_sint64 retval = -1;
- if (h->funcs->getLastModTime == NULL)
- __PHYSFS_setError(ERR_NOT_SUPPORTED);
- else
- retval = h->funcs->getLastModTime(h, fname);
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* else */
- } /* if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(-1); /* error set in verifysecurity/exists */
- } /* PHYSFS_getLastModTime */
- int PHYSFS_isDirectory(const char *fname)
- {
- PhysDirInfo *i;
- BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
- while (*fname == '/')
- fname++;
- if (*fname == '\0')
- return(1);
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- DirHandle *h = i->dirHandle;
- if (__PHYSFS_verifySecurity(h, fname))
- {
- if (!h->funcs->exists(h, fname))
- __PHYSFS_setError(ERR_NO_SUCH_FILE);
- else
- {
- int retval = h->funcs->isDirectory(h, fname);
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* else */
- } /* if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(0);
- } /* PHYSFS_isDirectory */
- int PHYSFS_isSymbolicLink(const char *fname)
- {
- PhysDirInfo *i;
- if (!allowSymLinks)
- return(0);
- BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
- while (*fname == '/')
- fname++;
- __PHYSFS_platformGrabMutex(stateLock);
- for (i = searchPath; i != NULL; i = i->next)
- {
- DirHandle *h = i->dirHandle;
- if (__PHYSFS_verifySecurity(h, fname))
- {
- if (!h->funcs->exists(h, fname))
- __PHYSFS_setError(ERR_NO_SUCH_FILE);
- else
- {
- int retval = h->funcs->isSymLink(h, fname);
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* else */
- } /* if */
- } /* for */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(0);
- } /* PHYSFS_isSymbolicLink */
- static PHYSFS_file *doOpenWrite(const char *fname, int appending)
- {
- PHYSFS_file *retval = NULL;
- FileHandle *rc = NULL;
- DirHandle *h;
- const DirFunctions *f;
- FileHandleList *list;
- BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
- while (*fname == '/')
- fname++;
- __PHYSFS_platformGrabMutex(stateLock);
- h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
- BAIL_IF_MACRO_MUTEX(!h, ERR_NO_WRITE_DIR, stateLock, NULL);
- BAIL_IF_MACRO_MUTEX(!__PHYSFS_verifySecurity(h, fname), NULL,
- stateLock, NULL);
- list = (FileHandleList *) malloc(sizeof (FileHandleList));
- BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
- f = h->funcs;
- rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
- if (rc == NULL)
- free(list);
- else
- {
- list->handle.opaque = (void *) rc;
- list->next = openWriteList;
- openWriteList = list;
- retval = &(list->handle);
- } /* else */
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* doOpenWrite */
- PHYSFS_file *PHYSFS_openWrite(const char *filename)
- {
- return(doOpenWrite(filename, 0));
- } /* PHYSFS_openWrite */
- PHYSFS_file *PHYSFS_openAppend(const char *filename)
- {
- return(doOpenWrite(filename, 1));
- } /* PHYSFS_openAppend */
- PHYSFS_file *PHYSFS_openRead(const char *fname)
- {
- PHYSFS_file *retval;
- FileHandle *rc = NULL;
- FileHandleList *list;
- PhysDirInfo *i;
- BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
- while (*fname == '/')
- fname++;
- __PHYSFS_platformGrabMutex(stateLock);
- BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
- for (i = searchPath; i != NULL; i = i->next)
- {
- DirHandle *h = i->dirHandle;
- if (__PHYSFS_verifySecurity(h, fname))
- {
- rc = h->funcs->openRead(h, fname);
- if (rc != NULL)
- break;
- } /* if */
- } /* for */
- BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
- list = (FileHandleList *) malloc(sizeof (FileHandleList));
- BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
- list->handle.opaque = (void *) rc;
- list->next = openReadList;
- openReadList = list;
- retval = &(list->handle);
- __PHYSFS_platformReleaseMutex(stateLock);
- return(retval);
- } /* PHYSFS_openRead */
- static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
- {
- FileHandle *h = (FileHandle *) handle->opaque;
- FileHandleList *prev = NULL;
- FileHandleList *i;
- int rc;
- for (i = *list; i != NULL; i = i->next)
- {
- if (&i->handle == handle) /* handle is in this list? */
- {
- rc = h->funcs->fileClose(h);
- if (!rc)
- return(-1);
- if (prev == NULL)
- *list = i->next;
- else
- prev->next = i->next;
- free(i);
- return(1);
- } /* if */
- prev = i;
- } /* for */
- return(0);
- } /* closeHandleInOpenList */
- int PHYSFS_close(PHYSFS_file *handle)
- {
- int rc;
- __PHYSFS_platformGrabMutex(stateLock);
- /* -1 == close failure. 0 == not found. 1 == success. */
- rc = closeHandleInOpenList(&openReadList, handle);
- BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
- if (!rc)
- {
- rc = closeHandleInOpenList(&openWriteList, handle);
- BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
- } /* if */
- __PHYSFS_platformReleaseMutex(stateLock);
- BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
- return(1);
- } /* PHYSFS_close */
- PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
- PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
- {
- FileHandle *h = (FileHandle *) handle->opaque;
- assert(h != NULL);
- assert(h->funcs != NULL);
- BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
- return(h->funcs->read(h, buffer, objSize, objCount));
- } /* PHYSFS_read */
- PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
- PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
- {
- FileHandle *h = (FileHandle *) handle->opaque;
- assert(h != NULL);
- assert(h->funcs != NULL);
- BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
- return(h->funcs->write(h, buffer, objSize, objCount));
- } /* PHYSFS_write */
- int PHYSFS_eof(PHYSFS_file *handle)
- {
- FileHandle *h = (FileHandle *) handle->opaque;
- assert(h != NULL);
- assert(h->funcs != NULL);
- BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
- return(h->funcs->eof(h));
- } /* PHYSFS_eof */
- PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
- {
- FileHandle *h = (FileHandle *) handle->opaque;
- assert(h != NULL);
- assert(h->funcs != NULL);
- BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
- return(h->funcs->tell(h));
- } /* PHYSFS_tell */
- int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
- {
- FileHandle *h = (FileHandle *) handle->opaque;
- assert(h != NULL);
- assert(h->funcs != NULL);
- BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
- BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
- return(h->funcs->seek(h, pos));
- } /* PHYSFS_seek */
- PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
- {
- FileHandle *h = (FileHandle *) handle->opaque;
- assert(h != NULL);
- assert(h->funcs != NULL);
- BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
- return(h->funcs->fileLength(h));
- } /* PHYSFS_filelength */
- LinkedStringList *__PHYSFS_addToLinkedStringList(LinkedStringList *retval,
- LinkedStringList **prev,
- const char *str,
- PHYSFS_sint32 len)
- {
- LinkedStringList *l;
- l = (LinkedStringList *) malloc(sizeof (LinkedStringList));
- BAIL_IF_MACRO(l == NULL, ERR_OUT_OF_MEMORY, retval);
- if (len < 0)
- len = strlen(str);
- l->str = (char *) malloc(len + 1);
- if (l->str == NULL)
- {
- free(l);
- BAIL_MACRO(ERR_OUT_OF_MEMORY, retval);
- } /* if */
- strncpy(l->str, str, len);
- l->str[len] = '\0';
- if (retval == NULL)
- retval = l;
- else
- (*prev)->next = l;
- *prev = l;
- l->next = NULL;
- return(retval);
- } /* __PHYSFS_addToLinkedStringList */
- /* end of physfs.c ... */
|