2001-07-07 10:24:47 +02:00
|
|
|
/*
|
|
|
|
* Standard directory I/O support routines for PhysicsFS.
|
|
|
|
*
|
|
|
|
* 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-07 10:24:47 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2001-07-08 05:25:12 +02:00
|
|
|
#include <string.h>
|
2001-07-08 12:58:10 +02:00
|
|
|
#include "physfs.h"
|
2001-07-07 10:24:47 +02:00
|
|
|
|
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 DIR_read(fvoid *opaque, void *buffer,
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
PHYSFS_sint64 retval;
|
2004-09-26 15:00:59 +02:00
|
|
|
retval = __PHYSFS_platformRead(opaque, buffer, objSize, objCount);
|
2002-03-25 05:06:06 +01:00
|
|
|
return(retval);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_read */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 DIR_write(fvoid *opaque, const void *buffer,
|
2002-03-24 20:47:33 +01:00
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
PHYSFS_sint64 retval;
|
2004-09-26 15:00:59 +02:00
|
|
|
retval = __PHYSFS_platformWrite(opaque, buffer, objSize, objCount);
|
2002-03-25 05:06:06 +01:00
|
|
|
return(retval);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_write */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_eof(fvoid *opaque)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(__PHYSFS_platformEOF(opaque));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_eof */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 DIR_tell(fvoid *opaque)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(__PHYSFS_platformTell(opaque));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_tell */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_seek(fvoid *opaque, PHYSFS_uint64 offset)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(__PHYSFS_platformSeek(opaque, offset));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_seek */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 DIR_fileLength(fvoid *opaque)
|
2001-07-09 06:15:35 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(__PHYSFS_platformFileLength(opaque));
|
2001-07-09 06:15:35 +02:00
|
|
|
} /* DIR_fileLength */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_fileClose(fvoid *opaque)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
/*
|
2002-03-25 05:06:06 +01:00
|
|
|
* we manually flush the buffer, since that's the place a close will
|
2001-07-08 05:25:12 +02:00
|
|
|
* most likely fail, but that will leave the file handle in an undefined
|
2002-03-25 05:06:06 +01:00
|
|
|
* state if it fails. Flush failures we can recover from.
|
2001-07-08 05:25:12 +02:00
|
|
|
*/
|
2004-09-26 15:00:59 +02:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformFlush(opaque), NULL, 0);
|
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformClose(opaque), NULL, 0);
|
2001-07-08 05:25:12 +02:00
|
|
|
return(1);
|
|
|
|
} /* DIR_fileClose */
|
|
|
|
|
|
|
|
|
|
|
|
static int DIR_isArchive(const char *filename, int forWriting)
|
|
|
|
{
|
|
|
|
/* directories ARE archives in this driver... */
|
|
|
|
return(__PHYSFS_platformIsDirectory(filename));
|
|
|
|
} /* DIR_isArchive */
|
|
|
|
|
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
static void *DIR_openArchive(const char *name, int forWriting)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2001-07-08 15:57:28 +02:00
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
2004-09-26 02:25:04 +02:00
|
|
|
char *retval = NULL;
|
2001-09-02 06:55:25 +02:00
|
|
|
size_t namelen = strlen(name);
|
|
|
|
size_t seplen = strlen(dirsep);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2004-09-26 02:25:04 +02:00
|
|
|
/* !!! FIXME: when is this not called right before openArchive? */
|
2001-07-08 07:27:05 +02:00
|
|
|
BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
|
2004-09-26 02:25:04 +02:00
|
|
|
ERR_UNSUPPORTED_ARCHIVE, 0);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2005-03-14 12:49:30 +01:00
|
|
|
retval = allocator.Malloc(namelen + seplen + 1);
|
2001-07-08 05:25:12 +02:00
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
|
|
|
|
/* make sure there's a dir separator at the end of the string */
|
2004-09-26 02:25:04 +02:00
|
|
|
strcpy(retval, name);
|
2001-07-08 05:25:12 +02:00
|
|
|
if (strcmp((name + namelen) - seplen, dirsep) != 0)
|
2004-09-26 02:25:04 +02:00
|
|
|
strcat(retval, dirsep);
|
2001-10-09 17:34:10 +02:00
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_openArchive */
|
|
|
|
|
|
|
|
|
2004-09-29 08:09:29 +02:00
|
|
|
static void DIR_enumerateFiles(dvoid *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
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *d = __PHYSFS_platformCvtToDependent((char *)opaque, dname, NULL);
|
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);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(d);
|
2004-09-29 08:09:29 +02:00
|
|
|
} /* if */
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_enumerateFiles */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_exists(dvoid *opaque, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
|
|
|
retval = __PHYSFS_platformExists(f);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_exists */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
2002-08-21 04:59:15 +02:00
|
|
|
int retval = 0;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(d == NULL, NULL, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
*fileExists = __PHYSFS_platformExists(d);
|
|
|
|
if (*fileExists)
|
|
|
|
retval = __PHYSFS_platformIsDirectory(d);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(d);
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_isDirectory */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
2002-08-21 04:59:15 +02:00
|
|
|
int retval = 0;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
*fileExists = __PHYSFS_platformExists(f);
|
|
|
|
if (*fileExists)
|
|
|
|
retval = __PHYSFS_platformIsSymLink(f);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_isSymLink */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static PHYSFS_sint64 DIR_getLastModTime(dvoid *opaque,
|
2002-08-21 04:59:15 +02:00
|
|
|
const char *name,
|
|
|
|
int *fileExists)
|
2002-05-25 11:41:14 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
2002-08-21 04:59:15 +02:00
|
|
|
PHYSFS_sint64 retval = -1;
|
2002-05-25 11:41:14 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(d == NULL, NULL, 0);
|
2002-08-21 04:59:15 +02:00
|
|
|
*fileExists = __PHYSFS_platformExists(d);
|
|
|
|
if (*fileExists)
|
|
|
|
retval = __PHYSFS_platformGetLastModTime(d);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(d);
|
2002-05-25 11:41:14 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_getLastModTime */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static fvoid *doOpen(dvoid *opaque, const char *name,
|
|
|
|
void *(*openFunc)(const char *filename),
|
|
|
|
int *fileExists)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
2004-09-26 15:00:59 +02:00
|
|
|
void *rc = NULL;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, NULL);
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
if (fileExists != NULL)
|
|
|
|
{
|
|
|
|
*fileExists = __PHYSFS_platformExists(f);
|
|
|
|
if (!(*fileExists))
|
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(f);
|
2002-08-21 04:59:15 +02:00
|
|
|
return(NULL);
|
|
|
|
} /* if */
|
|
|
|
} /* if */
|
|
|
|
|
2002-03-25 05:06:06 +01:00
|
|
|
rc = openFunc(f);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
return((fvoid *) rc);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* doOpen */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static fvoid *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(doOpen(opaque, fnm, __PHYSFS_platformOpenRead, exist));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openRead */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static fvoid *DIR_openWrite(dvoid *opaque, const char *filename)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(doOpen(opaque, filename, __PHYSFS_platformOpenWrite, NULL));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openWrite */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static fvoid *DIR_openAppend(dvoid *opaque, const char *filename)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 15:00:59 +02:00
|
|
|
return(doOpen(opaque, filename, __PHYSFS_platformOpenAppend, NULL));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openAppend */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_remove(dvoid *opaque, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
2002-03-25 06:02:12 +01:00
|
|
|
retval = __PHYSFS_platformDelete(f);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_remove */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static int DIR_mkdir(dvoid *opaque, const char *name)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2004-09-26 02:25:04 +02:00
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
2001-08-23 17:23:21 +02:00
|
|
|
retval = __PHYSFS_platformMkDir(f);
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_mkdir */
|
|
|
|
|
|
|
|
|
2004-09-26 15:00:59 +02:00
|
|
|
static void DIR_dirClose(dvoid *opaque)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2005-03-14 12:49:30 +01:00
|
|
|
allocator.Free(opaque);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_dirClose */
|
|
|
|
|
2004-09-29 08:18:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
|
|
|
|
{
|
|
|
|
"",
|
|
|
|
DIR_ARCHIVE_DESCRIPTION,
|
2006-01-01 13:33:19 +01:00
|
|
|
"Ryan C. Gordon <icculus@icculus.org>",
|
2004-09-29 08:18:04 +02:00
|
|
|
"http://icculus.org/physfs/",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
|
|
|
|
{
|
|
|
|
&__PHYSFS_ArchiveInfo_DIR,
|
|
|
|
DIR_isArchive, /* isArchive() method */
|
|
|
|
DIR_openArchive, /* openArchive() method */
|
|
|
|
DIR_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
DIR_exists, /* exists() method */
|
|
|
|
DIR_isDirectory, /* isDirectory() method */
|
|
|
|
DIR_isSymLink, /* isSymLink() method */
|
|
|
|
DIR_getLastModTime, /* getLastModTime() method */
|
|
|
|
DIR_openRead, /* openRead() method */
|
|
|
|
DIR_openWrite, /* openWrite() method */
|
|
|
|
DIR_openAppend, /* openAppend() method */
|
|
|
|
DIR_remove, /* remove() method */
|
|
|
|
DIR_mkdir, /* mkdir() method */
|
|
|
|
DIR_dirClose, /* dirClose() method */
|
|
|
|
DIR_read, /* read() method */
|
|
|
|
DIR_write, /* write() method */
|
|
|
|
DIR_eof, /* eof() method */
|
|
|
|
DIR_tell, /* tell() method */
|
|
|
|
DIR_seek, /* seek() method */
|
|
|
|
DIR_fileLength, /* fileLength() method */
|
|
|
|
DIR_fileClose /* fileClose() method */
|
|
|
|
};
|
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
/* end of dir.c ... */
|
|
|
|
|