2001-07-05 10:18:39 +02:00
|
|
|
/**
|
|
|
|
* PhysicsFS; a portable, flexible file i/o abstraction.
|
|
|
|
*
|
|
|
|
* Documentation is in physfs.h. It's verbose, honest. :)
|
|
|
|
*
|
|
|
|
* Please see the file LICENSE in the source's root directory.
|
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
2002-05-10 11:25:25 +02:00
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "physfs.h"
|
|
|
|
|
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__
|
|
|
|
{
|
|
|
|
void *opaque; /* Instance data unique to the archiver for this file. */
|
|
|
|
PHYSFS_uint8 forReading; /* Non-zero if reading, zero if write/append */
|
|
|
|
const DirHandle *dirHandle; /* Archiver instance that created this */
|
|
|
|
const PHYSFS_Archiver *funcs; /* Ptr to archiver info for this handle. */
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
typedef struct __PHYSFS_ERRMSGTYPE__
|
|
|
|
{
|
2002-04-03 09:40:27 +02:00
|
|
|
PHYSFS_uint64 tid;
|
2001-07-05 10:18:39 +02:00
|
|
|
int errorAvailable;
|
|
|
|
char errorString[80];
|
|
|
|
struct __PHYSFS_ERRMSGTYPE__ *next;
|
|
|
|
} ErrMsg;
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
/* The various i/o drivers... */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
|
|
|
#if (defined PHYSFS_SUPPORTS_ZIP)
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_ZIP;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_ZIP;
|
2001-07-06 03:27:14 +02:00
|
|
|
#endif
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2006-04-11 16:33:48 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_LZMA)
|
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_LZMA;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_LZMA;
|
|
|
|
#endif
|
|
|
|
|
2001-07-08 12:58:10 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_GRP)
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_GRP;
|
2001-07-08 12:58:10 +02:00
|
|
|
#endif
|
|
|
|
|
2003-07-21 02:27:27 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_QPAK)
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_QPAK;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_QPAK;
|
2003-07-21 02:27:27 +02:00
|
|
|
#endif
|
|
|
|
|
2003-03-30 20:59:54 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_HOG)
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_HOG;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_HOG;
|
2003-03-30 20:59:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined PHYSFS_SUPPORTS_MVL)
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MVL;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_MVL;
|
2003-03-30 20:59:54 +02:00
|
|
|
#endif
|
|
|
|
|
2003-12-15 05:01:18 +01:00
|
|
|
#if (defined PHYSFS_SUPPORTS_WAD)
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_WAD;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_WAD;
|
2003-12-15 05:01:18 +01:00
|
|
|
#endif
|
|
|
|
|
2004-04-09 08:36:09 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_MIX)
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_MIX;
|
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_MIX;
|
2004-04-09 08:36:09 +02:00
|
|
|
#endif
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
extern const PHYSFS_Archiver __PHYSFS_Archiver_DIR;
|
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
|
|
|
{
|
|
|
|
#if (defined PHYSFS_SUPPORTS_ZIP)
|
2001-07-06 03:27:14 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_ZIP,
|
2001-07-05 10:18:39 +02:00
|
|
|
#endif
|
|
|
|
|
2006-04-11 16:33:48 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_LZMA)
|
|
|
|
&__PHYSFS_ArchiveInfo_LZMA,
|
|
|
|
#endif
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_GRP)
|
|
|
|
&__PHYSFS_ArchiveInfo_GRP,
|
|
|
|
#endif
|
|
|
|
|
2003-07-21 02:27:27 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_QPAK)
|
|
|
|
&__PHYSFS_ArchiveInfo_QPAK,
|
|
|
|
#endif
|
|
|
|
|
2003-03-30 20:59:54 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_HOG)
|
|
|
|
&__PHYSFS_ArchiveInfo_HOG,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined PHYSFS_SUPPORTS_MVL)
|
|
|
|
&__PHYSFS_ArchiveInfo_MVL,
|
|
|
|
#endif
|
|
|
|
|
2003-12-15 05:01:18 +01:00
|
|
|
#if (defined PHYSFS_SUPPORTS_WAD)
|
|
|
|
&__PHYSFS_ArchiveInfo_WAD,
|
|
|
|
#endif
|
|
|
|
|
2004-04-09 08:36:09 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_MIX)
|
|
|
|
&__PHYSFS_ArchiveInfo_MIX,
|
|
|
|
#endif
|
2003-12-15 05:01:18 +01:00
|
|
|
|
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
|
|
|
{
|
|
|
|
#if (defined PHYSFS_SUPPORTS_ZIP)
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_ZIP,
|
2001-07-06 10:47:23 +02:00
|
|
|
#endif
|
|
|
|
|
2006-04-11 16:33:48 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_LZMA)
|
|
|
|
&__PHYSFS_Archiver_LZMA,
|
|
|
|
#endif
|
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_GRP)
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_GRP,
|
2001-07-08 07:27:05 +02:00
|
|
|
#endif
|
|
|
|
|
2003-07-21 02:27:27 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_QPAK)
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_QPAK,
|
2003-07-21 02:27:27 +02:00
|
|
|
#endif
|
|
|
|
|
2003-03-30 20:59:54 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_HOG)
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_HOG,
|
2003-03-30 20:59:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (defined PHYSFS_SUPPORTS_MVL)
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_MVL,
|
2003-03-30 20:59:54 +02:00
|
|
|
#endif
|
|
|
|
|
2003-12-15 05:01:18 +01:00
|
|
|
#if (defined PHYSFS_SUPPORTS_WAD)
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_WAD,
|
2003-12-15 05:01:18 +01:00
|
|
|
#endif
|
|
|
|
|
2004-04-09 08:36:09 +02:00
|
|
|
#if (defined PHYSFS_SUPPORTS_MIX)
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_MIX,
|
2004-04-09 08:36:09 +02:00
|
|
|
#endif
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
&__PHYSFS_Archiver_DIR,
|
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;
|
|
|
|
static ErrMsg *errorMessages = 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;
|
|
|
|
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
|
|
|
|
|
|
|
/* functions ... */
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char **list;
|
|
|
|
PHYSFS_uint32 size;
|
|
|
|
const char *errorstr;
|
|
|
|
} EnumStringListCallbackData;
|
|
|
|
|
|
|
|
static void enumStringListCallback(void *data, const char *str)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
char *newstr;
|
|
|
|
EnumStringListCallbackData *pecd = (EnumStringListCallbackData *) data;
|
|
|
|
|
|
|
|
if (pecd->errorstr)
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
pecd->errorstr = ERR_OUT_OF_MEMORY;
|
|
|
|
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 *));
|
2004-09-29 08:09:29 +02:00
|
|
|
BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
func(enumStringListCallback, &ecd);
|
|
|
|
BAIL_IF_MACRO(ecd.errorstr != NULL, ecd.errorstr, NULL);
|
|
|
|
ecd.list[ecd.size] = NULL;
|
|
|
|
return(ecd.list);
|
|
|
|
} /* 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 */
|
|
|
|
swapfn(a, i, hi-1);
|
|
|
|
__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 */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
static ErrMsg *findErrorForCurrentThread(void)
|
|
|
|
{
|
|
|
|
ErrMsg *i;
|
2002-04-03 09:40:27 +02:00
|
|
|
PHYSFS_uint64 tid;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
2002-04-08 15:35:29 +02:00
|
|
|
__PHYSFS_platformGrabMutex(errorLock);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
if (errorMessages != NULL)
|
|
|
|
{
|
2002-04-03 09:40:27 +02:00
|
|
|
tid = __PHYSFS_platformGetThreadID();
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
for (i = errorMessages; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
if (i->tid == tid)
|
2002-03-30 17:44:09 +01:00
|
|
|
{
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformReleaseMutex(errorLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
return(i);
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* if */
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* for */
|
|
|
|
} /* if */
|
2002-04-08 15:35:29 +02:00
|
|
|
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
2002-04-08 15:35:29 +02:00
|
|
|
__PHYSFS_platformReleaseMutex(errorLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
return(NULL); /* no error available. */
|
|
|
|
} /* findErrorForCurrentThread */
|
|
|
|
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
void __PHYSFS_setError(const char *str)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
ErrMsg *err;
|
|
|
|
|
|
|
|
if (str == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
err = findErrorForCurrentThread();
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
if (err == NULL)
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
err = (ErrMsg *) allocator.Malloc(sizeof (ErrMsg));
|
2001-07-05 10:18:39 +02:00
|
|
|
if (err == NULL)
|
|
|
|
return; /* uhh...? */
|
|
|
|
|
2001-08-01 12:18:56 +02:00
|
|
|
memset((void *) err, '\0', sizeof (ErrMsg));
|
2002-04-03 09:40:27 +02:00
|
|
|
err->tid = __PHYSFS_platformGetThreadID();
|
2002-03-30 17:44:09 +01:00
|
|
|
|
2002-06-08 10:50:00 +02:00
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformGrabMutex(errorLock);
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
err->next = errorMessages;
|
|
|
|
errorMessages = err;
|
2002-06-08 10:50:00 +02:00
|
|
|
|
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformReleaseMutex(errorLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
err->errorAvailable = 1;
|
|
|
|
strncpy(err->errorString, str, sizeof (err->errorString));
|
|
|
|
err->errorString[sizeof (err->errorString) - 1] = '\0';
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* __PHYSFS_setError */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
const char *PHYSFS_getLastError(void)
|
|
|
|
{
|
|
|
|
ErrMsg *err = findErrorForCurrentThread();
|
|
|
|
|
|
|
|
if ((err == NULL) || (!err->errorAvailable))
|
|
|
|
return(NULL);
|
|
|
|
|
|
|
|
err->errorAvailable = 0;
|
|
|
|
return(err->errorString);
|
|
|
|
} /* PHYSFS_getLastError */
|
|
|
|
|
|
|
|
|
|
|
|
/* MAKE SURE that errorLock is held before calling this! */
|
2001-07-07 05:52:43 +02:00
|
|
|
static void freeErrorMessages(void)
|
|
|
|
{
|
|
|
|
ErrMsg *i;
|
|
|
|
ErrMsg *next;
|
|
|
|
|
|
|
|
for (i = errorMessages; i != NULL; i = next)
|
|
|
|
{
|
2001-07-09 02:51:46 +02:00
|
|
|
next = i->next;
|
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
|
|
|
|
|
|
|
errorMessages = NULL;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* freeErrorMessages */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
void PHYSFS_getLinkedVersion(PHYSFS_Version *ver)
|
|
|
|
{
|
|
|
|
if (ver != NULL)
|
|
|
|
{
|
|
|
|
ver->major = PHYSFS_VER_MAJOR;
|
|
|
|
ver->minor = PHYSFS_VER_MINOR;
|
|
|
|
ver->patch = PHYSFS_VER_PATCH;
|
|
|
|
} /* if */
|
|
|
|
} /* PHYSFS_getLinkedVersion */
|
|
|
|
|
|
|
|
|
2002-07-26 08:18:30 +02:00
|
|
|
static const char *find_filename_extension(const char *fname)
|
|
|
|
{
|
|
|
|
const char *retval = strchr(fname, '.');
|
|
|
|
const char *p = retval;
|
|
|
|
|
|
|
|
while (p != NULL)
|
|
|
|
{
|
|
|
|
p = strchr(p + 1, '.');
|
|
|
|
if (p != NULL)
|
|
|
|
retval = p;
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
if (retval != NULL)
|
|
|
|
retval++; /* skip '.' */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* find_filename_extension */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static DirHandle *tryOpenDir(const PHYSFS_Archiver *funcs,
|
|
|
|
const char *d, int forWriting)
|
2004-09-26 02:25:04 +02:00
|
|
|
{
|
|
|
|
DirHandle *retval = NULL;
|
2004-09-26 15:00:59 +02:00
|
|
|
if (funcs->isArchive(d, forWriting))
|
2004-09-26 02:25:04 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
void *opaque = funcs->openArchive(d, forWriting);
|
2004-09-26 02:25:04 +02:00
|
|
|
if (opaque != NULL)
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = (DirHandle *) allocator.Malloc(sizeof (DirHandle));
|
2004-09-26 02:25:04 +02:00
|
|
|
if (retval == NULL)
|
2004-09-26 15:00:59 +02:00
|
|
|
funcs->dirClose(opaque);
|
2004-09-26 02:25:04 +02:00
|
|
|
else
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
memset(retval, '\0', sizeof (DirHandle));
|
2005-03-13 10:09:26 +01:00
|
|
|
retval->mountPoint = NULL;
|
2004-09-26 15:00:59 +02:00
|
|
|
retval->funcs = funcs;
|
2004-09-26 02:25:04 +02:00
|
|
|
retval->opaque = opaque;
|
|
|
|
} /* else */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* tryOpenDir */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
static DirHandle *openDirectory(const char *d, int forWriting)
|
|
|
|
{
|
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
|
|
|
|
2001-07-23 06:46:42 +02:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformExists(d), ERR_NO_SUCH_FILE, NULL);
|
|
|
|
|
2002-07-26 08:18:30 +02:00
|
|
|
ext = find_filename_extension(d);
|
|
|
|
if (ext != NULL)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2002-07-26 08:18:30 +02:00
|
|
|
/* Look for archivers with matching file extensions first... */
|
2004-09-26 15:00:59 +02:00
|
|
|
for (i = archivers; (*i != NULL) && (retval == NULL); i++)
|
2002-07-26 08:18:30 +02:00
|
|
|
{
|
|
|
|
if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) == 0)
|
2004-09-26 02:25:04 +02:00
|
|
|
retval = tryOpenDir(*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
|
|
|
{
|
|
|
|
if (__PHYSFS_platformStricmp(ext, (*i)->info->extension) != 0)
|
2004-09-26 02:25:04 +02:00
|
|
|
retval = tryOpenDir(*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++)
|
2004-09-26 02:25:04 +02:00
|
|
|
retval = tryOpenDir(*i, d, forWriting);
|
2002-07-26 08:18:30 +02:00
|
|
|
} /* else */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_UNSUPPORTED_ARCHIVE, NULL);
|
|
|
|
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. */
|
|
|
|
BAIL_MACRO(ERR_INSECURE_FNAME, 0);
|
|
|
|
|
|
|
|
if (ch == '/') /* path separator. */
|
|
|
|
{
|
|
|
|
*dst = '\0'; /* "." and ".." are illegal pathnames. */
|
|
|
|
if ((strcmp(prev, ".") == 0) || (strcmp(prev, "..") == 0))
|
|
|
|
BAIL_MACRO(ERR_INSECURE_FNAME, 0);
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
return(1);
|
|
|
|
} /* 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)
|
|
|
|
return(0);
|
|
|
|
else if (*fname == '\0')
|
|
|
|
return(1);
|
|
|
|
|
|
|
|
len = strlen(fname);
|
|
|
|
mntpntlen = strlen(h->mountPoint);
|
|
|
|
if (len > mntpntlen) /* can't be a subset of mountpoint. */
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
/* if true, must be not a match or a complete match, but not a subset. */
|
|
|
|
if ((len + 1) == mntpntlen)
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
rc = strncmp(fname, h->mountPoint, len); /* !!! FIXME: case insensitive? */
|
|
|
|
if (rc != 0)
|
|
|
|
return(0); /* not a match. */
|
|
|
|
|
|
|
|
/* make sure /a/b matches /a/b/ and not /a/bc ... */
|
|
|
|
return(h->mountPoint[len] == '/');
|
|
|
|
} /* partOfMountPoint */
|
|
|
|
|
|
|
|
|
2005-03-13 10:09:26 +01:00
|
|
|
static DirHandle *createDirHandle(const char *newDir,
|
|
|
|
const char *mountPoint,
|
|
|
|
int forWriting)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
|
|
|
DirHandle *dirHandle = NULL;
|
2005-03-13 13:03:05 +01:00
|
|
|
|
2005-03-13 10:09:26 +01:00
|
|
|
GOTO_IF_MACRO(!newDir, ERR_INVALID_ARGUMENT, badDirHandle);
|
2005-03-13 13:03:05 +01:00
|
|
|
if (mountPoint != NULL)
|
|
|
|
{
|
|
|
|
char *mntpnt = (char *) alloca(strlen(mountPoint) + 1);
|
|
|
|
GOTO_IF_MACRO(!mntpnt, ERR_OUT_OF_MEMORY, badDirHandle);
|
|
|
|
if (!sanitizePlatformIndependentPath(mountPoint, mntpnt))
|
|
|
|
goto badDirHandle;
|
|
|
|
mountPoint = mntpnt; /* sanitized version. */
|
|
|
|
} /* if */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
dirHandle = openDirectory(newDir, forWriting);
|
2005-03-13 10:09:26 +01:00
|
|
|
GOTO_IF_MACRO(!dirHandle, NULL, badDirHandle);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
dirHandle->dirName = (char *) allocator.Malloc(strlen(newDir) + 1);
|
2005-03-13 10:09:26 +01:00
|
|
|
GOTO_IF_MACRO(!dirHandle->dirName, ERR_OUT_OF_MEMORY, badDirHandle);
|
|
|
|
strcpy(dirHandle->dirName, newDir);
|
|
|
|
|
|
|
|
if ((mountPoint != NULL) && (*mountPoint != '\0'))
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
dirHandle->mountPoint = (char *)allocator.Malloc(strlen(mountPoint)+2);
|
2005-03-13 10:09:26 +01:00
|
|
|
GOTO_IF_MACRO(!dirHandle->mountPoint, ERR_OUT_OF_MEMORY, badDirHandle);
|
|
|
|
strcpy(dirHandle->mountPoint, mountPoint);
|
|
|
|
strcat(dirHandle->mountPoint, "/");
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(dirHandle);
|
|
|
|
|
|
|
|
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
|
|
|
|
2005-03-13 10:09:26 +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)
|
2001-07-07 05:52:43 +02:00
|
|
|
return(1);
|
|
|
|
|
|
|
|
for (i = openList; i != NULL; i = i->next)
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO(i->dirHandle == dh, 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);
|
2001-07-07 05:52:43 +02:00
|
|
|
return(1);
|
2004-09-26 15:00:59 +02:00
|
|
|
} /* freeDirHandle */
|
2001-07-07 05:52:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
static char *calculateUserDir(void)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
|
|
|
char *retval = NULL;
|
|
|
|
const char *str = NULL;
|
|
|
|
|
|
|
|
str = __PHYSFS_platformGetUserDir();
|
|
|
|
if (str != NULL)
|
2001-07-07 05:52:43 +02:00
|
|
|
retval = (char *) str;
|
2001-07-06 10:47:23 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
|
|
|
const char *uname = __PHYSFS_platformGetUserName();
|
|
|
|
|
|
|
|
str = (uname != NULL) ? uname : "default";
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = (char *) allocator.Malloc(strlen(baseDir) + strlen(str) +
|
|
|
|
strlen(dirsep) + 6);
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
if (retval == NULL)
|
|
|
|
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
|
|
|
|
else
|
2002-04-02 15:39:44 +02:00
|
|
|
sprintf(retval, "%susers%s%s", baseDir, dirsep, str);
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
if (uname != NULL)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free((void *) uname);
|
2001-07-06 10:47:23 +02:00
|
|
|
} /* else */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* calculateUserDir */
|
|
|
|
|
|
|
|
|
2001-07-08 15:57:28 +02:00
|
|
|
static int appendDirSep(char **dir)
|
|
|
|
{
|
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
if (strcmp((*dir + strlen(*dir)) - strlen(dirsep), dirsep) == 0)
|
|
|
|
return(1);
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
ptr = (char *) allocator.Realloc(*dir, strlen(*dir) + strlen(dirsep) + 1);
|
2001-07-08 15:57:28 +02:00
|
|
|
if (!ptr)
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(*dir);
|
2001-07-08 15:57:28 +02:00
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
strcat(ptr, dirsep);
|
|
|
|
*dir = ptr;
|
|
|
|
return(1);
|
|
|
|
} /* appendDirSep */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
static char *calculateBaseDir(const char *argv0)
|
|
|
|
{
|
2001-07-08 15:57:28 +02:00
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
|
|
|
char *retval;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if the platform driver wants to handle this for us...
|
|
|
|
*/
|
|
|
|
retval = __PHYSFS_platformCalcBaseDir(argv0);
|
|
|
|
if (retval != NULL)
|
|
|
|
return(retval);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine if there's a path on argv0. If there is, that's the base dir.
|
|
|
|
*/
|
|
|
|
ptr = strstr(argv0, dirsep);
|
|
|
|
if (ptr != NULL)
|
|
|
|
{
|
|
|
|
char *p = ptr;
|
|
|
|
size_t size;
|
|
|
|
while (p != NULL)
|
|
|
|
{
|
|
|
|
ptr = p;
|
|
|
|
p = strstr(p + 1, dirsep);
|
|
|
|
} /* while */
|
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
size = (size_t) (ptr - argv0);
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = (char *) allocator.Malloc(size + 1);
|
2001-07-08 15:57:28 +02:00
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
memcpy(retval, argv0, size);
|
|
|
|
retval[size] = '\0';
|
|
|
|
return(retval);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Last ditch effort: it's the current working directory. (*shrug*)
|
|
|
|
*/
|
2001-09-15 00:59:53 +02:00
|
|
|
retval = __PHYSFS_platformCurrentDir();
|
2004-09-26 15:00:59 +02:00
|
|
|
if (retval != NULL)
|
|
|
|
return(retval);
|
2001-09-15 00:59:53 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, current directory doesn't exist, use the root directory.
|
|
|
|
* Not a good alternative, but it only happens if the current
|
|
|
|
* directory was deleted from under the program.
|
|
|
|
*/
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = (char *) allocator.Malloc(strlen(dirsep) + 1);
|
2001-09-15 00:59:53 +02:00
|
|
|
strcpy(retval, dirsep);
|
|
|
|
return(retval);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* calculateBaseDir */
|
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
static int initializeMutexes(void)
|
|
|
|
{
|
|
|
|
errorLock = __PHYSFS_platformCreateMutex();
|
|
|
|
if (errorLock == NULL)
|
|
|
|
goto initializeMutexes_failed;
|
|
|
|
|
|
|
|
stateLock = __PHYSFS_platformCreateMutex();
|
|
|
|
if (stateLock == NULL)
|
|
|
|
goto initializeMutexes_failed;
|
|
|
|
|
|
|
|
return(1); /* success. */
|
|
|
|
|
|
|
|
initializeMutexes_failed:
|
|
|
|
if (errorLock != NULL)
|
|
|
|
__PHYSFS_platformDestroyMutex(errorLock);
|
|
|
|
|
|
|
|
if (stateLock != NULL)
|
|
|
|
__PHYSFS_platformDestroyMutex(stateLock);
|
|
|
|
|
|
|
|
errorLock = stateLock = NULL;
|
|
|
|
return(0); /* failed. */
|
|
|
|
} /* initializeMutexes */
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2001-07-16 16:36:02 +02:00
|
|
|
char *ptr;
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
|
2004-09-23 08:45:36 +02:00
|
|
|
|
|
|
|
if (!externalAllocator)
|
|
|
|
setDefaultAllocator();
|
|
|
|
|
2005-09-09 16:07:43 +02:00
|
|
|
if (allocator.Init != NULL)
|
|
|
|
BAIL_IF_MACRO(!allocator.Init(), NULL, 0);
|
2004-09-26 02:25:04 +02:00
|
|
|
|
2002-03-24 07:36:48 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformInit(), NULL, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(!initializeMutexes(), NULL, 0);
|
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
baseDir = calculateBaseDir(argv0);
|
2001-07-07 05:52:43 +02:00
|
|
|
BAIL_IF_MACRO(baseDir == NULL, NULL, 0);
|
2002-04-06 07:18:49 +02:00
|
|
|
|
2001-07-16 16:36:02 +02:00
|
|
|
ptr = __PHYSFS_platformRealPath(baseDir);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(baseDir);
|
2001-07-16 16:36:02 +02:00
|
|
|
BAIL_IF_MACRO(ptr == NULL, NULL, 0);
|
|
|
|
baseDir = ptr;
|
|
|
|
|
2001-07-08 15:57:28 +02:00
|
|
|
BAIL_IF_MACRO(!appendDirSep(&baseDir), NULL, 0);
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
userDir = calculateUserDir();
|
2001-07-16 16:36:02 +02:00
|
|
|
if (userDir != NULL)
|
|
|
|
{
|
|
|
|
ptr = __PHYSFS_platformRealPath(userDir);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(userDir);
|
2001-07-16 16:36:02 +02:00
|
|
|
userDir = ptr;
|
|
|
|
} /* if */
|
|
|
|
|
2001-07-08 15:57:28 +02:00
|
|
|
if ((userDir == NULL) || (!appendDirSep(&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;
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
initialized = 1;
|
2002-04-04 19:58:02 +02:00
|
|
|
|
|
|
|
/* This makes sure that the error subsystem is initialized. */
|
|
|
|
__PHYSFS_setError(PHYSFS_getLastError());
|
2002-08-20 02:59:03 +02:00
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
return(1);
|
|
|
|
} /* PHYSFS_init */
|
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* MAKE SURE you hold stateLock before calling this! */
|
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)
|
|
|
|
{
|
|
|
|
next = i->next;
|
2004-09-26 15:00:59 +02:00
|
|
|
if (!i->funcs->fileClose(i->opaque))
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
|
|
|
*list = i;
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(i);
|
2001-07-06 10:47:23 +02:00
|
|
|
} /* for */
|
|
|
|
|
|
|
|
*list = NULL;
|
2001-07-07 05:52:43 +02:00
|
|
|
return(1);
|
|
|
|
} /* closeFileHandleList */
|
2001-07-06 10:47:23 +02:00
|
|
|
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
/* MAKE SURE you hold the stateLock before calling this! */
|
2001-07-05 10:18:39 +02:00
|
|
|
static void freeSearchPath(void)
|
|
|
|
{
|
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
|
|
|
{
|
|
|
|
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
|
2002-03-24 07:36:48 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformDeinit(), NULL, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2001-07-06 10:47:23 +02:00
|
|
|
closeFileHandleList(&openWriteList);
|
2001-07-07 05:52:43 +02:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_setWriteDir(NULL), ERR_FILES_STILL_OPEN, 0);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
freeSearchPath();
|
|
|
|
freeErrorMessages();
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
if (baseDir != NULL)
|
2001-07-06 10:47:23 +02:00
|
|
|
{
|
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
|
|
|
|
|
|
|
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;
|
2001-07-05 10:18:39 +02:00
|
|
|
return(1);
|
|
|
|
} /* PHYSFS_deinit */
|
|
|
|
|
|
|
|
|
|
|
|
const PHYSFS_ArchiveInfo **PHYSFS_supportedArchiveTypes(void)
|
|
|
|
{
|
|
|
|
return(supported_types);
|
|
|
|
} /* PHYSFS_supportedArchiveTypes */
|
|
|
|
|
|
|
|
|
|
|
|
void PHYSFS_freeList(void *list)
|
|
|
|
{
|
|
|
|
void **i;
|
|
|
|
for (i = (void **) list; *i != NULL; i++)
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(*i);
|
2001-07-05 10:18:39 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(list);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_freeList */
|
|
|
|
|
|
|
|
|
|
|
|
const char *PHYSFS_getDirSeparator(void)
|
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
return(__PHYSFS_platformDirSeparator);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getDirSeparator */
|
|
|
|
|
|
|
|
|
|
|
|
char **PHYSFS_getCdRomDirs(void)
|
|
|
|
{
|
2004-09-29 08:09:29 +02: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 */
|
|
|
|
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
const char *PHYSFS_getBaseDir(void)
|
|
|
|
{
|
|
|
|
return(baseDir); /* this is calculated in PHYSFS_init()... */
|
|
|
|
} /* PHYSFS_getBaseDir */
|
|
|
|
|
|
|
|
|
|
|
|
const char *PHYSFS_getUserDir(void)
|
|
|
|
{
|
2001-07-06 10:47:23 +02:00
|
|
|
return(userDir); /* this is calculated in PHYSFS_init()... */
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getUserDir */
|
|
|
|
|
|
|
|
|
|
|
|
const char *PHYSFS_getWriteDir(void)
|
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
const char *retval = NULL;
|
|
|
|
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
if (writeDir != NULL)
|
|
|
|
retval = writeDir->dirName;
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
return(retval);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_getWriteDir */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_setWriteDir(const char *newDir)
|
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
int retval = 1;
|
|
|
|
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
|
2001-07-05 10:18:39 +02:00
|
|
|
if (writeDir != NULL)
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO_MUTEX(!freeDirHandle(writeDir, openWriteList), NULL,
|
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)
|
|
|
|
{
|
2005-03-13 10:09:26 +01:00
|
|
|
writeDir = createDirHandle(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);
|
|
|
|
|
|
|
|
return(retval);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_setWriteDir */
|
|
|
|
|
|
|
|
|
2005-03-13 10:09:26 +01:00
|
|
|
int PHYSFS_mount(const char *newDir, 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-03-13 10:09:26 +01:00
|
|
|
BAIL_IF_MACRO(newDir == NULL, ERR_INVALID_ARGUMENT, 0);
|
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
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
/* already in search path? */
|
|
|
|
BAIL_IF_MACRO_MUTEX(strcmp(newDir, i->dirName)==0, NULL, stateLock, 1);
|
2001-07-23 11:23:17 +02:00
|
|
|
prev = i;
|
2002-03-30 17:44:09 +01:00
|
|
|
} /* for */
|
2001-07-23 11:23:17 +02:00
|
|
|
|
2005-03-13 10:09:26 +01:00
|
|
|
dh = createDirHandle(newDir, mountPoint, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO_MUTEX(dh == NULL, NULL, 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);
|
2001-07-05 10:18:39 +02:00
|
|
|
return(1);
|
2005-03-13 10:09:26 +01:00
|
|
|
} /* PHYSFS_mount */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_addToSearchPath(const char *newDir, int appendToPath)
|
|
|
|
{
|
2005-03-13 13:03:05 +01:00
|
|
|
return(PHYSFS_mount(newDir, NULL, appendToPath));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_addToSearchPath */
|
|
|
|
|
|
|
|
|
|
|
|
int PHYSFS_removeFromSearchPath(const char *oldDir)
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
|
|
|
DirHandle *prev = NULL;
|
|
|
|
DirHandle *next = NULL;
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(oldDir == NULL, ERR_INVALID_ARGUMENT, 0);
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2001-07-05 10:18:39 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
|
|
|
if (strcmp(i->dirName, oldDir) == 0)
|
|
|
|
{
|
2001-07-06 10:47:23 +02:00
|
|
|
next = i->next;
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO_MUTEX(!freeDirHandle(i, openReadList), NULL,
|
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
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_MACRO_MUTEX(NULL, stateLock, 1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* if */
|
|
|
|
prev = i;
|
|
|
|
} /* for */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_MACRO_MUTEX(ERR_NOT_IN_SEARCH_PATH, stateLock, 0);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_removeFromSearchPath */
|
|
|
|
|
|
|
|
|
|
|
|
char **PHYSFS_getSearchPath(void)
|
|
|
|
{
|
2004-09-29 08:09:29 +02:00
|
|
|
return(doEnumStringList(PHYSFS_getSearchPathCallback));
|
|
|
|
} /* 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);
|
|
|
|
return(retval);
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
|
|
|
BAIL_MACRO(ERR_NOT_IN_SEARCH_PATH, NULL);
|
|
|
|
} /* 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
|
|
|
|
|
|
|
|
2001-09-26 03:44:41 +02:00
|
|
|
int PHYSFS_setSaneConfig(const char *organization, const char *appName,
|
|
|
|
const char *archiveExt, int includeCdRoms,
|
|
|
|
int archivesFirst)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
const char *basedir = PHYSFS_getBaseDir();
|
|
|
|
const char *userdir = PHYSFS_getUserDir();
|
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
|
|
|
char *str;
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(!initialized, ERR_NOT_INITIALIZED, 0);
|
|
|
|
|
2005-08-20 06:46:25 +02:00
|
|
|
/* set write dir... */
|
2005-03-14 12:49:30 +01:00
|
|
|
str = (char *) allocator.Malloc(
|
|
|
|
strlen(userdir) + (strlen(organization) * 2) +
|
|
|
|
(strlen(appName) * 2) + (strlen(dirsep) * 3) + 2);
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, 0);
|
2001-09-26 03:44:41 +02:00
|
|
|
sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
|
|
|
if (!PHYSFS_setWriteDir(str))
|
|
|
|
{
|
2002-03-15 15:53:23 +01:00
|
|
|
int no_write = 0;
|
|
|
|
sprintf(str, ".%s/%s", organization, appName);
|
2001-09-26 05:08:57 +02:00
|
|
|
if ( (PHYSFS_setWriteDir(userdir)) &&
|
2002-03-15 15:53:23 +01:00
|
|
|
(PHYSFS_mkdir(str)) )
|
2001-09-26 05:08:57 +02:00
|
|
|
{
|
2002-03-15 15:53:23 +01:00
|
|
|
sprintf(str, "%s.%s%s%s", userdir, organization, dirsep, appName);
|
|
|
|
if (!PHYSFS_setWriteDir(str))
|
|
|
|
no_write = 1;
|
2001-09-26 05:08:57 +02:00
|
|
|
} /* if */
|
|
|
|
else
|
2001-07-16 16:36:02 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
no_write = 1;
|
2001-09-26 05:08:57 +02:00
|
|
|
} /* else */
|
2002-03-15 15:53:23 +01:00
|
|
|
|
|
|
|
if (no_write)
|
|
|
|
{
|
|
|
|
PHYSFS_setWriteDir(NULL); /* just in case. */
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(str);
|
2002-03-15 15:53:23 +01:00
|
|
|
BAIL_MACRO(ERR_CANT_SET_WRITE_DIR, 0);
|
|
|
|
} /* if */
|
2001-07-16 16:36:02 +02:00
|
|
|
} /* if */
|
|
|
|
|
2001-09-26 05:08:57 +02:00
|
|
|
/* Put write dir first in search path... */
|
|
|
|
PHYSFS_addToSearchPath(str, 0);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(str);
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2001-09-26 03:44:41 +02:00
|
|
|
/* Put base path on search path... */
|
2001-07-06 03:27:14 +02:00
|
|
|
PHYSFS_addToSearchPath(basedir, 1);
|
|
|
|
|
|
|
|
/* handle CD-ROMs... */
|
|
|
|
if (includeCdRoms)
|
|
|
|
{
|
|
|
|
char **cds = PHYSFS_getCdRomDirs();
|
|
|
|
char **i;
|
|
|
|
for (i = cds; *i != NULL; i++)
|
2001-07-07 05:52:43 +02:00
|
|
|
PHYSFS_addToSearchPath(*i, 1);
|
2001-09-26 03:44:41 +02:00
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
PHYSFS_freeList(cds);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* Root out archives, and add them to search path... */
|
|
|
|
if (archiveExt != NULL)
|
|
|
|
{
|
2002-07-28 23:03:27 +02:00
|
|
|
char **rc = PHYSFS_enumerateFiles("/");
|
2001-07-06 03:27:14 +02:00
|
|
|
char **i;
|
2001-09-02 06:55:25 +02:00
|
|
|
size_t extlen = strlen(archiveExt);
|
2001-07-06 03:27:14 +02:00
|
|
|
char *ext;
|
|
|
|
|
|
|
|
for (i = rc; *i != NULL; i++)
|
|
|
|
{
|
2001-09-02 06:55:25 +02:00
|
|
|
size_t l = strlen(*i);
|
|
|
|
if ((l > extlen) && ((*i)[l - extlen - 1] == '.'))
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
|
|
|
ext = (*i) + (l - extlen);
|
|
|
|
if (__PHYSFS_platformStricmp(ext, archiveExt) == 0)
|
|
|
|
{
|
|
|
|
const char *d = PHYSFS_getRealDir(*i);
|
2005-03-14 12:49:30 +01:00
|
|
|
size_t allocsize = strlen(d) + strlen(dirsep) + l + 1;
|
|
|
|
str = (char *) allocator.Malloc(allocsize);
|
2001-07-06 03:27:14 +02:00
|
|
|
if (str != NULL)
|
|
|
|
{
|
|
|
|
sprintf(str, "%s%s%s", d, dirsep, *i);
|
2001-07-07 05:52:43 +02:00
|
|
|
PHYSFS_addToSearchPath(str, archivesFirst == 0);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(str);
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
} /* for */
|
|
|
|
|
|
|
|
PHYSFS_freeList(rc);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_setSaneConfig */
|
|
|
|
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
void PHYSFS_permitSymbolicLinks(int allow)
|
|
|
|
{
|
|
|
|
allowSymLinks = allow;
|
|
|
|
} /* PHYSFS_permitSymbolicLinks */
|
|
|
|
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
/* string manipulation in C makes my ass itch. */
|
2005-03-14 12:49:30 +01:00
|
|
|
char *__PHYSFS_convertToDependent(const char *prepend,
|
|
|
|
const char *dirName,
|
|
|
|
const char *append)
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
const char *dirsep = __PHYSFS_platformDirSeparator;
|
2001-09-02 06:55:25 +02:00
|
|
|
size_t sepsize = strlen(dirsep);
|
2001-07-06 03:27:14 +02:00
|
|
|
char *str;
|
|
|
|
char *i1;
|
|
|
|
char *i2;
|
|
|
|
size_t allocSize;
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
while (*dirName == '/') /* !!! FIXME: pass through sanitize function. */
|
2001-07-16 16:36:02 +02:00
|
|
|
dirName++;
|
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
allocSize = strlen(dirName) + 1;
|
2001-07-06 03:27:14 +02:00
|
|
|
if (prepend != NULL)
|
|
|
|
allocSize += strlen(prepend) + sepsize;
|
|
|
|
if (append != NULL)
|
|
|
|
allocSize += strlen(append) + sepsize;
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
/* make sure there's enough space if the dir separator is bigger. */
|
2001-07-06 03:27:14 +02:00
|
|
|
if (sepsize > 1)
|
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
str = (char *) dirName;
|
|
|
|
do
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
str = strchr(str, '/');
|
|
|
|
if (str != NULL)
|
|
|
|
{
|
2001-07-06 03:27:14 +02:00
|
|
|
allocSize += (sepsize - 1);
|
2001-07-07 05:52:43 +02:00
|
|
|
str++;
|
|
|
|
} /* if */
|
|
|
|
} while (str != NULL);
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* if */
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
str = (char *) allocator.Malloc(allocSize);
|
2001-07-06 03:27:14 +02:00
|
|
|
BAIL_IF_MACRO(str == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
if (prepend == NULL)
|
|
|
|
*str = '\0';
|
|
|
|
else
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
|
|
|
strcpy(str, prepend);
|
|
|
|
strcat(str, dirsep);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* else */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
for (i1 = (char *) dirName, i2 = str + strlen(str); *i1; i1++, i2++)
|
2001-07-06 03:27:14 +02:00
|
|
|
{
|
|
|
|
if (*i1 == '/')
|
|
|
|
{
|
|
|
|
strcpy(i2, dirsep);
|
|
|
|
i2 += sepsize;
|
|
|
|
} /* if */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*i2 = *i1;
|
|
|
|
} /* else */
|
|
|
|
} /* for */
|
|
|
|
*i2 = '\0';
|
|
|
|
|
|
|
|
if (append)
|
|
|
|
{
|
|
|
|
strcat(str, dirsep);
|
2004-10-30 11:31:54 +02:00
|
|
|
strcat(str, append);
|
2001-07-06 03:27:14 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(str);
|
2002-03-24 20:47:33 +01:00
|
|
|
} /* __PHYSFS_convertToDependent */
|
2001-07-06 03:27:14 +02:00
|
|
|
|
|
|
|
|
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. */
|
|
|
|
return(1);
|
|
|
|
|
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. */
|
2005-03-14 08:15:40 +01:00
|
|
|
BAIL_IF_MACRO(len < mntpntlen-1, ERR_NO_SUCH_PATH, 0);
|
|
|
|
/* !!! FIXME: Case insensitive? */
|
|
|
|
retval = strncmp(h->mountPoint, fname, mntpntlen-1);
|
2005-03-13 22:03:31 +01:00
|
|
|
BAIL_IF_MACRO(retval != 0, ERR_NO_SUCH_PATH, 0);
|
2005-03-14 08:15:40 +01:00
|
|
|
if (len > mntpntlen-1) /* corner case... */
|
|
|
|
BAIL_IF_MACRO(fname[mntpntlen-1] != '/', ERR_NO_SUCH_PATH, 0);
|
|
|
|
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
|
|
|
{
|
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';
|
|
|
|
rc = h->funcs->isSymLink(h->opaque, fname, &retval);
|
|
|
|
if (end != NULL) *end = '/';
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(rc, ERR_SYMLINK_DISALLOWED, 0); /* insecure. */
|
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
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
return(retval);
|
2005-03-14 08:15:40 +01:00
|
|
|
} /* verifyPath */
|
2001-07-05 10:18:39 +02:00
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
int PHYSFS_mkdir(const char *_dname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2001-07-07 05:52:43 +02:00
|
|
|
DirHandle *h;
|
|
|
|
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. */
|
2005-03-13 13:03:05 +01:00
|
|
|
char *dname;
|
2001-07-06 03:27:14 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
dname = ((_dname) ? (char *) alloca(strlen(_dname) + 1) : NULL);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(dname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2005-03-13 13:03:05 +01:00
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_dname, dname), NULL, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
h = writeDir;
|
2005-03-14 08:15:40 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!verifyPath(h, &dname, 1), NULL, 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)
|
2005-03-13 22:16:15 +01:00
|
|
|
retval = h->funcs->isDirectory(h->opaque, dname, &exists);
|
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);
|
2001-07-07 05:52:43 +02:00
|
|
|
return(retval);
|
|
|
|
} /* PHYSFS_mkdir */
|
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
int PHYSFS_delete(const char *_fname)
|
2001-07-05 10:18:39 +02:00
|
|
|
{
|
2002-03-30 17:44:09 +01:00
|
|
|
int retval;
|
2001-07-07 05:52:43 +02:00
|
|
|
DirHandle *h;
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2005-03-13 13:03:05 +01:00
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
|
|
|
|
|
|
|
BAIL_IF_MACRO_MUTEX(writeDir == NULL, ERR_NO_WRITE_DIR, stateLock, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
h = writeDir;
|
2005-03-14 08:15:40 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!verifyPath(h, &fname, 0), NULL, 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);
|
|
|
|
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
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
2002-08-21 04:59:15 +02:00
|
|
|
const char *retval = NULL;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, NULL);
|
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, NULL);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2002-08-21 04:59:15 +02:00
|
|
|
for (i = searchPath; ((i != NULL) && (retval == NULL)); i = i->next)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if (partOfMountPoint(i, arcfname))
|
|
|
|
retval = i->dirName;
|
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
if (i->funcs->exists(i->opaque, arcfname))
|
2002-08-21 04:59:15 +02:00
|
|
|
retval = i->dirName;
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* if */
|
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2002-08-21 04:59:15 +02: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. */
|
|
|
|
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;
|
2004-09-29 08:09:29 +02:00
|
|
|
return(0);
|
|
|
|
} /* 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 *));
|
2004-09-29 08:09:29 +02:00
|
|
|
BAIL_IF_MACRO(ecd.list == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
PHYSFS_enumerateFilesCallback(path, enumFilesCallback, &ecd);
|
|
|
|
ecd.list[ecd.size] = NULL;
|
|
|
|
return(ecd.list);
|
|
|
|
} /* PHYSFS_enumerateFiles */
|
|
|
|
|
|
|
|
|
2005-09-19 00:27:05 +02:00
|
|
|
/*
|
|
|
|
* Broke out to seperate function so we can use alloca() gratuitously.
|
|
|
|
*/
|
|
|
|
static void enumerateFromMountPoint(DirHandle *i, const char *arcfname,
|
|
|
|
PHYSFS_EnumFilesCallback callback,
|
|
|
|
const char *_fname, void *data)
|
|
|
|
{
|
|
|
|
size_t len = strlen(arcfname);
|
|
|
|
char *mountPoint = (char *) alloca(strlen(i->mountPoint) + 1);
|
|
|
|
strcpy(mountPoint, i->mountPoint);
|
|
|
|
char *ptr = mountPoint + ((len) ? len + 1 : 0);
|
|
|
|
char *end = strchr(ptr, '/');
|
|
|
|
assert(end); /* should always find a terminating '/'. */
|
|
|
|
*end = '\0';
|
|
|
|
callback(data, _fname, ptr);
|
|
|
|
} /* enumerateFromMountPoint */
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
2004-09-29 08:09:29 +02:00
|
|
|
int noSyms;
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
|
|
|
if ((fname == NULL) || (callback == NULL))
|
|
|
|
return; /* oh well. */
|
2004-09-29 08:09:29 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
if (!sanitizePlatformIndependentPath(_fname, fname))
|
2004-09-29 08:09:29 +02:00
|
|
|
return;
|
2001-07-06 23:29:37 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2004-09-29 08:09:29 +02:00
|
|
|
noSyms = !allowSymLinks;
|
2001-07-06 23:29:37 +02:00
|
|
|
for (i = searchPath; i != NULL; i = i->next)
|
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if (partOfMountPoint(i, arcfname))
|
2005-09-19 00:27:05 +02:00
|
|
|
enumerateFromMountPoint(i, arcfname, callback, _fname, data);
|
2005-03-14 08:15:40 +01:00
|
|
|
|
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
|
|
|
{
|
2005-09-18 23:44:42 +02:00
|
|
|
i->funcs->enumerateFiles(i->opaque, arcfname, noSyms,
|
|
|
|
callback, _fname, data);
|
2005-03-14 08:15:40 +01:00
|
|
|
} /* else if */
|
2001-07-06 23:29:37 +02:00
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
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)
|
|
|
|
{
|
|
|
|
return(PHYSFS_getRealDir(fname) != NULL);
|
|
|
|
} /* PHYSFS_exists */
|
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
PHYSFS_sint64 PHYSFS_getLastModTime(const char *_fname)
|
2002-05-25 11:41:14 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
2002-08-21 04:59:15 +02:00
|
|
|
PHYSFS_sint64 retval = -1;
|
|
|
|
int fileExists = 0;
|
2002-05-25 11:41:14 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
2002-05-25 11:41:14 +02:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2005-03-13 13:03:05 +01:00
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
|
2002-05-25 11:41:14 +02:00
|
|
|
|
|
|
|
if (*fname == '\0') /* eh...punt if it's the root dir. */
|
2005-03-13 13:03:05 +01:00
|
|
|
return(1); /* !!! FIXME: Maybe this should be an error? */
|
2002-05-25 11:41:14 +02:00
|
|
|
|
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2002-08-21 04:59:15 +02:00
|
|
|
for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
|
2002-05-25 11:41:14 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
fileExists = partOfMountPoint(i, arcfname);
|
|
|
|
if (fileExists)
|
|
|
|
retval = 1; /* !!! FIXME: What's the right value? */
|
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
|
|
|
retval = i->funcs->getLastModTime(i->opaque,arcfname,&fileExists);
|
2002-05-25 11:41:14 +02:00
|
|
|
} /* for */
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
return(retval);
|
2002-05-25 11:41:14 +02:00
|
|
|
} /* PHYSFS_getLastModTime */
|
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
int PHYSFS_isDirectory(const char *_fname)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
2002-08-21 04:59:15 +02:00
|
|
|
int retval = 0;
|
|
|
|
int fileExists = 0;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2005-03-13 13:03:05 +01:00
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
BAIL_IF_MACRO(*fname == '\0', NULL, 1); /* Root is always a dir. :) */
|
2001-07-23 06:46:42 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2002-08-21 04:59:15 +02:00
|
|
|
for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if ((fileExists = partOfMountPoint(i, arcfname)) != 0)
|
|
|
|
retval = 1;
|
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
|
|
|
retval = i->funcs->isDirectory(i->opaque, arcfname, &fileExists);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
return(retval);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* PHYSFS_isDirectory */
|
|
|
|
|
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
int PHYSFS_isSymbolicLink(const char *_fname)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i;
|
2002-08-21 04:59:15 +02:00
|
|
|
int retval = 0;
|
|
|
|
int fileExists = 0;
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname;
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
BAIL_IF_MACRO(!allowSymLinks, ERR_SYMLINK_DISALLOWED, 0);
|
2001-07-07 05:52:43 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
2005-03-13 13:03:05 +01:00
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
BAIL_IF_MACRO(*fname == '\0', NULL, 0); /* Root is never a symlink */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2002-08-21 04:59:15 +02:00
|
|
|
for (i = searchPath; ((i != NULL) && (!fileExists)); i = i->next)
|
2001-07-07 05:52:43 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if ((fileExists = partOfMountPoint(i, arcfname)) != 0)
|
|
|
|
retval = 0; /* virtual dir...not a symlink. */
|
|
|
|
else if (verifyPath(i, &arcfname, 0))
|
|
|
|
retval = i->funcs->isSymLink(i->opaque, arcfname, &fileExists);
|
2001-07-07 05:52:43 +02:00
|
|
|
} /* for */
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
return(retval);
|
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
|
|
|
void *opaque = NULL;
|
|
|
|
FileHandle *fh = NULL;
|
|
|
|
DirHandle *h = NULL;
|
|
|
|
const PHYSFS_Archiver *f;
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO_MUTEX(!writeDir, ERR_NO_WRITE_DIR, stateLock, NULL);
|
|
|
|
|
|
|
|
h = writeDir;
|
2005-03-14 08:15:40 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!verifyPath(h, &fname, 0), NULL, stateLock, NULL);
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
f = h->funcs;
|
2004-09-26 02:25:04 +02:00
|
|
|
if (appending)
|
2004-09-26 15:00:59 +02:00
|
|
|
opaque = f->openAppend(h->opaque, fname);
|
2004-09-26 02:25:04 +02:00
|
|
|
else
|
2004-09-26 15:00:59 +02:00
|
|
|
opaque = f->openWrite(h->opaque, fname);
|
2004-09-26 02:25:04 +02:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO_MUTEX(opaque == NULL, NULL, stateLock, NULL);
|
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
|
2004-09-26 15:00:59 +02:00
|
|
|
if (fh == NULL)
|
|
|
|
{
|
|
|
|
f->fileClose(opaque);
|
|
|
|
BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
|
|
|
|
} /* if */
|
2001-07-07 11:05:19 +02:00
|
|
|
else
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
memset(fh, '\0', sizeof (FileHandle));
|
|
|
|
fh->opaque = opaque;
|
|
|
|
fh->dirHandle = h;
|
|
|
|
fh->funcs = h->funcs;
|
|
|
|
fh->next = openWriteList;
|
|
|
|
openWriteList = fh;
|
2001-07-07 11:05:19 +02:00
|
|
|
} /* else */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2004-09-26 15:17:54 +02: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
|
|
|
{
|
2001-07-07 11:05:19 +02: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
|
|
|
{
|
2001-07-07 11:05:19 +02: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;
|
2002-08-21 04:59:15 +02:00
|
|
|
int fileExists = 0;
|
2004-09-26 15:00:59 +02:00
|
|
|
DirHandle *i = NULL;
|
|
|
|
fvoid *opaque = NULL;
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2005-03-13 13:03:05 +01:00
|
|
|
char *fname = ((_fname) ? (char *) alloca(strlen(_fname) + 1) : NULL);
|
|
|
|
BAIL_IF_MACRO(fname == NULL, ERR_INVALID_ARGUMENT, 0);
|
|
|
|
BAIL_IF_MACRO(!sanitizePlatformIndependentPath(_fname, fname), NULL, 0);
|
2001-07-16 16:36:02 +02:00
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformGrabMutex(stateLock);
|
2005-03-14 08:15:40 +01:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(!searchPath, ERR_NO_SUCH_PATH, stateLock, NULL);
|
2004-09-26 15:00:59 +02:00
|
|
|
|
2005-03-14 08:15:40 +01:00
|
|
|
/* !!! FIXME: Why aren't we using a for loop here? */
|
2004-09-26 15:00:59 +02:00
|
|
|
i = searchPath;
|
|
|
|
|
|
|
|
do
|
2001-07-07 11:05:19 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
char *arcfname = fname;
|
|
|
|
if (verifyPath(i, &arcfname, 0))
|
2004-09-26 15:00:59 +02:00
|
|
|
{
|
2005-03-14 08:15:40 +01:00
|
|
|
opaque = i->funcs->openRead(i->opaque, arcfname, &fileExists);
|
2004-09-26 15:00:59 +02:00
|
|
|
if (opaque)
|
|
|
|
break;
|
|
|
|
} /* if */
|
|
|
|
i = i->next;
|
|
|
|
} while ((i != NULL) && (!fileExists));
|
2001-07-07 11:05:19 +02:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO_MUTEX(opaque == NULL, NULL, stateLock, NULL);
|
2004-09-26 02:25:04 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
fh = (FileHandle *) allocator.Malloc(sizeof (FileHandle));
|
2004-09-26 15:00:59 +02:00
|
|
|
if (fh == NULL)
|
|
|
|
{
|
|
|
|
i->funcs->fileClose(opaque);
|
|
|
|
BAIL_MACRO_MUTEX(ERR_OUT_OF_MEMORY, stateLock, NULL);
|
|
|
|
} /* if */
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
memset(fh, '\0', sizeof (FileHandle));
|
|
|
|
fh->opaque = opaque;
|
|
|
|
fh->forReading = 1;
|
|
|
|
fh->dirHandle = i;
|
|
|
|
fh->funcs = i->funcs;
|
|
|
|
fh->next = openReadList;
|
|
|
|
openReadList = fh;
|
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2004-09-26 15:17:54 +02: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
|
|
|
{
|
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);
|
2002-12-01 12:21:27 +01:00
|
|
|
if (rc)
|
2004-09-26 15:00:59 +02:00
|
|
|
rc = handle->funcs->fileClose(handle->opaque);
|
2001-07-09 03:45:13 +02:00
|
|
|
if (!rc)
|
|
|
|
return(-1);
|
|
|
|
|
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);
|
2001-07-09 03:45:13 +02:00
|
|
|
return(1);
|
|
|
|
} /* if */
|
|
|
|
prev = i;
|
|
|
|
} /* for */
|
2001-07-06 10:47:23 +02:00
|
|
|
|
2001-07-07 05:52:43 +02:00
|
|
|
return(0);
|
2001-07-09 03:45:13 +02:00
|
|
|
} /* closeHandleInOpenList */
|
|
|
|
|
|
|
|
|
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);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
|
2001-07-09 03:45:13 +02:00
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
rc = closeHandleInOpenList(&openWriteList, handle);
|
2002-03-30 17:44:09 +01:00
|
|
|
BAIL_IF_MACRO_MUTEX(rc == -1, NULL, stateLock, 0);
|
2001-07-09 03:45:13 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-03-30 17:44:09 +01:00
|
|
|
__PHYSFS_platformReleaseMutex(stateLock);
|
|
|
|
BAIL_IF_MACRO(!rc, ERR_NOT_A_HANDLE, 0);
|
|
|
|
return(1);
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_close */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 doBufferedRead(FileHandle *fh, void *buffer,
|
2002-12-01 12:21:27 +01:00
|
|
|
PHYSFS_uint32 objSize,
|
|
|
|
PHYSFS_uint32 objCount)
|
|
|
|
{
|
|
|
|
PHYSFS_sint64 retval = 0;
|
|
|
|
PHYSFS_uint32 remainder = 0;
|
|
|
|
|
|
|
|
while (objCount > 0)
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
PHYSFS_uint32 buffered = fh->buffill - fh->bufpos;
|
2002-12-01 12:21:27 +01:00
|
|
|
PHYSFS_uint64 mustread = (objSize * objCount) - remainder;
|
2003-03-12 07:19:37 +01:00
|
|
|
PHYSFS_uint32 copied;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
|
|
|
if (buffered == 0) /* need to refill buffer? */
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
PHYSFS_sint64 rc = fh->funcs->read(fh->opaque, fh->buffer,
|
|
|
|
1, fh->bufsize);
|
2002-12-01 12:21:27 +01:00
|
|
|
if (rc <= 0)
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->bufpos -= remainder;
|
2002-12-01 12:21:27 +01:00
|
|
|
return(((rc == -1) && (retval == 0)) ? -1 : retval);
|
|
|
|
} /* if */
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
buffered = fh->buffill = (PHYSFS_uint32) rc;
|
|
|
|
fh->bufpos = 0;
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (buffered > mustread)
|
2003-03-12 07:19:37 +01:00
|
|
|
buffered = (PHYSFS_uint32) mustread;
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
memcpy(buffer, fh->buffer + fh->bufpos, (size_t) buffered);
|
2002-12-01 12:21:27 +01:00
|
|
|
buffer = ((PHYSFS_uint8 *) buffer) + buffered;
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->bufpos += buffered;
|
2002-12-01 12:21:27 +01:00
|
|
|
buffered += remainder; /* take remainder into account. */
|
|
|
|
copied = (buffered / objSize);
|
|
|
|
remainder = (buffered % objSize);
|
|
|
|
retval += copied;
|
|
|
|
objCount -= copied;
|
|
|
|
} /* while */
|
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* doBufferedRead */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
PHYSFS_sint64 PHYSFS_read(PHYSFS_File *handle, void *buffer,
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
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
|
|
|
BAIL_IF_MACRO(!fh->forReading, ERR_FILE_ALREADY_OPEN_W, -1);
|
|
|
|
if (fh->buffer != NULL)
|
|
|
|
return(doBufferedRead(fh, buffer, objSize, objCount));
|
2002-12-01 12:21:27 +01:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
return(fh->funcs->read(fh->opaque, buffer, objSize, objCount));
|
2001-07-05 10:18:39 +02:00
|
|
|
} /* PHYSFS_read */
|
|
|
|
|
|
|
|
|
2004-09-26 15:17:54 +02:00
|
|
|
static PHYSFS_sint64 doBufferedWrite(PHYSFS_File *handle, const void *buffer,
|
2002-12-01 12:21:27 +01:00
|
|
|
PHYSFS_uint32 objSize,
|
|
|
|
PHYSFS_uint32 objCount)
|
|
|
|
{
|
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? */
|
2004-09-26 15:00:59 +02:00
|
|
|
if (fh->buffill + (objSize * objCount) < fh->bufsize)
|
2002-12-01 12:21:27 +01:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
memcpy(fh->buffer + fh->buffill, buffer, objSize * objCount);
|
|
|
|
fh->buffill += (objSize * objCount);
|
2002-12-01 12:21:27 +01:00
|
|
|
return(objCount);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* would overflow buffer. Flush and then write the new objects, too. */
|
|
|
|
BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, -1);
|
2004-09-26 15:00:59 +02:00
|
|
|
return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
|
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,
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
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
|
|
|
BAIL_IF_MACRO(fh->forReading, ERR_FILE_ALREADY_OPEN_R, -1);
|
|
|
|
if (fh->buffer != NULL)
|
2002-12-01 12:21:27 +01:00
|
|
|
return(doBufferedWrite(handle, buffer, objSize, objCount));
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
return(fh->funcs->write(fh->opaque, buffer, objSize, objCount));
|
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. */
|
2002-12-01 12:21:27 +01:00
|
|
|
return(0);
|
|
|
|
|
|
|
|
/* eof if buffer is empty and archiver says so. */
|
2004-09-26 15:00:59 +02:00
|
|
|
return((fh->bufpos == fh->buffill) && (fh->funcs->eof(fh->opaque)));
|
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;
|
|
|
|
PHYSFS_sint64 pos = fh->funcs->tell(fh->opaque);
|
|
|
|
PHYSFS_sint64 retval = fh->forReading ?
|
|
|
|
(pos - fh->buffill) + fh->bufpos :
|
|
|
|
(pos + fh->buffill);
|
2002-12-01 12:21:27 +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;
|
2002-12-01 12:21:27 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 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;
|
2004-08-23 05:34:03 +02:00
|
|
|
return(1); /* successful seek */
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* we have to fall back to a 'raw' seek. */
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->buffill = fh->bufpos = 0;
|
|
|
|
return(fh->funcs->seek(fh->opaque, 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
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
FileHandle *fh = (FileHandle *) handle;
|
|
|
|
return(fh->funcs->fileLength(fh->opaque));
|
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. */
|
2003-03-12 07:19:37 +01:00
|
|
|
BAIL_IF_MACRO(_bufsize > 0xFFFFFFFF, "buffer must fit in 32-bits", 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
bufsize = (PHYSFS_uint32) _bufsize;
|
|
|
|
|
2002-12-01 12:21:27 +01:00
|
|
|
BAIL_IF_MACRO(!PHYSFS_flush(handle), NULL, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2004-09-26 15:00:59 +02:00
|
|
|
PHYSFS_sint64 curpos = fh->funcs->tell(fh->opaque);
|
2002-12-01 12:21:27 +01:00
|
|
|
BAIL_IF_MACRO(curpos == -1, NULL, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
pos = ((curpos - fh->buffill) + fh->bufpos);
|
|
|
|
BAIL_IF_MACRO(!fh->funcs->seek(fh->opaque, pos), NULL, 0);
|
2002-12-01 12:21:27 +01:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
if (bufsize == 0) /* delete existing buffer. */
|
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
if (fh->buffer != NULL)
|
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);
|
2002-12-01 12:21:27 +01:00
|
|
|
BAIL_IF_MACRO(newbuf == NULL, 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;
|
2002-12-01 12:21:27 +01:00
|
|
|
return(1);
|
|
|
|
} /* 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;
|
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))
|
2002-12-01 12:21:27 +01:00
|
|
|
return(1); /* open for read or buffer empty are successful no-ops. */
|
|
|
|
|
|
|
|
/* dump buffer to disk. */
|
2004-09-26 15:00:59 +02:00
|
|
|
rc = fh->funcs->write(fh->opaque, fh->buffer + fh->bufpos,
|
|
|
|
fh->buffill - fh->bufpos, 1);
|
2002-12-01 12:21:27 +01:00
|
|
|
BAIL_IF_MACRO(rc <= 0, NULL, 0);
|
2004-09-26 15:00:59 +02:00
|
|
|
fh->bufpos = fh->buffill = 0;
|
2002-12-01 12:21:27 +01:00
|
|
|
return(1);
|
|
|
|
} /* PHYSFS_flush */
|
|
|
|
|
|
|
|
|
2005-09-09 16:07:43 +02:00
|
|
|
int PHYSFS_setAllocator(const PHYSFS_Allocator *a)
|
2004-09-23 08:45:36 +02:00
|
|
|
{
|
|
|
|
BAIL_IF_MACRO(initialized, ERR_IS_INITIALIZED, 0);
|
|
|
|
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
|
|
|
|
|
|
|
return(1);
|
|
|
|
} /* PHYSFS_setAllocator */
|
|
|
|
|
|
|
|
|
|
|
|
static void setDefaultAllocator(void)
|
|
|
|
{
|
|
|
|
assert(!externalAllocator);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Init = __PHYSFS_platformAllocatorInit;
|
|
|
|
allocator.Deinit = __PHYSFS_platformAllocatorDeinit;
|
|
|
|
allocator.Malloc = __PHYSFS_platformAllocatorMalloc;
|
|
|
|
allocator.Realloc = __PHYSFS_platformAllocatorRealloc;
|
|
|
|
allocator.Free = __PHYSFS_platformAllocatorFree;
|
2004-09-23 08:45:36 +02:00
|
|
|
} /* setDefaultAllocator */
|
|
|
|
|
2001-07-06 03:27:14 +02:00
|
|
|
/* end of physfs.c ... */
|
2001-07-05 10:18:39 +02:00
|
|
|
|