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. :)
|
|
|
|
*
|
2007-03-11 11:10:28 +01:00
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
2001-07-05 10:18:39 +02:00
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
/* !!! FIXME: ERR_PAST_EOF shouldn't trigger for reads. Just return zero. */
|
2012-03-15 06:58:39 +01:00
|
|
|
/* !!! FIXME: use snprintf(), not sprintf(). */
|
2010-08-21 08:47:58 +02:00
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
|
|
|
|
typedef struct __PHYSFS_DIRHANDLE__
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
void *opaque; /* Instance data unique to the archiver. */
|
|
|
|
char *dirName; /* Path to archive in platform-dependent notation. */
|
2005-03-13 10:09:26 +01:00
|
|
|
char *mountPoint; /* Mountpoint in virtual file tree. */
|
2004-09-26 15:00:59 +02:00
|
|
|
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
|
|
|
|
struct __PHYSFS_DIRHANDLE__ *next; /* linked list stuff. */
|
2004-09-26 02:25:04 +02:00
|
|
|
} DirHandle;
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
typedef struct __PHYSFS_FILEHANDLE__
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io; /* Instance data unique to the archiver for this file. */
|
2004-09-26 15:00:59 +02:00
|
|
|
PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
|
|
|
|
const DirHandle *dirHandle; /* Archiver instance that created this */
|
|
|
|
PHYSFS_uint8 *buffer; /* Buffer, if set (NULL otherwise). Don't touch! */
|
|
|
|
PHYSFS_uint32 bufsize; /* Bufsize, if set (0 otherwise). Don't touch! */
|
|
|
|
PHYSFS_uint32 buffill; /* Buffer fill size. Don't touch! */
|
|
|
|
PHYSFS_uint32 bufpos; /* Buffer position. Don't touch! */
|
|
|
|
struct __PHYSFS_FILEHANDLE__ *next; /* linked list stuff. */
|
|
|
|
} FileHandle;
|
|
|
|
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
typedef struct __PHYSFS_ERRSTATETYPE__
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2009-09-06 07:05:27 +02:00
|
|
|
void *tid;
|
2012-03-20 20:38:12 +01:00
|
|
|
PHYSFS_ErrorCode code;
|
|
|
|
struct __PHYSFS_ERRSTATETYPE__ *next;
|
|
|
|
} ErrState;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
|
2007-03-10 07:24:56 +01:00
|
|
|
/* The various i/o drivers...some of these may not be compiled in. */
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
|
2006-04-11 16:33:48 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_LZMA;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_LZMA;
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
|
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
|
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
|
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
|
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
|
2007-03-10 07:24:56 +01:00
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
|
2010-03-17 19:50:54 +01:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ISO9660;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_ISO9660;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2002-08-09 21:47:35 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
static const PHYSFS_ArchiveInfo *supported_types[] =
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_ZIP
|
2001-07-06 03:27:14 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_ZIP,
|
2001-07-05 10:18:39 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_7Z
|
2006-04-11 16:33:48 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_LZMA,
|
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_GRP
|
2001-07-08 07:27:05 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_GRP,
|
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_QPAK
|
2003-07-21 02:27:27 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_QPAK,
|
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_HOG
|
2003-03-30 20:59:54 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_HOG,
|
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_MVL
|
2003-03-30 20:59:54 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_MVL,
|
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_WAD
|
2003-12-15 05:01:18 +01:00
|
|
|
&__PHYSFS_ArchiveInfo_WAD,
|
2004-04-09 08:36:09 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_ISO9660
|
2010-03-17 19:50:54 +01:00
|
|
|
&__PHYSFS_ArchiveInfo_ISO9660,
|
2010-03-17 20:35:29 +01:00
|
|
|
#endif
|
2001-07-05 10:18:39 +02:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static const PHYSFS_Archiver *archivers[] =
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_ZIP
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_ZIP,
|
2001-07-06 10:47:23 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_7Z
|
2006-04-11 16:33:48 +02:00
|
|
|
&__PHYSFS_Archiver_LZMA,
|
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_GRP
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_GRP,
|
2001-07-08 07:27:05 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_QPAK
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_QPAK,
|
2003-07-21 02:27:27 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_HOG
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_HOG,
|
2003-03-30 20:59:54 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_MVL
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_MVL,
|
2003-03-30 20:59:54 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_WAD
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_WAD,
|
2004-04-09 08:36:09 +02:00
|
|
|
#endif
|
2012-03-23 05:52:32 +01:00
|
|
|
#if PHYSFS_SUPPORTS_ISO9660
|
2010-03-17 19:50:54 +01:00
|
|
|
&__PHYSFS_Archiver_ISO9660,
|
2010-03-17 20:35:29 +01:00
|
|
|
#endif
|
2001-07-06 10:47:23 +02:00
|
|
|
NULL
|
|
|
|
};
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
/* General PhysicsFS state ... */
|
|
|
|
static int initialized = 0;
|
2012-03-20 20:38:12 +01:00
|
|
|
static ErrState *errorStates = NULL;
|
2004-09-26 15:00:59 +02:00
|
|
|
static DirHandle *searchPath = NULL;
|
|
|
|
static DirHandle *writeDir = NULL;
|
|
|
|
static FileHandle *openWriteList = NULL;
|
|
|
|
static FileHandle *openReadList = NULL;
|
2001-07-06 10:47:23 +02:00
|
|
|
static char *baseDir = NULL;
|
|
|
|
static char *userDir = NULL;
|
2012-03-22 04:30:50 +01:00
|
|
|
static char *prefDir = NULL;
|
2001-07-06 10:47:23 +02:00
|
|
|
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
|
|
|
|
2004-09-23 08:45:36 +02:00
|
|
|
/* allocator ... */
|
|
|
|
static int externalAllocator = 0;
|
2005-03-14 12:49:30 +01:00
|
|
|
PHYSFS_Allocator allocator;
|
2004-09-23 08:45:36 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* PHYSFS_Io implementation for i/o to physical filesystem... */
|
|
|
|
|
|
|
|
/* !!! FIXME: maybe refcount the paths in a string pool? */
|
|
|
|
typedef struct __PHYSFS_NativeIoInfo
|
|
|
|
{
|
|
|
|
void *handle;
|
|
|
|
const char *path;
|
|
|
|
int mode; /* 'r', 'w', or 'a' */
|
|
|
|
} NativeIoInfo;
|
|
|
|
|
|
|
|
static PHYSFS_sint64 nativeIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
|
|
|
|
{
|
|
|
|
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
|
|
|
|
return __PHYSFS_platformRead(info->handle, buf, len);
|
|
|
|
} /* nativeIo_read */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 nativeIo_write(PHYSFS_Io *io, const void *buffer,
|
|
|
|
PHYSFS_uint64 len)
|
|
|
|
{
|
|
|
|
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
|
|
|
|
return __PHYSFS_platformWrite(info->handle, buffer, len);
|
|
|
|
} /* nativeIo_write */
|
|
|
|
|
|
|
|
static int nativeIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
|
|
|
|
{
|
|
|
|
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
|
|
|
|
return __PHYSFS_platformSeek(info->handle, offset);
|
|
|
|
} /* nativeIo_seek */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 nativeIo_tell(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
|
|
|
|
return __PHYSFS_platformTell(info->handle);
|
|
|
|
} /* nativeIo_tell */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 nativeIo_length(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
|
|
|
|
return __PHYSFS_platformFileLength(info->handle);
|
|
|
|
} /* nativeIo_length */
|
|
|
|
|
|
|
|
static PHYSFS_Io *nativeIo_duplicate(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
|
|
|
|
return __PHYSFS_createNativeIo(info->path, info->mode);
|
|
|
|
} /* nativeIo_duplicate */
|
|
|
|
|
|
|
|
static int nativeIo_flush(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
return __PHYSFS_platformFlush(io->opaque);
|
|
|
|
} /* nativeIo_flush */
|
|
|
|
|
|
|
|
static void nativeIo_destroy(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
NativeIoInfo *info = (NativeIoInfo *) io->opaque;
|
|
|
|
__PHYSFS_platformClose(info->handle);
|
|
|
|
allocator.Free((void *) info->path);
|
|
|
|
allocator.Free(info);
|
|
|
|
allocator.Free(io);
|
|
|
|
} /* nativeIo_destroy */
|
|
|
|
|
|
|
|
static const PHYSFS_Io __PHYSFS_nativeIoInterface =
|
|
|
|
{
|
|
|
|
nativeIo_read,
|
|
|
|
nativeIo_write,
|
|
|
|
nativeIo_seek,
|
|
|
|
nativeIo_tell,
|
|
|
|
nativeIo_length,
|
|
|
|
nativeIo_duplicate,
|
|
|
|
nativeIo_flush,
|
|
|
|
nativeIo_destroy,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
PHYSFS_Io *__PHYSFS_createNativeIo(const char *path, const int mode)
|
|
|
|
{
|
|
|
|
PHYSFS_Io *io = NULL;
|
|
|
|
NativeIoInfo *info = NULL;
|
|
|
|
void *handle = NULL;
|
|
|
|
char *pathdup = NULL;
|
|
|
|
|
|
|
|
assert((mode == 'r') || (mode == 'w') || (mode == 'a'));
|
|
|
|
|
|
|
|
io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
info = (NativeIoInfo *) allocator.Malloc(sizeof (NativeIoInfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
pathdup = (char *) allocator.Malloc(strlen(path) + 1);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!pathdup, PHYSFS_ERR_OUT_OF_MEMORY, createNativeIo_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
if (mode == 'r')
|
|
|
|
handle = __PHYSFS_platformOpenRead(path);
|
|
|
|
else if (mode == 'w')
|
|
|
|
handle = __PHYSFS_platformOpenWrite(path);
|
|
|
|
else if (mode == 'a')
|
|
|
|
handle = __PHYSFS_platformOpenAppend(path);
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!handle, ERRPASS, createNativeIo_failed);
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
strcpy(pathdup, path);
|
|
|
|
info->handle = handle;
|
|
|
|
info->path = pathdup;
|
|
|
|
info->mode = mode;
|
|
|
|
memcpy(io, &__PHYSFS_nativeIoInterface, sizeof (*io));
|
|
|
|
io->opaque = info;
|
|
|
|
return io;
|
|
|
|
|
|
|
|
createNativeIo_failed:
|
|
|
|
if (handle != NULL) __PHYSFS_platformClose(handle);
|
|
|
|
if (pathdup != NULL) allocator.Free(pathdup);
|
|
|
|
if (info != NULL) allocator.Free(info);
|
|
|
|
if (io != NULL) allocator.Free(io);
|
|
|
|
return NULL;
|
|
|
|
} /* __PHYSFS_createNativeIo */
|
|
|
|
|
|
|
|
|
2010-08-30 08:39:11 +02:00
|
|
|
/* PHYSFS_Io implementation for i/o to a memory buffer... */
|
|
|
|
|
|
|
|
typedef struct __PHYSFS_MemoryIoInfo
|
|
|
|
{
|
|
|
|
const PHYSFS_uint8 *buf;
|
|
|
|
PHYSFS_uint64 len;
|
|
|
|
PHYSFS_uint64 pos;
|
|
|
|
PHYSFS_Io *parent;
|
|
|
|
volatile PHYSFS_uint32 refcount;
|
|
|
|
void (*destruct)(void *);
|
|
|
|
} MemoryIoInfo;
|
|
|
|
|
|
|
|
static PHYSFS_sint64 memoryIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
|
|
|
|
{
|
|
|
|
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
|
|
|
|
const PHYSFS_uint64 avail = info->len - info->pos;
|
|
|
|
assert(avail <= info->len);
|
|
|
|
|
|
|
|
if (avail == 0)
|
|
|
|
return 0; /* we're at EOF; nothing to do. */
|
|
|
|
|
|
|
|
if (len > avail)
|
|
|
|
len = avail;
|
|
|
|
|
2012-03-10 09:18:33 +01:00
|
|
|
memcpy(buf, info->buf + info->pos, (size_t) len);
|
2010-08-30 08:39:11 +02:00
|
|
|
info->pos += len;
|
|
|
|
return len;
|
|
|
|
} /* memoryIo_read */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 memoryIo_write(PHYSFS_Io *io, const void *buffer,
|
|
|
|
PHYSFS_uint64 len)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_OPEN_FOR_READING, -1);
|
2010-08-30 08:39:11 +02:00
|
|
|
} /* memoryIo_write */
|
|
|
|
|
|
|
|
static int memoryIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
|
|
|
|
{
|
|
|
|
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(offset > info->len, PHYSFS_ERR_PAST_EOF, 0);
|
2010-08-30 08:39:11 +02:00
|
|
|
info->pos = offset;
|
|
|
|
return 1;
|
|
|
|
} /* memoryIo_seek */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 memoryIo_tell(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
|
|
|
|
return (PHYSFS_sint64) info->pos;
|
|
|
|
} /* memoryIo_tell */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 memoryIo_length(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
const MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
|
|
|
|
return (PHYSFS_sint64) info->len;
|
|
|
|
} /* memoryIo_length */
|
|
|
|
|
|
|
|
static PHYSFS_Io *memoryIo_duplicate(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
|
|
|
|
MemoryIoInfo *newinfo = NULL;
|
|
|
|
PHYSFS_Io *parent = info->parent;
|
|
|
|
PHYSFS_Io *retval = NULL;
|
|
|
|
|
|
|
|
/* avoid deep copies. */
|
|
|
|
assert((!parent) || (!((MemoryIoInfo *) parent->opaque)->parent) );
|
|
|
|
|
|
|
|
/* share the buffer between duplicates. */
|
|
|
|
if (parent != NULL) /* dup the parent, increment its refcount. */
|
|
|
|
return parent->duplicate(parent);
|
|
|
|
|
|
|
|
/* we're the parent. */
|
|
|
|
|
|
|
|
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2010-08-30 08:39:11 +02:00
|
|
|
newinfo = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
|
|
|
|
if (!newinfo)
|
|
|
|
{
|
|
|
|
allocator.Free(retval);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2010-08-30 08:39:11 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* !!! FIXME: want lockless atomic increment. */
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
info->refcount++;
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
|
|
|
memset(newinfo, '\0', sizeof (*info));
|
|
|
|
newinfo->buf = info->buf;
|
|
|
|
newinfo->len = info->len;
|
|
|
|
newinfo->pos = 0;
|
|
|
|
newinfo->parent = io;
|
|
|
|
newinfo->refcount = 0;
|
|
|
|
newinfo->destruct = NULL;
|
|
|
|
|
|
|
|
memcpy(retval, io, sizeof (*retval));
|
|
|
|
retval->opaque = newinfo;
|
|
|
|
return retval;
|
|
|
|
} /* memoryIo_duplicate */
|
|
|
|
|
|
|
|
static int memoryIo_flush(PHYSFS_Io *io) { return 1; /* it's read-only. */ }
|
|
|
|
|
|
|
|
static void memoryIo_destroy(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
|
|
|
|
PHYSFS_Io *parent = info->parent;
|
|
|
|
int should_die = 0;
|
|
|
|
|
|
|
|
if (parent != NULL)
|
|
|
|
{
|
|
|
|
assert(info->buf == ((MemoryIoInfo *) info->parent->opaque)->buf);
|
|
|
|
assert(info->len == ((MemoryIoInfo *) info->parent->opaque)->len);
|
|
|
|
assert(info->refcount == 0);
|
|
|
|
assert(info->destruct == NULL);
|
|
|
|
allocator.Free(info);
|
|
|
|
allocator.Free(io);
|
|
|
|
parent->destroy(parent); /* decrements refcount. */
|
|
|
|
return;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* we _are_ the parent. */
|
|
|
|
assert(info->refcount > 0); /* even in a race, we hold a reference. */
|
|
|
|
|
|
|
|
/* !!! FIXME: want lockless atomic decrement. */
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
info->refcount--;
|
|
|
|
should_die = (info->refcount == 0);
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
|
|
|
if (should_die)
|
|
|
|
{
|
|
|
|
void (*destruct)(void *) = info->destruct;
|
|
|
|
void *buf = (void *) info->buf;
|
|
|
|
io->opaque = NULL; /* kill this here in case of race. */
|
|
|
|
allocator.Free(info);
|
|
|
|
allocator.Free(io);
|
|
|
|
if (destruct != NULL)
|
2011-10-18 21:55:29 +02:00
|
|
|
destruct(buf);
|
2010-08-30 08:39:11 +02:00
|
|
|
} /* if */
|
|
|
|
} /* memoryIo_destroy */
|
|
|
|
|
|
|
|
|
|
|
|
static const PHYSFS_Io __PHYSFS_memoryIoInterface =
|
|
|
|
{
|
|
|
|
memoryIo_read,
|
|
|
|
memoryIo_write,
|
|
|
|
memoryIo_seek,
|
|
|
|
memoryIo_tell,
|
|
|
|
memoryIo_length,
|
|
|
|
memoryIo_duplicate,
|
|
|
|
memoryIo_flush,
|
|
|
|
memoryIo_destroy,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
PHYSFS_Io *__PHYSFS_createMemoryIo(const void *buf, PHYSFS_uint64 len,
|
|
|
|
void (*destruct)(void *))
|
|
|
|
{
|
|
|
|
PHYSFS_Io *io = NULL;
|
|
|
|
MemoryIoInfo *info = NULL;
|
|
|
|
|
|
|
|
io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
|
2010-08-30 08:39:11 +02:00
|
|
|
info = (MemoryIoInfo *) allocator.Malloc(sizeof (MemoryIoInfo));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!info, PHYSFS_ERR_OUT_OF_MEMORY, createMemoryIo_failed);
|
2010-08-30 08:39:11 +02:00
|
|
|
|
|
|
|
memset(info, '\0', sizeof (*info));
|
|
|
|
info->buf = (const PHYSFS_uint8 *) buf;
|
|
|
|
info->len = len;
|
|
|
|
info->pos = 0;
|
|
|
|
info->parent = NULL;
|
|
|
|
info->refcount = 1;
|
|
|
|
info->destruct = destruct;
|
|
|
|
|
|
|
|
memcpy(io, &__PHYSFS_memoryIoInterface, sizeof (*io));
|
|
|
|
io->opaque = info;
|
|
|
|
return io;
|
|
|
|
|
|
|
|
createMemoryIo_failed:
|
|
|
|
if (info != NULL) allocator.Free(info);
|
|
|
|
if (io != NULL) allocator.Free(io);
|
|
|
|
return NULL;
|
|
|
|
} /* __PHYSFS_createMemoryIo */
|
|
|
|
|
|
|
|
|
2010-08-30 09:02:32 +02:00
|
|
|
/* PHYSFS_Io implementation for i/o to a PHYSFS_File... */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 handleIo_read(PHYSFS_Io *io, void *buf, PHYSFS_uint64 len)
|
|
|
|
{
|
|
|
|
return PHYSFS_readBytes((PHYSFS_File *) io->opaque, buf, len);
|
|
|
|
} /* handleIo_read */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 handleIo_write(PHYSFS_Io *io, const void *buffer,
|
|
|
|
PHYSFS_uint64 len)
|
|
|
|
{
|
|
|
|
return PHYSFS_writeBytes((PHYSFS_File *) io->opaque, buffer, len);
|
|
|
|
} /* handleIo_write */
|
|
|
|
|
|
|
|
static int handleIo_seek(PHYSFS_Io *io, PHYSFS_uint64 offset)
|
|
|
|
{
|
|
|
|
return PHYSFS_seek((PHYSFS_File *) io->opaque, offset);
|
|
|
|
} /* handleIo_seek */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 handleIo_tell(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
return PHYSFS_tell((PHYSFS_File *) io->opaque);
|
|
|
|
} /* handleIo_tell */
|
|
|
|
|
|
|
|
static PHYSFS_sint64 handleIo_length(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
return PHYSFS_fileLength((PHYSFS_File *) io->opaque);
|
|
|
|
} /* handleIo_length */
|
|
|
|
|
|
|
|
static PHYSFS_Io *handleIo_duplicate(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* There's no duplicate at the PHYSFS_File level, so we break the
|
|
|
|
* abstraction. We're allowed to: we're physfs.c!
|
|
|
|
*/
|
|
|
|
FileHandle *origfh = (FileHandle *) io->opaque;
|
|
|
|
FileHandle *newfh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
|
|
|
|
PHYSFS_Io *retval = NULL;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!newfh, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
|
2010-08-30 09:02:32 +02:00
|
|
|
memset(newfh, '\0', sizeof (*newfh));
|
|
|
|
|
|
|
|
retval = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
|
2010-08-30 09:02:32 +02:00
|
|
|
|
|
|
|
#if 0 /* we don't buffer the duplicate, at least not at the moment. */
|
|
|
|
if (origfh->buffer != NULL)
|
|
|
|
{
|
|
|
|
newfh->buffer = (PHYSFS_uint8 *) allocator.Malloc(origfh->bufsize);
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!newfh->buffer)
|
|
|
|
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, handleIo_dupe_failed);
|
2010-08-30 09:02:32 +02:00
|
|
|
newfh->bufsize = origfh->bufsize;
|
|
|
|
} /* if */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
newfh->io = origfh->io->duplicate(origfh->io);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!newfh->io, ERRPASS, handleIo_dupe_failed);
|
2010-08-30 09:02:32 +02:00
|
|
|
|
|
|
|
newfh->forReading = origfh->forReading;
|
|
|
|
newfh->dirHandle = origfh->dirHandle;
|
|
|
|
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
if (newfh->forReading)
|
|
|
|
{
|
|
|
|
newfh->next = openReadList;
|
|
|
|
openReadList = newfh;
|
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newfh->next = openWriteList;
|
|
|
|
openWriteList = newfh;
|
|
|
|
} /* else */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
|
|
|
memcpy(retval, io, sizeof (PHYSFS_Io));
|
|
|
|
retval->opaque = newfh;
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
handleIo_dupe_failed:
|
|
|
|
if (newfh)
|
|
|
|
{
|
|
|
|
if (newfh->io != NULL) newfh->io->destroy(newfh->io);
|
|
|
|
if (newfh->buffer != NULL) allocator.Free(newfh->buffer);
|
|
|
|
allocator.Free(newfh);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
} /* handleIo_duplicate */
|
|
|
|
|
|
|
|
static int handleIo_flush(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
return PHYSFS_flush((PHYSFS_File *) io->opaque);
|
|
|
|
} /* handleIo_flush */
|
|
|
|
|
|
|
|
static void handleIo_destroy(PHYSFS_Io *io)
|
|
|
|
{
|
|
|
|
if (io->opaque != NULL)
|
|
|
|
PHYSFS_close((PHYSFS_File *) io->opaque);
|
|
|
|
allocator.Free(io);
|
|
|
|
} /* handleIo_destroy */
|
|
|
|
|
|
|
|
static const PHYSFS_Io __PHYSFS_handleIoInterface =
|
|
|
|
{
|
|
|
|
handleIo_read,
|
|
|
|
handleIo_write,
|
|
|
|
handleIo_seek,
|
|
|
|
handleIo_tell,
|
|
|
|
handleIo_length,
|
|
|
|
handleIo_duplicate,
|
|
|
|
handleIo_flush,
|
|
|
|
handleIo_destroy,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static PHYSFS_Io *__PHYSFS_createHandleIo(PHYSFS_File *f)
|
|
|
|
{
|
|
|
|
PHYSFS_Io *io = (PHYSFS_Io *) allocator.Malloc(sizeof (PHYSFS_Io));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2010-08-30 09:02:32 +02:00
|
|
|
memcpy(io, &__PHYSFS_handleIoInterface, sizeof (*io));
|
|
|
|
io->opaque = f;
|
|
|
|
return io;
|
|
|
|
} /* __PHYSFS_createHandleIo */
|
|
|
|
|
2010-08-30 08:39:11 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
/* functions ... */
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char **list;
|
|
|
|
PHYSFS_uint32 size;
|
2012-03-20 20:38:12 +01:00
|
|
|
PHYSFS_ErrorCode errcode;
|
2004-09-29 08:09:29 +02:00
|
|
|
} EnumStringListCallbackData;
|
|
|
|
|
|
|
|
static void enumStringListCallback(void *data, const char *str)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
char *newstr;
|
|
|
|
EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
if (pecd->errcode)
|
2004-09-29 08:09:29 +02:00
|
|
|
return;
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
|
|
|
|
newstr = (char *) allocator.Malloc(strlen(str) + 1);
|
2004-09-29 08:09:29 +02:00
|
|
|
if (ptr != NULL)
|
|
|
|
pecd->list = (char **) ptr;
|
|
|
|
|
|
|
|
if ((ptr == NULL) || (newstr == NULL))
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
pecd->errcode = PHYSFS_ERR_OUT_OF_MEMORY;
|
2004-09-29 08:09:29 +02:00
|
|
|
pecd->list[pecd->size] = NULL;
|
|
|
|
PHYSFS_freeList(pecd->list);
|
|
|
|
return;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
strcpy(newstr, str);
|
|
|
|
pecd->list[pecd->size] = newstr;
|
|
|
|
pecd->size++;
|
|
|
|
} /* enumStringListCallback */
|
|
|
|
|
|
|
|
|
|
|
|
static char **doEnumStringList(void (*func)(PHYSFS_StringCallback, void *))
|
|
|
|
{
|
|
|
|
EnumStringListCallbackData ecd;
|
|
|
|
memset(&ecd, '\0', sizeof (ecd));
|
2005-03-14 12:49:30 +01:00
|
|
|
ecd.list = (char **) allocator.Malloc(sizeof (char *));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2004-09-29 08:09:29 +02:00
|
|
|
func(enumStringListCallback, &ecd);
|
2012-03-20 20:38:12 +01:00
|
|
|
|
|
|
|
if (ecd.errcode)
|
|
|
|
{
|
|
|
|
__PHYSFS_setError(ecd.errcode);
|
|
|
|
return NULL;
|
|
|
|
} /* if */
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
ecd.list[ecd.size] = NULL;
|
2010-01-28 08:27:45 +01:00
|
|
|
return ecd.list;
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* doEnumStringList */
|
|
|
|
|
|
|
|
|
2003-01-31 05:07:48 +01:00
|
|
|
static void __PHYSFS_bubble_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
|
2002-08-20 02:59:03 +02:00
|
|
|
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
|
|
|
|
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
|
|
|
|
{
|
|
|
|
PHYSFS_uint32 i;
|
|
|
|
int sorted;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
sorted = 1;
|
|
|
|
for (i = lo; i < hi; i++)
|
|
|
|
{
|
|
|
|
if (cmpfn(a, i, i + 1) > 0)
|
|
|
|
{
|
|
|
|
swapfn(a, i, i + 1);
|
|
|
|
sorted = 0;
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
} while (!sorted);
|
|
|
|
} /* __PHYSFS_bubble_sort */
|
|
|
|
|
|
|
|
|
2003-01-31 05:07:48 +01:00
|
|
|
static void __PHYSFS_quick_sort(void *a, PHYSFS_uint32 lo, PHYSFS_uint32 hi,
|
2002-08-20 02:59:03 +02:00
|
|
|
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
|
|
|
|
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
|
|
|
|
{
|
|
|
|
PHYSFS_uint32 i;
|
|
|
|
PHYSFS_uint32 j;
|
|
|
|
PHYSFS_uint32 v;
|
|
|
|
|
2003-07-20 23:08:24 +02:00
|
|
|
if ((hi - lo) <= PHYSFS_QUICKSORT_THRESHOLD)
|
2002-08-20 02:59:03 +02:00
|
|
|
__PHYSFS_bubble_sort(a, lo, hi, cmpfn, swapfn);
|
|
|
|
else
|
2003-07-20 23:08:24 +02:00
|
|
|
{
|
|
|
|
i = (hi + lo) / 2;
|
2002-08-20 02:59:03 +02:00
|
|
|
|
|
|
|
if (cmpfn(a, lo, i) > 0) swapfn(a, lo, i);
|
2003-07-20 23:08:24 +02:00
|
|
|
if (cmpfn(a, lo, hi) > 0) swapfn(a, lo, hi);
|
|
|
|
if (cmpfn(a, i, hi) > 0) swapfn(a, i, hi);
|
|
|
|
|
|
|
|
j = hi - 1;
|
|
|
|
swapfn(a, i, j);
|
|
|
|
i = lo;
|
|
|
|
v = j;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
while(cmpfn(a, ++i, v) < 0) { /* do nothing */ }
|
|
|
|
while(cmpfn(a, --j, v) > 0) { /* do nothing */ }
|
|
|
|
if (j < i)
|
2002-08-20 02:59:03 +02:00
|
|
|
break;
|
2003-07-20 23:08:24 +02:00
|
|
|
swapfn(a, i, j);
|
|
|
|
} /* while */
|
2009-02-28 07:34:02 +01:00
|
|
|
if (i != (hi-1))
|
|
|
|
swapfn(a, i, hi-1);
|
2003-07-20 23:08:24 +02:00
|
|
|
__PHYSFS_quick_sort(a, lo, j, cmpfn, swapfn);
|
|
|
|
__PHYSFS_quick_sort(a, i+1, hi, cmpfn, swapfn);
|
|
|
|
} /* else */
|
2002-08-20 02:59:03 +02:00
|
|
|
} /* __PHYSFS_quick_sort */
|
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_sort(void *entries, PHYSFS_uint32 max,
|
|
|
|
int (*cmpfn)(void *, PHYSFS_uint32, PHYSFS_uint32),
|
|
|
|
void (*swapfn)(void *, PHYSFS_uint32, PHYSFS_uint32))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Quicksort w/ Bubblesort fallback algorithm inspired by code from here:
|
|
|
|
* http://www.cs.ubc.ca/spider/harrison/Java/sorting-demo.html
|
|
|
|
*/
|
|
|
|
__PHYSFS_quick_sort(entries, 0, max - 1, cmpfn, swapfn);
|
|
|
|
} /* __PHYSFS_sort */
|
|
|
|
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
static ErrState *findErrorForCurrentThread(void)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
ErrState *i;
|
2009-09-06 07:05:27 +02:00
|
|
|
void *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);
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
if (errorStates != NULL)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2002-04-03 09:40:27 +02:00
|
|
|
tid = __PHYSFS_platformGetThreadID();
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
for (i = errorStates; i != NULL; i = i->next)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
|
|
|
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);
|
2010-01-28 08:27:45 +01: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
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return NULL; /* no error available. */
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* findErrorForCurrentThread */
|
|
|
|
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
void __PHYSFS_setError(const PHYSFS_ErrorCode errcode)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
ErrState *err;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!errcode)
|
2001-07-07 05:52:43 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
err = findErrorForCurrentThread();
|
2001-07-05 10:18:39 +02:00
|
|
|
if (err == NULL)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
err = (ErrState *) allocator.Malloc(sizeof (ErrState));
|
2001-07-05 10:18:39 +02:00
|
|
|
if (err == NULL)
|
|
|
|
return; /* uhh...? */
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
memset(err, '\0', sizeof (ErrState));
|
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);
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
err->next = errorStates;
|
|
|
|
errorStates = err;
|
2002-06-08 10:50:00 +02:00
|
|
|
|
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformReleaseMutex(errorLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* if */
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
err->code = errcode;
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* __PHYSFS_setError */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
PHYSFS_ErrorCode PHYSFS_getLastErrorCode(void)
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
ErrState *err = findErrorForCurrentThread();
|
|
|
|
const PHYSFS_ErrorCode retval = (err) ? err->code : PHYSFS_ERR_OK;
|
|
|
|
if (err)
|
|
|
|
err->code = PHYSFS_ERR_OK;
|
|
|
|
return retval;
|
|
|
|
} /* PHYSFS_getLastErrorCode */
|
|
|
|
|
|
|
|
|
|
|
|
PHYSFS_DECL const char *PHYSFS_getErrorByCode(PHYSFS_ErrorCode code)
|
|
|
|
{
|
|
|
|
switch (code)
|
|
|
|
{
|
|
|
|
case PHYSFS_ERR_OK: return "no error";
|
|
|
|
case PHYSFS_ERR_OTHER_ERROR: return "unknown error";
|
|
|
|
case PHYSFS_ERR_OUT_OF_MEMORY: return "out of memory";
|
|
|
|
case PHYSFS_ERR_NOT_INITIALIZED: return "not initialized";
|
|
|
|
case PHYSFS_ERR_IS_INITIALIZED: return "already initialized";
|
|
|
|
case PHYSFS_ERR_ARGV0_IS_NULL: return "argv[0] is NULL";
|
|
|
|
case PHYSFS_ERR_UNSUPPORTED: return "unsupported";
|
|
|
|
case PHYSFS_ERR_PAST_EOF: return "past end of file";
|
|
|
|
case PHYSFS_ERR_FILES_STILL_OPEN: return "files still open";
|
|
|
|
case PHYSFS_ERR_INVALID_ARGUMENT: return "invalid argument";
|
|
|
|
case PHYSFS_ERR_NOT_MOUNTED: return "not mounted";
|
|
|
|
case PHYSFS_ERR_NO_SUCH_PATH: return "no such path";
|
|
|
|
case PHYSFS_ERR_SYMLINK_FORBIDDEN: return "symlinks are forbidden";
|
|
|
|
case PHYSFS_ERR_NO_WRITE_DIR: return "write directory is not set";
|
|
|
|
case PHYSFS_ERR_OPEN_FOR_READING: return "file open for reading";
|
|
|
|
case PHYSFS_ERR_OPEN_FOR_WRITING: return "file open for writing";
|
|
|
|
case PHYSFS_ERR_NOT_A_FILE: return "not a file";
|
|
|
|
case PHYSFS_ERR_READ_ONLY: return "read-only filesystem";
|
|
|
|
case PHYSFS_ERR_CORRUPT: return "corrupted";
|
|
|
|
case PHYSFS_ERR_SYMLINK_LOOP: return "infinite symbolic link loop";
|
|
|
|
case PHYSFS_ERR_IO: return "i/o error";
|
|
|
|
case PHYSFS_ERR_PERMISSION: return "permission denied";
|
|
|
|
case PHYSFS_ERR_NO_SPACE: return "no space available for writing";
|
|
|
|
case PHYSFS_ERR_BAD_FILENAME: return "filename is illegal or insecure";
|
|
|
|
case PHYSFS_ERR_BUSY: return "tried to modify a file the OS needs";
|
|
|
|
case PHYSFS_ERR_DIR_NOT_EMPTY: return "directory isn't empty";
|
|
|
|
case PHYSFS_ERR_OS_ERROR: return "OS reported an error";
|
|
|
|
} /* switch */
|
|
|
|
|
|
|
|
return NULL; /* don't know this error code. */
|
|
|
|
} /* PHYSFS_getErrorByCode */
|
|
|
|
|
|
|
|
|
|
|
|
void PHYSFS_setErrorCode(PHYSFS_ErrorCode code)
|
|
|
|
{
|
|
|
|
__PHYSFS_setError(code);
|
|
|
|
} /* PHYSFS_setErrorCode */
|
2002-03-30 17:44:09 +01:00
|
|
|
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
const char *PHYSFS_getLastError(void)
|
|
|
|
{
|
|
|
|
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
|
|
|
|
return (err) ? PHYSFS_getErrorByCode(err) : NULL;
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* PHYSFS_getLastError */
|
|
|
|
|
|
|
|
|
|
|
|
/* MAKE SURE that errorLock is held before calling this! */
|
2012-03-20 20:38:12 +01:00
|
|
|
static void freeErrorStates(void)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
ErrState *i;
|
|
|
|
ErrState *next;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
for (i = errorStates; i != NULL; i = next)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2001-07-09 02:51:46 +02:00
|
|
|
next = i->next;
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(i);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* for */
|
2002-06-08 10:50:00 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
errorStates = NULL;
|
|
|
|
} /* freeErrorStates */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
const char *retval = NULL;
|
|
|
|
if (fname != NULL)
|
2002-07-26 08:18:30 +02:00
|
|
|
{
|
2011-01-21 08:50:20 +01:00
|
|
|
const char *p = strchr(fname, '.');
|
|
|
|
retval = p;
|
2002-07-26 08:18:30 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
while (p != NULL)
|
|
|
|
{
|
|
|
|
p = strchr(p + 1, '.');
|
|
|
|
if (p != NULL)
|
|
|
|
retval = p;
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
if (retval != NULL)
|
|
|
|
retval++; /* skip '.' */
|
|
|
|
} /* if */
|
2002-07-26 08:18:30 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2002-07-26 08:18:30 +02:00
|
|
|
} /* find_filename_extension */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static DirHandle *tryOpenDir(PHYSFS_Io *io, const PHYSFS_Archiver *funcs,
|
2004-09-26 15:00:59 +02:00
|
|
|
const char *d, int forWriting)
|
2004-09-26 02:25:04 +02:00
|
|
|
{
|
|
|
|
DirHandle *retval = NULL;
|
2010-08-30 09:01:57 +02:00
|
|
|
void *opaque = NULL;
|
|
|
|
|
|
|
|
if (io != NULL)
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io->seek(io, 0), ERRPASS, NULL);
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
opaque = funcs->openArchive(io, d, forWriting);
|
2010-08-24 16:05:58 +02:00
|
|
|
if (opaque != NULL)
|
2004-09-26 02:25:04 +02:00
|
|
|
{
|
2010-08-24 16:05:58 +02:00
|
|
|
retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
|
|
|
|
if (retval == NULL)
|
|
|
|
funcs->dirClose(opaque);
|
|
|
|
else
|
2004-09-26 02:25:04 +02:00
|
|
|
{
|
2010-08-24 16:05:58 +02:00
|
|
|
memset(retval, '\0', sizeof (DirHandle));
|
|
|
|
retval->mountPoint = NULL;
|
|
|
|
retval->funcs = funcs;
|
|
|
|
retval->opaque = opaque;
|
|
|
|
} /* else */
|
2004-09-26 02:25:04 +02:00
|
|
|
} /* if */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2004-09-26 02:25:04 +02:00
|
|
|
} /* tryOpenDir */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static DirHandle *openDirectory(PHYSFS_Io *io, const char *d, int forWriting)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
DirHandle *retval = NULL;
|
2004-09-26 15:00:59 +02:00
|
|
|
const PHYSFS_Archiver **i;
|
2002-07-26 08:18:30 +02:00
|
|
|
const char *ext;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
assert((io != NULL) || (d != NULL));
|
2001-07-23 06:46:42 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (io == NULL)
|
|
|
|
{
|
|
|
|
/* DIR gets first shot (unlike the rest, it doesn't deal with files). */
|
|
|
|
retval = tryOpenDir(io, &__PHYSFS_Archiver_DIR, d, forWriting);
|
|
|
|
if (retval != NULL)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
io = __PHYSFS_createNativeIo(d, forWriting ? 'w' : 'r');
|
2012-03-20 20:28:19 +01:00
|
|
|
BAIL_IF_MACRO(!io, ERRPASS, 0);
|
2010-08-30 09:01:57 +02:00
|
|
|
} /* if */
|
2010-08-24 16:05:58 +02:00
|
|
|
|
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... */
|
2004-09-26 15:00:59 +02:00
|
|
|
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
|
2002-07-26 08:18:30 +02:00
|
|
|
{
|
2007-03-15 09:16:23 +01:00
|
|
|
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) == 0)
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = tryOpenDir(io, *i, d, forWriting);
|
2002-07-26 08:18:30 +02:00
|
|
|
} /* for */
|
|
|
|
|
|
|
|
/* failing an exact file extension match, try all the others... */
|
2004-09-26 15:00:59 +02:00
|
|
|
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
|
2002-07-26 08:18:30 +02:00
|
|
|
{
|
2007-03-15 09:16:23 +01:00
|
|
|
if (__PHYSFS_stricmpASCII(ext, (*i)->info->extension) != 0)
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = tryOpenDir(io, *i, d, forWriting);
|
2002-07-26 08:18:30 +02:00
|
|
|
} /* for */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
else /* no extension? Try them all. */
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
|
2010-08-30 09:01:57 +02:00
|
|
|
retval = tryOpenDir(io, *i, d, forWriting);
|
2002-07-26 08:18:30 +02:00
|
|
|
} /* else */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!retval, PHYSFS_ERR_UNSUPPORTED, NULL);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* openDirectory */
|
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
/*
|
|
|
|
* Make a platform-independent path string sane. Doesn't actually check the
|
|
|
|
* file hierarchy, it just cleans up the string.
|
|
|
|
* (dst) must be a buffer at least as big as (src), as this is where the
|
|
|
|
* cleaned up string is deposited.
|
|
|
|
* If there are illegal bits in the path (".." entries, etc) then we
|
|
|
|
* return zero and (dst) is undefined. Non-zero if the path was sanitized.
|
|
|
|
*/
|
|
|
|
static int sanitizePlatformIndependentPath(const char *src, char *dst)
|
|
|
|
{
|
|
|
|
char *prev;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
while (*src == '/') /* skip initial '/' chars... */
|
|
|
|
src++;
|
|
|
|
|
|
|
|
prev = dst;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ch = *(src++);
|
|
|
|
|
|
|
|
if ((ch == ':') || (ch == '\\')) /* illegal chars in a physfs path. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
|
2005-03-13 13:03:05 +01:00
|
|
|
|
|
|
|
if (ch == '/') /* path separator. */
|
|
|
|
{
|
|
|
|
*dst = '\0'; /* "." and ".." are illegal pathnames. */
|
|
|
|
if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_BAD_FILENAME, 0);
|
2005-03-13 13:03:05 +01:00
|
|
|
|
|
|
|
while (*src == '/') /* chop out doubles... */
|
|
|
|
src++;
|
|
|
|
|
|
|
|
if (*src == '\0') /* ends with a pathsep? */
|
|
|
|
break; /* we're done, don't add final pathsep to dst. */
|
|
|
|
|
|
|
|
prev = dst + 1;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
*(dst++) = ch;
|
|
|
|
} while (ch != '\0');
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2005-03-13 13:03:05 +01:00
|
|
|
} /* sanitizePlatformIndependentPath */
|
|
|
|
|
|
|
|
|
2005-03-14 08:15:40 +01:00
|
|
|
/*
|
|
|
|
* Figure out if (fname) is part of (h)'s mountpoint. (fname) must be an
|
|
|
|
* output from sanitizePlatformIndependentPath(), so that it is in a known
|
|
|
|
* state.
|
|
|
|
*
|
|
|
|
* This only finds legitimate segments of a mountpoint. If the mountpoint is
|
|
|
|
* "/a/b/c" and (fname) is "/a/b/c", "/", or "/a/b/c/d", then the results are
|
|
|
|
* all zero. "/a/b" will succeed, though.
|
|
|
|
*/
|
|
|
|
static int partOfMountPoint(DirHandle *h, char *fname)
|
|
|
|
{
|
|
|
|
/* !!! FIXME: This code feels gross. */
|
|
|
|
int rc;
|
|
|
|
size_t len, mntpntlen;
|
|
|
|
|
|
|
|
if (h->mountPoint == NULL)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2005-03-14 08:15:40 +01:00
|
|
|
else if (*fname == '\0')
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2005-03-14 08:15:40 +01:00
|
|
|
|
|
|
|
len = strlen(fname);
|
|
|
|
mntpntlen = strlen(h->mountPoint);
|
|
|
|
if (len > mntpntlen) /* can't be a subset of mountpoint. */
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2005-03-14 08:15:40 +01:00
|
|
|
|
|
|
|
/* if true, must be not a match or a complete match, but not a subset. */
|
|
|
|
if ((len + 1) == mntpntlen)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2005-03-14 08:15:40 +01:00
|
|
|
|
|
|
|
rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
|
|
|
|
if (rc != 0)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0; /* not a match. */
|
2005-03-14 08:15:40 +01:00
|
|
|
|
|
|
|
/* make sure /a/b matches /a/b/ and not /a/bc ... */
|
2010-01-28 08:27:45 +01:00
|
|
|
return h->mountPoint[len] == '/';
|
2005-03-14 08:15:40 +01:00
|
|
|
} /* partOfMountPoint */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static DirHandle *createDirHandle(PHYSFS_Io *io, const char *newDir,
|
|
|
|
const char *mountPoint, int forWriting)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
|
|
|
DirHandle *dirHandle = NULL;
|
2007-03-24 04:54:58 +01:00
|
|
|
char *tmpmntpnt = NULL;
|
2005-03-13 13:03:05 +01:00
|
|
|
|
|
|
|
if (mountPoint != NULL)
|
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
const size_t len = strlen(mountPoint) + 1;
|
|
|
|
tmpmntpnt = (char *) __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!tmpmntpnt, PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
|
2007-03-24 04:54:58 +01:00
|
|
|
if (!sanitizePlatformIndependentPath(mountPoint, tmpmntpnt))
|
2005-03-13 13:03:05 +01:00
|
|
|
goto badDirHandle;
|
2007-03-24 04:54:58 +01:00
|
|
|
mountPoint = tmpmntpnt; /* sanitized version. */
|
2005-03-13 13:03:05 +01:00
|
|
|
} /* if */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
dirHandle = openDirectory(io, newDir, forWriting);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!dirHandle, ERRPASS, badDirHandle);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (newDir == NULL)
|
|
|
|
dirHandle->dirName = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!dirHandle->dirName)
|
|
|
|
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
|
2010-08-30 09:01:57 +02:00
|
|
|
strcpy(dirHandle->dirName, newDir);
|
|
|
|
} /* else */
|
2005-03-13 10:09:26 +01:00
|
|
|
|
|
|
|
if ((mountPoint != NULL) && (*mountPoint != '\0'))
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!dirHandle->mountPoint)
|
|
|
|
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, badDirHandle);
|
2005-03-13 10:09:26 +01:00
|
|
|
strcpy(dirHandle->mountPoint, mountPoint);
|
|
|
|
strcat(dirHandle->mountPoint, "/");
|
|
|
|
} /* if */
|
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(tmpmntpnt);
|
2010-01-28 08:27:45 +01:00
|
|
|
return dirHandle;
|
2005-03-13 10:09:26 +01:00
|
|
|
|
|
|
|
badDirHandle:
|
|
|
|
if (dirHandle != NULL)
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
dirHandle->funcs->dirClose(dirHandle->opaque);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(dirHandle->dirName);
|
|
|
|
allocator.Free(dirHandle->mountPoint);
|
|
|
|
allocator.Free(dirHandle);
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* if */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(tmpmntpnt);
|
2010-01-28 08:27:45 +01:00
|
|
|
return NULL;
|
2004-09-26 15:00:59 +02:00
|
|
|
} /* createDirHandle */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* MAKE SURE you've got the stateLock held before calling this! */
|
2004-09-26 15:00:59 +02:00
|
|
|
static int freeDirHandle(DirHandle *dh, FileHandle *openList)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *i;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
if (dh == NULL)
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
for (i = openList; i != NULL; i = i->next)
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(i->dirHandle == dh, PHYSFS_ERR_FILES_STILL_OPEN, 0);
|
2006-04-11 16:33:48 +02:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
dh->funcs->dirClose(dh->opaque);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(dh->dirName);
|
|
|
|
allocator.Free(dh->mountPoint);
|
|
|
|
allocator.Free(dh);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2004-09-26 15:00:59 +02:00
|
|
|
} /* freeDirHandle */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
static char *calculateBaseDir(const char *argv0)
|
|
|
|
{
|
2012-03-15 06:54:57 +01:00
|
|
|
const char dirsep = __PHYSFS_platformDirSeparator;
|
2007-03-21 06:22:48 +01:00
|
|
|
char *retval = NULL;
|
|
|
|
char *ptr = NULL;
|
2001-07-08 15:57:28 +02:00
|
|
|
|
2007-03-21 06:22:48 +01:00
|
|
|
/* Give the platform layer first shot at this. */
|
2001-07-08 15:57:28 +02:00
|
|
|
retval = __PHYSFS_platformCalcBaseDir(argv0);
|
|
|
|
if (retval != NULL)
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 15:57:28 +02:00
|
|
|
|
2007-03-21 06:22:48 +01:00
|
|
|
/* We need argv0 to go on. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(argv0 == NULL, PHYSFS_ERR_ARGV0_IS_NULL, NULL);
|
2007-03-19 21:13:37 +01:00
|
|
|
|
2012-03-15 06:54:57 +01:00
|
|
|
ptr = strrchr(argv0, dirsep);
|
2007-03-21 06:22:48 +01:00
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
2012-03-23 09:14:01 +01:00
|
|
|
const size_t size = ((size_t) (ptr - argv0)) + 1;
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = (char *) allocator.Malloc(size + 1);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2001-07-08 15:57:28 +02:00
|
|
|
memcpy(retval, argv0, size);
|
|
|
|
retval[size] = '\0';
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 15:57:28 +02:00
|
|
|
} /* if */
|
|
|
|
|
2007-03-21 06:22:48 +01:00
|
|
|
/* argv0 wasn't helpful. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, NULL);
|
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;
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1; /* success. */
|
2002-03-30 17:44:09 +01:00
|
|
|
|
|
|
|
initializeMutexes_failed:
|
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformDestroyMutex(errorLock);
|
|
|
|
|
|
|
|
if (stateLock != NULL)
|
|
|
|
__PHYSFS_platformDestroyMutex(stateLock);
|
|
|
|
|
|
|
|
errorLock = stateLock = NULL;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0; /* failed. */
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* initializeMutexes */
|
|
|
|
|
|
|
|
|
2004-09-23 08:45:36 +02:00
|
|
|
static void setDefaultAllocator(void);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
int PHYSFS_init(const char *argv0)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
|
2004-09-23 08:45:36 +02:00
|
|
|
|
|
|
|
if (!externalAllocator)
|
|
|
|
setDefaultAllocator();
|
|
|
|
|
2005-09-09 16:07:43 +02:00
|
|
|
if (allocator.Init != NULL)
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!allocator.Init(), ERRPASS, 0);
|
2004-09-26 02:25:04 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformInit(), ERRPASS, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!initializeMutexes(), ERRPASS, 0);
|
2002-03-30 17:44:09 +01:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
baseDir = calculateBaseDir(argv0);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!baseDir, ERRPASS, 0);
|
2002-04-06 07:18:49 +02:00
|
|
|
|
2012-03-22 05:27:46 +01:00
|
|
|
userDir = __PHYSFS_platformCalcUserDir();
|
2012-03-24 05:26:04 +01:00
|
|
|
if (!userDir)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(baseDir);
|
2001-07-06 10:47:23 +02:00
|
|
|
baseDir = NULL;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2001-07-06 10:47:23 +02:00
|
|
|
} /* if */
|
|
|
|
|
2012-03-24 05:26:04 +01:00
|
|
|
/* Platform layer is required to append a dirsep. */
|
|
|
|
assert(baseDir[strlen(baseDir) - 1] == __PHYSFS_platformDirSeparator);
|
|
|
|
assert(userDir[strlen(userDir) - 1] == __PHYSFS_platformDirSeparator);
|
|
|
|
|
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. */
|
2012-03-20 20:38:12 +01:00
|
|
|
__PHYSFS_setError(PHYSFS_getLastErrorCode());
|
2002-08-20 02:59:03 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_init */
|
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* MAKE SURE you hold stateLock before calling this! */
|
2004-09-26 15:00:59 +02:00
|
|
|
static int closeFileHandleList(FileHandle **list)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *i;
|
|
|
|
FileHandle *next = NULL;
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
for (i = *list; i != NULL; i = next)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = i->io;
|
2001-07-06 10:47:23 +02:00
|
|
|
next = i->next;
|
2010-08-30 09:01:57 +02:00
|
|
|
|
|
|
|
if (!io->flush(io))
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
|
|
|
*list = i;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* if */
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
io->destroy(io);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(i);
|
2001-07-06 10:47:23 +02:00
|
|
|
} /* for */
|
|
|
|
|
|
|
|
*list = NULL;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* 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)
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
|
|
|
DirHandle *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;
|
2004-09-26 15:00:59 +02:00
|
|
|
freeDirHandle(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
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
|
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), ERRPASS, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
closeFileHandleList(&openWriteList);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), PHYSFS_ERR_FILES_STILL_OPEN, 0);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
freeSearchPath();
|
2012-03-20 20:38:12 +01:00
|
|
|
freeErrorStates();
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
if (baseDir != NULL)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(baseDir);
|
2001-07-06 10:47:23 +02:00
|
|
|
baseDir = NULL;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (userDir != NULL)
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(userDir);
|
2001-07-06 10:47:23 +02:00
|
|
|
userDir = NULL;
|
|
|
|
} /* if */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2012-03-22 04:30:50 +01:00
|
|
|
if (prefDir != NULL)
|
|
|
|
{
|
|
|
|
allocator.Free(prefDir);
|
|
|
|
prefDir = 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);
|
|
|
|
|
2005-09-09 16:07:43 +02:00
|
|
|
if (allocator.Deinit != NULL)
|
|
|
|
allocator.Deinit();
|
2004-09-26 02:25:04 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
errorLock = stateLock = NULL;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_deinit */
|
|
|
|
|
|
|
|
|
2007-04-01 23:24:19 +02:00
|
|
|
int PHYSFS_isInit(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return initialized;
|
2007-04-01 23:24:19 +02:00
|
|
|
} /* PHYSFS_isInit */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return supported_types;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_supportedArchiveTypes */
|
|
|
|
|
|
|
|
|
|
|
|
void PHYSFS_freeList(void *list)
|
|
|
|
{
|
|
|
|
void **i;
|
2009-03-28 22:50:54 +01:00
|
|
|
if (list != NULL)
|
|
|
|
{
|
|
|
|
for (i = (void **) list; *i != NULL; i++)
|
|
|
|
allocator.Free(*i);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2009-03-28 22:50:54 +01:00
|
|
|
allocator.Free(list);
|
|
|
|
} /* if */
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_freeList */
|
|
|
|
|
|
|
|
|
|
|
|
const char *PHYSFS_getDirSeparator(void)
|
|
|
|
{
|
2012-03-15 06:54:57 +01:00
|
|
|
static char retval[2] = { __PHYSFS_platformDirSeparator, '\0' };
|
|
|
|
return retval;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getDirSeparator */
|
|
|
|
|
|
|
|
|
|
|
|
char **PHYSFS_getCdRomDirs(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return doEnumStringList(__PHYSFS_platformDetectAvailableCDs);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getCdRomDirs */
|
|
|
|
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
void PHYSFS_getCdRomDirsCallback(PHYSFS_StringCallback callback, void *data)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformDetectAvailableCDs(callback, data);
|
|
|
|
} /* PHYSFS_getCdRomDirsCallback */
|
|
|
|
|
|
|
|
|
2012-03-22 04:30:50 +01:00
|
|
|
const char *PHYSFS_getPrefDir(const char *org, const char *app)
|
|
|
|
{
|
|
|
|
const char dirsep = __PHYSFS_platformDirSeparator;
|
|
|
|
PHYSFS_Stat statbuf;
|
2012-03-22 04:52:44 +01:00
|
|
|
char *ptr = NULL;
|
2012-03-22 04:59:43 +01:00
|
|
|
char *endstr = NULL;
|
2012-03-22 04:30:50 +01:00
|
|
|
int exists = 0;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
|
|
|
|
BAIL_IF_MACRO(!org, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
BAIL_IF_MACRO(*org == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
BAIL_IF_MACRO(!app, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
BAIL_IF_MACRO(*app == '\0', PHYSFS_ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
|
|
|
|
allocator.Free(prefDir);
|
|
|
|
prefDir = __PHYSFS_platformCalcPrefDir(org, app);
|
|
|
|
BAIL_IF_MACRO(!prefDir, ERRPASS, NULL);
|
|
|
|
|
2012-03-22 04:59:43 +01:00
|
|
|
assert(strlen(prefDir) > 0);
|
|
|
|
endstr = prefDir + (strlen(prefDir) - 1);
|
|
|
|
assert(*endstr == dirsep);
|
|
|
|
*endstr = '\0'; /* mask out the final dirsep for now. */
|
2012-03-22 04:30:50 +01:00
|
|
|
|
2012-03-22 04:59:43 +01:00
|
|
|
if (!__PHYSFS_platformStat(prefDir, &exists, &statbuf))
|
2012-03-22 04:30:50 +01:00
|
|
|
{
|
2012-03-22 04:59:43 +01:00
|
|
|
for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
|
|
|
|
{
|
|
|
|
*ptr = '\0';
|
|
|
|
__PHYSFS_platformMkDir(prefDir);
|
|
|
|
*ptr = dirsep;
|
|
|
|
} /* for */
|
2012-03-22 04:30:50 +01:00
|
|
|
|
2012-03-22 04:59:43 +01:00
|
|
|
if (!__PHYSFS_platformMkDir(prefDir))
|
|
|
|
{
|
|
|
|
allocator.Free(prefDir);
|
|
|
|
prefDir = NULL;
|
|
|
|
} /* if */
|
2012-03-22 04:30:50 +01:00
|
|
|
} /* if */
|
|
|
|
|
2012-03-22 04:59:43 +01:00
|
|
|
*endstr = dirsep; /* readd the final dirsep. */
|
|
|
|
|
2012-03-22 04:30:50 +01:00
|
|
|
return prefDir;
|
|
|
|
} /* PHYSFS_getPrefDir */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
const char *PHYSFS_getBaseDir(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return baseDir; /* this is calculated in PHYSFS_init()... */
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getBaseDir */
|
|
|
|
|
|
|
|
|
2012-03-22 04:30:50 +01:00
|
|
|
const char *__PHYSFS_getUserDir(void) /* not deprecated internal version. */
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return userDir; /* this is calculated in PHYSFS_init()... */
|
2012-03-22 04:30:50 +01:00
|
|
|
} /* __PHYSFS_getUserDir */
|
|
|
|
|
|
|
|
|
|
|
|
const char *PHYSFS_getUserDir(void)
|
|
|
|
{
|
|
|
|
return __PHYSFS_getUserDir();
|
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
|
|
|
|
2010-01-28 08:27:45 +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)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), ERRPASS,
|
2002-03-30 17:44:09 +01:00
|
|
|
stateLock, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
writeDir = NULL;
|
|
|
|
} /* if */
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
if (newDir != NULL)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
/* !!! FIXME: PHYSFS_Io shouldn't be NULL */
|
|
|
|
writeDir = createDirHandle(NULL, newDir, NULL, 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);
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_setWriteDir */
|
|
|
|
|
|
|
|
|
2010-08-30 02:56:35 +02:00
|
|
|
static int doMount(PHYSFS_Io *io, const char *fname,
|
|
|
|
const char *mountPoint, int appendToPath)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *dh;
|
|
|
|
DirHandle *prev = NULL;
|
|
|
|
DirHandle *i;
|
2001-07-23 11:23:17 +02:00
|
|
|
|
2005-07-24 01:10:27 +02:00
|
|
|
if (mountPoint == NULL)
|
|
|
|
mountPoint = "/";
|
2005-03-13 10:09:26 +01:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-23 11:23:17 +02:00
|
|
|
|
2010-08-30 02:56:35 +02:00
|
|
|
if (fname != NULL)
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
2010-08-30 02:56:35 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
/* already in search path? */
|
|
|
|
if ((i->dirName != NULL) && (strcmp(fname, i->dirName) == 0))
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
|
2010-08-30 02:56:35 +02:00
|
|
|
prev = i;
|
|
|
|
} /* for */
|
|
|
|
} /* if */
|
2001-07-23 11:23:17 +02:00
|
|
|
|
2010-08-30 02:56:35 +02:00
|
|
|
dh = createDirHandle(io, fname, mountPoint, 0);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!dh, ERRPASS, stateLock, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
if (appendToPath)
|
|
|
|
{
|
|
|
|
if (prev == NULL)
|
2004-09-26 15:00:59 +02:00
|
|
|
searchPath = dh;
|
2001-07-05 10:18:39 +02:00
|
|
|
else
|
2004-09-26 15:00:59 +02:00
|
|
|
prev->next = dh;
|
2001-07-08 15:57:28 +02:00
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
dh->next = searchPath;
|
|
|
|
searchPath = dh;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* else */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2010-08-30 02:56:35 +02:00
|
|
|
} /* doMount */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_mountIo(PHYSFS_Io *io, const char *fname,
|
|
|
|
const char *mountPoint, int appendToPath)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2010-08-30 02:56:35 +02:00
|
|
|
return doMount(io, fname, mountPoint, appendToPath);
|
|
|
|
} /* PHYSFS_mountIo */
|
|
|
|
|
|
|
|
|
2010-08-30 08:39:11 +02:00
|
|
|
int PHYSFS_mountMemory(const void *buf, PHYSFS_uint64 len, void (*del)(void *),
|
|
|
|
const char *fname, const char *mountPoint,
|
|
|
|
int appendToPath)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
PHYSFS_Io *io = NULL;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!buf, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2010-08-30 08:39:11 +02:00
|
|
|
|
|
|
|
io = __PHYSFS_createMemoryIo(buf, len, del);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io, ERRPASS, 0);
|
2010-08-30 08:39:11 +02:00
|
|
|
retval = doMount(io, fname, mountPoint, appendToPath);
|
|
|
|
if (!retval)
|
|
|
|
{
|
|
|
|
/* docs say not to call (del) in case of failure, so cheat. */
|
|
|
|
MemoryIoInfo *info = (MemoryIoInfo *) io->opaque;
|
|
|
|
info->destruct = NULL;
|
|
|
|
io->destroy(io);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
} /* PHYSFS_mountMemory */
|
|
|
|
|
|
|
|
|
2010-08-30 09:02:32 +02:00
|
|
|
int PHYSFS_mountHandle(PHYSFS_File *file, const char *fname,
|
|
|
|
const char *mountPoint, int appendToPath)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
PHYSFS_Io *io = NULL;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(file == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2010-08-30 09:02:32 +02:00
|
|
|
|
|
|
|
io = __PHYSFS_createHandleIo(file);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!io, ERRPASS, 0);
|
2010-08-30 09:02:32 +02:00
|
|
|
retval = doMount(io, fname, mountPoint, appendToPath);
|
|
|
|
if (!retval)
|
|
|
|
{
|
|
|
|
/* docs say not to destruct in case of failure, so cheat. */
|
|
|
|
io->opaque = NULL;
|
|
|
|
io->destroy(io);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
} /* PHYSFS_mountHandle */
|
|
|
|
|
|
|
|
|
2010-08-30 02:56:35 +02:00
|
|
|
int PHYSFS_mount(const char *newDir, const char *mountPoint, int appendToPath)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!newDir, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2010-08-30 02:56:35 +02:00
|
|
|
return doMount(NULL, newDir, mountPoint, appendToPath);
|
2005-03-13 10:09:26 +01:00
|
|
|
} /* PHYSFS_mount */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
|
|
|
|
{
|
2010-08-30 08:39:28 +02:00
|
|
|
return doMount(NULL, newDir, NULL, appendToPath);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_addToSearchPath */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_removeFromSearchPath(const char *oldDir)
|
2010-08-22 09:43:22 +02:00
|
|
|
{
|
|
|
|
return PHYSFS_unmount(oldDir);
|
|
|
|
} /* PHYSFS_removeFromSearchPath */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_unmount(const char *oldDir)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
|
|
|
DirHandle *prev = NULL;
|
|
|
|
DirHandle *next = NULL;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(oldDir == NULL, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
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;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!freeDirHandle(i, openReadList), ERRPASS,
|
2002-03-30 17:44:09 +01:00
|
|
|
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
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO_MUTEX(ERRPASS, stateLock, 1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* if */
|
|
|
|
prev = i;
|
|
|
|
} /* for */
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO_MUTEX(PHYSFS_ERR_NOT_MOUNTED, stateLock, 0);
|
2010-08-22 09:43:22 +02:00
|
|
|
} /* PHYSFS_unmount */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
|
|
|
char **PHYSFS_getSearchPath(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return doEnumStringList(PHYSFS_getSearchPathCallback);
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* PHYSFS_getSearchPath */
|
|
|
|
|
|
|
|
|
2005-03-14 08:15:40 +01:00
|
|
|
const char *PHYSFS_getMountPoint(const char *dir)
|
|
|
|
{
|
|
|
|
DirHandle *i;
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
if (strcmp(i->dirName, dir) == 0)
|
|
|
|
{
|
|
|
|
const char *retval = ((i->mountPoint) ? i->mountPoint : "/");
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2005-03-14 08:15:40 +01:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_MACRO(PHYSFS_ERR_NOT_MOUNTED, NULL);
|
2005-03-14 08:15:40 +01:00
|
|
|
} /* PHYSFS_getMountPoint */
|
|
|
|
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
void PHYSFS_getSearchPathCallback(PHYSFS_StringCallback callback, void *data)
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
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)
|
2004-09-29 08:09:29 +02:00
|
|
|
callback(data, i->dirName);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* PHYSFS_getSearchPathCallback */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
/* Split out to avoid stack allocation in a loop. */
|
|
|
|
static void setSaneCfgAddPath(const char *i, const size_t l, const char *dirsep,
|
|
|
|
int archivesFirst)
|
|
|
|
{
|
|
|
|
const char *d = PHYSFS_getRealDir(i);
|
|
|
|
const size_t allocsize = strlen(d) + strlen(dirsep) + l + 1;
|
|
|
|
char *str = (char *) __PHYSFS_smallAlloc(allocsize);
|
|
|
|
if (str != NULL)
|
|
|
|
{
|
|
|
|
sprintf(str, "%s%s%s", d, dirsep, i);
|
2010-08-22 09:43:22 +02:00
|
|
|
PHYSFS_mount(str, NULL, archivesFirst == 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(str);
|
|
|
|
} /* if */
|
|
|
|
} /* setSaneCfgAddPath */
|
|
|
|
|
|
|
|
|
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 *dirsep = PHYSFS_getDirSeparator();
|
2012-03-22 04:31:21 +01:00
|
|
|
const char *basedir;
|
|
|
|
const char *prefdir;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
|
2002-03-30 17:44:09 +01:00
|
|
|
|
2012-03-22 04:31:21 +01:00
|
|
|
prefdir = PHYSFS_getPrefDir(organization, appName);
|
|
|
|
BAIL_IF_MACRO(!prefdir, ERRPASS, 0);
|
2005-03-14 12:49:30 +01:00
|
|
|
|
2012-03-22 04:31:21 +01:00
|
|
|
basedir = PHYSFS_getBaseDir();
|
|
|
|
BAIL_IF_MACRO(!basedir, ERRPASS, 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
|
2012-03-22 04:31:21 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_setWriteDir(prefdir), PHYSFS_ERR_NO_WRITE_DIR, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2001-09-26 05:08:57 +02:00
|
|
|
/* Put write dir first in search path... */
|
2012-03-22 04:31:21 +01:00
|
|
|
PHYSFS_mount(prefdir, NULL, 0);
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2012-03-22 04:31:21 +01:00
|
|
|
/* Put base path on search path... */
|
2010-08-22 09:43:22 +02:00
|
|
|
PHYSFS_mount(basedir, NULL, 1);
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2012-03-22 04:31:21 +01:00
|
|
|
/* handle CD-ROMs... */
|
2001-07-06 03:27:14 +02:00
|
|
|
if (includeCdRoms)
|
|
|
|
{
|
|
|
|
char **cds = PHYSFS_getCdRomDirs();
|
|
|
|
char **i;
|
|
|
|
for (i = cds; *i != NULL; i++)
|
2010-08-22 09:43:22 +02:00
|
|
|
PHYSFS_mount(*i, NULL, 1);
|
2001-07-06 03:27:14 +02:00
|
|
|
PHYSFS_freeList(cds);
|
|
|
|
} /* if */
|
|
|
|
|
2012-03-22 04:31:21 +01:00
|
|
|
/* Root out archives, and add them to search path... */
|
2001-07-06 03:27:14 +02:00
|
|
|
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);
|
2007-03-15 09:16:23 +01:00
|
|
|
if (__PHYSFS_stricmpASCII(ext, archiveExt) == 0)
|
2007-03-24 04:54:58 +01:00
|
|
|
setSaneCfgAddPath(*i, l, dirsep, archivesFirst);
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
PHYSFS_freeList(rc);
|
|
|
|
} /* if */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
2007-04-02 00:06:37 +02:00
|
|
|
int PHYSFS_symbolicLinksPermitted(void)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return allowSymLinks;
|
2007-04-02 00:06:37 +02:00
|
|
|
} /* PHYSFS_symbolicLinksPermitted */
|
|
|
|
|
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
/*
|
|
|
|
* Verify that (fname) (in platform-independent notation), in relation
|
|
|
|
* to (h) is secure. That means that each element of fname is checked
|
2005-03-13 13:03:05 +01:00
|
|
|
* for symlinks (if they aren't permitted). This also allows for quick
|
|
|
|
* rejection of files that exist outside an archive's mountpoint.
|
2004-09-26 02:25:04 +02:00
|
|
|
*
|
|
|
|
* With some exceptions (like PHYSFS_mkdir(), which builds multiple subdirs
|
|
|
|
* at a time), you should always pass zero for "allowMissing" for efficiency.
|
|
|
|
*
|
2005-03-14 08:15:40 +01:00
|
|
|
* (fname) must point to an output from sanitizePlatformIndependentPath(),
|
|
|
|
* since it will make sure that path names are in the right format for
|
|
|
|
* passing certain checks. It will also do checks for "insecure" pathnames
|
|
|
|
* like ".." which should be done once instead of once per archive. This also
|
|
|
|
* gives us license to treat (fname) as scratch space in this function.
|
2005-03-13 13:03:05 +01:00
|
|
|
*
|
2004-09-26 02:25:04 +02:00
|
|
|
* Returns non-zero if string is safe, zero if there's a security issue.
|
2005-03-14 08:15:40 +01:00
|
|
|
* PHYSFS_getLastError() will specify what was wrong. (*fname) will be
|
|
|
|
* updated to point past any mount point elements so it is prepared to
|
|
|
|
* be used with the archiver directly.
|
2004-09-26 02:25:04 +02:00
|
|
|
*/
|
2005-03-14 08:15:40 +01:00
|
|
|
static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
char *fname = *_fname;
|
2001-07-07 05:52:43 +02:00
|
|
|
int retval = 1;
|
|
|
|
char *start;
|
|
|
|
char *end;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2002-08-29 01:27:10 +02:00
|
|
|
if (*fname == '\0') /* quick rejection. */
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2002-08-29 01:27:10 +02:00
|
|
|
|
2005-03-14 08:15:40 +01:00
|
|
|
/* !!! FIXME: This codeblock sucks. */
|
2005-03-13 10:09:26 +01:00
|
|
|
if (h->mountPoint != NULL) /* NULL mountpoint means "/". */
|
|
|
|
{
|
|
|
|
size_t mntpntlen = strlen(h->mountPoint);
|
2005-03-13 13:03:05 +01:00
|
|
|
size_t len = strlen(fname);
|
2005-03-14 13:05:07 +01:00
|
|
|
assert(mntpntlen > 1); /* root mount points should be NULL. */
|
2005-03-13 22:03:31 +01:00
|
|
|
/* not under the mountpoint, so skip this archive. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(len < mntpntlen-1, PHYSFS_ERR_NO_SUCH_PATH, 0);
|
2005-03-14 08:15:40 +01:00
|
|
|
/* !!! FIXME: Case insensitive? */
|
|
|
|
retval = strncmp(h->mountPoint, fname, mntpntlen-1);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(retval != 0, PHYSFS_ERR_NO_SUCH_PATH, 0);
|
2005-03-14 08:15:40 +01:00
|
|
|
if (len > mntpntlen-1) /* corner case... */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(fname[mntpntlen-1]!='/', PHYSFS_ERR_NO_SUCH_PATH, 0);
|
2005-03-14 08:15:40 +01:00
|
|
|
fname += mntpntlen-1; /* move to start of actual archive path. */
|
|
|
|
if (*fname == '/')
|
|
|
|
fname++;
|
|
|
|
*_fname = fname; /* skip mountpoint for later use. */
|
|
|
|
retval = 1; /* may be reset, below. */
|
2005-03-13 10:09:26 +01:00
|
|
|
} /* if */
|
|
|
|
|
2005-03-13 22:16:15 +01:00
|
|
|
start = fname;
|
2005-03-13 13:03:05 +01:00
|
|
|
if (!allowSymLinks)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2005-03-13 13:03:05 +01:00
|
|
|
while (1)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2010-09-05 08:41:13 +02:00
|
|
|
PHYSFS_Stat statbuf;
|
2005-08-20 06:46:25 +02:00
|
|
|
int rc = 0;
|
2005-03-13 13:03:05 +01:00
|
|
|
end = strchr(start, '/');
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2005-08-20 06:46:25 +02:00
|
|
|
if (end != NULL) *end = '\0';
|
2010-09-05 08:41:13 +02:00
|
|
|
rc = h->funcs->stat(h->opaque, fname, &retval, &statbuf);
|
|
|
|
if (rc)
|
|
|
|
rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
|
2005-08-20 06:46:25 +02:00
|
|
|
if (end != NULL) *end = '/';
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
/* insecure path (has a disallowed symlink in it)? */
|
|
|
|
BAIL_IF_MACRO(rc, PHYSFS_ERR_SYMLINK_FORBIDDEN, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
|
2002-12-01 12:21:27 +01:00
|
|
|
/* break out early if path element is missing. */
|
2003-03-19 07:31:53 +01:00
|
|
|
if (!retval)
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We need to clear it if it's the last element of the path,
|
|
|
|
* since this might be a non-existant file we're opening
|
|
|
|
* for writing...
|
|
|
|
*/
|
2003-03-19 07:31:53 +01:00
|
|
|
if ((end == NULL) || (allowMissing))
|
2002-12-01 12:21:27 +01:00
|
|
|
retval = 1;
|
2002-08-22 02:13:48 +02:00
|
|
|
break;
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* if */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
if (end == NULL)
|
|
|
|
break;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
start = end + 1;
|
|
|
|
} /* while */
|
|
|
|
} /* if */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2005-03-14 08:15:40 +01:00
|
|
|
} /* verifyPath */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
static int doMkdir(const char *_dname, char *dname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
DirHandle *h;
|
|
|
|
char *start;
|
|
|
|
char *end;
|
|
|
|
int retval = 0;
|
2003-03-19 07:31:53 +01:00
|
|
|
int exists = 1; /* force existance check on first path element. */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_dname, dname), ERRPASS, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
h = writeDir;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!verifyPath(h, &dname, 1), ERRPASS, stateLock, 0);
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2005-03-14 08:15:40 +01:00
|
|
|
start = dname;
|
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
|
|
|
|
2003-03-19 07:31:53 +01:00
|
|
|
/* only check for existance if all parent dirs existed, too... */
|
|
|
|
if (exists)
|
2010-09-05 08:41:13 +02:00
|
|
|
{
|
|
|
|
PHYSFS_Stat statbuf;
|
|
|
|
const int rc = h->funcs->stat(h->opaque, dname, &exists, &statbuf);
|
|
|
|
retval = ((rc) && (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY));
|
|
|
|
} /* if */
|
2003-03-19 07:31:53 +01:00
|
|
|
|
|
|
|
if (!exists)
|
2005-03-13 22:16:15 +01:00
|
|
|
retval = h->funcs->mkdir(h->opaque, dname);
|
2003-03-19 07:04:09 +01:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
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);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2007-03-24 04:54:58 +01:00
|
|
|
} /* doMkdir */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_mkdir(const char *_dname)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
char *dname;
|
|
|
|
size_t len;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!_dname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
len = strlen(_dname) + 1;
|
|
|
|
dname = (char *) __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!dname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
retval = doMkdir(_dname, dname);
|
|
|
|
__PHYSFS_smallFree(dname);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* PHYSFS_mkdir */
|
|
|
|
|
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
static int doDelete(const char *_fname, 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;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), ERRPASS, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, stateLock, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
h = writeDir;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!verifyPath(h, &fname, 0), ERRPASS, stateLock, 0);
|
2004-09-26 02:25:04 +02:00
|
|
|
retval = h->funcs->remove(h->opaque, fname);
|
2002-03-30 17:44:09 +01:00
|
|
|
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2007-03-24 04:54:58 +01:00
|
|
|
} /* doDelete */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_delete(const char *_fname)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
char *fname;
|
|
|
|
size_t len;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
len = strlen(_fname) + 1;
|
|
|
|
fname = (char *) __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
retval = doDelete(_fname, fname);
|
|
|
|
__PHYSFS_smallFree(fname);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* PHYSFS_delete */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
const char *PHYSFS_getRealDir(const char *_fname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2002-08-21 04:59:15 +02:00
|
|
|
const char *retval = NULL;
|
2007-03-24 04:54:58 +01:00
|
|
|
char *fname = NULL;
|
|
|
|
size_t len;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
|
2007-03-24 04:54:58 +01:00
|
|
|
len = strlen(_fname) + 1;
|
|
|
|
fname = __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2007-03-24 04:54:58 +01:00
|
|
|
if (sanitizePlatformIndependentPath(_fname, fname))
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
DirHandle *i;
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2010-08-30 03:36:38 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if (partOfMountPoint(i, arcfname))
|
2010-08-30 03:36:38 +02:00
|
|
|
{
|
2002-08-21 04:59:15 +02:00
|
|
|
retval = i->dirName;
|
2010-08-30 03:36:38 +02:00
|
|
|
break;
|
|
|
|
} /* if */
|
2007-03-24 04:54:58 +01:00
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
|
|
|
{
|
2010-09-05 08:41:13 +02:00
|
|
|
PHYSFS_Stat statbuf;
|
|
|
|
int exists = 0;
|
|
|
|
if (i->funcs->stat(i->opaque, arcfname, &exists, &statbuf))
|
2010-08-30 03:36:38 +02:00
|
|
|
{
|
2010-09-05 08:41:13 +02:00
|
|
|
if (exists)
|
|
|
|
retval = i->dirName;
|
2010-08-30 03:36:38 +02:00
|
|
|
break;
|
|
|
|
} /* if */
|
2007-03-24 04:54:58 +01:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
} /* if */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(fname);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getRealDir */
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
|
|
|
|
static int locateInStringList(const char *str,
|
|
|
|
char **list,
|
|
|
|
PHYSFS_uint32 *pos)
|
|
|
|
{
|
2005-10-13 00:03:28 +02:00
|
|
|
PHYSFS_uint32 len = *pos;
|
|
|
|
PHYSFS_uint32 half_len;
|
2004-09-29 08:09:29 +02:00
|
|
|
PHYSFS_uint32 lo = 0;
|
2005-10-13 00:03:28 +02:00
|
|
|
PHYSFS_uint32 middle;
|
2004-09-29 08:09:29 +02:00
|
|
|
int cmp;
|
|
|
|
|
2005-10-13 00:03:28 +02:00
|
|
|
while (len > 0)
|
2004-09-29 08:09:29 +02:00
|
|
|
{
|
2005-10-13 00:03:28 +02:00
|
|
|
half_len = len >> 1;
|
|
|
|
middle = lo + half_len;
|
|
|
|
cmp = strcmp(list[middle], str);
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
if (cmp == 0) /* it's in the list already. */
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2005-10-13 00:03:28 +02:00
|
|
|
else if (cmp > 0)
|
|
|
|
len = half_len;
|
2004-09-29 08:09:29 +02:00
|
|
|
else
|
2004-10-31 13:20:00 +01:00
|
|
|
{
|
2005-10-13 00:03:28 +02:00
|
|
|
lo = middle + 1;
|
|
|
|
len -= half_len + 1;
|
2004-10-31 13:20:00 +01:00
|
|
|
} /* else */
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* while */
|
|
|
|
|
2005-10-13 00:03:28 +02:00
|
|
|
*pos = lo;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* locateInStringList */
|
|
|
|
|
|
|
|
|
2005-09-18 23:44:42 +02:00
|
|
|
static void enumFilesCallback(void *data, const char *origdir, const char *str)
|
2004-09-29 08:09:29 +02:00
|
|
|
{
|
|
|
|
PHYSFS_uint32 pos;
|
|
|
|
void *ptr;
|
|
|
|
char *newstr;
|
|
|
|
EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if file is in the list already, and if not, insert it in there
|
|
|
|
* alphabetically...
|
|
|
|
*/
|
|
|
|
pos = pecd->size;
|
2005-10-13 00:03:28 +02:00
|
|
|
if (locateInStringList(str, pecd->list, &pos))
|
|
|
|
return; /* already in the list. */
|
2004-09-29 08:09:29 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
ptr = allocator.Realloc(pecd->list, (pecd->size + 2) * sizeof (char *));
|
|
|
|
newstr = (char *) allocator.Malloc(strlen(str) + 1);
|
2004-09-29 08:09:29 +02:00
|
|
|
if (ptr != NULL)
|
|
|
|
pecd->list = (char **) ptr;
|
|
|
|
|
|
|
|
if ((ptr == NULL) || (newstr == NULL))
|
|
|
|
return; /* better luck next time. */
|
|
|
|
|
|
|
|
strcpy(newstr, str);
|
|
|
|
|
|
|
|
if (pos != pecd->size)
|
|
|
|
{
|
|
|
|
memmove(&pecd->list[pos+1], &pecd->list[pos],
|
|
|
|
sizeof (char *) * ((pecd->size) - pos));
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
pecd->list[pos] = newstr;
|
|
|
|
pecd->size++;
|
|
|
|
} /* enumFilesCallback */
|
2001-07-06 23:29:37 +02:00
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
char **PHYSFS_enumerateFiles(const char *path)
|
2004-09-29 08:09:29 +02:00
|
|
|
{
|
|
|
|
EnumStringListCallbackData ecd;
|
|
|
|
memset(&ecd, '\0', sizeof (ecd));
|
2005-03-14 12:49:30 +01:00
|
|
|
ecd.list = (char **) allocator.Malloc(sizeof (char *));
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!ecd.list, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2004-09-29 08:09:29 +02:00
|
|
|
PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
|
|
|
|
ecd.list[ecd.size] = NULL;
|
2010-01-28 08:27:45 +01:00
|
|
|
return ecd.list;
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* PHYSFS_enumerateFiles */
|
|
|
|
|
|
|
|
|
2005-09-19 00:27:05 +02:00
|
|
|
/*
|
2007-03-24 04:54:58 +01:00
|
|
|
* Broke out to seperate function so we can use stack allocation gratuitously.
|
2005-09-19 00:27:05 +02:00
|
|
|
*/
|
|
|
|
static void enumerateFromMountPoint(DirHandle *i, const char *arcfname,
|
|
|
|
PHYSFS_EnumFilesCallback callback,
|
|
|
|
const char *_fname, void *data)
|
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
const size_t len = strlen(arcfname);
|
2007-03-12 04:41:20 +01:00
|
|
|
char *ptr = NULL;
|
|
|
|
char *end = NULL;
|
2007-03-24 04:54:58 +01:00
|
|
|
const size_t slen = strlen(i->mountPoint) + 1;
|
|
|
|
char *mountPoint = (char *) __PHYSFS_smallAlloc(slen);
|
|
|
|
|
|
|
|
if (mountPoint == NULL)
|
|
|
|
return; /* oh well. */
|
|
|
|
|
2005-09-19 00:27:05 +02:00
|
|
|
strcpy(mountPoint, i->mountPoint);
|
2007-03-12 04:41:20 +01:00
|
|
|
ptr = mountPoint + ((len) ? len + 1 : 0);
|
|
|
|
end = strchr(ptr, '/');
|
2005-09-19 00:27:05 +02:00
|
|
|
assert(end); /* should always find a terminating '/'. */
|
|
|
|
*end = '\0';
|
|
|
|
callback(data, _fname, ptr);
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(mountPoint);
|
2005-09-19 00:27:05 +02:00
|
|
|
} /* enumerateFromMountPoint */
|
|
|
|
|
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
/* !!! FIXME: this should report error conditions. */
|
2005-03-13 13:03:05 +01:00
|
|
|
void PHYSFS_enumerateFilesCallback(const char *_fname,
|
2005-09-18 23:44:42 +02:00
|
|
|
PHYSFS_EnumFilesCallback callback,
|
2004-09-29 08:09:29 +02:00
|
|
|
void *data)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
size_t len;
|
|
|
|
char *fname;
|
2004-09-29 08:09:29 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
|
|
|
|
BAIL_IF_MACRO(!callback, PHYSFS_ERR_INVALID_ARGUMENT, ) /*0*/;
|
2001-07-06 23:29:37 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
len = strlen(_fname) + 1;
|
|
|
|
fname = (char *) __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, ) /*0*/;
|
2007-03-24 04:54:58 +01:00
|
|
|
|
|
|
|
if (sanitizePlatformIndependentPath(_fname, fname))
|
2001-07-06 23:29:37 +02:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
DirHandle *i;
|
|
|
|
int noSyms;
|
2005-03-14 08:15:40 +01:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
noSyms = !allowSymLinks;
|
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
2005-03-14 08:15:40 +01:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if (partOfMountPoint(i, arcfname))
|
|
|
|
enumerateFromMountPoint(i, arcfname, callback, _fname, data);
|
|
|
|
|
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
|
|
|
{
|
|
|
|
i->funcs->enumerateFiles(i->opaque, arcfname, noSyms,
|
|
|
|
callback, _fname, data);
|
|
|
|
} /* else if */
|
|
|
|
} /* for */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
__PHYSFS_smallFree(fname);
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* PHYSFS_enumerateFilesCallback */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
int PHYSFS_exists(const char *fname)
|
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return (PHYSFS_getRealDir(fname) != NULL);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* PHYSFS_exists */
|
|
|
|
|
|
|
|
|
2010-08-21 23:34:00 +02:00
|
|
|
PHYSFS_sint64 PHYSFS_getLastModTime(const char *fname)
|
2002-05-25 11:41:14 +02:00
|
|
|
{
|
2010-08-21 23:34:00 +02:00
|
|
|
PHYSFS_Stat statbuf;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, -1);
|
2010-08-21 23:34:00 +02:00
|
|
|
return statbuf.modtime;
|
2002-05-25 11:41:14 +02:00
|
|
|
} /* PHYSFS_getLastModTime */
|
|
|
|
|
|
|
|
|
2010-09-05 08:41:13 +02:00
|
|
|
int PHYSFS_isDirectory(const char *fname)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2010-09-05 08:41:13 +02:00
|
|
|
PHYSFS_Stat statbuf;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
|
2010-09-05 08:41:13 +02:00
|
|
|
return (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* PHYSFS_isDirectory */
|
|
|
|
|
|
|
|
|
2010-09-05 08:41:13 +02:00
|
|
|
int PHYSFS_isSymbolicLink(const char *fname)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2010-09-05 08:41:13 +02:00
|
|
|
PHYSFS_Stat statbuf;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_stat(fname, &statbuf), ERRPASS, 0);
|
2010-09-05 08:41:13 +02:00
|
|
|
return (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* PHYSFS_isSymbolicLink */
|
|
|
|
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
static PHYSFS_File *doOpenWrite(const char *_fname, int appending)
|
2001-07-07 11:05:19 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = NULL;
|
2007-03-24 04:54:58 +01:00
|
|
|
size_t len;
|
|
|
|
char *fname;
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
len = strlen(_fname) + 1;
|
|
|
|
fname = (char *) __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
if (sanitizePlatformIndependentPath(_fname, fname))
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = NULL;
|
2007-03-24 04:54:58 +01:00
|
|
|
DirHandle *h = NULL;
|
|
|
|
const PHYSFS_Archiver *f;
|
2004-09-26 15:00:59 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!writeDir, PHYSFS_ERR_NO_WRITE_DIR, doOpenWriteEnd);
|
2004-09-26 02:25:04 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
h = writeDir;
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!verifyPath(h, &fname, 0), ERRPASS, doOpenWriteEnd);
|
2004-09-26 15:00:59 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
f = h->funcs;
|
|
|
|
if (appending)
|
2010-08-30 09:01:57 +02:00
|
|
|
io = f->openAppend(h->opaque, fname);
|
2007-03-24 04:54:58 +01:00
|
|
|
else
|
2010-08-30 09:01:57 +02:00
|
|
|
io = f->openWrite(h->opaque, fname);
|
2007-03-24 04:54:58 +01:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!io, ERRPASS, doOpenWriteEnd);
|
2007-03-24 04:54:58 +01:00
|
|
|
|
|
|
|
fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
|
|
|
|
if (fh == NULL)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
io->destroy(io);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, doOpenWriteEnd);
|
2007-03-24 04:54:58 +01:00
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memset(fh, '\0', sizeof (FileHandle));
|
2010-08-30 09:01:57 +02:00
|
|
|
fh->io = io;
|
2007-03-24 04:54:58 +01:00
|
|
|
fh->dirHandle = h;
|
|
|
|
fh->next = openWriteList;
|
|
|
|
openWriteList = fh;
|
|
|
|
} /* else */
|
|
|
|
|
|
|
|
doOpenWriteEnd:
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2004-09-26 15:00:59 +02:00
|
|
|
} /* if */
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(fname);
|
2010-01-28 08:27:45 +01:00
|
|
|
return ((PHYSFS_File *) fh);
|
2001-07-07 11:05:19 +02:00
|
|
|
} /* doOpenWrite */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
PHYSFS_File *PHYSFS_openWrite(const char *filename)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return doOpenWrite(filename, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_openWrite */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
PHYSFS_File *PHYSFS_openAppend(const char *filename)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2010-01-28 08:27:45 +01:00
|
|
|
return doOpenWrite(filename, 1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_openAppend */
|
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
PHYSFS_File *PHYSFS_openRead(const char *_fname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = NULL;
|
2007-03-24 04:54:58 +01:00
|
|
|
char *fname;
|
|
|
|
size_t len;
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
len = strlen(_fname) + 1;
|
|
|
|
fname = (char *) __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
if (sanitizePlatformIndependentPath(_fname, fname))
|
|
|
|
{
|
|
|
|
int fileExists = 0;
|
|
|
|
DirHandle *i = NULL;
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = NULL;
|
2005-03-14 08:15:40 +01:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2004-09-26 15:00:59 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!searchPath, PHYSFS_ERR_NO_SUCH_PATH, openReadEnd);
|
2004-09-26 15:00:59 +02:00
|
|
|
|
2012-03-22 07:57:29 +01:00
|
|
|
for (i = searchPath; (i != NULL) && (!fileExists); i = i->next)
|
2004-09-26 15:00:59 +02:00
|
|
|
{
|
2007-03-24 04:54:58 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if (verifyPath(i, &arcfname, 0))
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
io = i->funcs->openRead(i->opaque, arcfname, &fileExists);
|
|
|
|
if (io)
|
2007-03-24 04:54:58 +01:00
|
|
|
break;
|
|
|
|
} /* if */
|
2012-03-22 07:57:29 +01:00
|
|
|
} /* for */
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_IF_MACRO(!io, ERRPASS, openReadEnd);
|
2004-09-26 02:25:04 +02:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
|
|
|
|
if (fh == NULL)
|
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
io->destroy(io);
|
2012-03-20 20:38:12 +01:00
|
|
|
GOTO_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, openReadEnd);
|
2007-03-24 04:54:58 +01:00
|
|
|
} /* if */
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
memset(fh, '\0', sizeof (FileHandle));
|
2010-08-30 09:01:57 +02:00
|
|
|
fh->io = io;
|
2007-03-24 04:54:58 +01:00
|
|
|
fh->forReading = 1;
|
|
|
|
fh->dirHandle = i;
|
|
|
|
fh->next = openReadList;
|
|
|
|
openReadList = fh;
|
|
|
|
|
|
|
|
openReadEnd:
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
} /* if */
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
__PHYSFS_smallFree(fname);
|
2010-01-28 08:27:45 +01:00
|
|
|
return ((PHYSFS_File *) fh);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_openRead */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int closeHandleInOpenList(FileHandle **list, FileHandle *handle)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *prev = NULL;
|
|
|
|
FileHandle *i;
|
2002-12-01 12:21:27 +01:00
|
|
|
int rc = 1;
|
2001-07-06 10:47:23 +02:00
|
|
|
|
2001-07-09 03:45:13 +02:00
|
|
|
for (i = *list; i != NULL; i = i->next)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
if (i == handle) /* handle is in this list? */
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = handle->io;
|
2004-09-26 15:00:59 +02:00
|
|
|
PHYSFS_uint8 *tmp = handle->buffer;
|
2004-09-26 15:17:54 +02:00
|
|
|
rc = PHYSFS_flush((PHYSFS_File *) handle);
|
2001-07-09 03:45:13 +02:00
|
|
|
if (!rc)
|
2010-01-28 08:27:45 +01:00
|
|
|
return -1;
|
2010-08-30 09:01:57 +02:00
|
|
|
io->destroy(io);
|
2001-07-09 03:45:13 +02:00
|
|
|
|
2002-12-01 12:21:27 +01:00
|
|
|
if (tmp != NULL) /* free any associated buffer. */
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(tmp);
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2001-07-09 03:45:13 +02:00
|
|
|
if (prev == NULL)
|
2004-09-26 15:00:59 +02:00
|
|
|
*list = handle->next;
|
2001-07-09 03:45:13 +02:00
|
|
|
else
|
2004-09-26 15:00:59 +02:00
|
|
|
prev->next = handle->next;
|
2001-07-09 03:45:13 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(handle);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2001-07-09 03:45:13 +02:00
|
|
|
} /* if */
|
|
|
|
prev = i;
|
|
|
|
} /* for */
|
2001-07-06 10:47:23 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2001-07-09 03:45:13 +02:00
|
|
|
} /* closeHandleInOpenList */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
int PHYSFS_close(PHYSFS_File *_handle)
|
2001-07-09 03:45:13 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *handle = (FileHandle *) _handle;
|
2001-07-09 03:45:13 +02:00
|
|
|
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);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
|
2001-07-09 03:45:13 +02:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
rc = closeHandleInOpenList(&openWriteList, handle);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(rc == -1, ERRPASS, stateLock, 0);
|
2001-07-09 03:45:13 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!rc, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_close */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *buffer,
|
2010-08-21 08:47:58 +02:00
|
|
|
PHYSFS_uint64 len)
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = NULL;
|
2002-12-01 12:21:27 +01:00
|
|
|
PHYSFS_sint64 retval = 0;
|
2010-08-21 08:47:58 +02:00
|
|
|
PHYSFS_uint32 buffered = 0;
|
|
|
|
PHYSFS_sint64 rc = 0;
|
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
buffered = fh->buffill - fh->bufpos;
|
|
|
|
if (buffered >= len) /* totally in the buffer, just copy and return! */
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2010-08-21 08:47:58 +02:00
|
|
|
memcpy(buffer, fh->buffer + fh->bufpos, (size_t) len);
|
|
|
|
fh->bufpos += (PHYSFS_uint32) len;
|
|
|
|
return (PHYSFS_sint64) len;
|
|
|
|
} /* else if */
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
if (buffered > 0) /* partially in the buffer... */
|
|
|
|
{
|
|
|
|
memcpy(buffer, fh->buffer + fh->bufpos, (size_t) buffered);
|
|
|
|
buffer = ((PHYSFS_uint8 *) buffer) + buffered;
|
|
|
|
len -= buffered;
|
|
|
|
retval = buffered;
|
2011-10-18 21:55:29 +02:00
|
|
|
fh->buffill = fh->bufpos = 0;
|
2010-08-21 08:47:58 +02:00
|
|
|
} /* if */
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
/* if you got here, the buffer is drained and we still need bytes. */
|
|
|
|
assert(len > 0);
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
io = fh->io;
|
2010-08-21 08:47:58 +02:00
|
|
|
if (len >= fh->bufsize) /* need more than the buffer takes. */
|
|
|
|
{
|
|
|
|
/* leave buffer empty, go right to output instead. */
|
2010-08-30 09:01:57 +02:00
|
|
|
rc = io->read(io, buffer, len);
|
2010-08-21 08:47:58 +02:00
|
|
|
if (rc < 0)
|
|
|
|
return ((retval == 0) ? rc : retval);
|
|
|
|
return retval + rc;
|
|
|
|
} /* if */
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
/* need less than buffer can take. Fill buffer. */
|
2010-08-30 09:01:57 +02:00
|
|
|
rc = io->read(io, fh->buffer, fh->bufsize);
|
2010-08-21 08:47:58 +02:00
|
|
|
if (rc < 0)
|
|
|
|
return ((retval == 0) ? rc : retval);
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
assert(fh->bufpos == 0);
|
|
|
|
fh->buffill = (PHYSFS_uint32) rc;
|
|
|
|
rc = doBufferedRead(fh, buffer, len); /* go from the start, again. */
|
|
|
|
if (rc < 0)
|
|
|
|
return ((retval == 0) ? rc : retval);
|
|
|
|
|
|
|
|
return retval + rc;
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* doBufferedRead */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
|
2010-08-21 08:47:58 +02:00
|
|
|
PHYSFS_uint32 size, PHYSFS_uint32 count)
|
|
|
|
{
|
|
|
|
const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
|
|
|
|
const PHYSFS_sint64 retval = PHYSFS_readBytes(handle, buffer, len);
|
2012-03-14 10:47:15 +01:00
|
|
|
return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
|
2010-08-21 08:47:58 +02:00
|
|
|
} /* PHYSFS_read */
|
|
|
|
|
|
|
|
|
|
|
|
PHYSFS_sint64 PHYSFS_readBytes(PHYSFS_File *handle, void *buffer,
|
|
|
|
PHYSFS_uint64 len)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
#ifdef PHYSFS_NO_64BIT_SUPPORT
|
|
|
|
const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
|
|
|
|
#else
|
|
|
|
const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
|
|
|
|
#endif
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!__PHYSFS_ui64FitsAddressSpace(len))
|
|
|
|
BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
|
|
|
|
BAIL_IF_MACRO(!fh->forReading, PHYSFS_ERR_OPEN_FOR_WRITING, -1);
|
|
|
|
BAIL_IF_MACRO(len == 0, ERRPASS, 0);
|
|
|
|
if (fh->buffer)
|
2010-08-21 08:47:58 +02:00
|
|
|
return doBufferedRead(fh, buffer, len);
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
return fh->io->read(fh->io, buffer, len);
|
2010-08-21 08:47:58 +02:00
|
|
|
} /* PHYSFS_readBytes */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
|
2010-08-21 08:47:58 +02:00
|
|
|
PHYSFS_uint64 len)
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
2006-04-11 16:33:48 +02:00
|
|
|
|
2002-12-01 12:21:27 +01:00
|
|
|
/* whole thing fits in the buffer? */
|
2010-08-21 08:47:58 +02:00
|
|
|
if ( (((PHYSFS_uint64) fh->buffill) + len) < fh->bufsize )
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2010-08-21 08:47:58 +02:00
|
|
|
memcpy(fh->buffer + fh->buffill, buffer, (size_t) len);
|
|
|
|
fh->buffill += (PHYSFS_uint32) len;
|
|
|
|
return (PHYSFS_sint64) len;
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* would overflow buffer. Flush and then write the new objects, too. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, -1);
|
2010-08-30 09:01:57 +02:00
|
|
|
return fh->io->write(fh->io, buffer, len);
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* doBufferedWrite */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
PHYSFS_sint64 PHYSFS_write(PHYSFS_File *handle, const void *buffer,
|
2010-08-21 08:47:58 +02:00
|
|
|
PHYSFS_uint32 size, PHYSFS_uint32 count)
|
|
|
|
{
|
|
|
|
const PHYSFS_uint64 len = ((PHYSFS_uint64) size) * ((PHYSFS_uint64) count);
|
|
|
|
const PHYSFS_sint64 retval = PHYSFS_writeBytes(handle, buffer, len);
|
2012-03-14 10:47:15 +01:00
|
|
|
return ( (retval <= 0) ? retval : (retval / ((PHYSFS_sint64) size)) );
|
2010-08-21 08:47:58 +02:00
|
|
|
} /* PHYSFS_write */
|
|
|
|
|
|
|
|
|
|
|
|
PHYSFS_sint64 PHYSFS_writeBytes(PHYSFS_File *handle, const void *buffer,
|
|
|
|
PHYSFS_uint64 len)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-21 08:47:58 +02:00
|
|
|
#ifdef PHYSFS_NO_64BIT_SUPPORT
|
|
|
|
const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFF);
|
|
|
|
#else
|
|
|
|
const PHYSFS_uint64 maxlen = __PHYSFS_UI64(0x7FFFFFFFFFFFFFFF);
|
|
|
|
#endif
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!__PHYSFS_ui64FitsAddressSpace(len))
|
|
|
|
BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(len > maxlen, PHYSFS_ERR_INVALID_ARGUMENT, -1);
|
|
|
|
BAIL_IF_MACRO(fh->forReading, PHYSFS_ERR_OPEN_FOR_READING, -1);
|
|
|
|
BAIL_IF_MACRO(len == 0, ERRPASS, 0);
|
|
|
|
if (fh->buffer)
|
2010-08-21 08:47:58 +02:00
|
|
|
return doBufferedWrite(handle, buffer, len);
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
return fh->io->write(fh->io, buffer, len);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_write */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
int PHYSFS_eof(PHYSFS_File *handle)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
if (!fh->forReading) /* never EOF on files opened for write/append. */
|
2010-01-28 08:27:45 +01:00
|
|
|
return 0;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* can't be eof if buffer isn't empty */
|
|
|
|
if (fh->bufpos == fh->buffill)
|
|
|
|
{
|
|
|
|
/* check the Io. */
|
|
|
|
PHYSFS_Io *io = fh->io;
|
|
|
|
const PHYSFS_sint64 pos = io->tell(io);
|
|
|
|
const PHYSFS_sint64 len = io->length(io);
|
|
|
|
if ((pos < 0) || (len < 0))
|
|
|
|
return 0; /* beats me. */
|
|
|
|
return (pos >= len);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return 0;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_eof */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
PHYSFS_sint64 PHYSFS_tell(PHYSFS_File *handle)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
2010-08-30 09:01:57 +02:00
|
|
|
const PHYSFS_sint64 pos = fh->io->tell(fh->io);
|
|
|
|
const PHYSFS_sint64 retval = fh->forReading ?
|
|
|
|
(pos - fh->buffill) + fh->bufpos :
|
|
|
|
(pos + fh->buffill);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_tell */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
int PHYSFS_seek(PHYSFS_File *handle, PHYSFS_uint64 pos)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
|
2004-08-23 05:34:03 +02:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
if (fh->buffer && fh->forReading)
|
2004-08-23 05:34:03 +02:00
|
|
|
{
|
|
|
|
/* avoid throwing away our precious buffer if seeking within it. */
|
|
|
|
PHYSFS_sint64 offset = pos - PHYSFS_tell(handle);
|
|
|
|
if ( /* seeking within the already-buffered range? */
|
2004-09-26 15:00:59 +02:00
|
|
|
((offset >= 0) && (offset <= fh->buffill - fh->bufpos)) /* fwd */
|
|
|
|
|| ((offset < 0) && (-offset <= fh->bufpos)) /* backward */ )
|
2004-08-23 05:34:03 +02:00
|
|
|
{
|
2005-07-24 01:39:37 +02:00
|
|
|
fh->bufpos += (PHYSFS_uint32) offset;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1; /* successful seek */
|
2004-08-23 05:34:03 +02:00
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* we have to fall back to a 'raw' seek. */
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->buffill = fh->bufpos = 0;
|
2010-08-30 09:01:57 +02:00
|
|
|
return fh->io->seek(fh->io, pos);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_seek */
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
PHYSFS_sint64 PHYSFS_fileLength(PHYSFS_File *handle)
|
2001-07-09 06:15:35 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = ((FileHandle *) handle)->io;
|
|
|
|
return io->length(io);
|
2001-07-09 06:15:35 +02:00
|
|
|
} /* PHYSFS_filelength */
|
|
|
|
|
2002-07-23 09:48:08 +02:00
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
int PHYSFS_setBuffer(PHYSFS_File *handle, PHYSFS_uint64 _bufsize)
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
|
|
|
PHYSFS_uint32 bufsize;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
/* !!! FIXME: Unlocalized string. */
|
2010-08-21 21:07:13 +02:00
|
|
|
/* !!! FIXME: actually, why use 32 bits here? */
|
2012-03-20 20:38:12 +01:00
|
|
|
//BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
|
|
|
|
BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, PHYSFS_ERR_INVALID_ARGUMENT, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
bufsize = (PHYSFS_uint32) _bufsize;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_flush(handle), ERRPASS, 0);
|
2002-12-01 12:21:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For reads, we need to move the file pointer to where it would be
|
|
|
|
* if we weren't buffering, so that the next read will get the
|
|
|
|
* right chunk of stuff from the file. PHYSFS_flush() handles writes.
|
|
|
|
*/
|
2004-09-26 15:00:59 +02:00
|
|
|
if ((fh->forReading) && (fh->buffill != fh->bufpos))
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
|
|
|
PHYSFS_uint64 pos;
|
2010-08-30 09:01:57 +02:00
|
|
|
const PHYSFS_sint64 curpos = fh->io->tell(fh->io);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(curpos == -1, ERRPASS, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
pos = ((curpos - fh->buffill) + fh->bufpos);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!fh->io->seek(fh->io, pos), ERRPASS, 0);
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (bufsize == 0) /* delete existing buffer. */
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
if (fh->buffer)
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(fh->buffer);
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->buffer = NULL;
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
PHYSFS_uint8 *newbuf;
|
|
|
|
newbuf = (PHYSFS_uint8 *) allocator.Realloc(fh->buffer, bufsize);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!newbuf, PHYSFS_ERR_OUT_OF_MEMORY, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->buffer = newbuf;
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* else */
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->bufsize = bufsize;
|
|
|
|
fh->buffill = fh->bufpos = 0;
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* PHYSFS_setBuffer */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
int PHYSFS_flush(PHYSFS_File *handle)
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io;
|
2002-12-01 12:21:27 +01:00
|
|
|
PHYSFS_sint64 rc;
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
if ((fh->forReading) || (fh->bufpos == fh->buffill))
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1; /* open for read or buffer empty are successful no-ops. */
|
2002-12-01 12:21:27 +01:00
|
|
|
|
|
|
|
/* dump buffer to disk. */
|
2010-08-30 09:01:57 +02:00
|
|
|
io = fh->io;
|
|
|
|
rc = io->write(io, fh->buffer + fh->bufpos, fh->buffill - fh->bufpos);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(rc <= 0, ERRPASS, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->bufpos = fh->buffill = 0;
|
2010-08-30 09:01:57 +02:00
|
|
|
return io->flush(io);
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* PHYSFS_flush */
|
|
|
|
|
|
|
|
|
2010-02-15 20:02:36 +01:00
|
|
|
int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
char *fname;
|
|
|
|
size_t len;
|
|
|
|
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, -1);
|
|
|
|
BAIL_IF_MACRO(!stat, PHYSFS_ERR_INVALID_ARGUMENT, -1);
|
2010-02-15 20:02:36 +01:00
|
|
|
len = strlen(_fname) + 1;
|
|
|
|
fname = (char *) __PHYSFS_smallAlloc(len);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!fname, PHYSFS_ERR_OUT_OF_MEMORY, -1);
|
2010-02-15 20:02:36 +01:00
|
|
|
|
2010-08-22 01:10:42 +02:00
|
|
|
/* set some sane defaults... */
|
|
|
|
stat->filesize = -1;
|
|
|
|
stat->modtime = -1;
|
|
|
|
stat->createtime = -1;
|
|
|
|
stat->accesstime = -1;
|
|
|
|
stat->filetype = PHYSFS_FILETYPE_OTHER;
|
|
|
|
stat->readonly = 1; /* !!! FIXME */
|
2010-02-15 20:02:36 +01:00
|
|
|
|
|
|
|
if (sanitizePlatformIndependentPath(_fname, fname))
|
|
|
|
{
|
|
|
|
if (*fname == '\0')
|
|
|
|
{
|
|
|
|
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
|
|
|
|
stat->readonly = !writeDir; /* Writeable if we have a writeDir */
|
2010-08-22 01:10:42 +02:00
|
|
|
retval = 1;
|
2010-02-15 20:02:36 +01:00
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DirHandle *i;
|
|
|
|
int exists = 0;
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
for (i = searchPath; ((i != NULL) && (!exists)); i = i->next)
|
|
|
|
{
|
|
|
|
char *arcfname = fname;
|
|
|
|
exists = partOfMountPoint(i, arcfname);
|
|
|
|
if (exists)
|
2010-08-22 01:10:42 +02:00
|
|
|
{
|
|
|
|
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
|
|
|
|
stat->readonly = 1; /* !!! FIXME */
|
|
|
|
retval = 1;
|
|
|
|
} /* if */
|
2010-02-15 20:02:36 +01:00
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
|
|
|
{
|
2010-08-22 01:10:42 +02:00
|
|
|
/* !!! FIXME: this test is wrong and should be elsewhere. */
|
2010-02-15 20:02:36 +01:00
|
|
|
stat->readonly = !(writeDir &&
|
|
|
|
(strcmp(writeDir->dirName, i->dirName) == 0));
|
|
|
|
retval = i->funcs->stat(i->opaque, arcfname, &exists, stat);
|
|
|
|
} /* else if */
|
|
|
|
} /* for */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
__PHYSFS_smallFree(fname);
|
|
|
|
return retval;
|
|
|
|
} /* PHYSFS_stat */
|
|
|
|
|
|
|
|
|
2005-09-09 16:07:43 +02:00
|
|
|
int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
|
2004-09-23 08:45:36 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(initialized, PHYSFS_ERR_IS_INITIALIZED, 0);
|
2004-09-23 08:45:36 +02:00
|
|
|
externalAllocator = (a != NULL);
|
|
|
|
if (externalAllocator)
|
2004-09-26 15:00:59 +02:00
|
|
|
memcpy(&allocator, a, sizeof (PHYSFS_Allocator));
|
2004-09-23 08:45:36 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return 1;
|
2004-09-23 08:45:36 +02:00
|
|
|
} /* PHYSFS_setAllocator */
|
|
|
|
|
|
|
|
|
2009-03-28 21:15:03 +01:00
|
|
|
const PHYSFS_Allocator *PHYSFS_getAllocator(void)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, NULL);
|
2009-03-28 21:15:03 +01:00
|
|
|
return &allocator;
|
|
|
|
} /* PHYSFS_getAllocator */
|
|
|
|
|
|
|
|
|
2007-03-20 19:33:56 +01:00
|
|
|
static void *mallocAllocatorMalloc(PHYSFS_uint64 s)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!__PHYSFS_ui64FitsAddressSpace(s))
|
|
|
|
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2007-03-20 19:33:56 +01:00
|
|
|
#undef malloc
|
2010-01-28 08:27:45 +01:00
|
|
|
return malloc((size_t) s);
|
2007-03-20 19:33:56 +01:00
|
|
|
} /* mallocAllocatorMalloc */
|
|
|
|
|
|
|
|
|
|
|
|
static void *mallocAllocatorRealloc(void *ptr, PHYSFS_uint64 s)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
if (!__PHYSFS_ui64FitsAddressSpace(s))
|
|
|
|
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2007-03-20 19:33:56 +01:00
|
|
|
#undef realloc
|
2010-01-28 08:27:45 +01:00
|
|
|
return realloc(ptr, (size_t) s);
|
2007-03-20 19:33:56 +01:00
|
|
|
} /* mallocAllocatorRealloc */
|
|
|
|
|
|
|
|
|
|
|
|
static void mallocAllocatorFree(void *ptr)
|
|
|
|
{
|
|
|
|
#undef free
|
|
|
|
free(ptr);
|
|
|
|
} /* mallocAllocatorFree */
|
|
|
|
|
|
|
|
|
2004-09-23 08:45:36 +02:00
|
|
|
static void setDefaultAllocator(void)
|
|
|
|
{
|
|
|
|
assert(!externalAllocator);
|
2007-03-20 19:33:56 +01:00
|
|
|
if (!__PHYSFS_platformSetDefaultAllocator(&allocator))
|
|
|
|
{
|
|
|
|
allocator.Init = NULL;
|
|
|
|
allocator.Deinit = NULL;
|
|
|
|
allocator.Malloc = mallocAllocatorMalloc;
|
|
|
|
allocator.Realloc = mallocAllocatorRealloc;
|
|
|
|
allocator.Free = mallocAllocatorFree;
|
|
|
|
} /* if */
|
2004-09-23 08:45:36 +02:00
|
|
|
} /* setDefaultAllocator */
|
|
|
|
|
2007-03-24 04:54:58 +01:00
|
|
|
|
|
|
|
void *__PHYSFS_initSmallAlloc(void *ptr, PHYSFS_uint64 len)
|
|
|
|
{
|
2012-03-10 04:27:51 +01:00
|
|
|
void *useHeap = ((ptr == NULL) ? ((void *) 1) : ((void *) 0));
|
2007-03-24 04:54:58 +01:00
|
|
|
if (useHeap) /* too large for stack allocation or alloca() failed. */
|
2012-03-10 04:27:51 +01:00
|
|
|
ptr = allocator.Malloc(len+sizeof (void *));
|
2007-03-24 04:54:58 +01:00
|
|
|
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
2012-03-10 04:27:51 +01:00
|
|
|
void **retval = (void **) ptr;
|
2007-03-24 04:54:58 +01:00
|
|
|
/*printf("%s alloc'd (%d) bytes at (%p).\n",
|
|
|
|
useHeap ? "heap" : "stack", (int) len, ptr);*/
|
|
|
|
*retval = useHeap;
|
2012-03-10 04:27:51 +01:00
|
|
|
return retval + 1;
|
2007-03-24 04:54:58 +01:00
|
|
|
} /* if */
|
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return NULL; /* allocation failed. */
|
2007-03-24 04:54:58 +01:00
|
|
|
} /* __PHYSFS_initSmallAlloc */
|
|
|
|
|
|
|
|
|
|
|
|
void __PHYSFS_smallFree(void *ptr)
|
|
|
|
{
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
2012-03-10 04:27:51 +01:00
|
|
|
void **block = ((void **) ptr) - 1;
|
|
|
|
const int useHeap = (*block != 0);
|
2007-03-24 04:54:58 +01:00
|
|
|
if (useHeap)
|
|
|
|
allocator.Free(block);
|
|
|
|
/*printf("%s free'd (%p).\n", useHeap ? "heap" : "stack", block);*/
|
|
|
|
} /* if */
|
|
|
|
} /* __PHYSFS_smallFree */
|
|
|
|
|
2012-03-09 10:50:27 +01:00
|
|
|
|
|
|
|
int __PHYSFS_readAll(PHYSFS_Io *io, void *buf, const PHYSFS_uint64 len)
|
|
|
|
{
|
|
|
|
return (io->read(io, buf, len) == len);
|
|
|
|
} /* __PHYSFS_readAll */
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
/* end of physfs.c ... */
|
2001-07-05 10:18:39 +02:00
|
|
|
|