Rewrote the file closing routines to not be so gay. Cleaned up the opening
routines, too. One less malloc and free needed for an open/close pair, which makes things frighteningly more manageable. --ryan.
This commit is contained in:
parent
ce29f03218
commit
096d57e6a8
97
physfs.c
97
physfs.c
|
@ -35,7 +35,7 @@ typedef struct __PHYSFS_DIRINFO__
|
||||||
|
|
||||||
typedef struct __PHYSFS_FILEHANDLELIST__
|
typedef struct __PHYSFS_FILEHANDLELIST__
|
||||||
{
|
{
|
||||||
PHYSFS_file *handle;
|
PHYSFS_file handle;
|
||||||
struct __PHYSFS_FILEHANDLELIST__ *next;
|
struct __PHYSFS_FILEHANDLELIST__ *next;
|
||||||
} FileHandleList;
|
} FileHandleList;
|
||||||
|
|
||||||
|
@ -236,7 +236,7 @@ static int freeDirInfo(DirInfo *di, FileHandleList *openList)
|
||||||
|
|
||||||
for (i = openList; i != NULL; i = i->next)
|
for (i = openList; i != NULL; i = i->next)
|
||||||
{
|
{
|
||||||
const DirHandle *h = ((FileHandle *) i->handle->opaque)->dirHandle;
|
const DirHandle *h = ((FileHandle *) &(i->handle.opaque))->dirHandle;
|
||||||
BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
|
BAIL_IF_MACRO(h == di->dirHandle, ERR_FILES_STILL_OPEN, 0);
|
||||||
} /* for */
|
} /* for */
|
||||||
|
|
||||||
|
@ -387,14 +387,13 @@ static int closeFileHandleList(FileHandleList **list)
|
||||||
for (i = *list; i != NULL; i = next)
|
for (i = *list; i != NULL; i = next)
|
||||||
{
|
{
|
||||||
next = i->next;
|
next = i->next;
|
||||||
h = (FileHandle *) (i->handle->opaque);
|
h = (FileHandle *) (i->handle.opaque);
|
||||||
if (!h->funcs->fileClose(i->handle->opaque))
|
if (!h->funcs->fileClose(h))
|
||||||
{
|
{
|
||||||
*list = i;
|
*list = i;
|
||||||
return(0);
|
return(0);
|
||||||
} /* if */
|
} /* if */
|
||||||
|
|
||||||
free(i->handle);
|
|
||||||
free(i);
|
free(i);
|
||||||
} /* for */
|
} /* for */
|
||||||
|
|
||||||
|
@ -1052,13 +1051,12 @@ int PHYSFS_isSymbolicLink(const char *fname)
|
||||||
|
|
||||||
static PHYSFS_file *doOpenWrite(const char *fname, int appending)
|
static PHYSFS_file *doOpenWrite(const char *fname, int appending)
|
||||||
{
|
{
|
||||||
PHYSFS_file *retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
|
PHYSFS_file *retval = NULL;
|
||||||
FileHandle *rc = NULL;
|
FileHandle *rc = NULL;
|
||||||
DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
|
DirHandle *h = (writeDir == NULL) ? NULL : writeDir->dirHandle;
|
||||||
const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
|
const DirFunctions *f = (h == NULL) ? NULL : h->funcs;
|
||||||
FileHandleList *list;
|
FileHandleList *list;
|
||||||
|
|
||||||
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
|
BAIL_IF_MACRO(!h, ERR_NO_WRITE_DIR, NULL);
|
||||||
BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
|
BAIL_IF_MACRO(!__PHYSFS_verifySecurity(h, fname), NULL, NULL);
|
||||||
|
|
||||||
|
@ -1067,17 +1065,13 @@ static PHYSFS_file *doOpenWrite(const char *fname, int appending)
|
||||||
|
|
||||||
rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
|
rc = (appending) ? f->openAppend(h, fname) : f->openWrite(h, fname);
|
||||||
if (rc == NULL)
|
if (rc == NULL)
|
||||||
{
|
|
||||||
free(list);
|
free(list);
|
||||||
free(retval);
|
|
||||||
retval = NULL;
|
|
||||||
} /* if */
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
retval->opaque = (void *) rc;
|
list->handle.opaque = (void *) rc;
|
||||||
list->handle = retval;
|
|
||||||
list->next = openWriteList;
|
list->next = openWriteList;
|
||||||
openWriteList = list;
|
openWriteList = list;
|
||||||
|
retval = &(list->handle);
|
||||||
} /* else */
|
} /* else */
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
|
@ -1098,19 +1092,13 @@ PHYSFS_file *PHYSFS_openAppend(const char *filename)
|
||||||
|
|
||||||
PHYSFS_file *PHYSFS_openRead(const char *fname)
|
PHYSFS_file *PHYSFS_openRead(const char *fname)
|
||||||
{
|
{
|
||||||
PHYSFS_file *retval;
|
PHYSFS_file *retval = NULL;
|
||||||
FileHandle *rc = NULL;
|
FileHandle *rc = NULL;
|
||||||
FileHandleList *list;
|
FileHandleList *list;
|
||||||
DirInfo *i;
|
DirInfo *i;
|
||||||
|
|
||||||
retval = (PHYSFS_file *) malloc(sizeof (PHYSFS_file));
|
|
||||||
BAIL_IF_MACRO(!retval, ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
list = (FileHandleList *) malloc(sizeof (FileHandleList));
|
list = (FileHandleList *) malloc(sizeof (FileHandleList));
|
||||||
if (!list)
|
BAIL_IF_MACRO(!list, ERR_OUT_OF_MEMORY, NULL);
|
||||||
{
|
|
||||||
free(retval);
|
|
||||||
BAIL_IF_MACRO(1, ERR_OUT_OF_MEMORY, NULL);
|
|
||||||
} /* if */
|
|
||||||
|
|
||||||
for (i = searchPath; i != NULL; i = i->next)
|
for (i = searchPath; i != NULL; i = i->next)
|
||||||
{
|
{
|
||||||
|
@ -1124,57 +1112,66 @@ PHYSFS_file *PHYSFS_openRead(const char *fname)
|
||||||
} /* for */
|
} /* for */
|
||||||
|
|
||||||
if (rc == NULL)
|
if (rc == NULL)
|
||||||
{
|
|
||||||
free(list);
|
free(list);
|
||||||
free(retval);
|
|
||||||
retval = NULL;
|
|
||||||
} /* if */
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
retval->opaque = (void *) rc;
|
list->handle.opaque = (void *) rc;
|
||||||
list->handle = retval;
|
|
||||||
list->next = openReadList;
|
list->next = openReadList;
|
||||||
openReadList = list;
|
openReadList = list;
|
||||||
|
retval = &(list->handle);
|
||||||
} /* else */
|
} /* else */
|
||||||
|
|
||||||
return(retval);
|
return(retval);
|
||||||
} /* PHYSFS_openRead */
|
} /* PHYSFS_openRead */
|
||||||
|
|
||||||
|
|
||||||
int PHYSFS_close(PHYSFS_file *handle)
|
static int closeHandleInOpenList(FileHandleList **list, PHYSFS_file *handle)
|
||||||
{
|
{
|
||||||
FileHandle *h = (FileHandle *) handle->opaque;
|
FileHandle *h = (FileHandle *) handle->opaque;
|
||||||
|
FileHandleList *prev = NULL;
|
||||||
FileHandleList *i;
|
FileHandleList *i;
|
||||||
FileHandleList *prev;
|
|
||||||
FileHandleList **_lists[] = { &openWriteList, &openReadList, NULL };
|
|
||||||
FileHandleList ***lists = _lists; /* gay. */
|
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
while (lists != NULL)
|
for (i = *list; i != NULL; i = i->next)
|
||||||
{
|
{
|
||||||
for (i = *(*lists), prev = NULL; i != NULL; prev = i, i = i->next)
|
if (&i->handle == handle) /* handle is in this list? */
|
||||||
{
|
{
|
||||||
if (i->handle == handle)
|
rc = h->funcs->fileClose(h);
|
||||||
{
|
if (!rc)
|
||||||
rc = h->funcs->fileClose(h);
|
return(-1);
|
||||||
if (!rc)
|
|
||||||
return(0);
|
|
||||||
|
|
||||||
if (prev == NULL)
|
if (prev == NULL)
|
||||||
*(*lists) = i->next;
|
*list = i->next;
|
||||||
else
|
else
|
||||||
prev->next = i->next;
|
prev->next = i->next;
|
||||||
|
|
||||||
free(handle);
|
free(i);
|
||||||
free(i);
|
return(1);
|
||||||
return(1);
|
} /* if */
|
||||||
} /* if */
|
prev = i;
|
||||||
} /* for */
|
} /* for */
|
||||||
lists++;
|
|
||||||
} /* while */
|
|
||||||
|
|
||||||
__PHYSFS_setError(ERR_NOT_A_HANDLE);
|
|
||||||
return(0);
|
return(0);
|
||||||
|
} /* closeHandleInOpenList */
|
||||||
|
|
||||||
|
|
||||||
|
int PHYSFS_close(PHYSFS_file *handle)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
/* -1 == close failure. 0 == not found. 1 == success. */
|
||||||
|
rc = closeHandleInOpenList(&openReadList, handle);
|
||||||
|
BAIL_IF_MACRO(rc == -1, NULL, 0);
|
||||||
|
if (!rc)
|
||||||
|
{
|
||||||
|
rc = closeHandleInOpenList(&openWriteList, handle);
|
||||||
|
BAIL_IF_MACRO(rc == -1, NULL, 0);
|
||||||
|
} /* if */
|
||||||
|
|
||||||
|
if (!rc)
|
||||||
|
__PHYSFS_setError(ERR_NOT_A_HANDLE);
|
||||||
|
|
||||||
|
return(rc);
|
||||||
} /* PHYSFS_close */
|
} /* PHYSFS_close */
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue