r426 - in trunk: . scripts

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Wed Jan 16 06:48:06 EST 2008


Author: icculus
Date: 2008-01-16 06:48:05 -0500 (Wed, 16 Jan 2008)
New Revision: 426

Modified:
   trunk/fileio.c
   trunk/fileio.h
   trunk/lua_glue.c
   trunk/scripts/mojosetup_mainline.lua
Log:
MojoInput_toPhysicalFile() and MojoSetup.writefile() can now take a maximum
 byte size, so you can write only the first X bytes of a MojoInput (or pass
 -1 or nil to write everything).


Modified: trunk/fileio.c
===================================================================
--- trunk/fileio.c	2008-01-16 10:12:18 UTC (rev 425)
+++ trunk/fileio.c	2008-01-16 11:48:05 UTC (rev 426)
@@ -81,7 +81,7 @@
 // !!! FIXME: I'd rather not use a callback here, but I can't see a cleaner
 // !!! FIXME:  way right now...
 boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
-                                 MojoChecksums *checksums,
+                                 MojoChecksums *checksums, int64 maxbytes,
                                  MojoInput_FileCopyCallback cb, void *data)
 {
     boolean retval = false;
@@ -113,6 +113,8 @@
     } // while
 
     flen = in->length(in);
+    if ((maxbytes >= 0) && (flen > maxbytes))
+        flen = maxbytes;
 
     MojoPlatform_unlink(fname);
     if (!iofailure)
@@ -127,14 +129,27 @@
         while (!iofailure)
         {
             int64 br = 0;
+            int64 maxread = sizeof (scratchbuf_128k);
 
+            // see if we need to clamp to eof or maxbytes...
+            if (flen >= 0)
+            {
+                const int64 avail = flen - bw;
+                if (avail < maxread)
+                {
+                    maxread = avail;
+                    if (maxread == 0)
+                        break;  // nothing left to do, break out.
+                } // if
+            } // if
+
             // If there's a callback, then poll. Otherwise, just block on
             //  the reads from the MojoInput.
             if ((cb != NULL) && (!in->ready(in)))
                 MojoPlatform_sleep(100);
             else
             {
-                br = in->read(in, scratchbuf_128k, sizeof (scratchbuf_128k));
+                br = in->read(in, scratchbuf_128k, maxread);
                 if (br == 0)  // we're done!
                     break;
                 else if (br < 0)
@@ -161,6 +176,8 @@
 
         if (MojoPlatform_close(out) != 0)
             iofailure = true;
+        else if (bw != flen)
+            iofailure = true;
 
         if (iofailure)
             MojoPlatform_unlink(fname);

Modified: trunk/fileio.h
===================================================================
--- trunk/fileio.h	2008-01-16 10:12:18 UTC (rev 425)
+++ trunk/fileio.h	2008-01-16 11:48:05 UTC (rev 426)
@@ -104,7 +104,7 @@
 typedef boolean (*MojoInput_FileCopyCallback)(uint32 ticks, int64 justwrote,
                                             int64 bw, int64 total, void *data);
 boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms,
-                                 MojoChecksums *checksums,
+                                 MojoChecksums *checksums, int64 maxbytes,
                                  MojoInput_FileCopyCallback cb, void *data);
 
 MojoInput *MojoInput_fromURL(const char *url);

Modified: trunk/lua_glue.c
===================================================================
--- trunk/lua_glue.c	2008-01-16 10:12:18 UTC (rev 425)
+++ trunk/lua_glue.c	2008-01-16 11:48:05 UTC (rev 426)
@@ -755,6 +755,8 @@
     uint16 perms = archive->prevEnum.perms;
     MojoChecksums sums;
     MojoInput *in = archive->openCurrentEntry(archive);
+    int64 maxbytes = -1;
+
     if (in != NULL)
     {
         if (!lua_isnil(L, 3))
@@ -765,8 +767,12 @@
             if (!valid)
                 fatal(_("BUG: '%0' is not a valid permission string"), permstr);
         } // if
-        rc = MojoInput_toPhysicalFile(in, path, perms, &sums,
-                                          writeCallback, L);
+
+        if (!lua_isnil(L, 4))
+            maxbytes = luaL_checkinteger(L, 4);
+
+        rc = MojoInput_toPhysicalFile(in, path, perms, &sums, maxbytes,
+                                      writeCallback, L);
     } // if
 
     retval += retvalBoolean(L, rc);
@@ -798,7 +804,7 @@
     if (in != NULL)
     {
         // !!! FIXME: Unix-specific permissions thing here.
-        rc = MojoInput_toPhysicalFile(in, dst, 0644, &sums,
+        rc = MojoInput_toPhysicalFile(in, dst, 0644, &sums, -1,
                                           writeCallback, L);
     } // if
 
@@ -965,7 +971,7 @@
         {
             uint16 perms = 0;
             MojoPlatform_perms(src, &perms);
-            retval = MojoInput_toPhysicalFile(in, dst, perms, NULL, NULL, NULL);
+            retval = MojoInput_toPhysicalFile(in,dst,perms,NULL,-1,NULL,NULL);
             if (retval)
             {
                 retval = MojoPlatform_unlink(src);
@@ -1002,7 +1008,7 @@
         if (!valid)
             fatal(_("BUG: '%0' is not a valid permission string"), permstr);
     } // if
-    rc = MojoInput_toPhysicalFile(in, path, perms, &sums, writeCallback, L);
+    rc = MojoInput_toPhysicalFile(in, path, perms, &sums, -1, writeCallback, L);
 
     retval += retvalBoolean(L, rc);
     if (rc)

Modified: trunk/scripts/mojosetup_mainline.lua
===================================================================
--- trunk/scripts/mojosetup_mainline.lua	2008-01-16 10:12:18 UTC (rev 425)
+++ trunk/scripts/mojosetup_mainline.lua	2008-01-16 11:48:05 UTC (rev 426)
@@ -299,7 +299,7 @@
 
 local function install_file_from_archive(dest, archive, perms, desc, manifestkey)
     local fn = function(callback)
-        return MojoSetup.writefile(archive, dest, perms, callback)
+        return MojoSetup.writefile(archive, dest, perms, nil, callback)
     end
     return install_file(dest, perms, fn, desc, manifestkey)
 end




More information about the mojosetup-commits mailing list