2001-07-09 02:49:41 +02:00
|
|
|
/*
|
|
|
|
* GRP support routines for PhysicsFS.
|
|
|
|
*
|
|
|
|
* This driver handles BUILD engine archives ("groupfiles"). This format
|
|
|
|
* (but not this driver) was put together by Ken Silverman.
|
|
|
|
*
|
|
|
|
* The format is simple enough. In Ken's words:
|
|
|
|
*
|
|
|
|
* What's the .GRP file format?
|
|
|
|
*
|
|
|
|
* The ".grp" file format is just a collection of a lot of files stored
|
|
|
|
* into 1 big one. I tried to make the format as simple as possible: The
|
|
|
|
* first 12 bytes contains my name, "KenSilverman". The next 4 bytes is
|
|
|
|
* the number of files that were compacted into the group file. Then for
|
|
|
|
* each file, there is a 16 byte structure, where the first 12 bytes are
|
|
|
|
* the filename, and the last 4 bytes are the file's size. The rest of
|
|
|
|
* the group file is just the raw data packed one after the other in the
|
|
|
|
* same order as the list of files.
|
|
|
|
*
|
|
|
|
* (That info is from http://www.advsys.net/ken/build.htm ...)
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
|
|
|
#if (defined PHYSFS_SUPPORTS_GRP)
|
|
|
|
|
2001-07-09 02:49:41 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "physfs.h"
|
|
|
|
|
|
|
|
#define __PHYSICSFS_INTERNAL__
|
|
|
|
#include "physfs_internal.h"
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
char name[13];
|
2002-08-28 10:58:39 +02:00
|
|
|
PHYSFS_uint32 startPos;
|
|
|
|
PHYSFS_uint32 size;
|
2002-07-23 09:48:43 +02:00
|
|
|
} GRPentry;
|
|
|
|
|
2001-07-09 02:49:41 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2002-03-25 09:13:06 +01:00
|
|
|
char *filename;
|
2002-07-23 09:48:43 +02:00
|
|
|
PHYSFS_sint64 last_mod_time;
|
|
|
|
PHYSFS_uint32 entryCount;
|
|
|
|
GRPentry *entries;
|
2001-07-09 02:49:41 +02:00
|
|
|
} GRPinfo;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2002-03-25 09:13:06 +01:00
|
|
|
void *handle;
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPentry *entry;
|
2002-08-28 10:58:39 +02:00
|
|
|
PHYSFS_uint32 curPos;
|
2001-07-09 02:49:41 +02:00
|
|
|
} GRPfileinfo;
|
|
|
|
|
|
|
|
|
2001-09-02 06:55:25 +02:00
|
|
|
static void GRP_dirClose(DirHandle *h);
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
|
2002-08-21 04:59:15 +02:00
|
|
|
static PHYSFS_sint64 GRP_write(FileHandle *handle, const void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
|
2001-09-02 06:55:25 +02:00
|
|
|
static int GRP_eof(FileHandle *handle);
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 GRP_tell(FileHandle *handle);
|
|
|
|
static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset);
|
|
|
|
static PHYSFS_sint64 GRP_fileLength(FileHandle *handle);
|
2001-09-02 06:55:25 +02:00
|
|
|
static int GRP_fileClose(FileHandle *handle);
|
|
|
|
static int GRP_isArchive(const char *filename, int forWriting);
|
|
|
|
static DirHandle *GRP_openArchive(const char *name, int forWriting);
|
|
|
|
static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
|
|
|
|
const char *dirname,
|
|
|
|
int omitSymLinks);
|
|
|
|
static int GRP_exists(DirHandle *h, const char *name);
|
2002-08-21 04:59:15 +02:00
|
|
|
static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists);
|
|
|
|
static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists);
|
|
|
|
static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *n, int *e);
|
|
|
|
static FileHandle *GRP_openRead(DirHandle *h, const char *name, int *exist);
|
|
|
|
static FileHandle *GRP_openWrite(DirHandle *h, const char *name);
|
|
|
|
static FileHandle *GRP_openAppend(DirHandle *h, const char *name);
|
|
|
|
static int GRP_remove(DirHandle *h, const char *name);
|
|
|
|
static int GRP_mkdir(DirHandle *h, const char *name);
|
2001-09-02 06:55:25 +02:00
|
|
|
|
2002-07-26 08:19:09 +02:00
|
|
|
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
|
|
|
|
{
|
|
|
|
"GRP",
|
2002-07-28 23:03:27 +02:00
|
|
|
GRP_ARCHIVE_DESCRIPTION,
|
2006-01-01 13:31:34 +01:00
|
|
|
"Ryan C. Gordon <icculus@icculus.org>",
|
2002-07-26 08:19:09 +02:00
|
|
|
"http://icculus.org/physfs/",
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-09-02 06:55:25 +02:00
|
|
|
static const FileFunctions __PHYSFS_FileFunctions_GRP =
|
|
|
|
{
|
|
|
|
GRP_read, /* read() method */
|
2002-08-21 04:59:15 +02:00
|
|
|
GRP_write, /* write() method */
|
2001-09-02 06:55:25 +02:00
|
|
|
GRP_eof, /* eof() method */
|
|
|
|
GRP_tell, /* tell() method */
|
|
|
|
GRP_seek, /* seek() method */
|
|
|
|
GRP_fileLength, /* fileLength() method */
|
|
|
|
GRP_fileClose /* fileClose() method */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const DirFunctions __PHYSFS_DirFunctions_GRP =
|
|
|
|
{
|
2002-07-26 08:19:09 +02:00
|
|
|
&__PHYSFS_ArchiveInfo_GRP,
|
2001-09-02 06:55:25 +02:00
|
|
|
GRP_isArchive, /* isArchive() method */
|
|
|
|
GRP_openArchive, /* openArchive() method */
|
|
|
|
GRP_enumerateFiles, /* enumerateFiles() method */
|
|
|
|
GRP_exists, /* exists() method */
|
|
|
|
GRP_isDirectory, /* isDirectory() method */
|
|
|
|
GRP_isSymLink, /* isSymLink() method */
|
2002-06-06 07:47:41 +02:00
|
|
|
GRP_getLastModTime, /* getLastModTime() method */
|
2001-09-02 06:55:25 +02:00
|
|
|
GRP_openRead, /* openRead() method */
|
2002-08-21 04:59:15 +02:00
|
|
|
GRP_openWrite, /* openWrite() method */
|
|
|
|
GRP_openAppend, /* openAppend() method */
|
|
|
|
GRP_remove, /* remove() method */
|
|
|
|
GRP_mkdir, /* mkdir() method */
|
2001-09-02 06:55:25 +02:00
|
|
|
GRP_dirClose /* dirClose() method */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void GRP_dirClose(DirHandle *h)
|
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPinfo *info = ((GRPinfo *) h->opaque);
|
|
|
|
free(info->filename);
|
|
|
|
free(info->entries);
|
|
|
|
free(info);
|
2001-09-02 06:55:25 +02:00
|
|
|
free(h);
|
|
|
|
} /* GRP_dirClose */
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
|
|
|
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPentry *entry = finfo->entry;
|
2002-08-28 10:58:39 +02:00
|
|
|
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
|
|
|
|
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
|
2002-07-23 09:48:43 +02:00
|
|
|
PHYSFS_sint64 rc;
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
if (objsLeft < objCount)
|
2002-08-28 10:58:39 +02:00
|
|
|
objCount = objsLeft;
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
|
|
|
|
if (rc > 0)
|
2002-08-28 10:58:39 +02:00
|
|
|
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
|
2002-07-23 09:48:43 +02:00
|
|
|
|
|
|
|
return(rc);
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* GRP_read */
|
|
|
|
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
static PHYSFS_sint64 GRP_write(FileHandle *handle, const void *buffer,
|
|
|
|
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
|
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
|
|
|
|
} /* GRP_write */
|
|
|
|
|
|
|
|
|
2001-07-09 02:49:41 +02:00
|
|
|
static int GRP_eof(FileHandle *handle)
|
|
|
|
{
|
|
|
|
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPentry *entry = finfo->entry;
|
2002-08-28 10:58:39 +02:00
|
|
|
return(finfo->curPos >= entry->size);
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* GRP_eof */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 GRP_tell(FileHandle *handle)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
return(((GRPfileinfo *) (handle->opaque))->curPos);
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* GRP_tell */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
|
|
|
GRPfileinfo *finfo = (GRPfileinfo *) (handle->opaque);
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPentry *entry = finfo->entry;
|
|
|
|
int rc;
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(offset < 0, ERR_INVALID_ARGUMENT, 0);
|
2002-08-28 10:58:39 +02:00
|
|
|
BAIL_IF_MACRO(offset >= entry->size, ERR_PAST_EOF, 0);
|
|
|
|
rc = __PHYSFS_platformSeek(finfo->handle, entry->startPos + offset);
|
2002-07-23 09:48:43 +02:00
|
|
|
if (rc)
|
2002-08-28 10:58:39 +02:00
|
|
|
finfo->curPos = (PHYSFS_uint32) offset;
|
2002-07-23 09:48:43 +02:00
|
|
|
|
|
|
|
return(rc);
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* GRP_seek */
|
|
|
|
|
|
|
|
|
2002-03-24 20:47:33 +01:00
|
|
|
static PHYSFS_sint64 GRP_fileLength(FileHandle *handle)
|
2001-07-09 06:15:35 +02:00
|
|
|
{
|
2002-08-28 10:58:39 +02:00
|
|
|
GRPfileinfo *finfo = ((GRPfileinfo *) handle->opaque);
|
|
|
|
return((PHYSFS_sint64) finfo->entry->size);
|
2001-07-09 06:15:35 +02:00
|
|
|
} /* GRP_fileLength */
|
|
|
|
|
|
|
|
|
2001-07-09 02:49:41 +02:00
|
|
|
static int GRP_fileClose(FileHandle *handle)
|
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPfileinfo *finfo = ((GRPfileinfo *) handle->opaque);
|
|
|
|
BAIL_IF_MACRO(!__PHYSFS_platformClose(finfo->handle), NULL, 0);
|
|
|
|
free(finfo);
|
2001-07-09 02:49:41 +02:00
|
|
|
free(handle);
|
|
|
|
return(1);
|
|
|
|
} /* GRP_fileClose */
|
|
|
|
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
static int grp_open(const char *filename, int forWriting,
|
|
|
|
void **fh, PHYSFS_uint32 *count)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-03-25 09:13:06 +01:00
|
|
|
PHYSFS_uint8 buf[12];
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
*fh = NULL;
|
|
|
|
BAIL_IF_MACRO(forWriting, ERR_ARC_IS_READ_ONLY, 0);
|
|
|
|
|
2002-03-25 09:13:06 +01:00
|
|
|
*fh = __PHYSFS_platformOpenRead(filename);
|
|
|
|
BAIL_IF_MACRO(*fh == NULL, NULL, 0);
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-03-25 09:13:06 +01:00
|
|
|
if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
|
|
|
|
goto openGrp_failed;
|
|
|
|
|
|
|
|
if (memcmp(buf, "KenSilverman", 12) != 0)
|
|
|
|
{
|
|
|
|
__PHYSFS_setError(ERR_UNSUPPORTED_ARCHIVE);
|
|
|
|
goto openGrp_failed;
|
|
|
|
} /* if */
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
|
2002-03-25 09:13:06 +01:00
|
|
|
goto openGrp_failed;
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
*count = PHYSFS_swapULE32(*count);
|
2002-04-05 20:05:50 +02:00
|
|
|
|
2001-07-09 02:49:41 +02:00
|
|
|
return(1);
|
2002-03-25 09:13:06 +01:00
|
|
|
|
|
|
|
openGrp_failed:
|
|
|
|
if (*fh != NULL)
|
|
|
|
__PHYSFS_platformClose(*fh);
|
|
|
|
|
|
|
|
*count = -1;
|
|
|
|
*fh = NULL;
|
|
|
|
return(0);
|
2002-07-23 09:48:43 +02:00
|
|
|
} /* grp_open */
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int GRP_isArchive(const char *filename, int forWriting)
|
|
|
|
{
|
2002-03-25 09:13:06 +01:00
|
|
|
void *fh;
|
2002-07-23 09:48:43 +02:00
|
|
|
PHYSFS_uint32 fileCount;
|
|
|
|
int retval = grp_open(filename, forWriting, &fh, &fileCount);
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
if (fh != NULL)
|
2002-03-25 09:13:06 +01:00
|
|
|
__PHYSFS_platformClose(fh);
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* GRP_isArchive */
|
|
|
|
|
|
|
|
|
2002-08-20 03:34:27 +02:00
|
|
|
static int grp_entry_cmp(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
|
2002-07-23 09:48:43 +02:00
|
|
|
{
|
2008-02-20 13:24:10 +01:00
|
|
|
if (one != two)
|
|
|
|
{
|
|
|
|
const GRPentry *a = (const GRPentry *) _a;
|
|
|
|
return(strcmp(a[one].name, a[two].name));
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return 0;
|
2002-08-20 03:34:27 +02:00
|
|
|
} /* grp_entry_cmp */
|
2002-07-23 09:48:43 +02:00
|
|
|
|
|
|
|
|
2002-08-20 03:34:27 +02:00
|
|
|
static void grp_entry_swap(void *_a, PHYSFS_uint32 one, PHYSFS_uint32 two)
|
2002-07-23 09:48:43 +02:00
|
|
|
{
|
2008-02-20 13:24:10 +01:00
|
|
|
if (one != two)
|
|
|
|
{
|
|
|
|
GRPentry tmp;
|
|
|
|
GRPentry *first = &(((GRPentry *) _a)[one]);
|
|
|
|
GRPentry *second = &(((GRPentry *) _a)[two]);
|
|
|
|
memcpy(&tmp, first, sizeof (GRPentry));
|
|
|
|
memcpy(first, second, sizeof (GRPentry));
|
|
|
|
memcpy(second, &tmp, sizeof (GRPentry));
|
|
|
|
} /* if */
|
2002-08-20 03:34:27 +02:00
|
|
|
} /* grp_entry_swap */
|
2002-07-23 09:48:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-03-25 09:13:06 +01:00
|
|
|
void *fh = NULL;
|
2002-07-23 09:48:43 +02:00
|
|
|
PHYSFS_uint32 fileCount;
|
2002-07-23 20:11:12 +02:00
|
|
|
PHYSFS_uint32 location = 16; /* sizeof sig. */
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPentry *entry;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
BAIL_IF_MACRO(!grp_open(name, forWriting, &fh, &fileCount), NULL, 0);
|
|
|
|
info->entryCount = fileCount;
|
|
|
|
info->entries = (GRPentry *) malloc(sizeof (GRPentry) * fileCount);
|
|
|
|
if (info->entries == NULL)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, 0);
|
|
|
|
} /* if */
|
|
|
|
|
2002-07-23 20:11:12 +02:00
|
|
|
location += (16 * fileCount);
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
for (entry = info->entries; fileCount > 0; fileCount--, entry++)
|
|
|
|
{
|
|
|
|
if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
entry->name[12] = '\0'; /* name isn't null-terminated in file. */
|
|
|
|
if ((ptr = strchr(entry->name, ' ')) != NULL)
|
|
|
|
*ptr = '\0'; /* trim extra spaces. */
|
|
|
|
|
|
|
|
if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
|
|
|
|
{
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
return(0);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
entry->size = PHYSFS_swapULE32(entry->size);
|
|
|
|
entry->startPos = location;
|
2002-07-23 20:11:12 +02:00
|
|
|
location += entry->size;
|
2002-07-23 09:48:43 +02:00
|
|
|
} /* for */
|
|
|
|
|
|
|
|
__PHYSFS_platformClose(fh);
|
|
|
|
|
2002-08-20 03:34:27 +02:00
|
|
|
__PHYSFS_sort(info->entries, info->entryCount,
|
|
|
|
grp_entry_cmp, grp_entry_swap);
|
2002-07-23 09:48:43 +02:00
|
|
|
return(1);
|
|
|
|
} /* grp_load_entries */
|
|
|
|
|
|
|
|
|
|
|
|
static DirHandle *GRP_openArchive(const char *name, int forWriting)
|
|
|
|
{
|
|
|
|
GRPinfo *info;
|
2001-07-09 02:49:41 +02:00
|
|
|
DirHandle *retval = malloc(sizeof (DirHandle));
|
2002-07-23 09:48:43 +02:00
|
|
|
PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
2002-07-23 09:48:43 +02:00
|
|
|
info = retval->opaque = malloc(sizeof (GRPinfo));
|
|
|
|
if (info == NULL)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-03-25 09:13:06 +01:00
|
|
|
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
|
|
|
|
goto GRP_openArchive_failed;
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
memset(info, '\0', sizeof (GRPinfo));
|
|
|
|
|
|
|
|
info->filename = (char *) malloc(strlen(name) + 1);
|
|
|
|
if (info->filename == NULL)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-03-25 09:13:06 +01:00
|
|
|
__PHYSFS_setError(ERR_OUT_OF_MEMORY);
|
|
|
|
goto GRP_openArchive_failed;
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
if (!grp_load_entries(name, forWriting, info))
|
2002-03-25 09:13:06 +01:00
|
|
|
goto GRP_openArchive_failed;
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
strcpy(info->filename, name);
|
|
|
|
info->last_mod_time = modtime;
|
2001-07-09 02:49:41 +02:00
|
|
|
retval->funcs = &__PHYSFS_DirFunctions_GRP;
|
|
|
|
return(retval);
|
2002-03-25 09:13:06 +01:00
|
|
|
|
|
|
|
GRP_openArchive_failed:
|
|
|
|
if (retval != NULL)
|
|
|
|
{
|
|
|
|
if (retval->opaque != NULL)
|
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
if (info->filename != NULL)
|
|
|
|
free(info->filename);
|
|
|
|
if (info->entries != NULL)
|
|
|
|
free(info->entries);
|
|
|
|
free(info);
|
2002-03-25 09:13:06 +01:00
|
|
|
} /* if */
|
|
|
|
free(retval);
|
|
|
|
} /* if */
|
|
|
|
|
|
|
|
return(NULL);
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* GRP_openArchive */
|
|
|
|
|
|
|
|
|
2001-07-16 19:36:28 +02:00
|
|
|
static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
|
|
|
|
const char *dirname,
|
|
|
|
int omitSymLinks)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPinfo *info = ((GRPinfo *) h->opaque);
|
|
|
|
GRPentry *entry = info->entries;
|
|
|
|
LinkedStringList *retval = NULL, *p = NULL;
|
|
|
|
PHYSFS_uint32 max = info->entryCount;
|
2002-04-03 06:20:07 +02:00
|
|
|
PHYSFS_uint32 i;
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
/* no directories in GRP files. */
|
|
|
|
BAIL_IF_MACRO(*dirname != '\0', ERR_NOT_A_DIR, NULL);
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
for (i = 0; i < max; i++, entry++)
|
|
|
|
retval = __PHYSFS_addToLinkedStringList(retval, &p, entry->name, -1);
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
return(retval);
|
|
|
|
} /* GRP_enumerateFiles */
|
|
|
|
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
static GRPentry *grp_find_entry(GRPinfo *info, const char *name)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
char *ptr = strchr(name, '.');
|
|
|
|
GRPentry *a = info->entries;
|
|
|
|
PHYSFS_sint32 lo = 0;
|
|
|
|
PHYSFS_sint32 hi = (PHYSFS_sint32) (info->entryCount - 1);
|
|
|
|
PHYSFS_sint32 middle;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rule out filenames to avoid unneeded processing...no dirs,
|
|
|
|
* big filenames, or extensions > 3 chars.
|
|
|
|
*/
|
|
|
|
BAIL_IF_MACRO((ptr) && (strlen(ptr) > 4), ERR_NO_SUCH_FILE, NULL);
|
|
|
|
BAIL_IF_MACRO(strlen(name) > 12, ERR_NO_SUCH_FILE, NULL);
|
|
|
|
BAIL_IF_MACRO(strchr(name, '/') != NULL, ERR_NO_SUCH_FILE, NULL);
|
|
|
|
|
|
|
|
while (lo <= hi)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
middle = lo + ((hi - lo) / 2);
|
|
|
|
rc = strcmp(name, a[middle].name);
|
|
|
|
if (rc == 0) /* found it! */
|
|
|
|
return(&a[middle]);
|
|
|
|
else if (rc > 0)
|
|
|
|
lo = middle + 1;
|
|
|
|
else
|
|
|
|
hi = middle - 1;
|
|
|
|
} /* while */
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
|
|
|
|
} /* grp_find_entry */
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
static int GRP_exists(DirHandle *h, const char *name)
|
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
return(grp_find_entry(((GRPinfo *) h->opaque), name) != NULL);
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* GRP_exists */
|
|
|
|
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-08-21 04:59:15 +02:00
|
|
|
*fileExists = GRP_exists(h, name);
|
2001-07-09 02:49:41 +02:00
|
|
|
return(0); /* never directories in a groupfile. */
|
|
|
|
} /* GRP_isDirectory */
|
|
|
|
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-08-21 04:59:15 +02:00
|
|
|
*fileExists = GRP_exists(h, name);
|
2001-07-09 02:49:41 +02:00
|
|
|
return(0); /* never symlinks in a groupfile. */
|
|
|
|
} /* GRP_isSymLink */
|
|
|
|
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h,
|
|
|
|
const char *name,
|
|
|
|
int *fileExists)
|
2002-06-06 07:47:41 +02:00
|
|
|
{
|
2002-07-25 23:52:07 +02:00
|
|
|
GRPinfo *info = ((GRPinfo *) h->opaque);
|
2002-08-21 04:59:15 +02:00
|
|
|
PHYSFS_sint64 retval = -1;
|
2002-07-25 23:52:07 +02:00
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
*fileExists = (grp_find_entry(info, name) != NULL);
|
|
|
|
if (*fileExists) /* use time of GRP itself in the physical filesystem. */
|
|
|
|
retval = ((GRPinfo *) h->opaque)->last_mod_time;
|
|
|
|
|
|
|
|
return(retval);
|
2002-06-06 07:47:41 +02:00
|
|
|
} /* GRP_getLastModTime */
|
|
|
|
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
static FileHandle *GRP_openRead(DirHandle *h, const char *fnm, int *fileExists)
|
2001-07-09 02:49:41 +02:00
|
|
|
{
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPinfo *info = ((GRPinfo *) h->opaque);
|
2001-07-09 02:49:41 +02:00
|
|
|
FileHandle *retval;
|
|
|
|
GRPfileinfo *finfo;
|
2002-07-23 09:48:43 +02:00
|
|
|
GRPentry *entry;
|
2001-07-09 02:49:41 +02:00
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
entry = grp_find_entry(info, fnm);
|
|
|
|
*fileExists = (entry != NULL);
|
2002-07-23 09:48:43 +02:00
|
|
|
BAIL_IF_MACRO(entry == NULL, NULL, NULL);
|
2001-07-09 02:49:41 +02:00
|
|
|
|
|
|
|
retval = (FileHandle *) malloc(sizeof (FileHandle));
|
|
|
|
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
|
|
|
|
finfo = (GRPfileinfo *) malloc(sizeof (GRPfileinfo));
|
|
|
|
if (finfo == NULL)
|
|
|
|
{
|
|
|
|
free(retval);
|
2002-07-23 09:48:43 +02:00
|
|
|
BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
|
2001-07-09 02:49:41 +02:00
|
|
|
} /* if */
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
finfo->handle = __PHYSFS_platformOpenRead(info->filename);
|
2002-03-25 09:13:06 +01:00
|
|
|
if ( (finfo->handle == NULL) ||
|
2002-07-23 09:48:43 +02:00
|
|
|
(!__PHYSFS_platformSeek(finfo->handle, entry->startPos)) )
|
2002-03-25 09:13:06 +01:00
|
|
|
{
|
|
|
|
free(finfo);
|
|
|
|
free(retval);
|
|
|
|
return(NULL);
|
|
|
|
} /* if */
|
|
|
|
|
2002-07-23 09:48:43 +02:00
|
|
|
finfo->curPos = 0;
|
|
|
|
finfo->entry = entry;
|
2001-07-09 02:49:41 +02:00
|
|
|
retval->opaque = (void *) finfo;
|
|
|
|
retval->funcs = &__PHYSFS_FileFunctions_GRP;
|
|
|
|
retval->dirHandle = h;
|
|
|
|
return(retval);
|
|
|
|
} /* GRP_openRead */
|
|
|
|
|
2002-08-21 04:59:15 +02:00
|
|
|
|
|
|
|
static FileHandle *GRP_openWrite(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
|
|
|
|
} /* GRP_openWrite */
|
|
|
|
|
|
|
|
|
|
|
|
static FileHandle *GRP_openAppend(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
|
|
|
|
} /* GRP_openAppend */
|
|
|
|
|
|
|
|
|
|
|
|
static int GRP_remove(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
|
|
|
|
} /* GRP_remove */
|
|
|
|
|
|
|
|
|
|
|
|
static int GRP_mkdir(DirHandle *h, const char *name)
|
|
|
|
{
|
|
|
|
BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
|
|
|
|
} /* GRP_mkdir */
|
|
|
|
|
2002-05-10 11:25:25 +02:00
|
|
|
#endif /* defined PHYSFS_SUPPORTS_GRP */
|
|
|
|
|
2001-07-09 02:49:41 +02:00
|
|
|
/* end of grp.c ... */
|
|
|
|
|