Skip to content
Permalink
Browse files
Cleaned out the "exists" nonsense in the stat() API.
  • Loading branch information
icculus committed Nov 30, 2012
1 parent ada24e9 commit 7824a093fb99ba069c70b5cfd49614e770baa924
Showing with 63 additions and 85 deletions.
  1. +11 −21 src/archiver_dir.c
  2. +7 −15 src/archiver_iso9660.c
  3. +2 −4 src/archiver_lzma.c
  4. +1 −5 src/archiver_unpacked.c
  5. +3 −5 src/archiver_zip.c
  6. +22 −11 src/physfs.c
  7. +1 −2 src/physfs.h
  8. +2 −2 src/physfs_internal.h
  9. +2 −8 src/platform_posix.c
  10. +12 −12 src/platform_windows.c
@@ -43,10 +43,9 @@ static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
char *retval = NULL;
const size_t namelen = strlen(name);
const size_t seplen = 1;
int exists = 0;

assert(io == NULL); /* shouldn't create an Io for these. */
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &st), ERRPASS, NULL);
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &st), ERRPASS, NULL);
if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);

@@ -81,53 +80,45 @@ static void DIR_enumerateFiles(void *opaque, const char *dname,
} /* DIR_enumerateFiles */


static PHYSFS_Io *doOpen(void *opaque, const char *name,
const int mode, int *fileExists)
static PHYSFS_Io *doOpen(void *opaque, const char *name, const int mode)
{
char *f;
PHYSFS_Io *io = NULL;
int existtmp = 0;
char *f = NULL;

CVT_TO_DEPENDENT(f, opaque, name);
BAIL_IF_MACRO(!f, ERRPASS, NULL);

if (fileExists == NULL)
fileExists = &existtmp;

io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
{
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
PHYSFS_Stat statbuf;
__PHYSFS_platformStat(f, fileExists, &statbuf);
__PHYSFS_platformStat(f, &statbuf);
__PHYSFS_setError(err);
} /* if */
else
{
*fileExists = 1;
} /* else */

__PHYSFS_smallFree(f);

return io;
} /* doOpen */


static PHYSFS_Io *DIR_openRead(void *opaque, const char *fnm, int *exist)
static PHYSFS_Io *DIR_openRead(void *opaque, const char *filename, int *exists)
{
return doOpen(opaque, fnm, 'r', exist);
// !!! FIXME: exists
return doOpen(opaque, filename, 'r');
} /* DIR_openRead */


static PHYSFS_Io *DIR_openWrite(void *opaque, const char *filename)
{
return doOpen(opaque, filename, 'w', NULL);
return doOpen(opaque, filename, 'w');
} /* DIR_openWrite */


static PHYSFS_Io *DIR_openAppend(void *opaque, const char *filename)
{
return doOpen(opaque, filename, 'a', NULL);
return doOpen(opaque, filename, 'a');
} /* DIR_openAppend */


@@ -163,15 +154,14 @@ static void DIR_closeArchive(void *opaque)
} /* DIR_closeArchive */


