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

Commit f7a35a06 authored by Dennis Shen's avatar Dennis Shen Committed by Android (Google) Code Review
Browse files

Merge "Remove obsolete aconfig storage file cpp write API" into main

parents 5eb90384 589c533c
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -79,10 +79,6 @@
      // aconfig_storage write api rust integration tests
      "name": "aconfig_storage_write_api.test.rust"
    },
    {
      // aconfig_storage write api cpp integration tests
      "name": "aconfig_storage_write_api.test.cpp"
    },
    {
      // aconfig_storage read api rust integration tests
      "name": "aconfig_storage_read_api.test.rust"
+0 −48
Original line number Diff line number Diff line
@@ -10,7 +10,6 @@ rust_defaults {
    rustlibs: [
        "libanyhow",
        "libmemmap2",
        "libcxx",
        "libthiserror",
    ],
    min_sdk_version: "34",
@@ -45,50 +44,3 @@ rust_test_host {
        "libtempfile",
    ],
}

// cxx source codegen from rust api
genrule {
    name: "libcxx_aconfig_storage_write_api_bridge_code",
    tools: ["cxxbridge"],
    cmd: "$(location cxxbridge) $(in) > $(out)",
    srcs: ["src/lib.rs"],
    out: ["aconfig_storage/lib.rs.cc"],
}

// cxx header codegen from rust api
genrule {
    name: "libcxx_aconfig_storage_write_api_bridge_header",
    tools: ["cxxbridge"],
    cmd: "$(location cxxbridge) $(in) --header > $(out)",
    srcs: ["src/lib.rs"],
    out: ["aconfig_storage/lib.rs.h"],
}

// a static cc lib based on generated code
rust_ffi_static {
    name: "libaconfig_storage_write_api_cxx_bridge",
    crate_name: "aconfig_storage_write_api_cxx_bridge",
    host_supported: true,
    defaults: ["aconfig_storage_write_api.defaults"],
    rustlibs: [
        "libaconfig_storage_file",
    ],
}

// flag write api cc interface
cc_library_static {
    name: "libaconfig_storage_write_api_cc",
    srcs: ["aconfig_storage_write_api.cpp"],
    generated_headers: [
        "cxx-bridge-header",
        "libcxx_aconfig_storage_write_api_bridge_header",
    ],
    generated_sources: ["libcxx_aconfig_storage_write_api_bridge_code"],
    whole_static_libs: ["libaconfig_storage_write_api_cxx_bridge"],
    export_include_dirs: ["include"],
    static_libs: [
        "libaconfig_storage_read_api_cc",
        "libprotobuf-cpp-lite",
        "libbase",
    ],
}
+0 −4
Original line number Diff line number Diff line
@@ -9,12 +9,8 @@ cargo = []

[dependencies]
anyhow = "1.0.69"
cxx = "1.0"
memmap2 = "0.8.0"
tempfile = "3.9.0"
thiserror = "1.0.56"
aconfig_storage_file = { path = "../aconfig_storage_file" }
aconfig_storage_read_api = { path = "../aconfig_storage_read_api" }

[build-dependencies]
cxx-build = "1.0"
+0 −103
Original line number Diff line number Diff line

#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/unique_fd.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "rust/cxx.h"
#include "aconfig_storage/lib.rs.h"
#include "aconfig_storage/aconfig_storage_write_api.hpp"

namespace aconfig_storage {

/// Map a storage 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 android::base::ErrnoError() << "stat failed";
  }

  if ((file_stat.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) == 0) {
    return android::base::Error() << "cannot map nonwriteable file";
  }

  size_t file_size = file_stat.st_size;

  android::base::unique_fd ufd(open(file.c_str(), O_RDWR | O_NOFOLLOW | O_CLOEXEC));
  if (ufd.get() == -1) {
    return android::base::ErrnoError() << "failed to open " << file;
  };

  void *const map_result =
      mmap(nullptr, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, ufd.get(), 0);
  if (map_result == MAP_FAILED) {
    return android::base::ErrnoError() << "mmap failed";
  }

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

  return mapped_file;
}

/// Set boolean flag value
android::base::Result<void> set_boolean_flag_value(
    const MutableMappedStorageFile &file,
    uint32_t offset,
    bool value) {
  auto content = rust::Slice<uint8_t>(
      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 android::base::Error() << update_cxx.error_message.c_str();
  }
  if (!msync(static_cast<uint8_t *>(file.file_ptr) + update_cxx.offset, 1, MS_SYNC)) {
    return android::base::ErrnoError() << "msync failed";
  }
  return {};
}

/// Set if flag has server override
android::base::Result<void> set_flag_has_server_override(
    const MutableMappedStorageFile &file,
    FlagValueType value_type,
    uint32_t offset,
    bool value) {
  auto content = rust::Slice<uint8_t>(
      static_cast<uint8_t *>(file.file_ptr), file.file_size);
  auto update_cxx = update_flag_has_server_override_cxx(
      content, static_cast<uint16_t>(value_type), offset, value);
  if (!update_cxx.update_success) {
    return android::base::Error() << update_cxx.error_message.c_str();
  }
  if (!msync(static_cast<uint8_t *>(file.file_ptr) + update_cxx.offset, 1, MS_SYNC)) {
    return android::base::ErrnoError() << "msync failed";
  }
  return {};
}

/// Set if flag has local override
android::base::Result<void> set_flag_has_local_override(
    const MutableMappedStorageFile &file,
    FlagValueType value_type,
    uint32_t offset,
    bool value) {
  auto content = rust::Slice<uint8_t>(
      static_cast<uint8_t *>(file.file_ptr), file.file_size);
  auto update_cxx = update_flag_has_local_override_cxx(
      content, static_cast<uint16_t>(value_type), offset, value);
  if (!update_cxx.update_success) {
    return android::base::Error() << update_cxx.error_message.c_str();
  }
  if (!msync(static_cast<uint8_t *>(file.file_ptr) + update_cxx.offset, 1, MS_SYNC)) {
    return android::base::ErrnoError() << "msync failed";
  }
  return {};
}

} // namespace aconfig_storage
+0 −4
Original line number Diff line number Diff line
fn main() {
    let _ = cxx_build::bridge("src/lib.rs");
    println!("cargo:rerun-if-changed=src/lib.rs");
}
Loading