Merge "aconfig: remove read api lib's dependency on libbase and liblog" into main
diff --git a/tools/aconfig/aconfig/templates/cpp_source_file.template b/tools/aconfig/aconfig/templates/cpp_source_file.template
index 4c098c5..38dda7d 100644
--- a/tools/aconfig/aconfig/templates/cpp_source_file.template
+++ b/tools/aconfig/aconfig/templates/cpp_source_file.template
@@ -124,14 +124,14 @@
         "{item.container}",
         aconfig_storage::StorageFileType::package_map);
     if (!package_map_file.ok()) \{
-        ALOGI("error: failed to get package map file: %s", package_map_file.error().message().c_str());
+        ALOGI("error: failed to get package map file: %s", package_map_file.error().c_str());
         return result;
     }
 
     auto package_read_context = aconfig_storage::get_package_read_context(
         **package_map_file, "{package}");
     if (!package_read_context.ok()) \{
-        ALOGI("error: failed to get package read context: %s", package_map_file.error().message().c_str());
+        ALOGI("error: failed to get package read context: %s", package_map_file.error().c_str());
         return result;
     }
 
@@ -141,7 +141,7 @@
         "{item.container}",
         aconfig_storage::StorageFileType::flag_val);
     if (!flag_val_map.ok()) \{
-        ALOGI("error: failed to get flag val map: %s", package_map_file.error().message().c_str());
+        ALOGI("error: failed to get flag val map: %s", package_map_file.error().c_str());
         return result;
     }
 
@@ -149,7 +149,7 @@
         **flag_val_map,
         package_read_context->boolean_start_index + {item.flag_offset});
     if (!value.ok()) \{
-        ALOGI("error: failed to get flag val: %s", package_map_file.error().message().c_str());
+        ALOGI("error: failed to get flag val: %s", package_map_file.error().c_str());
         return result;
     }
 
diff --git a/tools/aconfig/aconfig_storage_read_api/Android.bp b/tools/aconfig/aconfig_storage_read_api/Android.bp
index d1c282d..9b84254 100644
--- a/tools/aconfig/aconfig_storage_read_api/Android.bp
+++ b/tools/aconfig/aconfig_storage_read_api/Android.bp
@@ -90,10 +90,6 @@
     host_supported: true,
     vendor_available: true,
     product_available: true,
-    shared_libs: [
-        "liblog",
-        "libbase",
-    ],
     apex_available: [
         "//apex_available:platform",
         "//apex_available:anyapex",
@@ -105,6 +101,7 @@
         },
     },
     double_loadable: true,
+    afdo: true,
 }
 
 cc_defaults {
diff --git a/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp b/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp
index 50076aa..97ada3a 100644
--- a/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp
+++ b/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp
@@ -1,16 +1,13 @@
-#include <android-base/file.h>
-#include <android-base/logging.h>
-
 #include <sys/mman.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <errno.h>
+#include <string.h>
 
 #include "rust/cxx.h"
 #include "aconfig_storage/lib.rs.h"
 #include "aconfig_storage/aconfig_storage_read_api.hpp"
 
-using namespace android::base;
-
 namespace aconfig_storage {
 
 /// Storage location pb file
@@ -36,7 +33,9 @@
     case StorageFileType::flag_info:
       return storage_dir + "/boot/" + container + ".info";
     default:
-      return Error() << "Invalid file type " << file_type;
+      auto result = Result<std::string>();
+      result.errmsg = "Invalid storage file type";
+      return result;
   }
 }
 
@@ -49,7 +48,9 @@
     StorageFileType file_type) {
   auto file_result = find_storage_file(storage_dir, container, file_type);
   if (!file_result.ok()) {
-    return Error() << file_result.error();
+    auto result = Result<MappedStorageFile*>();
+    result.errmsg = file_result.error();
+    return result;
   }
   return map_storage_file(*file_result);
 }
@@ -60,18 +61,24 @@
 Result<MappedStorageFile*> map_storage_file(std::string const& file) {
   int fd = open(file.c_str(), O_CLOEXEC | O_NOFOLLOW | O_RDONLY);
   if (fd == -1) {
-    return ErrnoError() << "failed to open " << file;
+    auto result = Result<MappedStorageFile*>();
+    result.errmsg = std::string("failed to open ") + file + ": " + strerror(errno);
+    return result;
   };
 
   struct stat fd_stat;
   if (fstat(fd, &fd_stat) < 0) {
-    return ErrnoError() << "fstat failed";
+    auto result = Result<MappedStorageFile*>();
+    result.errmsg = std::string("fstat failed: ") + strerror(errno);
+    return result;
   }
   size_t file_size = fd_stat.st_size;
 
   void* const map_result = mmap(nullptr, file_size, PROT_READ, MAP_SHARED, fd, 0);
   if (map_result == MAP_FAILED) {
-    return ErrnoError() << "mmap failed";
+    auto result = Result<MappedStorageFile*>();
+    result.errmsg = std::string("mmap failed: ") + strerror(errno);
+    return result;
   }
 
   auto mapped_file = new MappedStorageFile();
@@ -82,7 +89,7 @@
 }
 
 /// Map from StoredFlagType to FlagValueType
