Skip to content
Permalink
Browse files
Moved all the file i/o from stdio-style to POSIX-style.
Instead of trying to deal with a count of objects, just give 'em a stream of
bytes. This is WAY simpler to work with at the implementation level, and
removes confusion about what to do with a partial read.

This will be very useful when we expose the i/o interface to applications.
  • Loading branch information
icculus committed Aug 21, 2010
1 parent 68632d3 commit 4000b234c0ca87bdb83bd06aac73ec6fff1b57a2
Showing with 401 additions and 422 deletions.
  1. +4 −10 src/archiver_dir.c
  2. +18 −20 src/archiver_grp.c
  3. +22 −28 src/archiver_hog.c
  4. +29 −38 src/archiver_iso9660.c
  5. +11 −18 src/archiver_lzma.c
  6. +12 −15 src/archiver_mvl.c
  7. +20 −30 src/archiver_qpak.c
  8. +20 −27 src/archiver_wad.c
  9. +24 −32 src/archiver_zip.c
  10. +94 −50 src/physfs.c
  11. +63 −0 src/physfs.h
  12. +32 −31 src/physfs_internal.h
  13. +13 −38 src/platform_os2.c
  14. +12 −35 src/platform_pocketpc.c
  15. +16 −18 src/platform_posix.c
  16. +11 −32 src/platform_windows.c
@@ -14,21 +14,15 @@
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"

