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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
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
|
|
|
|
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
|
|
|
{
|
2001-07-08 15:57:28 +02:00
|
|
|
const char *dirsep = PHYSFS_getDirSeparator();
|
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);
|
|
|
|
const size_t seplen = strlen(dirsep);
|
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. */
|
2010-08-24 16:05:58 +02:00
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformIsDirectory(name), ERR_NOT_AN_ARCHIVE, NULL);
|
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);
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* make sure there's a dir separator at the end of the string */
|
|
|
|
/* !!! FIXME: is there really any place where (seplen != 1)? */
|
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
|
|
|
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_openArchive */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
/* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */
|
|
|
|
|
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
|
|
|
{
|
2010-08-30 09:01:57 +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);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* 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);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* 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);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* DIR_isSymLink */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
|
|
|
|
const int mode, 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);
|
2010-08-30 09:01:57 +02:00
|
|
|
PHYSFS_Io *io = NULL;
|
|
|
|
int existtmp = 0;
|
|
|
|
|
|
|
|
if (fileExists == NULL)
|
|
|
|
fileExists = &existtmp;
|
|
|
|
|
|
|
|
*fileExists = 0;
|
2001-07-08 05:25:12 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(f == NULL, NULL, NULL);
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
io = __PHYSFS_createNativeIo(f, mode);
|
|
|
|
allocator.Free(f);
|
|
|
|
if (io == NULL)
|
2002-08-21 04:59:15 +02:00
|
|
|
{
|
|
|
|
*fileExists = __PHYSFS_platformExists(f);
|
2010-08-30 09:01:57 +02:00
|
|
|
return NULL;
|
2002-08-21 04:59:15 +02:00
|
|
|
} /* if */
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
*fileExists = 1;
|
|
|
|
return io;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* doOpen */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *DIR_openRead(dvoid *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 */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *DIR_openWrite(dvoid *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 */
|
|
|
|
|
|
|
|
|
2010-08-30 09:01:57 +02:00
|
|
|
static PHYSFS_Io *DIR_openAppend(dvoid *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 */
|
|
|
|
|
|
|
|
|
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);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* 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);
|
2010-01-28 08:27:45 +01:00
|
|
|
return retval;
|
2001-07-08 05:25:12 +02:00
|
|
|
} /* 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
|
|
|
|
2010-08-22 06:37:25 +02:00
|
|
|
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
|
2010-02-15 20:02:36 +01:00
|
|
|
PHYSFS_Stat *stat)
|
|
|
|
{
|
|
|
|
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(d == NULL, NULL, 0);
|
|
|
|
retval = __PHYSFS_platformStat(d, exists, stat);
|
|
|
|
allocator.Free(d);
|
|
|
|
return retval;
|
|
|
|
} /* DIR_stat */
|
|
|
|
|
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_openArchive, /* openArchive() method */
|
|
|
|
DIR_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
DIR_exists, /* exists() method */
|
|
|
|
DIR_isDirectory, /* isDirectory() method */
|
|
|
|
DIR_isSymLink, /* isSymLink() 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 */
|
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 ... */
|
|
|
|
|