-android::base::Result<FlagValueType> map_to_flag_value_type(
+Result<FlagValueType> map_to_flag_value_type(
     StoredFlagType stored_type) {
   switch (stored_type) {
     case StoredFlagType::ReadWriteBoolean:
@@ -90,7 +97,9 @@
     case StoredFlagType::FixedReadOnlyBoolean:
       return FlagValueType::Boolean;
     default:
-      return Error() << "Unsupported stored flag type";
+      auto result = Result<FlagValueType>();
+      result.errmsg = "Unsupported stored flag type";
+      return result;
   }
 }
 
@@ -110,7 +119,9 @@
   if (version_cxx.query_success) {
     return version_cxx.version_number;
   } else {
-    return Error() << version_cxx.error_message;
+    auto result = Result<uint32_t>();
+    result.errmsg = version_cxx.error_message.c_str();
+    return result;
   }
 }
 
@@ -128,7 +139,9 @@
     context.boolean_start_index = context_cxx.boolean_start_index;
     return context;
   } else {
-    return Error() << context_cxx.error_message;
+    auto result = Result<PackageReadContext>();
+    result.errmsg = context_cxx.error_message.c_str();
+    return result;
   }
 }
 
@@ -147,7 +160,9 @@
     context.flag_index = context_cxx.flag_index;
     return context;
   } else {
-   return Error() << context_cxx.error_message;
+    auto result = Result<FlagReadContext>();
+    result.errmsg = context_cxx.error_message.c_str();
+    return result;
   }
 }
 
@@ -161,7 +176,9 @@
   if (value_cxx.query_success) {
     return value_cxx.flag_value;
   } else {
-    return Error() << value_cxx.error_message;
+    auto result = Result<bool>();
+    result.errmsg = value_cxx.error_message.c_str();
+    return result;
   }
 }
 
@@ -177,7 +194,9 @@
   if (info_cxx.query_success) {
     return info_cxx.flag_attribute;
   } else {
-    return Error() << info_cxx.error_message;
+    auto result = Result<uint8_t>();
+    result.errmsg = info_cxx.error_message.c_str();
+    return result;
   }
 }
 } // namespace aconfig_storage
diff --git a/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp b/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp
index e6d7537..b50935b 100644
--- a/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp
+++ b/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp
@@ -2,7 +2,7 @@
 
 #include <stdint.h>
 #include <string>
-#include <android-base/result.h>
+#include <cassert>
 
 namespace aconfig_storage {
 
@@ -58,46 +58,86 @@
   uint16_t flag_index;
 };
 