static int DIR_stat(void *opaque, const char *name,
int *exists, PHYSFS_Stat *stat)
static int DIR_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
{
int retval = 0;
char *d;

CVT_TO_DEPENDENT(d, opaque, name);
BAIL_IF_MACRO(!d, ERRPASS, 0);
retval = __PHYSFS_platformStat(d, exists, stat);
retval = __PHYSFS_platformStat(d, stat);
__PHYSFS_smallFree(d);
return retval;
} /* DIR_stat */
@@ -398,7 +398,7 @@ static void iso_extractsubpath(char *path, char **subpath)
* a file needs to branch to the directory extent sooner or later.
*/
static int iso_find_dir_entry(ISO9660Handle *handle,const char *path,
ISO9660FileDescriptor *descriptor, int *exists)
ISO9660FileDescriptor *descriptor)
{
char *subpath = 0;
PHYSFS_uint64 readpos, end_of_dir;
@@ -409,7 +409,6 @@ static int iso_find_dir_entry(ISO9660Handle *handle,const char *path,

strcpy(pathcopy, path);
mypath = pathcopy;
*exists = 0;

readpos = handle->rootdirstart;
end_of_dir = handle->rootdirstart + handle->rootdirsize;
@@ -442,10 +441,7 @@ static int iso_find_dir_entry(ISO9660Handle *handle,const char *path,
if (strcmp(filename, mypath) == 0)
{
if ( (subpath == 0) || (subpath[0] == 0) )
{
*exists = 1;
return 0; /* no subpaths left and we found the entry */
} /* if */

if (descriptor->flags.directory)
{
@@ -458,12 +454,14 @@ static int iso_find_dir_entry(ISO9660Handle *handle,const char *path,
} /* if */
else
{
/* !!! FIXME: set PHYSFS_ERR_NOT_FOUND? */
/* we're at a file but have a remaining subpath -> no match */
return 0;
} /* else */
} /* if */
} /* while */

/* !!! FIXME: set PHYSFS_ERR_NOT_FOUND? */
return 0;
} /* iso_find_dir_entry */

@@ -783,9 +781,8 @@ static PHYSFS_Io *ISO9660_openRead(void *opaque, const char *filename,
GOTO_IF_MACRO(retval == 0, PHYSFS_ERR_OUT_OF_MEMORY, errorhandling);

/* find file descriptor */
rc = iso_find_dir_entry(handle, filename, &descriptor, exists);
rc = iso_find_dir_entry(handle, filename, &descriptor);
GOTO_IF_MACRO(rc, ERRPASS, errorhandling);
GOTO_IF_MACRO(!*exists, PHYSFS_ERR_NOT_FOUND, errorhandling);

fhandle->startblock = descriptor.extentpos + descriptor.extattributelen;
fhandle->filesize = descriptor.datalen;
@@ -835,9 +832,7 @@ static void ISO9660_enumerateFiles(void *opaque, const char *dname,
else
{
printf("pfad %s\n",dname);
int exists = 0;
BAIL_IF_MACRO(iso_find_dir_entry(handle,dname, &descriptor, &exists), ERRPASS,);
BAIL_IF_MACRO(!exists, ERRPASS, );
BAIL_IF_MACRO(iso_find_dir_entry(handle,dname, &descriptor), ERRPASS,);
BAIL_IF_MACRO(!descriptor.flags.directory, ERRPASS,);

readpos = descriptor.extentpos * 2048;
@@ -872,15 +867,12 @@ static void ISO9660_enumerateFiles(void *opaque, const char *dname,
} /* ISO9660_enumerateFiles */


static int ISO9660_stat(void *opaque, const char *name, int *exists,
PHYSFS_Stat *stat)
static int ISO9660_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
{
ISO9660Handle *handle = (ISO9660Handle*) opaque;
ISO9660FileDescriptor descriptor;
ISO9660ExtAttributeRec extattr;
BAIL_IF_MACRO(iso_find_dir_entry(handle, name, &descriptor, exists), ERRPASS, -1);
if (!*exists)
return 0;
BAIL_IF_MACRO(iso_find_dir_entry(handle, name, &descriptor), ERRPASS, -1);

stat->readonly = 1;

@@ -639,13 +639,11 @@ static int LZMA_mkdir(void *opaque, const char *name)
BAIL_MACRO(PHYSFS_ERR_READ_ONLY, 0);
} /* LZMA_mkdir */

static int LZMA_stat(void *opaque, const char *filename,
int *exists, PHYSFS_Stat *stat)
static int LZMA_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
{
const LZMAarchive *archive = (const LZMAarchive *) opaque;
const LZMAfile *file = lzma_find_file(archive, filename);

*exists = (file != 0);
if (!file)
return 0;

@@ -699,5 +697,5 @@ const PHYSFS_Archiver __PHYSFS_Archiver_LZMA =

#endif /* defined PHYSFS_SUPPORTS_7Z */

/* end of lzma.c ... */
/* end of archiver_lzma.c ... */

@@ -414,28 +414,24 @@ int UNPK_mkdir(void *opaque, const char *name)
} /* UNPK_mkdir */


int UNPK_stat(void *opaque, const char *filename,
int *exists, PHYSFS_Stat *stat)
int UNPK_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
{
int isDir = 0;
const UNPKinfo *info = (const UNPKinfo *) opaque;
const UNPKentry *entry = findEntry(info, filename, &isDir);

if (isDir)
{
*exists = 1;
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
} /* if */
else if (entry != NULL)
{
*exists = 1;
stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = entry->size;
} /* else if */
else
{
*exists = 0;
return 0;
} /* else */

@@ -1649,20 +1649,18 @@ static int ZIP_mkdir(void *opaque, const char *name)
} /* ZIP_mkdir */


static int ZIP_stat(void *opaque, const char *filename, int *exists,
PHYSFS_Stat *stat)
static int ZIP_stat(void *opaque, const char *filename, PHYSFS_Stat *stat)
{
int isDir = 0;
const ZIPinfo *info = (const ZIPinfo *) opaque;
const ZIPentry *entry = zip_find_entry(info, filename, &isDir);

/* !!! FIXME: does this need to resolve entries here? */

*exists = isDir || (entry != 0);
if (!*exists)
if ((!isDir) && (entry == NULL))
return 0;

if (isDir)
else if (isDir)
{
stat->filesize = 0;
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
@@ -728,6 +728,14 @@ void __PHYSFS_setError(const PHYSFS_ErrorCode errcode)
} /* __PHYSFS_setError */


/* this doesn't reset the error state. */
static inline PHYSFS_ErrorCode currentErrorCode(void)
{
const ErrState *err = findErrorForCurrentThread();
return err ? err->code : PHYSFS_ERR_OK;
} /* currentErrorCode */


PHYSFS_ErrorCode PHYSFS_getLastErrorCode(void)
{
ErrState *err = findErrorForCurrentThread();
@@ -1537,7 +1545,6 @@ const char *PHYSFS_getPrefDir(const char *org, const char *app)
PHYSFS_Stat statbuf;
char *ptr = NULL;
char *endstr = NULL;
int exists = 0;

BAIL_IF_MACRO(!initialized, PHYSFS_ERR_NOT_INITIALIZED, 0);
BAIL_IF_MACRO(!org, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
@@ -1554,7 +1561,7 @@ const char *PHYSFS_getPrefDir(const char *org, const char *app)
assert(*endstr == dirsep);
*endstr = '\0'; /* mask out the final dirsep for now. */

if (!__PHYSFS_platformStat(prefDir, &exists, &statbuf))
if (!__PHYSFS_platformStat(prefDir, &statbuf))
{
for (ptr = strchr(prefDir, dirsep); ptr; ptr = strchr(ptr+1, dirsep))
{
@@ -1968,9 +1975,12 @@ static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
end = strchr(start, '/');

if (end != NULL) *end = '\0';
rc = h->funcs->stat(h->opaque, fname, &retval, &statbuf);
rc = h->funcs->stat(h->opaque, fname, &statbuf);
if (rc)
rc = (statbuf.filetype == PHYSFS_FILETYPE_SYMLINK);
else if (currentErrorCode() == PHYSFS_ERR_NOT_FOUND)
retval = 0;

if (end != NULL) *end = '/';

/* insecure path (has a disallowed symlink in it)? */
@@ -2026,7 +2036,9 @@ static int doMkdir(const char *_dname, char *dname)
if (exists)
{
PHYSFS_Stat statbuf;
const int rc = h->funcs->stat(h->opaque, dname, &exists, &statbuf);
const int rc = h->funcs->stat(h->opaque, dname, &statbuf);
if ((!rc) && (currentErrorCode() == PHYSFS_ERR_NOT_FOUND))
exists = 0;
retval = ((rc) && (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY));
} /* if */

@@ -2123,11 +2135,9 @@ const char *PHYSFS_getRealDir(const char *_fname)
else if (verifyPath(i, &arcfname, 0))
{
PHYSFS_Stat statbuf;
int exists = 0;
if (i->funcs->stat(i->opaque, arcfname, &exists, &statbuf))
if (i->funcs->stat(i->opaque, arcfname, &statbuf))
{
if (exists)
retval = i->dirName;
retval = i->dirName;
break;
} /* if */
} /* if */
@@ -2265,10 +2275,9 @@ static void enumCallbackFilterSymLinks(void *_data, const char *origdir,
SymlinkFilterData *data = (SymlinkFilterData *) _data;
const DirHandle *dh = data->dirhandle;
PHYSFS_Stat statbuf;
int exists = 0;

sprintf(path, "%s%s%s", trimmedDir, *trimmedDir ? "/" : "", fname);
if (dh->funcs->stat(dh->opaque, path, &exists, &statbuf))
if (dh->funcs->stat(dh->opaque, path, &statbuf))
{
/* Pass it on to the application if it's not a symlink. */
if (statbuf.filetype != PHYSFS_FILETYPE_SYMLINK)
@@ -2871,7 +2880,9 @@ int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
/* !!! FIXME: this test is wrong and should be elsewhere. */
stat->readonly = !(writeDir &&
(strcmp(writeDir->dirName, i->dirName) == 0));
retval = i->funcs->stat(i->opaque, arcfname, &exists, stat);
retval = i->funcs->stat(i->opaque, arcfname, stat);
if ((retval) || (currentErrorCode() != PHYSFS_ERR_NOT_FOUND))
exists = 1;
} /* else if */
} /* for */
__PHYSFS_platformReleaseMutex(stateLock);
@@ -3496,8 +3496,7 @@ typedef struct PHYSFS_Archiver
* Returns non-zero on success, zero on failure.
* On failure, call PHYSFS_setErrorCode().
*/
// !!! FIXME: remove this exists nonsense (check error code instead)
int (*stat)(void *opaque, const char *fn, int *exists, PHYSFS_Stat *stat);
int (*stat)(void *opaque, const char *fn, PHYSFS_Stat *stat);
} PHYSFS_Archiver;

/**
@@ -308,7 +308,7 @@ PHYSFS_Io *UNPK_openWrite(void *opaque, const char *name);
PHYSFS_Io *UNPK_openAppend(void *opaque, const char *name);
int UNPK_remove(void *opaque, const char *name);
int UNPK_mkdir(void *opaque, const char *name);
int UNPK_stat(void *opaque, const char *fn, int *exist, PHYSFS_Stat *st);
int UNPK_stat(void *opaque, const char *fn, PHYSFS_Stat *st);


/*--------------------------------------------------------------------------*/
@@ -474,7 +474,7 @@ PHYSFS_sint64 __PHYSFS_platformFileLength(void *handle);
/*
* !!! FIXME: comment me.
*/
int __PHYSFS_platformStat(const char *fn, int *exists, PHYSFS_Stat *stat);
int __PHYSFS_platformStat(const char *fn, PHYSFS_Stat *stat);

/*
* Flush any pending writes to disk. (opaque) should be cast to whatever data
@@ -299,17 +299,11 @@ int __PHYSFS_platformDelete(const char *path)
} /* __PHYSFS_platformDelete */


int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *st)
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
{
struct stat statbuf;

if (lstat(filename, &statbuf) == -1)
{
*exists = (errno != ENOENT);
BAIL_MACRO(errcodeFromErrno(), 0);
} /* if */

*exists = 1;
BAIL_IF_MACRO(lstat(filename, &statbuf) == -1, errcodeFromErrno(), 0);

if (S_ISREG(statbuf.st_mode))
{

0 comments on commit 7824a09

Please sign in to comment.