2001-07-05 10:18:39 +02:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2002-05-10 11:25:25 +02:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-07-16 16:36:02 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2001-07-05 10:18:39 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include "physfs.h"
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
typedef struct __PHYSFS_ERRMSGTYPE__
|
|
|
|
{
|
2002-04-03 09:40:27 +02:00
|
|
|
PHYSFS_uint64 tid;
|
2001-07-05 10:18:39 +02:00
|
|
|
int errorAvailable;
|
|
|
|
char errorString[80];
|
|
|
|
struct __PHYSFS_ERRMSGTYPE__ *next;
|
|
|
|
} ErrMsg;
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
typedef struct __PHYSFS_DIRINFO__
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
|
|
|
char *dirName;
|
2001-07-07 05:52:43 +02:00
|
|
|
DirHandle *dirHandle;
|
|
|
|
struct __PHYSFS_DIRINFO__ *next;
|
2002-04-01 20:48:24 +02:00
|
|
|
} PhysDirInfo;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
typedef struct __PHYSFS_FILEHANDLELIST__
|
|
|
|
{
|
2001-07-09 03:45:13 +02:00
|
|
|
PHYSFS_file handle;
|
2001-07-06 10:47:23 +02:00
|
|
|
struct __PHYSFS_FILEHANDLELIST__ *next;
|
|
|
|
} FileHandleList;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
/* The various i/o drivers... */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
|
|
|
#if (defined PHYSFS_SUPPORTS_ZIP)
|
2001-07-06 10:47:23 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
|
|
|
|
extern const DirFunctions __PHYSFS_DirFunctions_ZIP;
|
2001-07-06 03:27:14 +02:00
|
|
|
#endif
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-08 12:58:10 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_GRP)
|
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
|
|
|
|
extern const DirFunctions __PHYSFS_DirFunctions_GRP;
|
|
|
|
#endif
|
|
|
|
|
2002-08-09 21:47:35 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_QPAK)
|
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
|
|
|
|
extern const DirFunctions __PHYSFS_DirFunctions_QPAK;
|
|
|
|
#endif
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
extern const DirFunctions __PHYSFS_DirFunctions_DIR;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2002-08-09 21:47:35 +02:00
|
|
|
|
|
|
|
// !!! FIXME: This is stored with dirFunctions now, too.
|
2001-07-06 10:47:23 +02:00
|
|
|
static const PHYSFS_ArchiveInfo *supported_types[] =
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
|
|
|
#if (defined PHYSFS_SUPPORTS_ZIP)
|
2001-07-06 03:27:14 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_ZIP,
|
2001-07-05 10:18:39 +02:00
|
|
|
#endif
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_GRP)
|
|
|
|
&__PHYSFS_ArchiveInfo_GRP,
|
|
|
|
#endif
|
|
|
|
|
2002-08-09 21:47:35 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_QPAK)
|
|
|
|
&__PHYSFS_ArchiveInfo_QPAK,
|
|
|
|
#endif
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
static const DirFunctions *dirFunctions[] =
|
|
|
|
{
|
|
|
|
#if (defined PHYSFS_SUPPORTS_ZIP)
|
|
|
|
&__PHYSFS_DirFunctions_ZIP,
|
|
|
|
#endif
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_GRP)
|
|
|
|
&__PHYSFS_DirFunctions_GRP,
|
|
|
|
#endif
|
|
|
|
|
2002-08-09 21:47:35 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_QPAK)
|
|
|
|
&__PHYSFS_DirFunctions_QPAK,
|
|
|
|
#endif
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
&__PHYSFS_DirFunctions_DIR,
|
|
|
|
NULL
|
|
|
|
};
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
/* General PhysicsFS state ... */
|
|
|
|
static int initialized = 0;
|
|
|
|
static ErrMsg *errorMessages = NULL;
|
2002-04-01 20:48:24 +02:00
|
|
|
static PhysDirInfo *searchPath = NULL;
|
|
|
|
static PhysDirInfo *writeDir = NULL;
|
2001-07-06 10:47:23 +02:00
|
|
|
static FileHandleList *openWriteList = NULL;
|
|
|
|
static FileHandleList *openReadList = NULL;
|
|
|
|
static char *baseDir = NULL;
|
|
|
|
static char *userDir = NULL;
|
|
|
|
static int allowSymLinks = 0;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* mutexes ... */
|
|
|
|
static void *errorLock = NULL; /* protects error message list. */
|
|
|
|
static void *stateLock = NULL; /* protects other PhysFS static state. */
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* functions ... */
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
static ErrMsg *findErrorForCurrentThread(void)
|
|
|
|
{
|
|
|
|
ErrMsg *i;
|
2002-04-03 09:40:27 +02:00
|
|
|
PHYSFS_uint64 tid;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
2002-04-08 15:35:29 +02:00
|
|
|
__PHYSFS_platformGrabMutex(errorLock);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
if (errorMessages != NULL)
|
|
|
|
{
|
2002-04-03 09:40:27 +02:00
|
|
|
tid = __PHYSFS_platformGetThreadID();
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
for (i = errorMessages; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
if (i->tid == tid)
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformReleaseMutex(errorLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
return(i);
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* if */
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* for */
|
|
|
|
} /* if */
|
2002-04-08 15:35:29 +02:00
|
|
|
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
2002-04-08 15:35:29 +02:00
|
|
|
__PHYSFS_platformReleaseMutex(errorLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
return(NULL); /* no error available. */
|
|
|
|
} /* findErrorForCurrentThread */
|
|
|
|
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
void __PHYSFS_setError(const char *str)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
ErrMsg *err;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = findErrorForCurrentThread();
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
if (err == NULL)
|
|
|
|
{
|
|
|
|
err = (ErrMsg *) malloc(sizeof (ErrMsg));
|
|
|
|
if (err == NULL)
|
|
|
|
return; /* uhh...? */
|
|
|
|
|
2001-08-01 12:18:56 +02:00
|
|
|
memset((void *) err, '\0', sizeof (ErrMsg));
|
2002-04-03 09:40:27 +02:00
|
|
|
err->tid = __PHYSFS_platformGetThreadID();
|
2002-03-30 17:44:09 +01:00
|
|
|
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformGrabMutex(errorLock);
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
err->next = errorMessages;
|
|
|
|
errorMessages = err;
|
2002-06-08 10:50:00 +02:00
|
|
|
|
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformReleaseMutex(errorLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
err->errorAvailable = 1;
|
|
|
|
strncpy(err->errorString, str, sizeof (err->errorString));
|
|
|
|
err->errorString[sizeof (err->errorString) - 1] = '\0';
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* __PHYSFS_setError */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
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! */
|
2001-07-07 05:52:43 +02:00
|
|
|
static void freeErrorMessages(void)
|
|
|
|
{
|
|
|
|
ErrMsg *i;
|
|
|
|
ErrMsg *next;
|
|
|
|
|
|
|
|
for (i = errorMessages; i != NULL; i = next)
|
|
|
|
{
|
2001-07-09 02:51:46 +02:00
|
|
|
next = i->next;
|
2001-07-07 05:52:43 +02:00
|
|
|
free(i);
|
|
|
|
} /* for */
|
2002-06-08 10:50:00 +02:00
|
|
|
|
|
|
|
errorMessages = NULL;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* freeErrorMessages */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
2002-07-26 08:18:30 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
static DirHandle *openDirectory(const char *d, int forWriting)
|
|
|
|
{
|
|
|
|
const DirFunctions **i;
|
2002-07-26 08:18:30 +02:00
|
|
|
const char *ext;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2001-07-23 06:46:42 +02:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
|
|
|
|
|
2002-07-26 08:18:30 +02:00
|
|
|
ext = find_filename_extension(d);
|
|
|
|
if (ext != NULL)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2002-07-26 08:18:30 +02:00
|
|
|
/* 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 */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
|
|
|
|
return(NULL);
|
|
|
|
} /* openDirectory */
|
|
|
|
|
|
|
|
|
2002-04-01 20:48:24 +02:00
|
|
|
static PhysDirInfo *buildDirInfo(const char *newDir, int forWriting)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
|
|
|
DirHandle *dirHandle = NULL;
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *di = NULL;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
|
|
|
|
|
|
|
|
dirHandle = openDirectory(newDir, forWriting);
|
|
|
|
BAIL_IF_MACRO(dirHandle == NULL, NULL, 0);
|
|
|
|
|
2002-04-01 20:48:24 +02:00
|
|
|
di = (PhysDirInfo *) malloc(sizeof (PhysDirInfo));
|
2001-07-07 05:52:43 +02:00
|
|
|
if (di == NULL)
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
2001-07-08 05:25:12 +02:00
|
|
|
dirHandle->funcs->dirClose(dirHandle);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(di == NULL, ERR_OUT_OF_MEMORY, 0);
|
|
|
|
} /* if */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
di->dirName = (char *) malloc(strlen(newDir) + 1);
|
|
|
|
if (di->dirName == NULL)
|
|
|
|
{
|
|
|
|
free(di);
|
2001-07-08 05:25:12 +02:00
|
|
|
dirHandle->funcs->dirClose(dirHandle);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
di->next = NULL;
|
|
|
|
di->dirHandle = dirHandle;
|
|
|
|
strcpy(di->dirName, newDir);
|
|
|
|
return(di);
|
|
|
|
} /* buildDirInfo */
|
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* MAKE SURE you've got the stateLock held before calling this! */
|
2002-04-01 20:48:24 +02:00
|
|
|
static int freeDirInfo(PhysDirInfo *di, FileHandleList *openList)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
|
|
|
FileHandleList *i;
|
|
|
|
|
|
|
|
if (di == NULL)
|
|
|
|
return(1);
|
|
|
|
|
|
|
|
for (i = openList; i != NULL; i = i->next)
|
|
|
|
{
|
2001-07-09 03:45:13 +02:00
|
|
|
const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
|
2001-07-07 05:52:43 +02:00
|
|
|
BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
|
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
di->dirHandle->funcs->dirClose(di->dirHandle);
|
2001-07-07 05:52:43 +02:00
|
|
|
free(di->dirName);
|
|
|
|
free(di);
|
|
|
|
return(1);
|
|
|
|
} /* freeDirInfo */
|
|
|
|
|
|
|
|
|
|
|
|
static char *calculateUserDir(void)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
|
|
|
char *retval = NULL;
|
|
|
|
const char *str = NULL;
|
|
|
|
|
|
|
|
str = __PHYSFS_platformGetUserDir();
|
|
|
|
if (str != NULL)
|
2001-07-07 05:52:43 +02:00
|
|
|
retval = (char *) str;
|
2001-07-06 10:47:23 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
|
|
|
const char *uname = __PHYSFS_platformGetUserName();
|
|
|
|
|
|
|
|
str = (uname != NULL) ? uname : "default";
|
2001-07-07 05:52:43 +02:00
|
|
|
retval = (char *) malloc(strlen(baseDir) + strlen(str) +
|
2002-04-02 15:39:44 +02:00
|
|
|
strlen(dirsep) + 6);
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
if (retval == NULL)
|
|
|
|
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
|
|
|
|
else
|
2002-04-02 15:39:44 +02:00
|
|
|
sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
if (uname != NULL)
|
2001-07-07 05:52:43 +02:00
|
|
|
free((void *) uname);
|
2001-07-06 10:47:23 +02:00
|
|
|
} /* else */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* calculateUserDir */
|
|
|
|
|
|
|
|
|
2001-07-08 15:57:28 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
static char *calculateBaseDir(const char *argv0)
|
|
|
|
{
|
2001-07-08 15:57:28 +02:00
|
|
|
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 */
|
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
size = (size_t) (ptr - argv0);
|
2001-07-08 15:57:28 +02:00
|
|
|
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*)
|
|
|
|
*/
|
2001-09-15 00:59:53 +02:00
|
|
|
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);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* calculateBaseDir */
|
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
int PHYSFS_init(const char *argv0)
|
|
|
|
{
|
2001-07-16 16:36:02 +02:00
|
|
|
char *ptr;
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
|
2002-03-24 07:36:48 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
baseDir = calculateBaseDir(argv0);
|
2001-07-07 05:52:43 +02:00
|
|
|
BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
|
2002-04-06 07:18:49 +02:00
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
ptr = __PHYSFS_platformRealPath(baseDir);
|
|
|
|
free(baseDir);
|
|
|
|
BAIL_IF_MACRO(ptr == NULL, NULL, 0);
|
|
|
|
baseDir = ptr;
|
|
|
|
|
2001-07-08 15:57:28 +02:00
|
|
|
BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
userDir = calculateUserDir();
|
2001-07-16 16:36:02 +02:00
|
|
|
if (userDir != NULL)
|
|
|
|
{
|
|
|
|
ptr = __PHYSFS_platformRealPath(userDir);
|
|
|
|
free(userDir);
|
|
|
|
userDir = ptr;
|
|
|
|
} /* if */
|
|
|
|
|
2001-07-08 15:57:28 +02:00
|
|
|
if ((userDir == NULL) || (!appendDirSep(&userDir)))
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
|
|
|
free(baseDir);
|
|
|
|
baseDir = NULL;
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
initialized = 1;
|
2002-04-04 19:58:02 +02:00
|
|
|
|
|
|
|
/* This makes sure that the error subsystem is initialized. */
|
|
|
|
__PHYSFS_setError(PHYSFS_getLastError());
|
2001-07-05 10:18:39 +02:00
|
|
|
return(1);
|
|
|
|
} /* PHYSFS_init */
|
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* MAKE SURE you hold stateLock before calling this! */
|
2001-07-07 05:52:43 +02:00
|
|
|
static int closeFileHandleList(FileHandleList **list)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
|
|
|
FileHandleList *i;
|
|
|
|
FileHandleList *next = NULL;
|
2001-07-07 05:52:43 +02:00
|
|
|
FileHandle *h;
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
for (i = *list; i != NULL; i = next)
|
|
|
|
{
|
|
|
|
next = i->next;
|
2001-07-09 03:45:13 +02:00
|
|
|
h = (FileHandle *) (i->handle.opaque);
|
|
|
|
if (!h->funcs->fileClose(h))
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
|
|
|
*list = i;
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
free(i);
|
2001-07-06 10:47:23 +02:00
|
|
|
} /* for */
|
|
|
|
|
|
|
|
*list = NULL;
|
2001-07-07 05:52:43 +02:00
|
|
|
return(1);
|
|
|
|
} /* closeFileHandleList */
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* MAKE SURE you hold the stateLock before calling this! */
|
2001-07-05 10:18:39 +02:00
|
|
|
static void freeSearchPath(void)
|
|
|
|
{
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
|
|
|
PhysDirInfo *next = NULL;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
closeFileHandleList(&openReadList);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
if (searchPath != NULL)
|
|
|
|
{
|
|
|
|
for (i = searchPath; i != NULL; i = next)
|
|
|
|
{
|
2001-07-09 02:51:46 +02:00
|
|
|
next = i->next;
|
2001-07-07 05:52:43 +02:00
|
|
|
freeDirInfo(i, openReadList);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* for */
|
|
|
|
searchPath = NULL;
|
|
|
|
} /* if */
|
|
|
|
} /* freeSearchPath */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
int PHYSFS_deinit(void)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
|
|
|
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
|
2002-03-24 07:36:48 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
closeFileHandleList(&openWriteList);
|
2001-07-07 05:52:43 +02:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
freeSearchPath();
|
|
|
|
freeErrorMessages();
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
if (baseDir != NULL)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
free(baseDir);
|
2001-07-06 10:47:23 +02:00
|
|
|
baseDir = NULL;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (userDir != NULL)
|
|
|
|
{
|
|
|
|
free(userDir);
|
|
|
|
userDir = NULL;
|
|
|
|
} /* if */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
|
|
|
allowSymLinks = 0;
|
2001-07-05 10:18:39 +02:00
|
|
|
initialized = 0;
|
2002-03-30 17:44:09 +01:00
|
|
|
|
|
|
|
__PHYSFS_platformDestroyMutex(errorLock);
|
|
|
|
__PHYSFS_platformDestroyMutex(stateLock);
|
|
|
|
|
|
|
|
errorLock = stateLock = NULL;
|
2001-07-05 10:18:39 +02:00
|
|
|
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)
|
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
return(__PHYSFS_platformDirSeparator);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* 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)
|
|
|
|
{
|
2001-07-06 10:47:23 +02:00
|
|
|
return(userDir); /* this is calculated in PHYSFS_init()... */
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getUserDir */
|
|
|
|
|
|
|
|
|
|
|
|
const char *PHYSFS_getWriteDir(void)
|
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
const char *retval = NULL;
|
|
|
|
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
if (writeDir != NULL)
|
|
|
|
retval = writeDir->dirName;
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
return(retval);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getWriteDir */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_setWriteDir(const char *newDir)
|
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
int retval = 1;
|
|
|
|
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
if (writeDir != NULL)
|
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!freeDirInfo(writeDir, openWriteList), NULL,
|
|
|
|
stateLock, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
writeDir = NULL;
|
|
|
|
} /* if */
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
if (newDir != NULL)
|
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
writeDir = buildDirInfo(newDir, 1);
|
2002-03-30 17:44:09 +01:00
|
|
|
retval = (writeDir != NULL);
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* if */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
|
|
|
return(retval);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_setWriteDir */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
|
|
|
|
{
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *di;
|
|
|
|
PhysDirInfo *prev = NULL;
|
|
|
|
PhysDirInfo *i;
|
2001-07-23 11:23:17 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-23 11:23:17 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
/* already in search path? */
|
|
|
|
BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
|
2001-07-23 11:23:17 +02:00
|
|
|
prev = i;
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* for */
|
2001-07-23 11:23:17 +02:00
|
|
|
|
|
|
|
di = buildDirInfo(newDir, 0);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(di == NULL, NULL, stateLock, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
if (appendToPath)
|
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
di->next = NULL;
|
2001-07-05 10:18:39 +02:00
|
|
|
if (prev == NULL)
|
2001-07-07 05:52:43 +02:00
|
|
|
searchPath = di;
|
2001-07-05 10:18:39 +02:00
|
|
|
else
|
2001-07-07 05:52:43 +02:00
|
|
|
prev->next = di;
|
2001-07-08 15:57:28 +02:00
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
di->next = searchPath;
|
|
|
|
searchPath = di;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* else */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
return(1);
|
|
|
|
} /* PHYSFS_addToSearchPath */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_removeFromSearchPath(const char *oldDir)
|
|
|
|
{
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
|
|
|
PhysDirInfo *prev = NULL;
|
|
|
|
PhysDirInfo *next = NULL;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
if (strcmp(i->dirName, oldDir) == 0)
|
|
|
|
{
|
2001-07-06 10:47:23 +02:00
|
|
|
next = i->next;
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!freeDirInfo(i, openReadList), NULL,
|
|
|
|
stateLock, 0);
|
2001-07-06 10:47:23 +02:00
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
if (prev == NULL)
|
2001-07-06 10:47:23 +02:00
|
|
|
searchPath = next;
|
2001-07-05 10:18:39 +02:00
|
|
|
else
|
2001-07-06 10:47:23 +02:00
|
|
|
prev->next = next;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_MACRO_MUTEX(NULL, stateLock, 1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* if */
|
|
|
|
prev = i;
|
|
|
|
} /* for */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_removeFromSearchPath */
|
|
|
|
|
|
|
|
|
|
|
|
char **PHYSFS_getSearchPath(void)
|
|
|
|
{
|
|
|
|
int count = 1;
|
|
|
|
int x;
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
2001-07-05 10:18:39 +02:00
|
|
|
char **retval;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
count++;
|
|
|
|
|
|
|
|
retval = (char **) malloc(sizeof (char *) * count);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!retval, ERR_OUT_OF_MEMORY, stateLock, NULL);
|
2001-07-05 10:18:39 +02:00
|
|
|
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);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
strcpy(retval[x], i->dirName);
|
|
|
|
} /* for */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
return(retval);
|
|
|
|
} /* PHYSFS_getSearchPath */
|
|
|
|
|
|
|
|
|
2001-09-26 03:44:41 +02:00
|
|
|
int PHYSFS_setSaneConfig(const char *organization, const char *appName,
|
|
|
|
const char *archiveExt, int includeCdRoms,
|
|
|
|
int archivesFirst)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
const char *basedir = PHYSFS_getBaseDir();
|
|
|
|
const char *userdir = PHYSFS_getUserDir();
|
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
|
|
|
char *str;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
/* set write dir... */
|
2001-09-26 03:44:41 +02:00
|
|
|
str = malloc(strlen(userdir) + (strlen(organization) * 2) +
|
|
|
|
(strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
|
2001-07-06 03:27:14 +02:00
|
|
|
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
|
2001-09-26 03:44:41 +02:00
|
|
|
sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
|
|
|
if (!PHYSFS_setWriteDir(str))
|
|
|
|
{
|
2002-03-15 15:53:23 +01:00
|
|
|
int no_write = 0;
|
|
|
|
sprintf(str, ".%s/%s", organization, appName);
|
2001-09-26 05:08:57 +02:00
|
|
|
if ( (PHYSFS_setWriteDir(userdir)) &&
|
2002-03-15 15:53:23 +01:00
|
|
|
(PHYSFS_mkdir(str)) )
|
2001-09-26 05:08:57 +02:00
|
|
|
{
|
2002-03-15 15:53:23 +01:00
|
|
|
sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
|
|
|
|
if (!PHYSFS_setWriteDir(str))
|
|
|
|
no_write = 1;
|
2001-09-26 05:08:57 +02:00
|
|
|
} /* if */
|
|
|
|
else
|
2001-07-16 16:36:02 +02:00
|
|
|
{
|
2002-03-15 15:53:23 +01:00
|
|
|
no_write = 1;
|
2001-09-26 05:08:57 +02:00
|
|
|
} /* else */
|
2002-03-15 15:53:23 +01:00
|
|
|
|
|
|
|
if (no_write)
|
|
|
|
{
|
|
|
|
PHYSFS_setWriteDir(NULL); /* just in case. */
|
|
|
|
free(str);
|
|
|
|
BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
|
|
|
|
} /* if */
|
2001-07-16 16:36:02 +02:00
|
|
|
} /* if */
|
|
|
|
|
2001-09-26 05:08:57 +02:00
|
|
|
/* Put write dir first in search path... */
|
|
|
|
PHYSFS_addToSearchPath(str, 0);
|
2001-07-06 03:27:14 +02:00
|
|
|
free(str);
|
|
|
|
|
2001-09-26 03:44:41 +02:00
|
|
|
/* Put base path on search path... */
|
2001-07-06 03:27:14 +02:00
|
|
|
PHYSFS_addToSearchPath(basedir, 1);
|
|
|
|
|
|
|
|
/* handle CD-ROMs... */
|
|
|
|
if (includeCdRoms)
|
|
|
|
{
|
|
|
|
char **cds = PHYSFS_getCdRomDirs();
|
|
|
|
char **i;
|
|
|
|
for (i = cds; *i != NULL; i++)
|
2001-07-07 05:52:43 +02:00
|
|
|
PHYSFS_addToSearchPath(*i, 1);
|
2001-09-26 03:44:41 +02:00
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
PHYSFS_freeList(cds);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* Root out archives, and add them to search path... */
|
|
|
|
if (archiveExt != NULL)
|
|
|
|
{
|
2002-07-28 23:03:27 +02:00
|
|
|
char **rc = PHYSFS_enumerateFiles("/");
|
2001-07-06 03:27:14 +02:00
|
|
|
char **i;
|
2001-09-02 06:55:25 +02:00
|
|
|
size_t extlen = strlen(archiveExt);
|
2001-07-06 03:27:14 +02:00
|
|
|
char *ext;
|
|
|
|
|
|
|
|
for (i = rc; *i != NULL; i++)
|
|
|
|
{
|
2001-09-02 06:55:25 +02:00
|
|
|
size_t l = strlen(*i);
|
|
|
|
if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
|
|
|
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);
|
2001-07-07 05:52:43 +02:00
|
|
|
PHYSFS_addToSearchPath(str, archivesFirst == 0);
|
2001-07-06 03:27:14 +02:00
|
|
|
free(str);
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
PHYSFS_freeList(rc);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_setSaneConfig */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
void PHYSFS_permitSymbolicLinks(int allow)
|
|
|
|
{
|
|
|
|
allowSymLinks = allow;
|
|
|
|
} /* PHYSFS_permitSymbolicLinks */
|
|
|
|
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
/* string manipulation in C makes my ass itch. */
|
2001-08-23 20:01:43 +02:00
|
|
|
char * __PHYSFS_convertToDependent(const char *prepend,
|
|
|
|
const char *dirName,
|
|
|
|
const char *append)
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
const char *dirsep = __PHYSFS_platformDirSeparator;
|
2001-09-02 06:55:25 +02:00
|
|
|
size_t sepsize = strlen(dirsep);
|
2001-07-06 03:27:14 +02:00
|
|
|
char *str;
|
|
|
|
char *i1;
|
|
|
|
char *i2;
|
|
|
|
size_t allocSize;
|
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*dirName == '/')
|
|
|
|
dirName++;
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
allocSize = strlen(dirName) + 1;
|
2001-07-06 03:27:14 +02:00
|
|
|
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)
|
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
str = (char *) dirName;
|
|
|
|
do
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
str = strchr(str, '/');
|
|
|
|
if (str != NULL)
|
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
allocSize += (sepsize - 1);
|
2001-07-07 05:52:43 +02:00
|
|
|
str++;
|
|
|
|
} /* if */
|
|
|
|
} while (str != NULL);
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
str = (char *) malloc(allocSize);
|
|
|
|
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
if (prepend == NULL)
|
|
|
|
*str = '\0';
|
|
|
|
else
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
|
|
|
strcpy(str, prepend);
|
|
|
|
strcat(str, dirsep);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* else */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
|
|
|
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);
|
2002-03-24 20:47:33 +01:00
|
|
|
} /* __PHYSFS_convertToDependent */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
int __PHYSFS_verifySecurity(DirHandle *h, const char *fname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
int retval = 1;
|
|
|
|
char *start;
|
|
|
|
char *end;
|
2001-07-06 03:27:14 +02:00
|
|
|
char *str;
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
start = str = malloc(strlen(fname) + 1);
|
|
|
|
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
|
|
|
|
strcpy(str, fname);
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
end = strchr(start, '/');
|
|
|
|
if (end != NULL)
|
|
|
|
*end = '\0';
|
|
|
|
|
|
|
|
if ( (strcmp(start, ".") == 0) ||
|
|
|
|
(strcmp(start, "..") == 0) ||
|
2001-07-08 05:25:12 +02:00
|
|
|
(strchr(start, '\\') != NULL) ||
|
2001-07-07 05:52:43 +02:00
|
|
|
(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 */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
|
|
|
free(str);
|
2001-07-07 05:52:43 +02:00
|
|
|
return(retval);
|
|
|
|
} /* __PHYSFS_verifySecurity */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
int PHYSFS_mkdir(const char *dname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
DirHandle *h;
|
2001-07-06 03:27:14 +02:00
|
|
|
char *str;
|
2001-07-07 05:52:43 +02:00
|
|
|
char *start;
|
|
|
|
char *end;
|
|
|
|
int retval = 0;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
|
|
|
|
while (*dname == '/')
|
|
|
|
dname++;
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
|
2001-07-07 05:52:43 +02:00
|
|
|
h = writeDir->dirHandle;
|
2002-03-30 17:44:09 +01:00
|
|
|
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);
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
end = strchr(start, '/');
|
|
|
|
if (end != NULL)
|
|
|
|
*end = '\0';
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
retval = h->funcs->mkdir(h, str);
|
|
|
|
if (!retval)
|
|
|
|
break;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
if (end == NULL)
|
|
|
|
break;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
*end = '/';
|
|
|
|
start = end + 1;
|
|
|
|
} /* while */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
free(str);
|
|
|
|
return(retval);
|
|
|
|
} /* PHYSFS_mkdir */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_delete(const char *fname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
int retval;
|
2001-07-07 05:52:43 +02:00
|
|
|
DirHandle *h;
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*fname == '/')
|
|
|
|
fname++;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__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);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* PHYSFS_delete */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
const char *PHYSFS_getRealDir(const char *filename)
|
|
|
|
{
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*filename == '/')
|
|
|
|
filename++;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
DirHandle *h = i->dirHandle;
|
|
|
|
if (__PHYSFS_verifySecurity(h, filename))
|
|
|
|
{
|
2002-05-25 13:12:17 +02:00
|
|
|
if (!h->funcs->exists(h, filename))
|
|
|
|
__PHYSFS_setError(ERR_NO_SUCH_FILE);
|
|
|
|
else
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
return(i->dirName);
|
2002-05-25 13:12:17 +02:00
|
|
|
} /* else */
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
return(NULL);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getRealDir */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
static int countList(LinkedStringList *list)
|
2001-07-06 23:29:37 +02:00
|
|
|
{
|
|
|
|
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 */
|
|
|
|
|
2001-09-02 06:55:25 +02:00
|
|
|
if (retval != NULL)
|
2001-07-06 23:29:37 +02:00
|
|
|
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);
|
2001-07-16 16:36:02 +02:00
|
|
|
if (rc > 0) /* insertion point. */
|
|
|
|
break;
|
|
|
|
else if (rc == 0) /* already in list. */
|
2001-07-06 23:29:37 +02:00
|
|
|
{
|
|
|
|
free(item->str);
|
|
|
|
free(item);
|
|
|
|
return;
|
|
|
|
} /* else if */
|
|
|
|
prev = i;
|
|
|
|
} /* for */
|
2001-07-16 16:36:02 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2001-07-06 23:29:37 +02:00
|
|
|
} /* 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 */
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
char **PHYSFS_enumerateFiles(const char *path)
|
|
|
|
{
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
2001-07-06 23:29:37 +02:00
|
|
|
char **retval = NULL;
|
|
|
|
LinkedStringList *rc;
|
|
|
|
LinkedStringList *finalList = NULL;
|
2001-07-16 19:36:28 +02:00
|
|
|
int omitSymLinks = !allowSymLinks;
|
2001-07-06 23:29:37 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(path == NULL, ERR_INVALID_ARGUMENT, NULL);
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*path == '/')
|
|
|
|
path++;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-06 23:29:37 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
DirHandle *h = i->dirHandle;
|
|
|
|
if (__PHYSFS_verifySecurity(h, path))
|
|
|
|
{
|
2001-07-16 19:36:28 +02:00
|
|
|
rc = h->funcs->enumerateFiles(h, path, omitSymLinks);
|
2001-07-07 05:52:43 +02:00
|
|
|
interpolateStringLists(&finalList, rc);
|
|
|
|
} /* if */
|
2001-07-06 23:29:37 +02:00
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-06 23:29:37 +02:00
|
|
|
|
|
|
|
retval = convertStringListToPhysFSList(finalList);
|
|
|
|
return(retval);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_enumerateFiles */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
int PHYSFS_exists(const char *fname)
|
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*fname == '/')
|
|
|
|
fname++;
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
return(PHYSFS_getRealDir(fname) != NULL);
|
|
|
|
} /* PHYSFS_exists */
|
|
|
|
|
|
|
|
|
2002-05-25 11:41:14 +02:00
|
|
|
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))
|
|
|
|
{
|
2002-05-25 13:12:17 +02:00
|
|
|
if (!h->funcs->exists(h, fname))
|
|
|
|
__PHYSFS_setError(ERR_NO_SUCH_FILE);
|
|
|
|
else
|
2002-05-25 11:41:14 +02:00
|
|
|
{
|
2002-05-25 13:12:17 +02:00
|
|
|
PHYSFS_sint64 retval = -1;
|
|
|
|
if (h->funcs->getLastModTime == NULL)
|
|
|
|
__PHYSFS_setError(ERR_NOT_SUPPORTED);
|
|
|
|
else
|
|
|
|
retval = h->funcs->getLastModTime(h, fname);
|
|
|
|
|
2002-05-25 11:41:14 +02:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
return(retval);
|
2002-05-25 13:12:17 +02:00
|
|
|
} /* else */
|
2002-05-25 11:41:14 +02:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
2002-05-25 13:12:17 +02:00
|
|
|
return(-1); /* error set in verifysecurity/exists */
|
2002-05-25 11:41:14 +02:00
|
|
|
} /* PHYSFS_getLastModTime */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
int PHYSFS_isDirectory(const char *fname)
|
|
|
|
{
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*fname == '/')
|
|
|
|
fname++;
|
|
|
|
|
2001-07-23 06:46:42 +02:00
|
|
|
if (*fname == '\0')
|
|
|
|
return(1);
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
DirHandle *h = i->dirHandle;
|
|
|
|
if (__PHYSFS_verifySecurity(h, fname))
|
|
|
|
{
|
2002-07-25 23:52:07 +02:00
|
|
|
if (!h->funcs->exists(h, fname))
|
2002-05-25 13:12:17 +02:00
|
|
|
__PHYSFS_setError(ERR_NO_SUCH_FILE);
|
|
|
|
else
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
|
|
|
int retval = h->funcs->isDirectory(h, fname);
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
return(retval);
|
2002-05-25 13:12:17 +02:00
|
|
|
} /* else */
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
} /* PHYSFS_isDirectory */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_isSymbolicLink(const char *fname)
|
|
|
|
{
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
if (!allowSymLinks)
|
|
|
|
return(0);
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*fname == '/')
|
|
|
|
fname++;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
DirHandle *h = i->dirHandle;
|
|
|
|
if (__PHYSFS_verifySecurity(h, fname))
|
|
|
|
{
|
2002-05-25 13:12:17 +02:00
|
|
|
if (!h->funcs->exists(h, fname))
|
|
|
|
__PHYSFS_setError(ERR_NO_SUCH_FILE);
|
|
|
|
else
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
|
|
|
int retval = h->funcs->isSymLink(h, fname);
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
return(retval);
|
2002-05-25 13:12:17 +02:00
|
|
|
} /* else */
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
return(0);
|
|
|
|
} /* PHYSFS_isSymbolicLink */
|
|
|
|
|
2001-07-07 11:05:19 +02:00
|
|
|
|
|
|
|
static PHYSFS_file *doOpenWrite(const char *fname, int appending)
|
|
|
|
{
|
2001-07-09 03:45:13 +02:00
|
|
|
PHYSFS_file *retval = NULL;
|
2001-07-07 11:05:19 +02:00
|
|
|
FileHandle *rc = NULL;
|
2002-03-30 17:44:09 +01:00
|
|
|
DirHandle *h;
|
|
|
|
const DirFunctions *f;
|
2001-07-07 11:05:19 +02:00
|
|
|
FileHandleList *list;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*fname == '/')
|
|
|
|
fname++;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__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);
|
2001-07-07 11:05:19 +02:00
|
|
|
|
|
|
|
list = (FileHandleList *) malloc(sizeof (FileHandleList));
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!list, ERR_OUT_OF_MEMORY, stateLock, NULL);
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
f = h->funcs;
|
2001-07-07 11:05:19 +02:00
|
|
|
rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
|
|
|
|
if (rc == NULL)
|
|
|
|
free(list);
|
|
|
|
else
|
|
|
|
{
|
2001-07-09 03:45:13 +02:00
|
|
|
list->handle.opaque = (void *) rc;
|
2001-07-07 11:05:19 +02:00
|
|
|
list->next = openWriteList;
|
|
|
|
openWriteList = list;
|
2001-07-09 03:45:13 +02:00
|
|
|
retval = &(list->handle);
|
2001-07-07 11:05:19 +02:00
|
|
|
} /* else */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 11:05:19 +02:00
|
|
|
return(retval);
|
|
|
|
} /* doOpenWrite */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
PHYSFS_file *PHYSFS_openWrite(const char *filename)
|
|
|
|
{
|
2001-07-07 11:05:19 +02:00
|
|
|
return(doOpenWrite(filename, 0));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_openWrite */
|
|
|
|
|
|
|
|
|
|
|
|
PHYSFS_file *PHYSFS_openAppend(const char *filename)
|
|
|
|
{
|
2001-07-07 11:05:19 +02:00
|
|
|
return(doOpenWrite(filename, 1));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_openAppend */
|
|
|
|
|
|
|
|
|
2001-07-07 11:05:19 +02:00
|
|
|
PHYSFS_file *PHYSFS_openRead(const char *fname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
PHYSFS_file *retval;
|
2001-07-07 11:05:19 +02:00
|
|
|
FileHandle *rc = NULL;
|
|
|
|
FileHandleList *list;
|
2002-04-01 20:48:24 +02:00
|
|
|
PhysDirInfo *i;
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
|
2001-07-16 16:36:02 +02:00
|
|
|
while (*fname == '/')
|
|
|
|
fname++;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2002-06-30 00:24:52 +02:00
|
|
|
BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NOT_IN_SEARCH_PATH, stateLock, NULL);
|
2001-07-07 11:05:19 +02:00
|
|
|
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 */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(rc == NULL, NULL, stateLock, NULL);
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2001-07-23 11:23:17 +02:00
|
|
|
list = (FileHandleList *) malloc(sizeof (FileHandleList));
|
|
|
|
BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
list->handle.opaque = (void *) rc;
|
|
|
|
list->next = openReadList;
|
|
|
|
openReadList = list;
|
2002-03-30 17:44:09 +01:00
|
|
|
retval = &(list->handle);
|
2001-07-23 11:23:17 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
return(retval);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_openRead */
|
|
|
|
|
|
|
|
|
2001-07-09 03:45:13 +02:00
|
|
|
static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
FileHandle *h = (FileHandle *) handle->opaque;
|
2001-07-09 03:45:13 +02:00
|
|
|
FileHandleList *prev = NULL;
|
2001-07-06 10:47:23 +02:00
|
|
|
FileHandleList *i;
|
|
|
|
int rc;
|
|
|
|
|
2001-07-09 03:45:13 +02:00
|
|
|
for (i = *list; i != NULL; i = i->next)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2001-07-09 03:45:13 +02:00
|
|
|
if (&i->handle == handle) /* handle is in this list? */
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2001-07-09 03:45:13 +02:00
|
|
|
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 */
|
2001-07-06 10:47:23 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
return(0);
|
2001-07-09 03:45:13 +02:00
|
|
|
} /* closeHandleInOpenList */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_close(PHYSFS_file *handle)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
|
2001-07-09 03:45:13 +02:00
|
|
|
/* -1 == close failure. 0 == not found. 1 == success. */
|
|
|
|
rc = closeHandleInOpenList(&openReadList, handle);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
|
2001-07-09 03:45:13 +02:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
rc = closeHandleInOpenList(&openWriteList, handle);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
|
2001-07-09 03:45:13 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
|
|
|
|
return(1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_close */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_sint64 PHYSFS_read(PHYSFS_file *handle, void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
FileHandle *h = (FileHandle *) handle->opaque;
|
|
|
|
assert(h != NULL);
|
2001-07-06 10:47:23 +02:00
|
|
|
assert(h->funcs != NULL);
|
|
|
|
BAIL_IF_MACRO(h->funcs->read == NULL, ERR_NOT_SUPPORTED, -1);
|
|
|
|
return(h->funcs->read(h, buffer, objSize, objCount));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_read */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_sint64 PHYSFS_write(PHYSFS_file *handle, const void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
FileHandle *h = (FileHandle *) handle->opaque;
|
|
|
|
assert(h != NULL);
|
2001-07-06 10:47:23 +02:00
|
|
|
assert(h->funcs != NULL);
|
|
|
|
BAIL_IF_MACRO(h->funcs->write == NULL, ERR_NOT_SUPPORTED, -1);
|
|
|
|
return(h->funcs->write(h, buffer, objSize, objCount));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_write */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_eof(PHYSFS_file *handle)
|
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
FileHandle *h = (FileHandle *) handle->opaque;
|
|
|
|
assert(h != NULL);
|
2001-07-06 10:47:23 +02:00
|
|
|
assert(h->funcs != NULL);
|
|
|
|
BAIL_IF_MACRO(h->funcs->eof == NULL, ERR_NOT_SUPPORTED, -1);
|
|
|
|
return(h->funcs->eof(h));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_eof */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_sint64 PHYSFS_tell(PHYSFS_file *handle)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
FileHandle *h = (FileHandle *) handle->opaque;
|
|
|
|
assert(h != NULL);
|
2001-07-06 10:47:23 +02:00
|
|
|
assert(h->funcs != NULL);
|
|
|
|
BAIL_IF_MACRO(h->funcs->tell == NULL, ERR_NOT_SUPPORTED, -1);
|
|
|
|
return(h->funcs->tell(h));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_tell */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
int PHYSFS_seek(PHYSFS_file *handle, PHYSFS_uint64 pos)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
FileHandle *h = (FileHandle *) handle->opaque;
|
|
|
|
assert(h != NULL);
|
2001-07-06 10:47:23 +02:00
|
|
|
assert(h->funcs != NULL);
|
|
|
|
BAIL_IF_MACRO(h->funcs->seek == NULL, ERR_NOT_SUPPORTED, 0);
|
2001-07-23 09:15:21 +02:00
|
|
|
BAIL_IF_MACRO(pos < 0, ERR_INVALID_ARGUMENT, 0);
|
2001-07-06 10:47:23 +02:00
|
|
|
return(h->funcs->seek(h, pos));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_seek */
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_file *handle)
|
2001-07-09 06:15:35 +02:00
|
|
|
{
|
|
|
|
FileHandle *h = (FileHandle *) handle->opaque;
|
|
|
|
assert(h != NULL);
|
|
|
|
assert(h->funcs != NULL);
|
|
|
|
BAIL_IF_MACRO(h->funcs->fileLength == NULL, ERR_NOT_SUPPORTED, 0);
|
2002-05-25 11:41:14 +02:00
|
|
|
|
2001-07-09 06:15:35 +02:00
|
|
|
return(h->funcs->fileLength(h));
|
|
|
|
} /* PHYSFS_filelength */
|
|
|
|
|
2002-07-23 09:48:08 +02:00
|
|
|
|
|
|
|
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 */
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
/* end of physfs.c ... */
|
2001-07-05 10:18:39 +02:00
|
|
|
|