Cleaned out the "exists" nonsense in the stat() API.

This commit is contained in:
Ryan C. Gordon 2012-11-30 12:43:56 -05:00
parent ada24e9c5d
commit 7824a093fb
10 changed files with 63 additions and 85 deletions

View File

@ -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,31 +80,22 @@ 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);
@ -113,21 +103,22 @@ static PHYSFS_Io *doOpen(void *opaque, const char *name,
} /* 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 */

View File

@ -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;

View File

@ -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 ... */

View File

@ -414,8 +414,7 @@ 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;
@ -423,19 +422,16 @@ int UNPK_stat(void *opaque, const char *filename,
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 */

View File

@ -1649,8 +1649,7 @@ 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;
@ -1658,11 +1657,10 @@ static int ZIP_stat(void *opaque, const char *filename, int *exists,
/* !!! 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;

View File

@ -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);

View File

@ -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;
/**

View File

@ -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

View File

@ -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))
{

View File

@ -873,7 +873,8 @@ static PHYSFS_sint64 FileTimeToPhysfsTime(const FILETIME *ft)
return retval;
} /* FileTimeToPhysfsTime */
int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *stat)
int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
{
WIN32_FILE_ATTRIBUTE_DATA winstat;
WCHAR *wstr = NULL;
@ -884,37 +885,36 @@ int __PHYSFS_platformStat(const char *filename, int *exists, PHYSFS_Stat *stat)
BAIL_IF_MACRO(!wstr, PHYSFS_ERR_OUT_OF_MEMORY, 0);
rc = GetFileAttributesExW(wstr, GetFileExInfoStandard, &winstat);
err = (!rc) ? GetLastError() : 0;
*exists = ((err != ERROR_FILE_NOT_FOUND) && (err != ERROR_PATH_NOT_FOUND));
__PHYSFS_smallFree(wstr);
BAIL_IF_MACRO(!rc, errcodeFromWinApiError(err), 0);
stat->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
stat->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
stat->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
st->modtime = FileTimeToPhysfsTime(&winstat.ftLastWriteTime);
st->accesstime = FileTimeToPhysfsTime(&winstat.ftLastAccessTime);
st->createtime = FileTimeToPhysfsTime(&winstat.ftCreationTime);
if(winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
stat->filetype = PHYSFS_FILETYPE_DIRECTORY;
stat->filesize = 0;
st->filetype = PHYSFS_FILETYPE_DIRECTORY;
st->filesize = 0;
} /* if */
else if(winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE))
{
/* !!! FIXME: what are reparse points? */
stat->filetype = PHYSFS_FILETYPE_OTHER;
st->filetype = PHYSFS_FILETYPE_OTHER;
/* !!! FIXME: don't rely on this */
stat->filesize = 0;
st->filesize = 0;
} /* else if */
/* !!! FIXME: check for symlinks on Vista. */
else
{
stat->filetype = PHYSFS_FILETYPE_REGULAR;
stat->filesize = (((PHYSFS_uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow;
st->filetype = PHYSFS_FILETYPE_REGULAR;
st->filesize = (((PHYSFS_uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow;
} /* else */
stat->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
st->readonly = ((winstat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0);
return 1;
} /* __PHYSFS_platformStat */