Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Base implementation is now complete. Now to fill in archive/platform …
…drivers.
  • Loading branch information
icculus committed Jul 7, 2001
1 parent be67b1e commit f053b90
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 37 deletions.
113 changes: 76 additions & 37 deletions physfs.c
Expand Up @@ -956,52 +956,90 @@ int PHYSFS_isSymbolicLink(const char *fname)
return(0);
} /* PHYSFS_isSymbolicLink */

/**
* Open a file for writing, in platform-independent notation and in relation
* to the write path as the root of the writable filesystem. The specified
* file is created if it doesn't exist. If it does exist, it is truncated to
* zero bytes, and the writing offset is set to the start.
*
* @param filename File to open.
* @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/

static PHYSFS_file *doOpenWrite(const char *fname, int appending)
{
PHYSFS_file *retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
FileHandle *rc = NULL;
DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
FileHandleList *list;

BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);

list = (FileHandleList *) malloc(sizeof (FileHandleList));
BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);

rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
if (rc == NULL)
{
free(list);
free(retval);
retval = NULL;
} /* if */
else
{
retval->opaque = (void *) rc;
list->handle = retval;
list->next = openWriteList;
openWriteList = list;
} /* else */

return(retval);
} /* doOpenWrite */


PHYSFS_file *PHYSFS_openWrite(const char *filename)
{
return NULL;
return(doOpenWrite(filename, 0));
} /* PHYSFS_openWrite */


/**
* Open a file for writing, in platform-independent notation and in relation
* to the write path as the root of the writable filesystem. The specified
* file is created if it doesn't exist. If it does exist, the writing offset
* is set to the end of the file, so the first write will be the byte after
* the end.
*
* @param filename File to open.
* @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
PHYSFS_file *PHYSFS_openAppend(const char *filename)
{
return NULL;
return(doOpenWrite(filename, 1));
} /* PHYSFS_openAppend */


/**
* Open a file for reading, in platform-independent notation. The search path
* is checked one at a time until a matching file is found, in which case an
* abstract filehandle is associated with it, and reading may be done.
* The reading offset is set to the first byte of the file.
*
* @param filename File to open.
* @return A valid PhysicsFS filehandle on success, NULL on error. Specifics
* of the error can be gleaned from PHYSFS_getLastError().
*/
PHYSFS_file *PHYSFS_openRead(const char *filename)
PHYSFS_file *PHYSFS_openRead(const char *fname)
{
return NULL;
PHYSFS_file *retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
FileHandle *rc = NULL;
FileHandleList *list;
DirInfo *i;

BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
list = (FileHandleList *) malloc(sizeof (FileHandleList));
BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);

for (i = searchPath; i != NULL; i = i->next)
{
DirHandle *h = i->dirHandle;
if (__PHYSFS_verifySecurity(h, fname))
{
rc = h->funcs->openRead(h, fname);
if (rc != NULL)
break;
} /* if */
} /* for */

if (rc == NULL)
{
free(list);
free(retval);
retval = NULL;
} /* if */
else
{
retval->opaque = (void *) rc;
list->handle = retval;
list->next = openReadList;
openReadList = list;
} /* else */

return(retval);
} /* PHYSFS_openRead */


Expand All @@ -1018,7 +1056,7 @@ int PHYSFS_close(PHYSFS_file *handle)
{
for (i = *(*lists), prev = NULL; i != NULL; prev = i, i = i->next)
{
if (((FileHandle *) i->handle->opaque) == h)
if (i->handle == handle)
{
rc = h->funcs->close(h);
if (!rc)
Expand All @@ -1028,8 +1066,9 @@ int PHYSFS_close(PHYSFS_file *handle)
*(*lists) = i->next;
else
prev->next = i->next;
free(i);

free(handle);
free(i);
return(1);
} /* if */
} /* for */
Expand Down
1 change: 1 addition & 0 deletions physfs_internal.h
Expand Up @@ -230,6 +230,7 @@ typedef struct __PHYSFS_DIRFUNCTIONS__
#define ERR_INSECURE_FNAME "Insecure filename"
#define ERR_SYMLINK_DISALLOWED "Symbolic links are disabled"
#define ERR_NO_WRITE_DIR "Write directory is not set"
#define ERR_NO_SUCH_FILE "No such file"


/*
Expand Down

0 comments on commit f053b90

Please sign in to comment.