summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Zhi Dou <zhidou@google.com> 2024-07-26 22:25:43 +0000
committer Zhi Dou <zhidou@google.com> 2024-07-26 22:25:43 +0000
commit8add8e0c9b7b1baaccfa91cb3a38de7a95bfd7cc (patch)
tree8b92c5ad7a6fbafb2404cddb843d2457652abdb8
parente75610b4c7843987f18727dc880af1be729c30ac (diff)
use smart pointer for fd
Test: presubmit Bug: 348693143 Change-Id: Ibdb1e132a4840d0c3d82aa5f7546a0fc5f5f528b
-rw-r--r--tools/aconfig/aconfig_storage_read_api/Android.bp3
-rw-r--r--tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp9
2 files changed, 8 insertions, 4 deletions
diff --git a/tools/aconfig/aconfig_storage_read_api/Android.bp b/tools/aconfig/aconfig_storage_read_api/Android.bp
index d88d742e64..619b488814 100644
--- a/tools/aconfig/aconfig_storage_read_api/Android.bp
+++ b/tools/aconfig/aconfig_storage_read_api/Android.bp
@@ -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,
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 97ada3a33e..8e0c4e1a12 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,3 +1,4 @@
+#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);