Cleaned out the "exists" nonsense in the stat() API.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 30 Nov 2012 12:43:56 -0500
changeset 1327 998351d485d9
parent 1326 ac0fac4fe6d6
child 1328 a5314f07614a
Cleaned out the "exists" nonsense in the stat() API.
src/archiver_dir.c
src/archiver_iso9660.c
src/archiver_lzma.c
src/archiver_unpacked.c
src/archiver_zip.c
src/physfs.c
src/physfs.h
src/physfs_internal.h
src/platform_posix.c
src/platform_windows.c
--- a/src/archiver_dir.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/archiver_dir.c	Fri Nov 30 12:43:56 2012 -0500
@@ -43,10 +43,9 @@
     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 @@
 } /* 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 @@
 } /* 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 @@
 } /* 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 */
--- a/src/archiver_iso9660.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/archiver_iso9660.c	Fri Nov 30 12:43:56 2012 -0500
@@ -398,7 +398,7 @@
  * 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 @@
 
     strcpy(pathcopy, path);
     mypath = pathcopy;
-    *exists = 0;
 
     readpos = handle->rootdirstart;
     end_of_dir = handle->rootdirstart + handle->rootdirsize;
@@ -442,10 +441,7 @@
         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 @@
             } /* 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 @@
     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 @@
     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 @@
 } /* 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;
 
--- a/src/archiver_lzma.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/archiver_lzma.c	Fri Nov 30 12:43:56 2012 -0500
@@ -639,13 +639,11 @@
     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 @@
 
 #endif  /* defined PHYSFS_SUPPORTS_7Z */
 
-/* end of lzma.c ... */
+/* end of archiver_lzma.c ... */
 
--- a/src/archiver_unpacked.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/archiver_unpacked.c	Fri Nov 30 12:43:56 2012 -0500
@@ -414,8 +414,7 @@
 } /* 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 @@
 
     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 */
 
--- a/src/archiver_zip.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/archiver_zip.c	Fri Nov 30 12:43:56 2012 -0500
@@ -1649,8 +1649,7 @@
 } /* 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 @@
 
     /* !!! 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;
--- a/src/physfs.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/physfs.c	Fri Nov 30 12:43:56 2012 -0500
@@ -728,6 +728,14 @@
 } /* __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 @@
     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 @@
     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 @@
             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 @@
         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 @@
             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 @@
         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 @@
                     /* !!! 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);
--- a/src/physfs.h	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/physfs.h	Fri Nov 30 12:43:56 2012 -0500
@@ -3496,8 +3496,7 @@
      *  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;
 
 /**
--- a/src/physfs_internal.h	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/physfs_internal.h	Fri Nov 30 12:43:56 2012 -0500
@@ -308,7 +308,7 @@
 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 @@
 /*
  * !!! 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
--- a/src/platform_posix.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/platform_posix.c	Fri Nov 30 12:43:56 2012 -0500
@@ -299,17 +299,11 @@
 } /* __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))
     {
--- a/src/platform_windows.c	Fri Nov 30 00:04:52 2012 -0500
+++ b/src/platform_windows.c	Fri Nov 30 12:43:56 2012 -0500
@@ -873,7 +873,8 @@
     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 @@
     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 */