Skip to content

Commit

Permalink
Cleaned out the "exists" nonsense in the stat() API.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Nov 30, 2012
1 parent ada24e9 commit 7824a09
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 85 deletions.
32 changes: 11 additions & 21 deletions src/archiver_dir.c
Expand Up @@ -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);

Expand Down Expand Up @@ -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 */


Expand Down Expand Up @@ -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 */
Expand Down
22 changes: 7 additions & 15 deletions src/archiver_iso9660.c
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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 */

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 2 additions & 4 deletions src/archiver_lzma.c
Expand Up @@ -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;

Expand Down Expand Up @@ -699,5 +697,5 @@ const PHYSFS_Archiver __PHYSFS_Archiver_LZMA =

#endif /* defined PHYSFS_SUPPORTS_7Z */

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

6 changes: 1 addition & 5 deletions src/archiver_unpacked.c
Expand Up @@ -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 */

Expand Down
8 changes: 3 additions & 5 deletions src/archiver_zip.c
Expand Up @@ -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;
Expand Down
33 changes: 22 additions & 11 deletions src/physfs.c
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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))
{
Expand Down Expand Up @@ -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)? */
Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/physfs.h
Expand Up @@ -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;

/**
Expand Down
4 changes: 2 additions & 2 deletions src/physfs_internal.h
Expand Up @@ -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);


/*--------------------------------------------------------------------------*/
Expand Down Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions src/platform_posix.c
Expand Up @@ -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))
{
Expand Down

0 comments on commit 7824a09

Please sign in to comment.