Save the target file when applypatch tests fail

Save the target file to tempfile upon unittest failures so that we can
try to decompress the deflate chunks in the flaky unittests. And print
the zlib version in case that gets changed.

Also the SHA1 of the uncompressed data seems correct; so only keep the
final SHA1 to double confirm.

Bug: 67849209
Test: recovery_component_test
Change-Id: Ic6447c2b75c29379d6844cd23a0ff1c4305694a0
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp
index b06a64f..c4c2707 100644
--- a/applypatch/imgpatch.cpp
+++ b/applypatch/imgpatch.cpp
@@ -60,6 +60,11 @@
   int mem_level = Read4(deflate_header + 52);
   int strategy = Read4(deflate_header + 56);
 
+  // TODO(b/67849209) Remove after debugging the unit test flakiness.
+  if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG) {
+    LOG(DEBUG) << "zlib version " << zlibVersion();
+  }
+
   z_stream strm;
   strm.zalloc = Z_NULL;
   strm.zfree = Z_NULL;
@@ -101,26 +106,17 @@
 
       size_t have = buffer_size - strm.avail_out;
       total_written += have;
-
-      // TODO(b/67849209) Remove after debugging the unit test flakiness.
-      if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG &&
-          have != 0) {
-        SHA1_Update(&sha_ctx, data, len - strm.avail_in);
-        SHA_CTX temp_ctx;
-        memcpy(&temp_ctx, &sha_ctx, sizeof(SHA_CTX));
-        uint8_t digest_so_far[SHA_DIGEST_LENGTH];
-        SHA1_Final(digest_so_far, &temp_ctx);
-        LOG(DEBUG) << "Processed " << actual_target_length + len - strm.avail_in
-                   << " bytes input data in the sink function, sha1 so far: "
-                   << short_sha1(digest_so_far);
-      }
-
       if (sink(buffer.data(), have) != have) {
         LOG(ERROR) << "Failed to write " << have << " compressed bytes to output.";
         return 0;
       }
     } while ((strm.avail_in != 0 || strm.avail_out == 0) && ret != Z_STREAM_END);
 
+    // TODO(b/67849209) Remove after debugging the unit test flakiness.
+    if (android::base::GetMinimumLogSeverity() <= android::base::LogSeverity::DEBUG) {
+      SHA1_Update(&sha_ctx, data, len);
+    }
+
     actual_target_length += len;
     return len;
   };