PHYSFS_flush() shouldn't call PHYSFS_Io::flush().
authorRyan C. Gordon <icculus@icculus.org>
Tue, 27 Nov 2018 23:53:33 -0500
changeset 1650 8b3cc36531c6
parent 1649 9edc8d92bc61
child 1652 357f25632ea0
PHYSFS_flush() shouldn't call PHYSFS_Io::flush().

The former is meant to send PhysicsFS-buffered data to the PHYSFS_Io's
implementation, the latter is meant to tell the OS to definitely make sure the
data is safely written to disk (or at least, that's what it does in practice).

This was making PHYSFS_setBuffer()'d handles _slower_, since they would end
up blocking whenever the buffer was full until the data made the full trip to
physical media, instead of just letting the OS do its own buffering.

Now we still PHYSFS_Io::flush() on PHYSFS_close(), like this has always
worked. That might also be overkill, but that remains a historical artifact
of trying to keep the underlying file handle usable if pending writes fail
for possibly-recoverable reasons (which isn't guaranteed if you just close()
it, at least as far as I remember).
src/physfs.c
--- a/src/physfs.c	Wed Oct 17 23:44:02 2018 -0400
+++ b/src/physfs.c	Tue Nov 27 23:53:33 2018 -0500
@@ -2752,7 +2752,6 @@
 {
     FileHandle *prev = NULL;
     FileHandle *i;
-    int rc = 1;
 
     for (i = *list; i != NULL; i = i->next)
     {
@@ -2760,9 +2759,16 @@
         {
             PHYSFS_Io *io = handle->io;
             PHYSFS_uint8 *tmp = handle->buffer;
-            rc = PHYSFS_flush((PHYSFS_File *) handle);
-            if (!rc)
+
+            /* send our buffer to io... */
+            if (!PHYSFS_flush((PHYSFS_File *) handle))
                 return -1;
+
+            /* ...then have io send it to the disk... */
+            else if (io->flush && !io->flush(io))
+                return -1;
+
+            /* ...then close the underlying file. */
             io->destroy(io);
 
             if (tmp != NULL)  /* free any associated buffer. */
@@ -3060,7 +3066,7 @@
     rc = io->write(io, fh->buffer + fh->bufpos, fh->buffill - fh->bufpos);
     BAIL_IF_ERRPASS(rc <= 0, 0);
     fh->bufpos = fh->buffill = 0;
-    return io->flush ? io->flush(io) : 1;
+    return 1;
 } /* PHYSFS_flush */