Clear image file in image writer

The goal is to not erase the image file in installd in case we abort
compilation due to no new profile data. Currently aborting results in
an empty image file.

Bug: 22858531
Change-Id: I32eee3391f860f4a373db3d746d096cc36d623f2
diff --git a/compiler/image_writer.cc b/compiler/image_writer.cc
index c8720ea..6abc6fa 100644
--- a/compiler/image_writer.cc
+++ b/compiler/image_writer.cc
@@ -276,6 +276,11 @@
     if (image_fd != kInvalidFd) {
       if (strlen(image_filename) == 0u) {
         image_file.reset(new File(image_fd, unix_file::kCheckSafeUsage));
+        // Empty the file in case it already exists.
+        if (image_file != nullptr) {
+          TEMP_FAILURE_RETRY(image_file->SetLength(0));
+          TEMP_FAILURE_RETRY(image_file->Flush());
+        }
       } else {
         LOG(ERROR) << "image fd " << image_fd << " name " << image_filename;
       }