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

Bug: 9071417
Change-Id: I1ee9ff281867f90fba7a8ed8bbf06b33ac29d511
diff --git a/compiler/elf_writer_test.cc b/compiler/elf_writer_test.cc
index e48806e..ffe1f72 100644
--- a/compiler/elf_writer_test.cc
+++ b/compiler/elf_writer_test.cc
@@ -62,7 +62,7 @@
 
   ASSERT_EQ(0, dlclose(dl_oat_so));
 
-  UniquePtr<File> file(OS::OpenFile(elf_filename.c_str(), false));
+  UniquePtr<File> file(OS::OpenFileForReading(elf_filename.c_str()));
   ASSERT_TRUE(file.get() != NULL);
   {
     UniquePtr<ElfFile> ef(ElfFile::Open(file.get(), false, false));
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;
diff --git a/compiler/llvm/llvm_compilation_unit.cc b/compiler/llvm/llvm_compilation_unit.cc
index 7542b84..139100b 100644
--- a/compiler/llvm/llvm_compilation_unit.cc
+++ b/compiler/llvm/llvm_compilation_unit.cc
@@ -155,7 +155,7 @@
   std::string bitcode;
   DumpBitcodeToString(bitcode);
   std::string filename(StringPrintf("%s/Art%u.bc", DumpDirectory().c_str(), cunit_id_));
-  UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
+  UniquePtr<File> output(OS::CreateEmptyFile(filename.c_str()));
   output->WriteFully(bitcode.data(), bitcode.size());
   LOG(INFO) << ".bc file written successfully: " << filename;
 }
@@ -182,7 +182,7 @@
   if (kDumpELF) {
     // Dump the ELF image for debugging
     std::string filename(StringPrintf("%s/Art%u.o", DumpDirectory().c_str(), cunit_id_));
-    UniquePtr<File> output(OS::OpenFile(filename.c_str(), true));
+    UniquePtr<File> output(OS::CreateEmptyFile(filename.c_str()));
     output->WriteFully(elf_object_.data(), elf_object_.size());
     LOG(INFO) << ".o file written successfully: " << filename;
   }
diff --git a/compiler/sea_ir/debug/dot_gen.h b/compiler/sea_ir/debug/dot_gen.h
index 675d83d..5270582 100644
--- a/compiler/sea_ir/debug/dot_gen.h
+++ b/compiler/sea_ir/debug/dot_gen.h
@@ -103,7 +103,7 @@
     LOG(INFO) << "Starting to write SEA string to file.";
     DotGenerationVisitor dgv = DotGenerationVisitor(&options_, types);
     graph->Accept(&dgv);
-    art::File* file = art::OS::OpenFile(filename.c_str(), true, true);
+    art::File* file = art::OS::OpenFile(filename.c_str(), O_RDWR | O_CREAT | O_TRUNC);
     art::FileOutputStream fos(file);
     std::string graph_as_string = dgv.GetResult();
     graph_as_string += "}";