retry patch using cache if in-place write fails

[cherry-pick from donut branch]

Instead of failing outright when yaffs lies about how much space is
free on the filesystem, delete the partially-written output file and
retry using the copy-source-to-cache method instead.

Change all the log statements to printf; mixing stdout and stderr
logging just makes the logs hard to read because they're buffered
differently.
diff --git a/tools/applypatch/applypatch.c b/tools/applypatch/applypatch.c
index 394c584..e69a2fb 100644
--- a/tools/applypatch/applypatch.c
+++ b/tools/applypatch/applypatch.c
@@ -21,6 +21,8 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <sys/statfs.h>
+#include <sys/types.h>
+#include <fcntl.h>
 #include <unistd.h>
 
 #include "mincrypt/sha.h"
@@ -30,6 +32,7 @@
 int SaveFileContents(const char* filename, FileContents file);
 int LoadMTDContents(const char* filename, FileContents* file);
 int ParseSha1(const char* str, uint8_t* digest);
+size_t FileSink(unsigned char* data, size_t len, void* token);
 
 static int mtd_partitions_scanned = 0;
 
@@ -45,7 +48,7 @@
   }
 
   if (stat(filename, &file->st) != 0) {
-    fprintf(stderr, "failed to stat \"%s\": %s\n", filename, strerror(errno));
+    printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
     return -1;
   }
 