static PHYSFS_sint64 DIR_read(fvoid *opaque, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 DIR_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
{
PHYSFS_sint64 retval;
retval = __PHYSFS_platformRead(opaque, buffer, objSize, objCount);
return retval;
return __PHYSFS_platformRead(opaque, buffer, len);
} /* DIR_read */


static PHYSFS_sint64 DIR_write(fvoid *opaque, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 DIR_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
{
PHYSFS_sint64 retval;
retval = __PHYSFS_platformWrite(opaque, buffer, objSize, objCount);
return retval;
return __PHYSFS_platformWrite(f, buf, len);
} /* DIR_write */


@@ -57,6 +57,12 @@ typedef struct
} GRPfileinfo;


static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
{
return (__PHYSFS_platformRead(fh, buf, len) == len);
} /* readAll */


static void GRP_dirClose(dvoid *opaque)
{
GRPinfo *info = ((GRPinfo *) opaque);
@@ -66,28 +72,25 @@ static void GRP_dirClose(dvoid *opaque)
} /* GRP_dirClose */


static PHYSFS_sint64 GRP_read(fvoid *opaque, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 GRP_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
{
GRPfileinfo *finfo = (GRPfileinfo *) opaque;
GRPentry *entry = finfo->entry;
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
const GRPentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
PHYSFS_sint64 rc;

if (objsLeft < objCount)
objCount = objsLeft;
if (bytesLeft < len)
len = bytesLeft;

rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
rc = __PHYSFS_platformRead(finfo->handle, buffer, len);
if (rc > 0)
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
finfo->curPos += (PHYSFS_uint32) rc;

return rc;
} /* GRP_read */


static PHYSFS_sint64 GRP_write(fvoid *opaque, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 GRP_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* GRP_write */
@@ -150,7 +153,7 @@ static int grp_open(const char *filename, int forWriting,
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);

if (__PHYSFS_platformRead(*fh, buf, 12, 1) != 1)
if (!readAll(*fh, buf, 12))
goto openGrp_failed;

if (memcmp(buf, "KenSilverman", 12) != 0)
@@ -159,7 +162,7 @@ static int grp_open(const char *filename, int forWriting,
goto openGrp_failed;
} /* if */

if (__PHYSFS_platformRead(*fh, count, sizeof (PHYSFS_uint32), 1) != 1)
if (!readAll(*fh, count, sizeof (PHYSFS_uint32)))
goto openGrp_failed;

*count = PHYSFS_swapULE32(*count);
@@ -236,7 +239,8 @@ static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)

for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
if (__PHYSFS_platformRead(fh, &entry->name, 12, 1) != 1)
if ( (!readAll(fh, &entry->name, 12)) ||
(!readAll(fh, &entry->size, sizeof (PHYSFS_uint32))) )
{
__PHYSFS_platformClose(fh);
return 0;
@@ -246,12 +250,6 @@ static int grp_load_entries(const char *name, int forWriting, GRPinfo *info)
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;
location += entry->size;
@@ -71,6 +71,12 @@ typedef struct
} HOGfileinfo;


static inline int readAll(void *fh, void *buf, const PHYSFS_uint64 len)
{
return (__PHYSFS_platformRead(fh, buf, len) == len);
} /* readAll */


static void HOG_dirClose(dvoid *opaque)
{
HOGinfo *info = ((HOGinfo *) opaque);
@@ -80,28 +86,25 @@ static void HOG_dirClose(dvoid *opaque)
} /* HOG_dirClose */


static PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 HOG_read(fvoid *opaque, void *buffer, PHYSFS_uint64 len)
{
HOGfileinfo *finfo = (HOGfileinfo *) opaque;
HOGentry *entry = finfo->entry;
PHYSFS_uint32 bytesLeft = entry->size - finfo->curPos;
PHYSFS_uint32 objsLeft = (bytesLeft / objSize);
const HOGentry *entry = finfo->entry;
const PHYSFS_uint64 bytesLeft = (PHYSFS_uint64)(entry->size-finfo->curPos);
PHYSFS_sint64 rc;

if (objsLeft < objCount)
objCount = objsLeft;
if (bytesLeft < len)
len = bytesLeft;

rc = __PHYSFS_platformRead(finfo->handle, buffer, objSize, objCount);
rc = __PHYSFS_platformRead(finfo->handle, buffer, len);
if (rc > 0)
finfo->curPos += (PHYSFS_uint32) (rc * objSize);
finfo->curPos += (PHYSFS_uint32) rc;

return rc;
} /* HOG_read */


static PHYSFS_sint64 HOG_write(fvoid *opaque, const void *buffer,
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
static PHYSFS_sint64 HOG_write(fvoid *f, const void *buf, PHYSFS_uint64 len)
{
BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
} /* HOG_write */
@@ -168,7 +171,7 @@ static int hog_open(const char *filename, int forWriting,
*fh = __PHYSFS_platformOpenRead(filename);
BAIL_IF_MACRO(*fh == NULL, NULL, 0);

if (__PHYSFS_platformRead(*fh, buf, 3, 1) != 1)
if (!readAll(*fh, buf, 3))
goto openHog_failed;

if (memcmp(buf, "DHF", 3) != 0)
@@ -179,10 +182,10 @@ static int hog_open(const char *filename, int forWriting,

while (1)
{
if (__PHYSFS_platformRead(*fh, buf, 13, 1) != 1)
if (!readAll(*fh, buf, 13))
break; /* eof here is ok */

if (__PHYSFS_platformRead(*fh, &size, 4, 1) != 1)
if (!readAll(*fh, &size, sizeof (PHYSFS_uint32)))
goto openHog_failed;

size = PHYSFS_swapULE32(size);
@@ -269,32 +272,23 @@ static int hog_load_entries(const char *name, int forWriting, HOGinfo *info)

for (entry = info->entries; fileCount > 0; fileCount--, entry++)
{
if (__PHYSFS_platformRead(fh, &entry->name, 13, 1) != 1)
{
__PHYSFS_platformClose(fh);
return 0;
} /* if */

if (__PHYSFS_platformRead(fh, &entry->size, 4, 1) != 1)
if ( (!readAll(fh, &entry->name, 13)) ||
(!readAll(fh, &entry->size, sizeof (PHYSFS_uint32))) )
{
__PHYSFS_platformClose(fh);
return 0;
} /* if */

entry->size = PHYSFS_swapULE32(entry->size);
entry->startPos = (unsigned int) __PHYSFS_platformTell(fh);
if (entry->startPos == -1)
{
__PHYSFS_platformClose(fh);
return 0;
}

/* Skip over entry */
if (!__PHYSFS_platformSeek(fh, entry->startPos + entry->size))
if ( (entry->startPos == -1) ||
(!__PHYSFS_platformSeek(fh, entry->startPos + entry->size)) )
{
__PHYSFS_platformClose(fh);
return 0;
}
} /* if */
} /* for */

__PHYSFS_platformClose(fh);

0 comments on commit 4000b23

Please sign in to comment.