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

Commit 8add8e0c authored by Zhi Dou's avatar Zhi Dou
Browse files

use smart pointer for fd

Test: presubmit
Bug: 348693143
Change-Id: Ibdb1e132a4840d0c3d82aa5f7546a0fc5f5f528b
parent e75610b4
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -87,6 +87,9 @@ cc_library {
    generated_sources: ["libcxx_aconfig_storage_read_api_bridge_code"],
    whole_static_libs: ["libaconfig_storage_read_api_cxx_bridge"],
    export_include_dirs: ["include"],
    static_libs: [
        "libbase",
    ],
    host_supported: true,
    vendor_available: true,
    product_available: true,
+5 −4
Original line number Diff line number Diff line
#include <android-base/unique_fd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
@@ -59,22 +60,22 @@ Result<MappedStorageFile*> get_mapped_file_impl(

/// Map a storage 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) {
  android::base::unique_fd ufd(open(file.c_str(), O_CLOEXEC | O_NOFOLLOW | O_RDONLY));
  if (ufd.get() == -1) {
    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) {
  if (fstat(ufd.get(), &fd_stat) < 0) {
    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);
  void* const map_result = mmap(nullptr, file_size, PROT_READ, MAP_SHARED, ufd.get(), 0);
  if (map_result == MAP_FAILED) {
    auto result = Result<MappedStorageFile*>();
    result.errmsg = std::string("mmap failed: ") + strerror(errno);