@@ -54,7 +57,7 @@
 
   FILE* f = fopen(filename, "rb");
   if (f == NULL) {
-    fprintf(stderr, "failed to open \"%s\": %s\n", filename, strerror(errno));
+    printf("failed to open \"%s\": %s\n", filename, strerror(errno));
     free(file->data);
     file->data = NULL;
     return -1;
@@ -62,7 +65,7 @@
 
   size_t bytes_read = fread(file->data, 1, file->size, f);
   if (bytes_read != file->size) {
-    fprintf(stderr, "short read of \"%s\" (%d bytes of %d)\n",
+    printf("short read of \"%s\" (%d bytes of %d)\n",
             filename, bytes_read, file->size);
     free(file->data);
     file->data = NULL;
@@ -108,7 +111,7 @@
   char* copy = strdup(filename);
   const char* magic = strtok(copy, ":");
   if (strcmp(magic, "MTD") != 0) {
-    fprintf(stderr, "LoadMTDContents called with bad filename (%s)\n",
+    printf("LoadMTDContents called with bad filename (%s)\n",
             filename);
     return -1;
   }
@@ -122,7 +125,7 @@
     }
   }
   if (colons < 3 || colons%2 == 0) {
-    fprintf(stderr, "LoadMTDContents called with bad filename (%s)\n",
+    printf("LoadMTDContents called with bad filename (%s)\n",
             filename);
   }
 
@@ -135,7 +138,7 @@
     const char* size_str = strtok(NULL, ":");
     size[i] = strtol(size_str, NULL, 10);
     if (size[i] == 0) {
-      fprintf(stderr, "LoadMTDContents called with bad size (%s)\n", filename);
+      printf("LoadMTDContents called with bad size (%s)\n", filename);
       return -1;
     }
     sha1sum[i] = strtok(NULL, ":");
@@ -154,14 +157,14 @@
 
   const MtdPartition* mtd = mtd_find_partition_by_name(partition);
   if (mtd == NULL) {
-    fprintf(stderr, "mtd partition \"%s\" not found (loading %s)\n",
+    printf("mtd partition \"%s\" not found (loading %s)\n",
             partition, filename);
     return -1;
   }
 
   MtdReadContext* ctx = mtd_read_partition(mtd);
   if (ctx == NULL) {
-    fprintf(stderr, "failed to initialize read of mtd partition \"%s\"\n",
+    printf("failed to initialize read of mtd partition \"%s\"\n",
             partition);
     return -1;
   }
@@ -184,7 +187,7 @@
     if (next > 0) {
       read = mtd_read_data(ctx, p, next);
       if (next != read) {
-        fprintf(stderr, "short read (%d bytes of %d) for partition \"%s\"\n",
+        printf("short read (%d bytes of %d) for partition \"%s\"\n",
                 read, next, partition);
         free(file->data);
         file->data = NULL;
@@ -201,7 +204,7 @@
     const uint8_t* sha_so_far = SHA_final(&temp_ctx);
 
     if (ParseSha1(sha1sum[index[i]], parsed_sha) != 0) {
-      fprintf(stderr, "failed to parse sha1 %s in %s\n",
+      printf("failed to parse sha1 %s in %s\n",
               sha1sum[index[i]], filename);
       free(file->data);
       file->data = NULL;
@@ -224,7 +227,7 @@
   if (i == pairs) {
     // Ran off the end of the list of (size,sha1) pairs without
     // finding a match.
-    fprintf(stderr, "contents of MTD partition \"%s\" didn't match %s\n",
+    printf("contents of MTD partition \"%s\" didn't match %s\n",
             partition, filename);
     free(file->data);
     file->data = NULL;
@@ -253,29 +256,29 @@
 // Save the contents of the given FileContents object under the given
 // filename.  Return 0 on success.
 int SaveFileContents(const char* filename, FileContents file) {
-  FILE* f = fopen(filename, "wb");
-  if (f == NULL) {
-    fprintf(stderr, "failed to open \"%s\" for write: %s\n",
+  int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC);
+  if (fd < 0) {
+    printf("failed to open \"%s\" for write: %s\n",
             filename, strerror(errno));
     return -1;
   }
 
-  size_t bytes_written = fwrite(file.data, 1, file.size, f);
+  size_t bytes_written = FileSink(file.data, file.size, &fd);
   if (bytes_written != file.size) {
-    fprintf(stderr, "short write of \"%s\" (%d bytes of %d)\n",
-            filename, bytes_written, file.size);
+    printf("short write of \"%s\" (%d bytes of %d) (%s)\n",
+           filename, bytes_written, file.size, strerror(errno));
+    close(fd);
     return -1;
   }
-  fflush(f);
-  fsync(fileno(f));
-  fclose(f);
+  fsync(fd);
+  close(fd);
 
   if (chmod(filename, file.st.st_mode) != 0) {
-    fprintf(stderr, "chmod of \"%s\" failed: %s\n", filename, strerror(errno));
+    printf("chmod of \"%s\" failed: %s\n", filename, strerror(errno));
     return -1;
   }
   if (chown(filename, file.st.st_uid, file.st.st_gid) != 0) {
-    fprintf(stderr, "chown of \"%s\" failed: %s\n", filename, strerror(errno));
+    printf("chown of \"%s\" failed: %s\n", filename, strerror(errno));
     return -1;
   }
 
@@ -288,7 +291,7 @@
                         const char* target_mtd) {
   char* partition = strchr(target_mtd, ':');
   if (partition == NULL) {
-    fprintf(stderr, "bad MTD target name \"%s\"\n", target_mtd);
+    printf("bad MTD target name \"%s\"\n", target_mtd);
     return -1;
   }
   ++partition;
@@ -306,33 +309,33 @@
 
   const MtdPartition* mtd = mtd_find_partition_by_name(partition);
   if (mtd == NULL) {
-    fprintf(stderr, "mtd partition \"%s\" not found for writing\n", partition);
+    printf("mtd partition \"%s\" not found for writing\n", partition);
     return -1;
   }
 
   MtdWriteContext* ctx = mtd_write_partition(mtd);
   if (ctx == NULL) {
-    fprintf(stderr, "failed to init mtd partition \"%s\" for writing\n",
+    printf("failed to init mtd partition \"%s\" for writing\n",
             partition);
     return -1;
   }
 
   size_t written = mtd_write_data(ctx, (char*)data, len);
   if (written != len) {
-    fprintf(stderr, "only wrote %d of %d bytes to MTD %s\n",
+    printf("only wrote %d of %d bytes to MTD %s\n",
             written, len, partition);
     mtd_write_close(ctx);
     return -1;
   }
 
   if (mtd_erase_blocks(ctx, -1) < 0) {
-    fprintf(stderr, "error finishing mtd write of %s\n", partition);
+    printf("error finishing mtd write of %s\n", partition);
     mtd_write_close(ctx);
     return -1;
   }
 
   if (mtd_write_close(ctx)) {
-    fprintf(stderr, "error closing mtd write of %s\n", partition);
+    printf("error closing mtd write of %s\n", partition);
     return -1;
   }
 
@@ -381,7 +384,7 @@
   int i;
   for (i = 0; i < *num_patches; ++i) {
     if (ParseSha1(argv[i], (*patches)[i].sha1) != 0) {
-      fprintf(stderr, "failed to parse sha1 \"%s\"\n", argv[i]);
+      printf("failed to parse sha1 \"%s\"\n", argv[i]);
       return -1;
     }
     if (argv[i][SHA_DIGEST_SIZE*2] == '\0') {
@@ -389,7 +392,7 @@
     } else if (argv[i][SHA_DIGEST_SIZE*2] == ':') {
       (*patches)[i].patch_filename = argv[i] + (SHA_DIGEST_SIZE*2+1);
     } else {
-      fprintf(stderr, "failed to parse filename \"%s\"\n", argv[i]);
+      printf("failed to parse filename \"%s\"\n", argv[i]);
       return -1;
     }
   }
@@ -414,7 +417,7 @@
 // nonzero otherwise.
 int CheckMode(int argc, char** argv) {
   if (argc < 3) {
-    fprintf(stderr, "no filename given\n");
+    printf("no filename given\n");
     return 2;
   }
 
@@ -432,7 +435,7 @@
   if (LoadFileContents(argv[2], &file) != 0 ||
       (num_patches > 0 &&
        FindMatchingPatch(file.sha1, patches, num_patches) == NULL)) {
-    fprintf(stderr, "file \"%s\" doesn't have any of expected "
+    printf("file \"%s\" doesn't have any of expected "
             "sha1 sums; checking cache\n", argv[2]);
 
     free(file.data);
@@ -444,12 +447,12 @@
     // passes.
 
     if (LoadFileContents(CACHE_TEMP_SOURCE, &file) != 0) {
-      fprintf(stderr, "failed to load cache file\n");
+      printf("failed to load cache file\n");
       return 1;
     }
 
     if (FindMatchingPatch(file.sha1, patches, num_patches) == NULL) {
-      fprintf(stderr, "cache bits don't match any sha1 for \"%s\"\n",
+      printf("cache bits don't match any sha1 for \"%s\"\n",
               argv[2]);
       return 1;
     }
@@ -465,7 +468,19 @@
 }
 
 size_t FileSink(unsigned char* data, size_t len, void* token) {
-  return fwrite(data, 1, len, (FILE*)token);
+  int fd = *(int *)token;
+  ssize_t done = 0;
+  ssize_t wrote;
+  while (done < (ssize_t) len) {
+    wrote = write(fd, data+done, len-done);
+    if (wrote <= 0) {
+      printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno));
+      return done;
+    }
+    done += wrote;
+  }
+  printf("wrote %d bytes to output\n", (int)done);
+  return done;
 }
 
 typedef struct {
@@ -489,7 +504,7 @@
 size_t FreeSpaceForFile(const char* filename) {
   struct statfs sf;
   if (statfs(filename, &sf) != 0) {
-    fprintf(stderr, "failed to statfs %s: %s\n", filename, strerror(errno));
+    printf("failed to statfs %s: %s\n", filename, strerror(errno));
     return -1;
   }
   return sf.f_bsize * sf.f_bfree;
@@ -583,8 +598,10 @@
     target_filename = source_filename;
   }
 
- if (ParseSha1(argv[3], target_sha1) != 0) {
-    fprintf(stderr, "failed to parse tgt-sha1 \"%s\"\n", argv[3]);
+  printf("\napplying patch to %s\n", source_filename);
+
+  if (ParseSha1(argv[3], target_sha1) != 0) {
+    printf("failed to parse tgt-sha1 \"%s\"\n", argv[3]);
     return 1;
   }
 
@@ -605,7 +622,7 @@
     if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) {
       // The early-exit case:  the patch was already applied, this file
       // has the desired hash, nothing for us to do.
-      fprintf(stderr, "\"%s\" is already target; no patch needed\n",
+      printf("\"%s\" is already target; no patch needed\n",
               target_filename);
       return 0;
     }
@@ -630,11 +647,11 @@
 
   if (source_patch_filename == NULL) {
     free(source_file.data);
-    fprintf(stderr, "source file is bad; trying copy\n");
+    printf("source file is bad; trying copy\n");
 
     if (LoadFileContents(CACHE_TEMP_SOURCE, &copy_file) < 0) {
       // fail.
-      fprintf(stderr, "failed to read copy file\n");
+      printf("failed to read copy file\n");
       return 1;
     }
 
@@ -646,179 +663,205 @@
 
     if (copy_patch_filename == NULL) {
       // fail.
-      fprintf(stderr, "copy file doesn't match source SHA-1s either\n");
+      printf("copy file doesn't match source SHA-1s either\n");
       return 1;
     }
   }
 
-  // Is there enough room in the target filesystem to hold the patched
-  // file?
+  int retry = 1;
+  SHA_CTX ctx;
+  int output;
+  MemorySinkInfo msi;
+  FileContents* source_to_use;
+  char* outname;
 
-  if (strncmp(target_filename, "MTD:", 4) == 0) {
-    // If the target is an MTD partition, we're actually going to
-    // write the output to /tmp and then copy it to the partition.
-    // statfs() always returns 0 blocks free for /tmp, so instead
-    // we'll just assume that /tmp has enough space to hold the file.
-
-    // We still write the original source to cache, in case the MTD
-    // write is interrupted.
-    if (MakeFreeSpaceOnCache(source_file.size) < 0) {
-      fprintf(stderr, "not enough free space on /cache\n");
-      return 1;
-    }
-    if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
-      fprintf(stderr, "failed to back up source file\n");
-      return 1;
-    }
-    made_copy = 1;
+  // assume that target_filename (eg "/system/app/Foo.apk") is located
+  // on the same filesystem as its top-level directory ("/system").
+  // We need something that exists for calling statfs().
+  char target_fs[strlen(target_filename)+1];
+  char* slash = strchr(target_filename+1, '/');
+  if (slash != NULL) {
+    int count = slash - target_filename;
+    strncpy(target_fs, target_filename, count);
+    target_fs[count] = '\0';
   } else {
-    // assume that target_filename (eg "/system/app/Foo.apk") is located
-    // on the same filesystem as its top-level directory ("/system").
-    // We need something that exists for calling statfs().
-    char* target_fs = strdup(target_filename);
-    char* slash = strchr(target_fs+1, '/');
-    if (slash != NULL) {
-      *slash = '\0';
-    }
+    strcpy(target_fs, target_filename);
+  }
 
-    size_t free_space = FreeSpaceForFile(target_fs);
-    int enough_space =
-        free_space > (target_size * 3 / 2);  // 50% margin of error
-    printf("target %ld bytes; free space %ld bytes; enough %d\n",
-           (long)target_size, (long)free_space, enough_space);
+  do {
+    // Is there enough room in the target filesystem to hold the patched
+    // file?
 
-    if (!enough_space && source_patch_filename != NULL) {
-      // Using the original source, but not enough free space.  First
-      // copy the source file to cache, then delete it from the original
-      // location.
+    if (strncmp(target_filename, "MTD:", 4) == 0) {
+      // If the target is an MTD partition, we're actually going to
+      // write the output to /tmp and then copy it to the partition.
+      // statfs() always returns 0 blocks free for /tmp, so instead
+      // we'll just assume that /tmp has enough space to hold the file.
 
-      if (strncmp(source_filename, "MTD:", 4) == 0) {
-        // It's impossible to free space on the target filesystem by
-        // deleting the source if the source is an MTD partition.  If
-        // we're ever in a state where we need to do this, fail.
-        fprintf(stderr, "not enough free space for target but source is MTD\n");
-        return 1;
-      }
-
+      // We still write the original source to cache, in case the MTD
+      // write is interrupted.
       if (MakeFreeSpaceOnCache(source_file.size) < 0) {
-        fprintf(stderr, "not enough free space on /cache\n");
+        printf("not enough free space on /cache\n");
         return 1;
       }
-
       if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
-        fprintf(stderr, "failed to back up source file\n");
+        printf("failed to back up source file\n");
         return 1;
       }
       made_copy = 1;
-      unlink(source_filename);
+      retry = 0;
+    } else {
+      int enough_space = 0;
+      if (retry > 0) {
+        size_t free_space = FreeSpaceForFile(target_fs);
+        int enough_space =
+          (free_space > (target_size * 3 / 2));  // 50% margin of error
+        printf("target %ld bytes; free space %ld bytes; retry %d; enough %d\n",
+               (long)target_size, (long)free_space, retry, enough_space);
+      }
 
-      size_t free_space = FreeSpaceForFile(target_fs);
-      printf("(now %ld bytes free for target)\n", (long)free_space);
+      if (!enough_space) {
+        retry = 0;
+      }
+
+      if (!enough_space && source_patch_filename != NULL) {
+        // Using the original source, but not enough free space.  First
+        // copy the source file to cache, then delete it from the original
+        // location.
+
+        if (strncmp(source_filename, "MTD:", 4) == 0) {
+          // It's impossible to free space on the target filesystem by
+          // deleting the source if the source is an MTD partition.  If
+          // we're ever in a state where we need to do this, fail.
+          printf("not enough free space for target but source is MTD\n");
+          return 1;
+        }
+
+        if (MakeFreeSpaceOnCache(source_file.size) < 0) {
+          printf("not enough free space on /cache\n");
+          return 1;
+        }
+
+        if (SaveFileContents(CACHE_TEMP_SOURCE, source_file) < 0) {
+          printf("failed to back up source file\n");
+          return 1;
+        }
+        made_copy = 1;
+        unlink(source_filename);
+
+        size_t free_space = FreeSpaceForFile(target_fs);
+        printf("(now %ld bytes free for target)\n", (long)free_space);
+      }
     }
-  }
 
-  FileContents* source_to_use;
-  const char* patch_filename;
-  if (source_patch_filename != NULL) {
-    source_to_use = &source_file;
-    patch_filename = source_patch_filename;
-  } else {
-    source_to_use = &copy_file;
-    patch_filename = copy_patch_filename;
-  }
-
-  char* outname = NULL;
-  FILE* output = NULL;
-  MemorySinkInfo msi;
-  SinkFn sink = NULL;
-  void* token = NULL;
-  if (strncmp(target_filename, "MTD:", 4) == 0) {
-    // We store the decoded output in memory.
-    msi.buffer = malloc(target_size);
-    if (msi.buffer == NULL) {
-      fprintf(stderr, "failed to alloc %ld bytes for output\n",
-              (long)target_size);
-      return 1;
+    const char* patch_filename;
+    if (source_patch_filename != NULL) {
+      source_to_use = &source_file;
+      patch_filename = source_patch_filename;
+    } else {
+      source_to_use = &copy_file;
+      patch_filename = copy_patch_filename;
     }
-    msi.pos = 0;
-    msi.size = target_size;
-    sink = MemorySink;
-    token = &msi;
-  } else {
-    // We write the decoded output to "<tgt-file>.patch".
-    outname = (char*)malloc(strlen(target_filename) + 10);
-    strcpy(outname, target_filename);
-    strcat(outname, ".patch");
 
-    output = fopen(outname, "wb");
-    if (output == NULL) {
-      fprintf(stderr, "failed to open output file %s: %s\n",
-              outname, strerror(errno));
-      return 1;
+    SinkFn sink = NULL;
+    void* token = NULL;
+    output = -1;
+    outname = NULL;
+    if (strncmp(target_filename, "MTD:", 4) == 0) {
+      // We store the decoded output in memory.
+      msi.buffer = malloc(target_size);
+      if (msi.buffer == NULL) {
+        printf("failed to alloc %ld bytes for output\n",
+               (long)target_size);
+        return 1;
+      }
+      msi.pos = 0;
+      msi.size = target_size;
+      sink = MemorySink;
+      token = &msi;
+    } else {
+      // We write the decoded output to "<tgt-file>.patch".
+      outname = (char*)malloc(strlen(target_filename) + 10);
+      strcpy(outname, target_filename);
+      strcat(outname, ".patch");
+
+      output = open(outname, O_WRONLY | O_CREAT | O_TRUNC);
+      if (output < 0) {
+        printf("failed to open output file %s: %s\n",
+               outname, strerror(errno));
+        return 1;
+      }
+      sink = FileSink;
+      token = &output;
     }
-    sink = FileSink;
-    token = output;
-  }
 
 #define MAX_HEADER_LENGTH 8
-  unsigned char header[MAX_HEADER_LENGTH];
-  FILE* patchf = fopen(patch_filename, "rb");
-  if (patchf == NULL) {
-    fprintf(stderr, "failed to open patch file %s: %s\n",
-            patch_filename, strerror(errno));
-    return 1;
-  }
-  int header_bytes_read = fread(header, 1, MAX_HEADER_LENGTH, patchf);
-  fclose(patchf);
-
-  SHA_CTX ctx;
-  SHA_init(&ctx);
-
-  if (header_bytes_read >= 4 &&
-      header[0] == 0xd6 && header[1] == 0xc3 &&
-      header[2] == 0xc4 && header[3] == 0) {
-    // xdelta3 patches begin "VCD" (with the high bits set) followed
-    // by a zero byte (the version number).
-    fprintf(stderr, "error:  xdelta3 patches no longer supported\n");
-    return 1;
-  } else if (header_bytes_read >= 8 &&
-             memcmp(header, "BSDIFF40", 8) == 0) {
-    int result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
-                                  patch_filename, 0, sink, token, &ctx);
-    if (result != 0) {
-      fprintf(stderr, "ApplyBSDiffPatch failed\n");
-      return result;
+    unsigned char header[MAX_HEADER_LENGTH];
+    FILE* patchf = fopen(patch_filename, "rb");
+    if (patchf == NULL) {
+      printf("failed to open patch file %s: %s\n",
+             patch_filename, strerror(errno));
+      return 1;
     }
-  } else if (header_bytes_read >= 8 &&
-             memcmp(header, "IMGDIFF", 7) == 0 &&
-             (header[7] == '1' || header[7] == '2')) {
-    int result = ApplyImagePatch(source_to_use->data, source_to_use->size,
-                                 patch_filename, sink, token, &ctx);
-    if (result != 0) {
-      fprintf(stderr, "ApplyImagePatch failed\n");
-      return result;
-    }
-  } else {
-    fprintf(stderr, "Unknown patch file format\n");
-    return 1;
-  }
+    int header_bytes_read = fread(header, 1, MAX_HEADER_LENGTH, patchf);
+    fclose(patchf);
 
-  if (output != NULL) {
-    fflush(output);
-    fsync(fileno(output));
-    fclose(output);
-  }
+    SHA_init(&ctx);
+
+    int result;
+
+    if (header_bytes_read >= 4 &&
+        header[0] == 0xd6 && header[1] == 0xc3 &&
+        header[2] == 0xc4 && header[3] == 0) {
+      // xdelta3 patches begin "VCD" (with the high bits set) followed
+      // by a zero byte (the version number).
+      printf("error:  xdelta3 patches no longer supported\n");
+      return 1;
+    } else if (header_bytes_read >= 8 &&
+               memcmp(header, "BSDIFF40", 8) == 0) {
+      result = ApplyBSDiffPatch(source_to_use->data, source_to_use->size,
+                                    patch_filename, 0, sink, token, &ctx);
+    } else if (header_bytes_read >= 8 &&
+               memcmp(header, "IMGDIFF", 7) == 0 &&
+               (header[7] == '1' || header[7] == '2')) {
+      result = ApplyImagePatch(source_to_use->data, source_to_use->size,
+                                   patch_filename, sink, token, &ctx);
+    } else {
+      printf("Unknown patch file format\n");
+      return 1;
+    }
+
+    if (output >= 0) {
+      fsync(output);
+      close(output);
+    }
+
+    if (result != 0) {
+      if (retry == 0) {
+        printf("applying patch failed\n");
+        return result;
+      } else {
+        printf("applying patch failed; retrying\n");
+      }
+      if (outname != NULL) {
+        unlink(outname);
+      }
+    } else {
+      // succeeded; no need to retry
+      break;
+    }
+  } while (retry-- > 0);
 
   const uint8_t* current_target_sha1 = SHA_final(&ctx);
   if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) {
-    fprintf(stderr, "patch did not produce expected sha1\n");
+    printf("patch did not produce expected sha1\n");
     return 1;
   }
 
-  if (output == NULL) {
+  if (output < 0) {
     // Copy the temp file to the MTD partition.
     if (WriteToMTDPartition(msi.buffer, msi.pos, target_filename) != 0) {
-      fprintf(stderr, "write of patched data to %s failed\n", target_filename);
+      printf("write of patched data to %s failed\n", target_filename);
       return 1;
     }
     free(msi.buffer);
@@ -826,18 +869,18 @@
     // Give the .patch file the same owner, group, and mode of the
     // original source file.
     if (chmod(outname, source_to_use->st.st_mode) != 0) {
-      fprintf(stderr, "chmod of \"%s\" failed: %s\n", outname, strerror(errno));
+      printf("chmod of \"%s\" failed: %s\n", outname, strerror(errno));
       return 1;
     }
     if (chown(outname, source_to_use->st.st_uid,
               source_to_use->st.st_gid) != 0) {
-      fprintf(stderr, "chown of \"%s\" failed: %s\n", outname, strerror(errno));
+      printf("chown of \"%s\" failed: %s\n", outname, strerror(errno));
       return 1;
     }
 
     // Finally, rename the .patch file to replace the target file.
     if (rename(outname, target_filename) != 0) {
-      fprintf(stderr, "rename of .patch to \"%s\" failed: %s\n",
+      printf("rename of .patch to \"%s\" failed: %s\n",
               target_filename, strerror(errno));
       return 1;
     }
diff --git a/tools/applypatch/freecache.c b/tools/applypatch/freecache.c
index ab71b81..9827fda 100644
--- a/tools/applypatch/freecache.c
+++ b/tools/applypatch/freecache.c
@@ -16,7 +16,7 @@
   struct dirent* de;
   d = opendir("/proc");
   if (d == NULL) {
-    fprintf(stderr, "error opening /proc: %s\n", strerror(errno));
+    printf("error opening /proc: %s\n", strerror(errno));
     return -1;
   }
   while ((de = readdir(d)) != 0) {
@@ -35,7 +35,7 @@
     struct dirent* fdde;
     fdd = opendir(path);
     if (fdd == NULL) {
-      fprintf(stderr, "error opening %s: %s\n", path, strerror(errno));
+      printf("error opening %s: %s\n", path, strerror(errno));
       continue;
     }
     while ((fdde = readdir(fdd)) != 0) {
@@ -88,7 +88,7 @@
   for (i = 0; i < sizeof(dirs)/sizeof(dirs[0]); ++i) {
     d = opendir(dirs[i]);
     if (d == NULL) {
-      fprintf(stderr, "error opening %s: %s\n", dirs[i], strerror(errno));
+      printf("error opening %s: %s\n", dirs[i], strerror(errno));
       continue;
     }
 
@@ -143,7 +143,7 @@
 
   if (entries == 0) {
     // nothing we can delete to free up space!
-    fprintf(stderr, "no files can be deleted to free space on /cache\n");
+    printf("no files can be deleted to free space on /cache\n");
     return -1;
   }
 
diff --git a/tools/applypatch/imgdiff.c b/tools/applypatch/imgdiff.c
index 6ff9b6f..6b9ebee 100644
--- a/tools/applypatch/imgdiff.c
+++ b/tools/applypatch/imgdiff.c
@@ -179,14 +179,14 @@
                        int include_pseudo_chunk) {
   struct stat st;
   if (stat(filename, &st) != 0) {
-    fprintf(stderr, "failed to stat \"%s\": %s\n", filename, strerror(errno));
+    printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
     return NULL;
   }
 
   unsigned char* img = malloc(st.st_size);
   FILE* f = fopen(filename, "rb");
   if (fread(img, 1, st.st_size, f) != st.st_size) {
-    fprintf(stderr, "failed to read \"%s\" %s\n", filename, strerror(errno));
+    printf("failed to read \"%s\" %s\n", filename, strerror(errno));
     fclose(f);
     return NULL;
   }
@@ -203,7 +203,7 @@
   }
   // double-check: this archive consists of a single "disk"
   if (!(img[i+4] == 0 && img[i+5] == 0 && img[i+6] == 0 && img[i+7] == 0)) {
-    fprintf(stderr, "can't process multi-disk archive\n");
+    printf("can't process multi-disk archive\n");
     return NULL;
   }
 
@@ -216,7 +216,7 @@
   unsigned char* cd = img+cdoffset;
   for (i = 0; i < cdcount; ++i) {
     if (!(cd[0] == 0x50 && cd[1] == 0x4b && cd[2] == 0x01 && cd[3] == 0x02)) {
-      fprintf(stderr, "bad central directory entry %d\n", i);
+      printf("bad central directory entry %d\n", i);
       return NULL;
     }
 
@@ -243,12 +243,12 @@
     unsigned char* lh = img + hoffset;
 
     if (!(lh[0] == 0x50 && lh[1] == 0x4b && lh[2] == 0x03 && lh[3] == 0x04)) {
-      fprintf(stderr, "bad local file header entry %d\n", i);
+      printf("bad local file header entry %d\n", i);
       return NULL;
     }
 
     if (Read2(lh+26) != nlen || memcmp(lh+30, filename, nlen) != 0) {
-      fprintf(stderr, "central dir filename doesn't match local header\n");
+      printf("central dir filename doesn't match local header\n");
       return NULL;
     }
 
@@ -320,7 +320,7 @@
       strm.next_out = curr->data;
       ret = inflate(&strm, Z_NO_FLUSH);
       if (ret != Z_STREAM_END) {
-        fprintf(stderr, "failed to inflate \"%s\"; %d\n", curr->filename, ret);
+        printf("failed to inflate \"%s\"; %d\n", curr->filename, ret);
         return NULL;
       }
 
@@ -369,14 +369,14 @@
                          int* num_chunks, ImageChunk** chunks) {
   struct stat st;
   if (stat(filename, &st) != 0) {
-    fprintf(stderr, "failed to stat \"%s\": %s\n", filename, strerror(errno));
+    printf("failed to stat \"%s\": %s\n", filename, strerror(errno));
     return NULL;
   }
 
   unsigned char* img = malloc(st.st_size + 4);
   FILE* f = fopen(filename, "rb");
   if (fread(img, 1, st.st_size, f) != st.st_size) {
-    fprintf(stderr, "failed to read \"%s\" %s\n", filename, strerror(errno));
+    printf("failed to read \"%s\" %s\n", filename, strerror(errno));
     fclose(f);
     return NULL;
   }
@@ -476,7 +476,7 @@
       // the decompression.
       size_t footer_size = Read4(p-4);
       if (footer_size != curr[-2].len) {
-        fprintf(stderr, "Error: footer size %d != decompressed size %d\n",
+        printf("Error: footer size %d != decompressed size %d\n",
                 footer_size, curr[-2].len);
         free(img);
         return NULL;
@@ -522,7 +522,7 @@
   size_t p = 0;
 
 #if 0
-  fprintf(stderr, "trying %d %d %d %d %d\n",
+  printf("trying %d %d %d %d %d\n",
           chunk->level, chunk->method, chunk->windowBits,
           chunk->memLevel, chunk->strategy);
 #endif
@@ -565,7 +565,7 @@
  */
 int ReconstructDeflateChunk(ImageChunk* chunk) {
   if (chunk->type != CHUNK_DEFLATE) {
-    fprintf(stderr, "attempt to reconstruct non-deflate chunk\n");
+    printf("attempt to reconstruct non-deflate chunk\n");
     return -1;
   }
 
@@ -610,13 +610,13 @@
 
   int r = bsdiff(src->data, src->len, &(src->I), tgt->data, tgt->len, ptemp);
   if (r != 0) {
-    fprintf(stderr, "bsdiff() failed: %d\n", r);
+    printf("bsdiff() failed: %d\n", r);
     return NULL;
   }
 
   struct stat st;
   if (stat(ptemp, &st) != 0) {
-    fprintf(stderr, "failed to stat patch file %s: %s\n",
+    printf("failed to stat patch file %s: %s\n",
             ptemp, strerror(errno));
     return NULL;
   }
@@ -635,11 +635,11 @@
 
   FILE* f = fopen(ptemp, "rb");
   if (f == NULL) {
-    fprintf(stderr, "failed to open patch %s: %s\n", ptemp, strerror(errno));
+    printf("failed to open patch %s: %s\n", ptemp, strerror(errno));
     return NULL;
   }
   if (fread(data, 1, st.st_size, f) != st.st_size) {
-    fprintf(stderr, "failed to read patch %s: %s\n", ptemp, strerror(errno));
+    printf("failed to read patch %s: %s\n", ptemp, strerror(errno));
     return NULL;
   }
   fclose(f);
@@ -691,7 +691,7 @@
                 memcmp(a->deflate_data, b->deflate_data, a->deflate_len) == 0;
 
         default:
-            fprintf(stderr, "unknown chunk type %d\n", a->type);
+            printf("unknown chunk type %d\n", a->type);
             return 0;
     }
 }
@@ -774,7 +774,7 @@
 int main(int argc, char** argv) {
   if (argc != 4 && argc != 5) {
     usage:
-    fprintf(stderr, "usage: %s [-z] <src-img> <tgt-img> <patch-file>\n",
+    printf("usage: %s [-z] <src-img> <tgt-img> <patch-file>\n",
             argv[0]);
     return 2;
   }
@@ -796,20 +796,20 @@
 
   if (zip_mode) {
     if (ReadZip(argv[1], &num_src_chunks, &src_chunks, 1) == NULL) {
-      fprintf(stderr, "failed to break apart source zip file\n");
+      printf("failed to break apart source zip file\n");
       return 1;
     }
     if (ReadZip(argv[2], &num_tgt_chunks, &tgt_chunks, 0) == NULL) {
-      fprintf(stderr, "failed to break apart target zip file\n");
+      printf("failed to break apart target zip file\n");
       return 1;
     }
   } else {
     if (ReadImage(argv[1], &num_src_chunks, &src_chunks) == NULL) {
-      fprintf(stderr, "failed to break apart source image\n");
+      printf("failed to break apart source image\n");
       return 1;
     }
     if (ReadImage(argv[2], &num_tgt_chunks, &tgt_chunks) == NULL) {
-      fprintf(stderr, "failed to break apart target image\n");
+      printf("failed to break apart target image\n");
       return 1;
     }
 
@@ -824,7 +824,7 @@
     }
 
     if (num_src_chunks != num_tgt_chunks) {
-      fprintf(stderr, "source and target don't have same number of chunks!\n");
+      printf("source and target don't have same number of chunks!\n");
       printf("source chunks:\n");
       DumpChunks(src_chunks, num_src_chunks);
       printf("target chunks:\n");
@@ -833,7 +833,7 @@
     }
     for (i = 0; i < num_src_chunks; ++i) {
       if (src_chunks[i].type != tgt_chunks[i].type) {
-        fprintf(stderr, "source and target don't have same chunk "
+        printf("source and target don't have same chunk "
                 "structure! (chunk %d)\n", i);
         printf("source chunks:\n");
         DumpChunks(src_chunks, num_src_chunks);
@@ -901,7 +901,7 @@
     MergeAdjacentNormalChunks(src_chunks, &num_src_chunks);
     if (num_src_chunks != num_tgt_chunks) {
       // This shouldn't happen.
-      fprintf(stderr, "merging normal chunks went awry\n");
+      printf("merging normal chunks went awry\n");
       return 1;
     }
   }
diff --git a/tools/applypatch/imgpatch.c b/tools/applypatch/imgpatch.c
index 74b041f..5322817 100644
--- a/tools/applypatch/imgpatch.c
+++ b/tools/applypatch/imgpatch.c
@@ -40,13 +40,13 @@
                     SinkFn sink, void* token, SHA_CTX* ctx) {
   FILE* f;
   if ((f = fopen(patch_filename, "rb")) == NULL) {
-    fprintf(stderr, "failed to open patch file\n");
+    printf("failed to open patch file\n");
     return -1;
   }
 
   unsigned char header[12];
   if (fread(header, 1, 12, f) != 12) {
-    fprintf(stderr, "failed to read patch file header\n");
+    printf("failed to read patch file header\n");
     return -1;
   }
 
@@ -54,7 +54,7 @@
   // IMGDIFF2 uses CHUNK_NORMAL, CHUNK_DEFLATE, and CHUNK_RAW.
   if (memcmp(header, "IMGDIFF", 7) != 0 ||
       (header[7] != '1' && header[7] != '2')) {
-    fprintf(stderr, "corrupt patch file header (magic number)\n");
+    printf("corrupt patch file header (magic number)\n");
     return -1;
   }
 
@@ -65,7 +65,7 @@
     // each chunk's header record starts with 4 bytes.
     unsigned char chunk[4];
     if (fread(chunk, 1, 4, f) != 4) {
-      fprintf(stderr, "failed to read chunk %d record\n", i);
+      printf("failed to read chunk %d record\n", i);
       return -1;
     }
 
@@ -74,7 +74,7 @@
     if (type == CHUNK_NORMAL) {
       unsigned char normal_header[24];
       if (fread(normal_header, 1, 24, f) != 24) {
-        fprintf(stderr, "failed to read chunk %d normal header data\n", i);
+        printf("failed to read chunk %d normal header data\n", i);
         return -1;
       }
 
@@ -82,7 +82,7 @@
       size_t src_len = Read8(normal_header+8);
       size_t patch_offset = Read8(normal_header+16);
 
-      fprintf(stderr, "CHUNK %d:  normal   patch offset %d\n", i, patch_offset);
+      printf("CHUNK %d:  normal   patch offset %d\n", i, patch_offset);
 
       ApplyBSDiffPatch(old_data + src_start, src_len,
                        patch_filename, patch_offset,
@@ -98,14 +98,14 @@
       // in their chunk header.
       unsigned char* gzip = malloc(64);
       if (fread(gzip, 1, 64, f) != 64) {
-        fprintf(stderr, "failed to read chunk %d initial gzip header data\n",
+        printf("failed to read chunk %d initial gzip header data\n",
                 i);
         return -1;
       }
       size_t gzip_header_len = Read4(gzip+60);
       gzip = realloc(gzip, 64 + gzip_header_len + 8);
       if (fread(gzip+64, 1, gzip_header_len+8, f) != gzip_header_len+8) {
-        fprintf(stderr, "failed to read chunk %d remaining gzip header data\n",
+        printf("failed to read chunk %d remaining gzip header data\n",
                 i);
         return -1;
       }
@@ -122,14 +122,14 @@
       int gz_memLevel = Read4(gzip+52);
       int gz_strategy = Read4(gzip+56);
 
-      fprintf(stderr, "CHUNK %d:  gzip     patch offset %d\n", i, patch_offset);
+      printf("CHUNK %d:  gzip     patch offset %d\n", i, patch_offset);
 
       // Decompress the source data; the chunk header tells us exactly
       // how big we expect it to be when decompressed.
 
       unsigned char* expanded_source = malloc(expanded_len);
       if (expanded_source == NULL) {
-        fprintf(stderr, "failed to allocate %d bytes for expanded_source\n",
+        printf("failed to allocate %d bytes for expanded_source\n",
                 expanded_len);
         return -1;
       }
@@ -146,7 +146,7 @@
       int ret;
       ret = inflateInit2(&strm, -15);
       if (ret != Z_OK) {
-        fprintf(stderr, "failed to init source inflation: %d\n", ret);
+        printf("failed to init source inflation: %d\n", ret);
         return -1;
       }
 
@@ -154,12 +154,12 @@
       // data, we expect one call to inflate() to suffice.
       ret = inflate(&strm, Z_SYNC_FLUSH);
       if (ret != Z_STREAM_END) {
-        fprintf(stderr, "source inflation returned %d\n", ret);
+        printf("source inflation returned %d\n", ret);
         return -1;
       }
       // We should have filled the output buffer exactly.
       if (strm.avail_out != 0) {
-        fprintf(stderr, "source inflation short by %d bytes\n", strm.avail_out);
+        printf("source inflation short by %d bytes\n", strm.avail_out);
         return -1;
       }
       inflateEnd(&strm);
@@ -208,7 +208,7 @@
         size_t have = temp_size - strm.avail_out;
 
         if (sink(temp_data, have, token) != have) {
-          fprintf(stderr, "failed to write %d compressed bytes to output\n",
+          printf("failed to write %d compressed bytes to output\n",
                   have);
           return -1;
         }
@@ -226,29 +226,29 @@
     } else if (type == CHUNK_RAW) {
       unsigned char raw_header[4];
       if (fread(raw_header, 1, 4, f) != 4) {
-        fprintf(stderr, "failed to read chunk %d raw header data\n", i);
+        printf("failed to read chunk %d raw header data\n", i);
         return -1;
       }
 
       size_t data_len = Read4(raw_header);
 
-      fprintf(stderr, "CHUNK %d:  raw      data %d\n", i, data_len);
+      printf("CHUNK %d:  raw      data %d\n", i, data_len);
 
       unsigned char* temp = malloc(data_len);
       if (fread(temp, 1, data_len, f) != data_len) {
-          fprintf(stderr, "failed to read chunk %d raw data\n", i);
+          printf("failed to read chunk %d raw data\n", i);
           return -1;
       }
       SHA_update(ctx, temp, data_len);
       if (sink(temp, data_len, token) != data_len) {
-          fprintf(stderr, "failed to write chunk %d raw data\n", i);
+          printf("failed to write chunk %d raw data\n", i);
           return -1;
       }
     } else if (type == CHUNK_DEFLATE) {
       // deflate chunks have an additional 60 bytes in their chunk header.
       unsigned char deflate_header[60];
       if (fread(deflate_header, 1, 60, f) != 60) {
-        fprintf(stderr, "failed to read chunk %d deflate header data\n", i);
+        printf("failed to read chunk %d deflate header data\n", i);
         return -1;
       }
 
@@ -263,14 +263,14 @@
       int memLevel = Read4(deflate_header+52);
       int strategy = Read4(deflate_header+56);
 
-      fprintf(stderr, "CHUNK %d:  deflate  patch offset %d\n", i, patch_offset);
+      printf("CHUNK %d:  deflate  patch offset %d\n", i, patch_offset);
 
       // Decompress the source data; the chunk header tells us exactly
       // how big we expect it to be when decompressed.
 
       unsigned char* expanded_source = malloc(expanded_len);
       if (expanded_source == NULL) {
-        fprintf(stderr, "failed to allocate %d bytes for expanded_source\n",
+        printf("failed to allocate %d bytes for expanded_source\n",
                 expanded_len);
         return -1;
       }
@@ -287,7 +287,7 @@
       int ret;
       ret = inflateInit2(&strm, -15);
       if (ret != Z_OK) {
-        fprintf(stderr, "failed to init source inflation: %d\n", ret);
+        printf("failed to init source inflation: %d\n", ret);
         return -1;
       }
 
@@ -295,12 +295,12 @@
       // data, we expect one call to inflate() to suffice.
       ret = inflate(&strm, Z_SYNC_FLUSH);
       if (ret != Z_STREAM_END) {
-        fprintf(stderr, "source inflation returned %d\n", ret);
+        printf("source inflation returned %d\n", ret);
         return -1;
       }
       // We should have filled the output buffer exactly.
       if (strm.avail_out != 0) {
-        fprintf(stderr, "source inflation short by %d bytes\n", strm.avail_out);
+        printf("source inflation short by %d bytes\n", strm.avail_out);
         return -1;
       }
       inflateEnd(&strm);
@@ -344,7 +344,7 @@
         size_t have = temp_size - strm.avail_out;
 
         if (sink(temp_data, have, token) != have) {
-          fprintf(stderr, "failed to write %d compressed bytes to output\n",
+          printf("failed to write %d compressed bytes to output\n",
                   have);
           return -1;
         }
@@ -355,7 +355,7 @@
       free(temp_data);
       free(uncompressed_target_data);
     } else {
-      fprintf(stderr, "patch chunk %d is unknown type %d\n", i, type);
+      printf("patch chunk %d is unknown type %d\n", i, type);
       return -1;
     }
   }
diff --git a/tools/applypatch/main.c b/tools/applypatch/main.c
index e25c730..e08f5c1 100644
--- a/tools/applypatch/main.c
+++ b/tools/applypatch/main.c
@@ -44,7 +44,7 @@
 int main(int argc, char** argv) {
   int result = applypatch(argc, argv);
   if (result == 2) {
-    fprintf(stderr,
+    printf(
             "usage: %s <src-file> <tgt-file> <tgt-sha1> <tgt-size> "
             "[<src-sha1>:<patch> ...]\n"
             "   or  %s -c <file> [<sha1> ...]\n"