From a0624828b2470f3f6a43eb48b78977afd58d3c20 Mon Sep 17 00:00:00 2001 From: Dennis Shen Date: Thu, 23 May 2024 01:53:29 +0000 Subject: [PATCH] aconfig: remove read api lib's dependency on libbase and liblog Bug: b/321077378 Test: atest -c Change-Id: I0bc7780de2123021e1cc9f7a29ca3f7dabebcd40 --- .../templates/cpp_source_file.template | 8 +-- .../aconfig_storage_read_api/Android.bp | 5 +- .../aconfig_storage_read_api.cpp | 53 +++++++++++----- .../aconfig_storage_read_api.hpp | 62 +++++++++++++++---- .../tests/storage_read_api_test.cpp | 6 +- .../aconfig_storage_write_api.cpp | 27 ++++---- .../aconfig_storage_write_api.hpp | 11 ++-- 7 files changed, 113 insertions(+), 59 deletions(-) diff --git a/tools/aconfig/aconfig/templates/cpp_source_file.template b/tools/aconfig/aconfig/templates/cpp_source_file.template index 4c098c5b41..38dda7df31 100644 --- a/tools/aconfig/aconfig/templates/cpp_source_file.template +++ b/tools/aconfig/aconfig/templates/cpp_source_file.template @@ -124,14 +124,14 @@ bool {header}_{item.flag_name}() \{ "{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 @@ bool {header}_{item.flag_name}() \{ "{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 @@ bool {header}_{item.flag_name}() \{ **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 d1c282d982..9b842546f5 100644 --- a/tools/aconfig/aconfig_storage_read_api/Android.bp +++ b/tools/aconfig/aconfig_storage_read_api/Android.bp @@ -90,10 +90,6 @@ cc_library { 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 @@ cc_library { }, }, 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 50076aab31..97ada3a33e 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 -#include - #include #include #include +#include +#include #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 @@ static Result find_storage_file( case StorageFileType::flag_info: return storage_dir + "/boot/" + container + ".info"; default: - return Error() << "Invalid file type " << file_type; + auto result = Result(); + result.errmsg = "Invalid storage file type"; + return result; } } @@ -49,7 +48,9 @@ Result get_mapped_file_impl( 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(); + result.errmsg = file_result.error(); + return result; } return map_storage_file(*file_result); } @@ -60,18 +61,24 @@ Result get_mapped_file_impl( Result 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(); + 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(); + 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(); + result.errmsg = std::string("mmap failed: ") + strerror(errno); + return result; } auto mapped_file = new MappedStorageFile(); @@ -82,7 +89,7 @@ Result map_storage_file(std::string const& file) { } /// Map from StoredFlagType to FlagValueType -android::base::Result map_to_flag_value_type( +Result map_to_flag_value_type( StoredFlagType stored_type) { switch (stored_type) { case StoredFlagType::ReadWriteBoolean: @@ -90,7 +97,9 @@ android::base::Result map_to_flag_value_type( case StoredFlagType::FixedReadOnlyBoolean: return FlagValueType::Boolean; default: - return Error() << "Unsupported stored flag type"; + auto result = Result(); + result.errmsg = "Unsupported stored flag type"; + return result; } } @@ -110,7 +119,9 @@ Result get_storage_file_version( if (version_cxx.query_success) { return version_cxx.version_number; } else { - return Error() << version_cxx.error_message; + auto result = Result(); + result.errmsg = version_cxx.error_message.c_str(); + return result; } } @@ -128,7 +139,9 @@ Result get_package_read_context( context.boolean_start_index = context_cxx.boolean_start_index; return context; } else { - return Error() << context_cxx.error_message; + auto result = Result(); + result.errmsg = context_cxx.error_message.c_str(); + return result; } } @@ -147,7 +160,9 @@ Result get_flag_read_context( context.flag_index = context_cxx.flag_index; return context; } else { - return Error() << context_cxx.error_message; + auto result = Result(); + result.errmsg = context_cxx.error_message.c_str(); + return result; } } @@ -161,7 +176,9 @@ Result get_boolean_flag_value( if (value_cxx.query_success) { return value_cxx.flag_value; } else { - return Error() << value_cxx.error_message; + auto result = Result(); + result.errmsg = value_cxx.error_message.c_str(); + return result; } } @@ -177,7 +194,9 @@ Result get_flag_attribute( if (info_cxx.query_success) { return info_cxx.flag_attribute; } else { - return Error() << info_cxx.error_message; + auto result = Result(); + 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 e6d75373a9..b50935bf69 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 #include -#include +#include namespace aconfig_storage { @@ -58,46 +58,86 @@ struct FlagReadContext { uint16_t flag_index; }; + +template +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 get_mapped_file_impl( +Result 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 map_storage_file( +Result 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 map_to_flag_value_type( +Result 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 get_mapped_file( +Result 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 get_storage_file_version( +Result 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 get_package_read_context( +Result get_package_read_context( MappedStorageFile const& file, std::string const& package); @@ -106,7 +146,7 @@ android::base::Result get_package_read_context( /// \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 get_flag_read_context( +Result get_flag_read_context( MappedStorageFile const& file, uint32_t package_id, std::string const& flag_name); @@ -115,7 +155,7 @@ android::base::Result get_flag_read_context( /// \input file: mapped storage file /// \input index: the boolean flag index in the file /// \returns the boolean flag value -android::base::Result get_boolean_flag_value( +Result get_boolean_flag_value( MappedStorageFile const& file, uint32_t index); @@ -124,7 +164,7 @@ android::base::Result get_boolean_flag_value( /// \input value_type: flag value type /// \input index: the boolean flag index in the file /// \returns the boolean flag attribute -android::base::Result get_flag_attribute( +Result 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 86a0541c53..6d29045efe 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 @@ TEST_F(AconfigStorageTest, test_none_exist_storage_file_mapping) { 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 @@ TEST_F(AconfigStorageTest, test_invalid_boolean_flag_value_query) { 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 @@ TEST_F(AconfigStorageTest, test_invalid_boolean_flag_info_query) { 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 b441e057ed..cabc65e40c 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 map_mutable_storage_file(std::string const& file) { +android::base::Result 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 @@ Result map_mutable_storage_file(std::string const& fi } /// Set boolean flag value -Result set_boolean_flag_value( +android::base::Result set_boolean_flag_value( const MutableMappedStorageFile& file, uint32_t offset, bool value) { @@ -54,13 +53,13 @@ Result set_boolean_flag_value( static_cast(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 set_flag_has_server_override( +android::base::Result set_flag_has_server_override( const MutableMappedStorageFile& file, FlagValueType value_type, uint32_t offset, @@ -70,13 +69,13 @@ Result set_flag_has_server_override( auto update_cxx = update_flag_has_server_override_cxx( content, static_cast(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 set_flag_has_local_override( +android::base::Result set_flag_has_local_override( const MutableMappedStorageFile& file, FlagValueType value_type, uint32_t offset, @@ -86,12 +85,12 @@ Result set_flag_has_local_override( auto update_cxx = update_flag_has_local_override_cxx( content, static_cast(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 create_flag_info( +android::base::Result 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 ff06cbc6de..0bba7ffcfc 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 #include -using namespace android::base; namespace aconfig_storage { @@ -14,24 +13,24 @@ namespace aconfig_storage { struct MutableMappedStorageFile : MappedStorageFile {}; /// Map a storage file -Result map_mutable_storage_file( +android::base::Result map_mutable_storage_file( std::string const& file); /// Set boolean flag value -Result set_boolean_flag_value( +android::base::Result set_boolean_flag_value( const MutableMappedStorageFile& file, uint32_t offset, bool value); /// Set if flag has server override -Result set_flag_has_server_override( +android::base::Result set_flag_has_server_override( const MutableMappedStorageFile& file, FlagValueType value_type, uint32_t offset, bool value); /// Set if flag has local override -Result set_flag_has_local_override( +android::base::Result set_flag_has_local_override( const MutableMappedStorageFile& file, FlagValueType value_type, uint32_t offset, @@ -41,7 +40,7 @@ Result set_flag_has_local_override( /// \input package_map: package map file /// \input flag_map: flag map file /// \input flag_info_out: flag info file to be created -Result create_flag_info( +android::base::Result create_flag_info( std::string const& package_map, std::string const& flag_map, std::string const& flag_info_out);