Merge "Dex header cleanup and windows simplification"
diff --git a/dexdump/dexdump.cc b/dexdump/dexdump.cc
index 8778b12..61a1209 100644
--- a/dexdump/dexdump.cc
+++ b/dexdump/dexdump.cc
@@ -34,19 +34,14 @@
 
 #include "dexdump.h"
 
-#include <fcntl.h>
 #include <inttypes.h>
 #include <stdio.h>
-#include <sys/mman.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 
-#include <iostream>
 #include <memory>
 #include <sstream>
 #include <vector>
 
+#include "android-base/file.h"
 #include "android-base/logging.h"
 #include "android-base/stringprintf.h"
 
@@ -1879,34 +1874,6 @@
   }
 }
 
-static bool openAndMapFile(const char* fileName,
-                           const uint8_t** base,
-                           size_t* size,
-                           std::string* error_msg) {
-  int fd = open(fileName, O_RDONLY);
-  if (fd < 0) {
-    *error_msg = "open failed";
-    return false;
-  }
-  struct stat st;
-  if (fstat(fd, &st) < 0) {
-    *error_msg = "stat failed";
-    return false;
-  }
-  *size = st.st_size;
-  if (*size == 0) {
-    *error_msg = "size == 0";
-    return false;
-  }
-  void* addr = mmap(nullptr /*addr*/, *size, PROT_READ, MAP_PRIVATE, fd, 0 /*offset*/);
-  if (addr == MAP_FAILED) {
-    *error_msg = "mmap failed";
-    return false;
-  }
-  *base = reinterpret_cast<const uint8_t*>(addr);
-  return true;
-}
-
 /*
  * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
  */
@@ -1918,17 +1885,22 @@
   // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
   // all of which are Zip archives with "classes.dex" inside.
   const bool kVerifyChecksum = !gOptions.ignoreBadChecksum;