+
+template <class T>
+class Result {
+  public:
+
+  Result()
+      : data()
+      , errmsg()
+      , has_data(false)
+  {}
+
+  Result(T const& value)
+      : data(value)
+      , errmsg()
+      , has_data(true)
+  {}
+
+  bool ok() {
+    return has_data;
+  }
+
+  T& operator*() {
+    assert(has_data);
+    return data;
+  }
+
+  T* operator->() {
+    assert(has_data);
+    return &data;
+  }
+
+  std::string const& error() {
+    assert(!has_data);
+    return errmsg;
+  }
+
+  T data;
+  std::string errmsg;
+  bool has_data;
+};
+
 /// DO NOT USE APIS IN THE FOLLOWING NAMESPACE DIRECTLY
 namespace private_internal_api {
 
-android::base::Result<MappedStorageFile*> get_mapped_file_impl(
+Result<MappedStorageFile*> get_mapped_file_impl(
     std::string const& pb_file,
     std::string const& container,
     StorageFileType file_type);
-
 } // namespace private_internal_api
 
 /// Map a storage file
-android::base::Result<MappedStorageFile*> map_storage_file(
+Result<MappedStorageFile*> map_storage_file(
     std::string const& file);
 
 
 /// Map from StoredFlagType to FlagValueType
 /// \input stored_type: stored flag type in the storage file
 /// \returns the flag value type enum
-android::base::Result<FlagValueType> map_to_flag_value_type(
+Result<FlagValueType> map_to_flag_value_type(
     StoredFlagType stored_type);
 
 /// Get mapped storage file
 /// \input container: stoarge container name
 /// \input file_type: storage file type enum
 /// \returns a MappedStorageFileQuery
-android::base::Result<MappedStorageFile*> get_mapped_file(
+Result<MappedStorageFile*> get_mapped_file(
     std::string const& container,
     StorageFileType file_type);
 
 /// Get storage file version number
 /// \input file_path: the path to the storage file
 /// \returns the storage file version
-android::base::Result<uint32_t> get_storage_file_version(
+Result<uint32_t> get_storage_file_version(
     std::string const& file_path);
 
 /// Get package read context
 /// \input file: mapped storage file
 /// \input package: the flag package name
 /// \returns a package read context
-android::base::Result<PackageReadContext> get_package_read_context(
+Result<PackageReadContext> get_package_read_context(
     MappedStorageFile const& file,
     std::string const& package);
 
@@ -106,7 +146,7 @@
 /// \input package_id: the flag package id obtained from package offset query
 /// \input flag_name: flag name
 /// \returns the flag read context
-android::base::Result<FlagReadContext> get_flag_read_context(
+Result<FlagReadContext> get_flag_read_context(
     MappedStorageFile const& file,
     uint32_t package_id,
     std::string const& flag_name);
@@ -115,7 +155,7 @@
 /// \input file: mapped storage file
 /// \input index: the boolean flag index in the file
 /// \returns the boolean flag value
-android::base::Result<bool> get_boolean_flag_value(
+Result<bool> get_boolean_flag_value(
     MappedStorageFile const& file,
     uint32_t index);
 
@@ -124,7 +164,7 @@
 /// \input value_type: flag value type
 /// \input index: the boolean flag index in the file
 /// \returns the boolean flag attribute
-android::base::Result<uint8_t> get_flag_attribute(
+Result<uint8_t> get_flag_attribute(
     MappedStorageFile const& file,
     FlagValueType value_type,
     uint32_t index);
diff --git a/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp b/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp
index 86a0541..6d29045 100644
--- a/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp
+++ b/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp
@@ -97,7 +97,7 @@
   auto mapped_file_result = private_api::get_mapped_file_impl(
       storage_dir, "vendor", api::StorageFileType::package_map);
   ASSERT_FALSE(mapped_file_result.ok());
-  ASSERT_EQ(mapped_file_result.error().message(),
+  ASSERT_EQ(mapped_file_result.error(),
             std::string("failed to open ") + storage_dir
             + "/maps/vendor.package.map: No such file or directory");
 }
@@ -211,7 +211,7 @@
 
   auto value = api::get_boolean_flag_value(*mapped_file, 8);
   ASSERT_FALSE(value.ok());
-  ASSERT_EQ(value.error().message(),
+  ASSERT_EQ(value.error(),
             std::string("InvalidStorageFileOffset(Flag value offset goes beyond the end of the file.)"));
 }
 
@@ -243,6 +243,6 @@
 
   auto attribute = api::get_flag_attribute(*mapped_file, api::FlagValueType::Boolean, 8);
   ASSERT_FALSE(attribute.ok());
-  ASSERT_EQ(attribute.error().message(),
+  ASSERT_EQ(attribute.error(),
             std::string("InvalidStorageFileOffset(Flag info offset goes beyond the end of the file.)"));
 }
diff --git a/tools/aconfig/aconfig_storage_write_api/aconfig_storage_write_api.cpp b/tools/aconfig/aconfig_storage_write_api/aconfig_storage_write_api.cpp
index b441e05..cabc65e 100644
--- a/tools/aconfig/aconfig_storage_write_api/aconfig_storage_write_api.cpp
+++ b/tools/aconfig/aconfig_storage_write_api/aconfig_storage_write_api.cpp
@@ -10,32 +10,31 @@
 #include "aconfig_storage/lib.rs.h"
 #include "aconfig_storage/aconfig_storage_write_api.hpp"
 
-using namespace android::base;
-
 namespace aconfig_storage {
 
 /// Map a storage file
-Result<MutableMappedStorageFile*> map_mutable_storage_file(std::string const& file) {
+android::base::Result<MutableMappedStorageFile*> map_mutable_storage_file(
+    std::string const& file) {
   struct stat file_stat;
   if (stat(file.c_str(), &file_stat) < 0) {
-    return ErrnoError() << "stat failed";
+    return android::base::ErrnoError() << "stat failed";
   }
 
   if ((file_stat.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
-    return Error() << "cannot map nonwriteable file";
+    return android::base::Error() << "cannot map nonwriteable file";
   }
 
   size_t file_size = file_stat.st_size;
 
   const int fd = open(file.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC);
   if (fd == -1) {
-    return ErrnoError() << "failed to open " << file;
+    return android::base::ErrnoError() << "failed to open " << file;
   };
 
   void* const map_result =
       mmap(nullptr, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
   if (map_result == MAP_FAILED) {
-    return ErrnoError() << "mmap failed";
+    return android::base::ErrnoError() << "mmap failed";
   }
 
   auto mapped_file = new MutableMappedStorageFile();
@@ -46,7 +45,7 @@
 }
 
 /// Set boolean flag value
-Result<void> set_boolean_flag_value(
+android::base::Result<void> set_boolean_flag_value(
     const MutableMappedStorageFile& file,
     uint32_t offset,
     bool value) {
@@ -54,13 +53,13 @@
       static_cast<uint8_t*>(file.file_ptr), file.file_size);
   auto update_cxx = update_boolean_flag_value_cxx(content, offset, value);
   if (!update_cxx.update_success) {
-    return Error() << std::string(update_cxx.error_message.c_str());
+    return android::base::Error() << update_cxx.error_message.c_str();
   }
   return {};
 }
 
 /// Set if flag has server override
-Result<void> set_flag_has_server_override(
+android::base::Result<void> set_flag_has_server_override(
     const MutableMappedStorageFile& file,
     FlagValueType value_type,
     uint32_t offset,
@@ -70,13 +69,13 @@
   auto update_cxx = update_flag_has_server_override_cxx(
       content, static_cast<uint16_t>(value_type), offset, value);
   if (!update_cxx.update_success) {
-    return Error() << std::string(update_cxx.error_message.c_str());
+    return android::base::Error() << update_cxx.error_message.c_str();
   }
   return {};
 }
 
 /// Set if flag has local override
-Result<void> set_flag_has_local_override(
+android::base::Result<void> set_flag_has_local_override(
     const MutableMappedStorageFile& file,
     FlagValueType value_type,
     uint32_t offset,
@@ -86,12 +85,12 @@
   auto update_cxx = update_flag_has_local_override_cxx(
       content, static_cast<uint16_t>(value_type), offset, value);
   if (!update_cxx.update_success) {
-    return Error() << std::string(update_cxx.error_message.c_str());
+    return android::base::Error() << update_cxx.error_message.c_str();
   }
   return {};
 }
 
-Result<void> create_flag_info(
+android::base::Result<void> create_flag_info(
     std::string const& package_map,
     std::string const& flag_map,
     std::string const& flag_info_out) {
diff --git a/tools/aconfig/aconfig_storage_write_api/include/aconfig_storage/aconfig_storage_write_api.hpp b/tools/aconfig/aconfig_storage_write_api/include/aconfig_storage/aconfig_storage_write_api.hpp
index ff06cbc..0bba7ff 100644
--- a/tools/aconfig/aconfig_storage_write_api/include/aconfig_storage/aconfig_storage_write_api.hpp
+++ b/tools/aconfig/aconfig_storage_write_api/include/aconfig_storage/aconfig_storage_write_api.hpp
@@ -6,7 +6,6 @@
 #include <android-base/result.h>
 #include <aconfig_storage/aconfig_storage_read_api.hpp>
 
-using namespace android::base;
 
 namespace aconfig_storage {
 
@@ -14,24 +13,24 @@
 struct MutableMappedStorageFile : MappedStorageFile {};
 
 /// Map a storage file
-Result<MutableMappedStorageFile*> map_mutable_storage_file(
+android::base::Result<MutableMappedStorageFile*> map_mutable_storage_file(
     std::string const& file);
 
 /// Set boolean flag value
-Result<void> set_boolean_flag_value(
+android::base::Result<void> set_boolean_flag_value(
     const MutableMappedStorageFile& file,
     uint32_t offset,
     bool value);
 
 /// Set if flag has server override
-Result<void> set_flag_has_server_override(
+android::base::Result<void> set_flag_has_server_override(
     const MutableMappedStorageFile& file,
     FlagValueType value_type,
     uint32_t offset,
     bool value);
 
 /// Set if flag has local override
-Result<void> set_flag_has_local_override(
+android::base::Result<void> set_flag_has_local_override(
     const MutableMappedStorageFile& file,
     FlagValueType value_type,
     uint32_t offset,
@@ -41,7 +40,7 @@
 /// \input package_map: package map file
 /// \input flag_map: flag map file
 /// \input flag_info_out: flag info file to be created
-Result<void> create_flag_info(
+android::base::Result<void> create_flag_info(
     std::string const& package_map,
     std::string const& flag_map,
     std::string const& flag_info_out);