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

Commit 25889d80 authored by Dennis Shen's avatar Dennis Shen Committed by Gerrit Code Review
Browse files

Merge "aconfig: remove read api lib's dependency on libbase and liblog" into main

parents cfceeceb a0624828
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -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;
    }

+1 −4
Original line number Diff line number Diff line
@@ -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 {
+36 −17
Original line number Diff line number Diff line
#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 @@ static Result<std::string> find_storage_file(
    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 @@ Result<MappedStorageFile*> 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<MappedStorageFile*>();
    result.errmsg = file_result.error();
    return result;
  }
  return map_storage_file(*file_result);
}
@@ -60,18 +61,24 @@ Result<MappedStorageFile*> get_mapped_file_impl(
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 @@ Result<MappedStorageFile*> map_storage_file(std::string const& file) {
}

/// 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 @@ android::base::Result<FlagValueType> map_to_flag_value_type(
    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 @@ Result<uint32_t> get_storage_file_version(
  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 @@ Result<PackageReadContext> 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<PackageReadContext>();
    result.errmsg = context_cxx.error_message.c_str();
    return result;
  }
}

@@ -147,7 +160,9 @@ Result<FlagReadContext> get_flag_read_context(
    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 @@ Result<bool> get_boolean_flag_value(
  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 @@ Result<uint8_t> get_flag_attribute(
  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
+51 −11
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

#include <stdint.h>
#include <string>
#include <android-base/result.h>
#include <cassert>

namespace aconfig_storage {

@@ -58,46 +58,86 @@ struct FlagReadContext {
  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 @@ android::base::Result<PackageReadContext> 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<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 @@ android::base::Result<FlagReadContext> 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<bool> get_boolean_flag_value(
Result<bool> get_boolean_flag_value(
    MappedStorageFile const& file,
    uint32_t index);

@@ -124,7 +164,7 @@ android::base::Result<bool> 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<uint8_t> get_flag_attribute(
Result<uint8_t> get_flag_attribute(
    MappedStorageFile const& file,
    FlagValueType value_type,
    uint32_t index);
+3 −3
Original line number Diff line number Diff line
@@ -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.)"));
}
Loading