-  const uint8_t* base = nullptr;
-  size_t size = 0;
-  std::string error_msg;
-  if (!openAndMapFile(fileName, &base, &size, &error_msg)) {
-    LOG(ERROR) << error_msg;
+  std::string content;
+  // TODO: add an api to android::base to read a std::vector<uint8_t>.
+  if (!android::base::ReadFileToString(fileName, &content)) {
+    LOG(ERROR) << "ReadFileToString failed";
     return -1;
   }
   const DexFileLoader dex_file_loader;
+  std::string error_msg;
   std::vector<std::unique_ptr<const DexFile>> dex_files;
-  if (!dex_file_loader.OpenAll(
-        base, size, fileName, /*verify*/ true, kVerifyChecksum, &error_msg, &dex_files)) {
+  if (!dex_file_loader.OpenAll(reinterpret_cast<const uint8_t*>(content.data()),
+                               content.size(),
+                               fileName,
+                               /*verify*/ true,
+                               kVerifyChecksum,
+                               &error_msg,
+                               &dex_files)) {
     // Display returned error message to user. Note that this error behavior
     // differs from the error messages shown by the original Dalvik dexdump.
     LOG(ERROR) << error_msg;
diff --git a/dexlist/dexlist.cc b/dexlist/dexlist.cc
index 31a146d..6a50258 100644
--- a/dexlist/dexlist.cc
+++ b/dexlist/dexlist.cc
@@ -23,15 +23,11 @@
  * List all methods in all concrete classes in one or more DEX files.
  */
 
-#include <fcntl.h>
 #include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <sys/mman.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
 
+#include <android-base/file.h>
 #include <android-base/logging.h>
 
 #include "dex/code_item_accessors-inl.h"
@@ -170,34 +166,6 @@
   }
 }
 
-static bool openAndMapFile(const char* fileName,
-                           const uint8_t** base,
-                           size_t* size,
-                           std::string* error_msg) {
-  int fd = open(fileName, O_RDONLY);
-  if (fd < 0) {
-    *error_msg = "open failed";
-    return false;
-  }
-  struct stat st;
-  if (fstat(fd, &st) < 0) {
-    *error_msg = "stat failed";
-    return false;
-  }
-  *size = st.st_size;
-  if (*size == 0) {
-    *error_msg = "size == 0";
-    return false;
-  }
-  void* addr = mmap(nullptr /*addr*/, *size, PROT_READ, MAP_PRIVATE, fd, 0 /*offset*/);
-  if (addr == MAP_FAILED) {
-    *error_msg = "mmap failed";
-    return false;
-  }
-  *base = reinterpret_cast<const uint8_t*>(addr);
-  return true;
-}
-
 /*
  * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
  */
@@ -205,17 +173,22 @@
   // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
   // all of which are Zip archives with "classes.dex" inside.
   static constexpr bool kVerifyChecksum = true;
-  const uint8_t* base = nullptr;
-  size_t size = 0;
-  std::string error_msg;
-  if (!openAndMapFile(fileName, &base, &size, &error_msg)) {
-    LOG(ERROR) << error_msg;
+  std::string content;
+  // TODO: add an api to android::base to read a std::vector<uint8_t>.
+  if (!android::base::ReadFileToString(fileName, &content)) {
+    LOG(ERROR) << "ReadFileToString failed";
     return -1;
   }
   std::vector<std::unique_ptr<const DexFile>> dex_files;
+  std::string error_msg;
   const DexFileLoader dex_file_loader;
-  if (!dex_file_loader.OpenAll(
-        base, size, fileName, /*verify*/ true, kVerifyChecksum, &error_msg, &dex_files)) {
+  if (!dex_file_loader.OpenAll(reinterpret_cast<const uint8_t*>(content.data()),
+                               content.size(),
+                               fileName,
+                               /*verify*/ true,
+                               kVerifyChecksum,
+                               &error_msg,
+                               &dex_files)) {
     LOG(ERROR) << error_msg;
     return -1;
   }
diff --git a/libdexfile/dex/base64_test_util.h b/libdexfile/dex/base64_test_util.h
index 683e429..5d73759 100644
--- a/libdexfile/dex/base64_test_util.h
+++ b/libdexfile/dex/base64_test_util.h
@@ -17,13 +17,14 @@
 #ifndef ART_LIBDEXFILE_DEX_BASE64_TEST_UTIL_H_
 #define ART_LIBDEXFILE_DEX_BASE64_TEST_UTIL_H_
 
-#include <base/logging.h>
 #include <stdint.h>
 #include <stdlib.h>
 
 #include <memory>
 #include <vector>
 
+#include <android-base/logging.h>
+
 namespace art {
 
 static inline uint8_t* DecodeBase64(const char* src, size_t* dst_size) {
diff --git a/libdexfile/dex/compact_dex_debug_info_test.cc b/libdexfile/dex/compact_dex_debug_info_test.cc
index 3267612..7911a86 100644
--- a/libdexfile/dex/compact_dex_debug_info_test.cc
+++ b/libdexfile/dex/compact_dex_debug_info_test.cc
@@ -16,7 +16,7 @@
 
 #include <vector>
 
-#include "base/logging.h"
+#include <android-base/logging.h>
 #include "dex/compact_dex_debug_info.h"
 #include "gtest/gtest.h"
 
@@ -74,7 +74,7 @@
                                         /*out*/ &table_offset);
   EXPECT_LT(sorted_data.size(), data.size());
   {
-    ScopedLogSeverity sls(LogSeverity::INFO);
+    android::base::ScopedLogSeverity sls(android::base::LogSeverity::INFO);
     LOG(INFO) << "raw size " << before_size
               << " table size " << data.size()
               << " sorted table size " << sorted_data.size();
diff --git a/libdexfile/dex/compact_dex_level.h b/libdexfile/dex/compact_dex_level.h
index 2325ac2..599ec4d 100644
--- a/libdexfile/dex/compact_dex_level.h
+++ b/libdexfile/dex/compact_dex_level.h
@@ -19,7 +19,6 @@
 
 #include <string>
 
-#include "base/macros.h"
 #include "dex_file.h"
 
 namespace art {
diff --git a/runtime/dex_cache_resolved_classes.h b/libdexfile/dex/dex_cache_resolved_classes.h
similarity index 93%
rename from runtime/dex_cache_resolved_classes.h
rename to libdexfile/dex/dex_cache_resolved_classes.h
index ca6afc5..4c9acbf 100644
--- a/runtime/dex_cache_resolved_classes.h
+++ b/libdexfile/dex/dex_cache_resolved_classes.h
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef ART_RUNTIME_DEX_CACHE_RESOLVED_CLASSES_H_
-#define ART_RUNTIME_DEX_CACHE_RESOLVED_CLASSES_H_
+#ifndef ART_LIBDEXFILE_DEX_DEX_CACHE_RESOLVED_CLASSES_H_
+#define ART_LIBDEXFILE_DEX_DEX_CACHE_RESOLVED_CLASSES_H_
 
 #include <string>
 #include <unordered_set>
@@ -90,4 +90,4 @@
 
 }  // namespace art
 
-#endif  // ART_RUNTIME_DEX_CACHE_RESOLVED_CLASSES_H_
+#endif  // ART_LIBDEXFILE_DEX_DEX_CACHE_RESOLVED_CLASSES_H_
diff --git a/libdexfile/dex/dex_file-inl.h b/libdexfile/dex/dex_file-inl.h
index b424b50..c86e879 100644
--- a/libdexfile/dex/dex_file-inl.h
+++ b/libdexfile/dex/dex_file-inl.h
@@ -17,7 +17,6 @@
 #ifndef ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
 #define ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_
 
-#include "base/bit_utils.h"
 #include "base/casts.h"
 #include "base/stringpiece.h"
 #include "compact_dex_file.h"
diff --git a/runtime/class_linker.h b/runtime/class_linker.h
index 6bb924f..712e3ae 100644
--- a/runtime/class_linker.h
+++ b/runtime/class_linker.h
@@ -27,9 +27,9 @@
 #include "base/enums.h"
 #include "base/macros.h"
 #include "base/mutex.h"
+#include "dex/dex_cache_resolved_classes.h"
 #include "dex/dex_file.h"
 #include "dex/dex_file_types.h"
-#include "dex_cache_resolved_classes.h"
 #include "gc_root.h"
 #include "handle.h"
 #include "jni.h"
diff --git a/runtime/jit/profile_compilation_info.h b/runtime/jit/profile_compilation_info.h
index 5488a9e..3213c85 100644
--- a/runtime/jit/profile_compilation_info.h
+++ b/runtime/jit/profile_compilation_info.h
@@ -24,7 +24,7 @@
 #include "base/arena_containers.h"
 #include "base/arena_object.h"
 #include "bit_memory_region.h"
-#include "dex_cache_resolved_classes.h"
+#include "dex/dex_cache_resolved_classes.h"
 #include "dex/dex_file.h"
 #include "dex/dex_file_types.h"
 #include "method_reference.h"