Make DexFiles opened from files readonly by default, but writable during dex2oat

Bug: 9618388
Change-Id: I83f2e16ee8446a79a94a84971146d807bb0c9ee0
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index 5c96d74..b6a7683 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -417,9 +417,13 @@
     const char* dex_location = dex_locations[i];
     const DexFile* dex_file = DexFile::Open(dex_filename, dex_location);
     if (dex_file == NULL) {
-      LOG(WARNING) << "Could not open .dex from file '" << dex_filename << "'\n";
+      LOG(WARNING) << "Failed to open .dex from file '" << dex_filename << "'\n";
       ++failure_count;
     } else {
+      // Ensure writable for dex-to-dex transformations.
+      if (!dex_file->EnableWrite()) {
+        PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_filename << "'\n";
+      }
       dex_files.push_back(dex_file);
     }
   }
@@ -567,7 +571,7 @@
   argc--;
 
   if (argc == 0) {
-    Usage("no arguments specified");
+    Usage("No arguments specified");
   }
 
   std::vector<const char*> dex_filenames;
@@ -622,7 +626,7 @@
     } else if (option.starts_with("--zip-fd=")) {
       const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
       if (!ParseInt(zip_fd_str, &zip_fd)) {
-        Usage("could not parse --zip-fd argument '%s' as an integer", zip_fd_str);
+        Usage("Failed to parse --zip-fd argument '%s' as an integer", zip_fd_str);
       }
     } else if (option.starts_with("--zip-location=")) {
       zip_location = option.substr(strlen("--zip-location=")).data();
@@ -633,7 +637,7 @@
     } else if (option.starts_with("--oat-fd=")) {
       const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
       if (!ParseInt(oat_fd_str, &oat_fd)) {
-        Usage("could not parse --oat-fd argument '%s' as an integer", oat_fd_str);
+        Usage("Failed to parse --oat-fd argument '%s' as an integer", oat_fd_str);
       }
     } else if (option == "--watch-dog") {
       watch_dog_enabled = true;
@@ -642,7 +646,7 @@
     } else if (option.starts_with("-j")) {
       const char* thread_count_str = option.substr(strlen("-j")).data();
       if (!ParseInt(thread_count_str, &thread_count)) {
-        Usage("could not parse -j argument '%s' as an integer", thread_count_str);
+        Usage("Failed to parse -j argument '%s' as an integer", thread_count_str);
       }
     } else if (option.starts_with("--oat-location=")) {
       oat_location = option.substr(strlen("--oat-location=")).data();
@@ -696,7 +700,7 @@
     } else if (option == "--dump-timing") {
       dump_timing = true;
     } else {
-      Usage("unknown argument %s", option.data());
+      Usage("Unknown argument %s", option.data());
     }
   }
 
@@ -789,7 +793,7 @@
 
   if (boot_image_option.empty()) {
     if (image_base == 0) {
-      Usage("non-zero --base not specified");
+      Usage("Non-zero --base not specified");
     }
   }