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>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.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"
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
|
|
|
|
static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
|
2001-09-02 06:55:25 +02:00
|
|
|
static int DIR_eof(FileHandle *handle);
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 DIR_tell(FileHandle *handle);
|
|
|
|
static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
|
|
|
|
static PHYSFS_sint64 DIR_fileLength(FileHandle *handle);
|
2001-09-02 06:55:25 +02:00
|
|
|
static int DIR_fileClose(FileHandle *handle);
|
|
|
|
static int DIR_isArchive(const char *filename, int forWriting);
|
|
|
|
static DirHandle *DIR_openArchive(const char *name, int forWriting);
|
|
|
|
static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
|
|
|
|
const char *dname,
|
|
|
|
int omitSymLinks);
|
|
|
|
static int DIR_exists(DirHandle *h, const char *name);
|
|
|
|
static int DIR_isDirectory(DirHandle *h, const char *name);
|
|
|
|
static int DIR_isSymLink(DirHandle *h, const char *name);
|
|
|
|
static FileHandle *DIR_openRead(DirHandle *h, const char *filename);
|
2002-05-25 11:41:14 +02:00
|
|
|
static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *name);
|
2001-09-02 06:55:25 +02:00
|
|
|
static FileHandle *DIR_openWrite(DirHandle *h, const char *filename);
|
|
|
|
static FileHandle *DIR_openAppend(DirHandle *h, const char *filename);
|
|
|
|
static int DIR_remove(DirHandle *h, const char *name);
|
|
|
|
static int DIR_mkdir(DirHandle *h, const char *name);
|
|
|
|
static void DIR_dirClose(DirHandle *h);
|
|
|
|
|
|
|
|
|
|
|
|
static const FileFunctions __PHYSFS_FileFunctions_DIR =
|
|
|
|
{
|
|
|
|
DIR_read, /* read() method */
|
|
|
|
NULL, /* write() method */
|
|
|
|
DIR_eof, /* eof() method */
|
|
|
|
DIR_tell, /* tell() method */
|
|
|
|
DIR_seek, /* seek() method */
|
|
|
|
DIR_fileLength, /* fileLength() method */
|
|
|
|
DIR_fileClose /* fileClose() method */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static const FileFunctions __PHYSFS_FileFunctions_DIRW =
|
|
|
|
{
|
|
|
|
NULL, /* 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 */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const DirFunctions __PHYSFS_DirFunctions_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 */
|
2002-05-25 11:41:14 +02:00
|
|
|
DIR_getLastModTime, /* getLastModTime() method */
|
2001-09-02 06:55:25 +02:00
|
|
|
DIR_openRead, /* openRead() method */
|
|
|
|
DIR_openWrite, /* openWrite() method */
|
|
|
|
DIR_openAppend, /* openAppend() method */
|
|
|
|
DIR_remove, /* remove() method */
|
|
|
|
DIR_mkdir, /* mkdir() method */
|
|
|
|
DIR_dirClose /* dirClose() method */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* This doesn't get listed, since it's technically not an archive... */
|
|
|
|
#if 0
|
|
|
|
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
|
|
|
|
{
|
|
|
|
"DIR",
|
|
|
|
"non-archive directory I/O",
|
2001-09-15 00:55:44 +02:00
|
|
|
"Ryan C. Gordon <icculus@clutteredmind.org>",
|
2001-09-02 06:55:25 +02:00
|
|
|
"http://www.icculus.org/physfs/",
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
|
|
|
|
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;
|
|
|
|
retval = __PHYSFS_platformRead(handle->opaque, buffer, objSize, objCount);
|
|
|
|
return(retval);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_read */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
|
|
|
|
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;
|
|
|
|
retval = __PHYSFS_platformWrite(handle->opaque, buffer, objSize, objCount);
|
|
|
|
return(retval);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_write */
|
|
|
|
|
|
|
|
|
|
|
|
static int DIR_eof(FileHandle *handle)
|
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
return(__PHYSFS_platformEOF(handle->opaque));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_eof */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 DIR_tell(FileHandle *handle)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
return(__PHYSFS_platformTell(handle->opaque));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_tell */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
return(__PHYSFS_platformSeek(handle->opaque, offset));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_seek */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 DIR_fileLength(FileHandle *handle)
|
2001-07-09 06:15:35 +02:00
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
return(__PHYSFS_platformFileLength(handle->opaque));
|
2001-07-09 06:15:35 +02:00
|
|
|
} /* DIR_fileLength */
|
|
|
|
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
static int DIR_fileClose(FileHandle *handle)
|
|
|
|
{
|
|
|
|
/*
|
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
|
|
|
*/
|
2002-03-25 05:06:06 +01:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformFlush(handle->opaque), NULL, 0);
|
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformClose(handle->opaque), NULL, 0);
|
2001-07-08 05:25:12 +02:00
|
|
|
free(handle);
|
|
|
|
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 */
|
|
|
|
|
|
|
|
|
|
|
|
static DirHandle *DIR_openArchive(const char *name, int forWriting)
|
|
|
|
{
|
2001-07-08 15:57:28 +02:00
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
2001-07-08 05:25:12 +02:00
|
|
|
DirHandle *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
|
|
|
|
2001-07-08 07:27:05 +02:00
|
|
|
BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
|
|
|
|
ERR_UNSUPPORTED_ARCHIVE, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
|
2001-10-09 17:15:24 +02:00
|
|
|
retval = (DirHandle *) malloc(sizeof (DirHandle));
|
2001-07-08 05:25:12 +02:00
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
retval->opaque = malloc(namelen + seplen + 1);
|
|
|
|
if (retval->opaque == NULL)
|
|
|
|
{
|
|
|
|
free(retval);
|
2001-10-09 17:15:24 +02:00
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
/* make sure there's a dir separator at the end of the string */
|
|
|
|
strcpy((char *) (retval->opaque), name);
|
|
|
|
if (strcmp((name + namelen) - seplen, dirsep) != 0)
|
|
|
|
strcat((char *) (retval->opaque), dirsep);
|
|
|
|
|
2001-10-09 17:34:10 +02:00
|
|
|
retval->funcs = &__PHYSFS_DirFunctions_DIR;
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* DIR_openArchive */
|
|
|
|
|
|
|
|
|
2001-07-16 19:36:28 +02:00
|
|
|
static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
|
|
|
|
const char *dname,
|
|
|
|
int omitSymLinks)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque),dname,NULL);
|
|
|
|
LinkedStringList *retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(d == NULL, NULL, NULL);
|
2001-07-16 19:36:28 +02:00
|
|
|
retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
|
2001-07-08 05:25:12 +02:00
|
|
|
free(d);
|
|
|
|
return(retval);
|
|
|
|
} /* DIR_enumerateFiles */
|
|
|
|
|
|
|
|
|
|
|
|
static int DIR_exists(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
|
|
|
retval = __PHYSFS_platformExists(f);
|
|
|
|
free(f);
|
|
|
|
return(retval);
|
|
|
|
} /* DIR_exists */
|
|
|
|
|
|
|
|
|
|
|
|
static int DIR_isDirectory(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(d == NULL, NULL, 0);
|
|
|
|
retval = __PHYSFS_platformIsDirectory(d);
|
|
|
|
free(d);
|
|
|
|
return(retval);
|
|
|
|
} /* DIR_isDirectory */
|
|
|
|
|
|
|
|
|
|
|
|
static int DIR_isSymLink(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
|
|
|
|
int retval;
|
|
|
|
|
2002-05-21 13:29:00 +02:00
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
2001-07-08 05:25:12 +02:00
|
|
|
retval = __PHYSFS_platformIsSymLink(f);
|
|
|
|
free(f);
|
|
|
|
return(retval);
|
|
|
|
} /* DIR_isSymLink */
|
|
|
|
|
|
|
|
|
2002-05-25 11:41:14 +02:00
|
|
|
static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
|
|
|
|
PHYSFS_sint64 retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(d == NULL, NULL, 0);
|
2002-05-25 13:12:40 +02:00
|
|
|
retval = __PHYSFS_platformGetLastModTime(d);
|
2002-05-25 11:41:14 +02:00
|
|
|
free(d);
|
|
|
|
return(retval);
|
|
|
|
} /* DIR_getLastModTime */
|
|
|
|
|
|
|
|
|
2002-03-25 05:06:06 +01:00
|
|
|
static FileHandle *doOpen(DirHandle *h, const char *name,
|
|
|
|
void *(*openFunc)(const char *filename),
|
|
|
|
const FileFunctions *fileFuncs)
|
2001-07-08 05:25:12 +02:00
|
|
|
{
|
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
|
2002-03-25 05:06:06 +01:00
|
|
|
void *rc;
|
2001-07-08 05:25:12 +02:00
|
|
|
FileHandle *retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, NULL);
|
|
|
|
|
|
|
|
retval = (FileHandle *) malloc(sizeof (FileHandle));
|
|
|
|
if (!retval)
|
|
|
|
{
|
|
|
|
free(f);
|
2002-03-25 05:06:06 +01:00
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-03-25 05:06:06 +01:00
|
|
|
rc = openFunc(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
free(f);
|
|
|
|
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
free(retval);
|
2002-03-25 09:11:55 +01:00
|
|
|
return(NULL);
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* if */
|
|
|
|
|
|
|
|
retval->opaque = (void *) rc;
|
|
|
|
retval->dirHandle = h;
|
2002-03-25 05:06:06 +01:00
|
|
|
retval->funcs = fileFuncs;
|
|
|
|
|
2001-07-08 05:25:12 +02:00
|
|
|
return(retval);
|
|
|
|
} /* doOpen */
|
|
|
|
|
|
|
|
|
|
|
|
static FileHandle *DIR_openRead(DirHandle *h, const char *filename)
|
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
return(doOpen(h, filename, __PHYSFS_platformOpenRead,
|
|
|
|
&__PHYSFS_FileFunctions_DIR));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openRead */
|
|
|
|
|
|
|
|
|
|
|
|
static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
|
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
return(doOpen(h, filename, __PHYSFS_platformOpenWrite,
|
|
|
|
&__PHYSFS_FileFunctions_DIRW));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openWrite */
|
|
|
|
|
|
|
|
|
|
|
|
static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
|
|
|
|
{
|
2002-03-25 05:06:06 +01:00
|
|
|
return(doOpen(h, filename, __PHYSFS_platformOpenAppend,
|
|
|
|
&__PHYSFS_FileFunctions_DIRW));
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openAppend */
|
|
|
|
|
|
|
|
|
|
|
|
static int DIR_remove(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
2002-03-25 06:02:12 +01:00
|
|
|
retval = __PHYSFS_platformDelete(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
free(f);
|
|
|
|
return(retval);
|
|
|
|
} /* DIR_remove */
|
|
|
|
|
|
|
|
|
|
|
|
static int DIR_mkdir(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, 0);
|
2001-08-23 17:23:21 +02:00
|
|
|
retval = __PHYSFS_platformMkDir(f);
|
2001-07-08 05:25:12 +02:00
|
|
|
free(f);
|
|
|
|
return(retval);
|
|
|
|
} /* DIR_mkdir */
|
|
|
|
|
|
|
|
|
|
|
|
static void DIR_dirClose(DirHandle *h)
|
|
|
|
{
|
|
|
|
free(h->opaque);
|
|
|
|
free(h);
|
|
|
|
} /* DIR_dirClose */
|
|
|
|
|
2001-07-07 10:24:47 +02:00
|
|
|
/* end of dir.c ... */
|
|
|
|
|