Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 260663dd authored by Dennis Shen's avatar Dennis Shen Committed by Gerrit Code Review
Browse files

Merge "aconfig: update storage file mapping api" into main

parents 5413a1e5 a49f1ba5
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -129,12 +129,14 @@ bool {header}_{item.flag_name}() \{
    }

    auto package_read_context = aconfig_storage::get_package_read_context(
        *package_map_file, "{package}");
        **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());
        return result;
    }

    delete *package_map_file;

    auto flag_val_map = aconfig_storage::get_mapped_file(
        "{item.container}",
        aconfig_storage::StorageFileType::flag_val);
@@ -144,13 +146,15 @@ bool {header}_{item.flag_name}() \{
    }

    auto value = aconfig_storage::get_boolean_flag_value(
        *flag_val_map,
        package_read_context->package_id + {item.flag_offset});
        **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());
        return result;
    }

    delete *flag_val_map;

    if (*value != result) \{
        ALOGI("error: new storage value '%d' does not match current value '%d'", *value, result);
    } else \{
+1 −1
Original line number Diff line number Diff line
@@ -92,10 +92,10 @@ cc_library {
    static_libs: [
        "libaconfig_storage_protos_cc",
        "libprotobuf-cpp-lite",
        "libbase",
    ],
    shared_libs: [
        "liblog",
        "libbase",
    ],
    apex_available: [
        "//apex_available:platform",
+11 −6
Original line number Diff line number Diff line
@@ -20,6 +20,11 @@ namespace aconfig_storage {
static constexpr char kAvailableStorageRecordsPb[] =
    "/metadata/aconfig/boot/available_storage_file_records.pb";

/// destructor
MappedStorageFile::~MappedStorageFile() {
  munmap(file_ptr, file_size);
}

/// Read aconfig storage records pb file
static Result<storage_records_pb> read_storage_records_pb(std::string const& pb_file) {
  auto records = storage_records_pb();
@@ -68,7 +73,7 @@ static Result<std::string> find_storage_file(
namespace private_internal_api {

/// Get mapped file implementation.
Result<MappedStorageFile> get_mapped_file_impl(
Result<MappedStorageFile*> get_mapped_file_impl(
    std::string const& pb_file,
    std::string const& container,
    StorageFileType file_type) {
@@ -82,7 +87,7 @@ Result<MappedStorageFile> get_mapped_file_impl(
} // namespace private internal api

/// Map a storage file
Result<MappedStorageFile> map_storage_file(std::string const& file) {
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;
@@ -99,9 +104,9 @@ Result<MappedStorageFile> map_storage_file(std::string const& file) {
    return ErrnoError() << "mmap failed";
  }

  auto mapped_file = MappedStorageFile();
  mapped_file.file_ptr = map_result;
  mapped_file.file_size = file_size;
  auto mapped_file = new MappedStorageFile();
  mapped_file->file_ptr = map_result;
  mapped_file->file_size = file_size;

  return mapped_file;
}
@@ -120,7 +125,7 @@ android::base::Result<FlagValueType> map_to_flag_value_type(
}

/// Get mapped storage file
Result<MappedStorageFile> get_mapped_file(
Result<MappedStorageFile*> get_mapped_file(
    std::string const& container,
    StorageFileType file_type) {
  return private_internal_api::get_mapped_file_impl(
+4 −3
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ enum FlagInfoBit {
struct MappedStorageFile {
  void* file_ptr;
  size_t file_size;
  ~MappedStorageFile();
};

/// Package read context query result
@@ -60,7 +61,7 @@ struct FlagReadContext {
/// DO NOT USE APIS IN THE FOLLOWING NAMESPACE DIRECTLY
namespace private_internal_api {

android::base::Result<MappedStorageFile> get_mapped_file_impl(
android::base::Result<MappedStorageFile*> get_mapped_file_impl(
    std::string const& pb_file,
    std::string const& container,
    StorageFileType file_type);
@@ -68,7 +69,7 @@ android::base::Result<MappedStorageFile> get_mapped_file_impl(
} // namespace private_internal_api

/// Map a storage file
android::base::Result<MappedStorageFile> map_storage_file(
android::base::Result<MappedStorageFile*> map_storage_file(
    std::string const& file);


@@ -82,7 +83,7 @@ android::base::Result<FlagValueType> map_to_flag_value_type(
/// \input container: stoarge container name
/// \input file_type: storage file type enum
/// \returns a MappedStorageFileQuery
android::base::Result<MappedStorageFile> get_mapped_file(
android::base::Result<MappedStorageFile*> get_mapped_file(
    std::string const& container,
    StorageFileType file_type);

+28 −19
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include <string>
#include <vector>
#include <memory>
#include <cstdio>

#include <sys/stat.h>
@@ -111,18 +112,19 @@ TEST_F(AconfigStorageTest, test_storage_version_query) {

/// Negative test to lock down the error when mapping none exist storage files
TEST_F(AconfigStorageTest, test_none_exist_storage_file_mapping) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "vendor", api::StorageFileType::package_map);
  ASSERT_FALSE(mapped_file.ok());
  ASSERT_EQ(mapped_file.error().message(),
  ASSERT_FALSE(mapped_file_result.ok());
  ASSERT_EQ(mapped_file_result.error().message(),
            "Unable to find storage files for container vendor");
}

/// Test to lock down storage package context query api
TEST_F(AconfigStorageTest, test_package_context_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::package_map);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto context = api::get_package_read_context(
      *mapped_file, "com.android.aconfig.storage.test_1");
@@ -148,9 +150,10 @@ TEST_F(AconfigStorageTest, test_package_context_query) {

/// Test to lock down when querying none exist package
TEST_F(AconfigStorageTest, test_none_existent_package_context_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::package_map);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto context = api::get_package_read_context(
      *mapped_file, "com.android.aconfig.storage.test_3");
@@ -160,9 +163,10 @@ TEST_F(AconfigStorageTest, test_none_existent_package_context_query) {

/// Test to lock down storage flag context query api
TEST_F(AconfigStorageTest, test_flag_context_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::flag_map);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto baseline = std::vector<std::tuple<int, std::string, api::StoredFlagType, int>>{
    {0, "enabled_ro", api::StoredFlagType::ReadOnlyBoolean, 1},
@@ -185,9 +189,10 @@ TEST_F(AconfigStorageTest, test_flag_context_query) {

/// Test to lock down when querying none exist flag
TEST_F(AconfigStorageTest, test_none_existent_flag_context_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::flag_map);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto context = api::get_flag_read_context(*mapped_file, 0, "none_exist");
  ASSERT_TRUE(context.ok());
@@ -200,9 +205,10 @@ TEST_F(AconfigStorageTest, test_none_existent_flag_context_query) {

/// Test to lock down storage flag value query api
TEST_F(AconfigStorageTest, test_boolean_flag_value_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::flag_val);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto expected_value = std::vector<bool>{
    false, true, true, false, true, true, true, true};
@@ -215,9 +221,10 @@ TEST_F(AconfigStorageTest, test_boolean_flag_value_query) {

/// Negative test to lock down the error when querying flag value out of range
TEST_F(AconfigStorageTest, test_invalid_boolean_flag_value_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::flag_val);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto value = api::get_boolean_flag_value(*mapped_file, 8);
  ASSERT_FALSE(value.ok());
@@ -227,9 +234,10 @@ TEST_F(AconfigStorageTest, test_invalid_boolean_flag_value_query) {

/// Test to lock down storage flag info query api
TEST_F(AconfigStorageTest, test_boolean_flag_info_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::flag_info);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto expected_value = std::vector<bool>{
    true, false, true, true, false, false, false, true};
@@ -245,9 +253,10 @@ TEST_F(AconfigStorageTest, test_boolean_flag_info_query) {

/// Negative test to lock down the error when querying flag info out of range
TEST_F(AconfigStorageTest, test_invalid_boolean_flag_info_query) {
  auto mapped_file = private_api::get_mapped_file_impl(
  auto mapped_file_result = private_api::get_mapped_file_impl(
      storage_record_pb, "mockup", api::StorageFileType::flag_info);
  ASSERT_TRUE(mapped_file.ok());
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = std::unique_ptr<api::MappedStorageFile>(*mapped_file_result);

  auto attribute = api::get_flag_attribute(*mapped_file, api::FlagValueType::Boolean, 8);
  ASSERT_FALSE(attribute.ok());
Loading