Expose sysCopyFileToFile() and use it.

Moved from ZipArchive.c, with tweaks to for argument order and name. Also
tweaked a related function in ZipArchive to match.

Change-Id: I4d2e3c8b44386d87c5aa1c4565f72da87357fce8
diff --git a/libdex/SysUtil.c b/libdex/SysUtil.c
index e2b1fff..951ca1d 100644
--- a/libdex/SysUtil.c
+++ b/libdex/SysUtil.c
@@ -406,3 +406,28 @@
 
     return 0;
 }
+
+/* See documentation comment in header file. */
+int sysCopyFileToFile(int outFd, int inFd, size_t count)
+{
+    const size_t kBufSize = 32768;
+    unsigned char buf[kBufSize];
+
+    while (count != 0) {
+        size_t getSize = (count > kBufSize) ? kBufSize : count;
+
+        ssize_t actual = TEMP_FAILURE_RETRY(read(inFd, buf, getSize));
+        if (actual != (ssize_t) getSize) {
+            LOGW("sysCopyFileToFile: copy read failed (%d vs %zd)\n",
+                (int) actual, getSize);
+            return -1;
+        }
+
+        if (sysWriteFully(outFd, buf, getSize, "sysCopyFileToFile") != 0)
+            return -1;
+
+        count -= getSize;
+    }
+
+    return 0;
+}
diff --git a/libdex/SysUtil.h b/libdex/SysUtil.h
index 01b4e1a..4de0289 100644
--- a/libdex/SysUtil.h
+++ b/libdex/SysUtil.h
@@ -113,4 +113,10 @@
  */
 int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg);
 
+/*
+ * Copy the given number of bytes from one fd to another. Returns
+ * 0 on success, -1 on failure.
+ */
+int sysCopyFileToFile(int outFd, int inFd, size_t count);
+
 #endif /*_DALVIK_SYSUTIL*/
diff --git a/libdex/ZipArchive.c b/libdex/ZipArchive.c
index 756f488..1f65a23 100644
--- a/libdex/ZipArchive.c
+++ b/libdex/ZipArchive.c
@@ -573,7 +573,7 @@
  * Uncompress "deflate" data from the archive's file to an open file
  * descriptor.
  */
-static int inflateToFile(int inFd, int outFd, size_t uncompLen, size_t compLen)
+static int inflateToFile(int outFd, int inFd, size_t uncompLen, size_t compLen)
 {
     int result = -1;
     const size_t kBufSize = 32768;
@@ -677,32 +677,6 @@
 }
 
 /*
- * Copy bytes from input to output.
- */
-static int copyFileToFile(int inFd, int outFd, size_t uncompLen)
-{
-    const size_t kBufSize = 32768;
-    unsigned char buf[kBufSize];
-
-    while (uncompLen != 0) {
-        size_t getSize = (uncompLen > kBufSize) ? kBufSize : uncompLen;
-
-        ssize_t actual = TEMP_FAILURE_RETRY(read(inFd, buf, getSize));
-        if (actual != (ssize_t) getSize) {
-            LOGW("Zip: copy read failed (%d vs %zd)\n", (int)actual, getSize);
-            return -1;
-        }
-
-        if (sysWriteFully(outFd, buf, getSize, "Zip copy") != 0)
-            return -1;
-
-        uncompLen -= getSize;
-    }
-
-    return 0;
-}
-
-/*
  * Uncompress an entry, in its entirety, to an open file descriptor.
  *
  * TODO: this doesn't verify the data's CRC, but probably should (especially
@@ -733,10 +707,10 @@
     }
 
     if (method == kCompressStored) {
-        if (copyFileToFile(pArchive->mFd, fd, uncompLen) != 0)
+        if (sysCopyFileToFile(fd, pArchive->mFd, uncompLen) != 0)
             goto bail;
     } else {
-        if (inflateToFile(pArchive->mFd, fd, uncompLen, compLen) != 0)
+        if (inflateToFile(fd, pArchive->mFd, uncompLen, compLen) != 0)
             goto bail;
     }
 
diff --git a/vm/RawDexFile.c b/vm/RawDexFile.c
index 1d8784c..5da4907 100644
--- a/vm/RawDexFile.c
+++ b/vm/RawDexFile.c
@@ -30,57 +30,20 @@
  * Copy the given number of bytes from one fd to another, first
  * seeking the source fd to the start of the file.
  */
-static int copyFileToFile(int destFd, int srcFd, u4 size)
+static int copyFileToFile(int destFd, int srcFd, size_t size)
 {
-    u1* buf = malloc(size);
-    int result = -1;
-    ssize_t amt;
-
-    if (buf == NULL) {
-        LOGE("malloc failure (errno %d)\n", errno);
-        goto bail;
-    }
-
     if (lseek(srcFd, 0, SEEK_SET) != 0) {
         LOGE("lseek failure (errno %d)\n", errno);
-        goto bail;
+        return -1;
     }
 
-    amt = read(srcFd, buf, size);
-
-    if (amt < 0) {
-        LOGE("read failure (errno %d)\n", errno);
-        goto bail;
-    }
-
-    if (amt != (ssize_t) size) {
-        LOGE("short read (%d < %ud)\n", (int) amt, size);
-        goto bail;
-    }
-
-    amt = write(destFd, buf, size);
-
-    if (amt < 0) {
-        LOGE("write failure (errno %d)\n", errno);
-        goto bail;
-    }
-
-    if (amt != (ssize_t) size) {
-        LOGE("short write (%d < %ud)\n", (int) amt, size);
-        goto bail;
-    }
-
-    result = 0; // Success!
-
-bail:
-    free(buf);
-    return result;
+    return sysCopyFileToFile(destFd, srcFd, size);
 }
 
 /*
  * Get the modification time and size in bytes for the given fd.
  */
-static int getModTimeAndSize(int fd, u4* modTime, u4* size)
+static int getModTimeAndSize(int fd, u4* modTime, size_t* size)
 {
     struct stat buf;
     int result = fstat(fd, &buf);
@@ -91,7 +54,8 @@
     }
 
     *modTime = (u4) buf.st_mtime;
-    *size = (u4) buf.st_size;
+    *size = (size_t) buf.st_size;
+    assert((size_t) buf.st_size == buf.st_size);
 
     return 0;
 }
@@ -161,7 +125,7 @@
     int optFd = -1;
     u4 modTime = 0;
     u4 adler32 = 0;
-    u4 fileSize = 0;
+    size_t fileSize = 0;
     bool newFile = false;
     bool locked = false;