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/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;
     }
   }