Add flock(2)ing on dex-cache files to prevent races

Bug: 9071417
Change-Id: I1ee9ff281867f90fba7a8ed8bbf06b33ac29d511
diff --git a/compiler/image_writer.cc b/compiler/image_writer.cc
index a40e3fc..4e9ae54 100644
--- a/compiler/image_writer.cc
+++ b/compiler/image_writer.cc
@@ -79,7 +79,7 @@
     dex_caches_.insert(dex_cache);
   }
 
-  UniquePtr<File> oat_file(OS::OpenFile(oat_filename.c_str(), true, false));
+  UniquePtr<File> oat_file(OS::OpenFileReadWrite(oat_filename.c_str()));
   if (oat_file.get() == NULL) {
     LOG(ERROR) << "Failed to open oat file " << oat_filename << " for " << oat_location;
     return false;
@@ -145,7 +145,7 @@
   PatchOatCodeAndMethods();
   Thread::Current()->TransitionFromRunnableToSuspended(kNative);
 
-  UniquePtr<File> image_file(OS::OpenFile(image_filename.c_str(), true));
+  UniquePtr<File> image_file(OS::CreateEmptyFile(image_filename.c_str()));
   if (image_file.get() == NULL) {
     LOG(ERROR) << "Failed to open image file " << image_filename;
     return false;