2001-07-07 10:24:47 +02:00
|
|
|
/*
|
|
|
|
* Standard directory I/O support routines for PhysicsFS.
|
|
|
|
*
|
2007-03-11 11:10:28 +01:00
|
|
|
* Please see the file LICENSE.txt in the source's root directory.
|
2001-07-07 10:24:47 +02:00
|
|
|
*
|
|
|
|
* This file written by Ryan C. Gordon.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
|
2001-07-09 06:15:35 +02:00
|
|
|
|
2012-03-15 06:58:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
static char *cvtToDependent(const char *prepend, const char *path, char *buf)
|
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(buf == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2012-03-15 06:58:39 +01:00
|
|
|
sprintf(buf, "%s%s", prepend ? prepend : "", path);
|
|
|
|
|
|
|
|
if (__PHYSFS_platformDirSeparator != '/')
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
for (p = strchr(buf, '/'); p != NULL; p = strchr(p + 1, '/'))
|
|
|
|
*p = __PHYSFS_platformDirSeparator;
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
} /* cvtToDependent */
|
|
|
|
|
|
|
|
|
|
|
|
#define CVT_TO_DEPENDENT(buf, pre, dir) { \
|
|
|
|
const size_t len = ((pre) ? strlen((char *) pre) : 0) + strlen(dir) + 1; \
|
|
|
|
buf = cvtToDependent((char*)pre,dir,(char*)__PHYSFS_smallAlloc(len)); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2012-03-20 20:38:12 +01:00
|
|
|
PHYSFS_Stat st;
|
2012-03-15 06:54:57 +01:00
|
|
|
const char dirsep = __PHYSFS_platformDirSeparator;
|
2004-09-26 02:25:04 +02:00
|
|
|
char *retval = NULL;
|
2010-08-30 09:01:57 +02:00
|
|
|
const size_t namelen = strlen(name);
|
2012-03-15 06:54:57 +01:00
|
|
|
const size_t seplen = 1;
|
2010-09-05 08:41:13 +02:00
|
|
|
int exists = 0;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
assert(io == NULL); /* shouldn't create an Io for these. */
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &st), ERRPASS, NULL);
|
|
|
|
if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
|
|
|
|
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = allocator.Malloc(namelen + seplen + 1);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
strcpy(retval, name);
|
2012-03-15 06:54:57 +01:00
|
|
|
|
|
|
|
/* make sure there's a dir separator at the end of the string */
|
|
|
|
if (retval[namelen - 1] != dirsep)
|
|
|
|
{
|
|
|
|
retval[namelen] = dirsep;
|
|
|
|
retval[namelen + 1] = '\0';
|
|
|
|
} /* if */
|
2001-10-09 17:34:10 +02:00
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openArchive */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static void DIR_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
|
2005-09-18 23:44:42 +02:00
|
|
|
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
|
|
|
|
const char *origdir, void *callbackdata)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2012-03-15 06:58:39 +01:00
|
|
|
char *d;
|
|
|
|
|
|
|
|
CVT_TO_DEPENDENT(d, opaque, dname);
|
2004-09-29 08:09:29 +02:00
|
|
|
if (d != NULL)
|
|
|
|
{
|
2005-09-18 23:44:42 +02:00
|
|
|
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
|
|
|
|
origdir, callbackdata);
|
2012-03-15 06:58:39 +01:00
|
|
|
__PHYSFS_smallFree(d);
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* if */
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_enumerateFiles */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static PHYSFS_Io *doOpen(PHYSFS_Dir *opaque, const char *name,
|
2010-08-30 09:01:57 +02:00
|
|
|
const int mode, int *fileExists)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2012-03-15 06:58:39 +01:00
|
|
|
char *f;
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = NULL;
|
|
|
|
int existtmp = 0;
|
|
|
|
|
2012-03-15 06:58:39 +01:00
|
|
|
CVT_TO_DEPENDENT(f, opaque, name);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!f, ERRPASS, NULL);
|
2012-03-15 06:58:39 +01:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
if (fileExists == NULL)
|
|
|
|
fileExists = &existtmp;
|
|
|
|
|
|
|
|
io = __PHYSFS_createNativeIo(f, mode);
|
|
|
|
if (io == NULL)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
2012-03-22 05:21:28 +01:00
|
|
|
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
|
|
|
|
PHYSFS_Stat statbuf;
|
2010-09-05 08:41:13 +02:00
|
|
|
__PHYSFS_platformStat(f, fileExists, &statbuf);
|
2012-03-22 05:21:28 +01:00
|
|
|
__PHYSFS_setError(err);
|
2002-08-21 04:59:15 +02:00
|
|
|
} /* if */
|
2012-03-13 12:49:59 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
*fileExists = 1;
|
|
|
|
} /* else */
|
|
|
|
|
2012-03-15 06:58:39 +01:00
|
|
|
__PHYSFS_smallFree(f);
|
2002-08-21 04:59:15 +02:00
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
return io;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* doOpen */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static PHYSFS_Io *DIR_openRead(PHYSFS_Dir *opaque, const char *fnm, int *exist)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
return doOpen(opaque, fnm, 'r', exist);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openRead */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static PHYSFS_Io *DIR_openWrite(PHYSFS_Dir *opaque, const char *filename)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
return doOpen(opaque, filename, 'w', NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openWrite */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static PHYSFS_Io *DIR_openAppend(PHYSFS_Dir *opaque, const char *filename)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2010-08-30 09:01:57 +02:00
|
|
|
return doOpen(opaque, filename, 'a', NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openAppend */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static int DIR_remove(PHYSFS_Dir *opaque, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
int retval;
|
2012-03-15 06:58:39 +01:00
|
|
|
char *f;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2012-03-15 06:58:39 +01:00
|
|
|
CVT_TO_DEPENDENT(f, opaque, name);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!f, ERRPASS, 0);
|
2002-03-25 06:02:12 +01:00
|
|
|
retval = __PHYSFS_platformDelete(f);
|
2012-03-15 06:58:39 +01:00
|
|
|
__PHYSFS_smallFree(f);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_remove */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static int DIR_mkdir(PHYSFS_Dir *opaque, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
int retval;
|
2012-03-15 06:58:39 +01:00
|
|
|
char *f;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2012-03-15 06:58:39 +01:00
|
|
|
CVT_TO_DEPENDENT(f, opaque, name);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!f, ERRPASS, 0);
|
2001-08-23 17:23:21 +02:00
|
|
|
retval = __PHYSFS_platformMkDir(f);
|
2012-03-15 06:58:39 +01:00
|
|
|
__PHYSFS_smallFree(f);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_mkdir */
|
|
|
|
|
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static void DIR_closeArchive(PHYSFS_Dir *opaque)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(opaque);
|
2012-03-24 15:36:11 +01:00
|
|
|
} /* DIR_closeArchive */
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
|
2012-03-25 20:52:24 +02:00
|
|
|
static int DIR_stat(PHYSFS_Dir *opaque, const char *name,
|
|
|
|
int *exists, PHYSFS_Stat *stat)
|
2010-02-15 20:02:36 +01:00
|
|
|
{
|
|
|
|
int retval = 0;
|
2012-03-15 06:58:39 +01:00
|
|
|
char *d;
|
2010-02-15 20:02:36 +01:00
|
|
|
|
2012-03-15 06:58:39 +01:00
|
|
|
CVT_TO_DEPENDENT(d, opaque, name);
|
2012-03-20 20:38:12 +01:00
|
|
|
BAIL_IF_MACRO(!d, ERRPASS, 0);
|
2010-02-15 20:02:36 +01:00
|
|
|
retval = __PHYSFS_platformStat(d, exists, stat);
|
2012-03-15 06:58:39 +01:00
|
|
|
__PHYSFS_smallFree(d);
|
2010-02-15 20:02:36 +01:00
|
|
|
return retval;
|
|
|
|
} /* DIR_stat */
|
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
|
|
|
|
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
|
|
|
|
{
|
2012-03-25 23:17:56 +02:00
|
|
|
{
|
|
|
|
"",
|
|
|
|
"Non-archive, direct filesystem I/O",
|
|
|
|
"Ryan C. Gordon <icculus@icculus.org>",
|
|
|
|
"http://icculus.org/physfs/",
|
|
|
|
},
|
2004-09-29 08:18:04 +02:00
|
|
|
DIR_openArchive, /* openArchive() method */
|
|
|
|
DIR_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
DIR_openRead, /* openRead() method */
|
|
|
|
DIR_openWrite, /* openWrite() method */
|
|
|
|
DIR_openAppend, /* openAppend() method */
|
|
|
|
DIR_remove, /* remove() method */
|
|
|
|
DIR_mkdir, /* mkdir() method */
|
2012-03-24 15:36:11 +01:00
|
|
|
DIR_closeArchive, /* closeArchive() method */
|
2010-08-30 09:01:57 +02:00
|
|
|
DIR_stat /* stat() method */
|
2004-09-29 08:18:04 +02:00
|
|
|
};
|
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
/* end of dir.c ... */
|
|
|
|
|