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

Bug: 9071417
Change-Id: I1ee9ff281867f90fba7a8ed8bbf06b33ac29d511
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 25cfda6..ceb6bf6 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -313,7 +313,7 @@
       oat_data_begin = image_writer.GetOatDataBegin();
     }
 
-    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) {
       PLOG(ERROR) << "Failed to open ELF file: " << oat_filename;
       return false;
@@ -808,7 +808,7 @@
   UniquePtr<File> oat_file;
   bool create_file = !oat_unstripped.empty();  // as opposed to using open file descriptor
   if (create_file) {
-    oat_file.reset(OS::OpenFile(oat_unstripped.c_str(), true));
+    oat_file.reset(OS::CreateEmptyFile(oat_unstripped.c_str()));
     if (oat_location.empty()) {
       oat_location = oat_filename;
     }
@@ -1023,8 +1023,8 @@
   if (oat_unstripped != oat_stripped) {
     timings.NewSplit("dex2oat OatFile copy");
     oat_file.reset();
-    UniquePtr<File> in(OS::OpenFile(oat_unstripped.c_str(), false));
-    UniquePtr<File> out(OS::OpenFile(oat_stripped.c_str(), true));
+     UniquePtr<File> in(OS::OpenFileForReading(oat_unstripped.c_str()));
+    UniquePtr<File> out(OS::CreateEmptyFile(oat_stripped.c_str()));
     size_t buffer_size = 8192;
     UniquePtr<uint8_t> buffer(new uint8_t[buffer_size]);
     while (true) {