Merge "use smart pointer for fd" into main am: 78decbf865
Original change: https://android-review.googlesource.com/c/platform/build/+/3195552 Change-Id: I2456f7cae1669c634804b73bc78a152d3df6d880 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
@@ -87,6 +87,9 @@ cc_library {
|
|||||||
generated_sources: ["libcxx_aconfig_storage_read_api_bridge_code"],
|
generated_sources: ["libcxx_aconfig_storage_read_api_bridge_code"],
|
||||||
whole_static_libs: ["libaconfig_storage_read_api_cxx_bridge"],
|
whole_static_libs: ["libaconfig_storage_read_api_cxx_bridge"],
|
||||||
export_include_dirs: ["include"],
|
export_include_dirs: ["include"],
|
||||||
|
static_libs: [
|
||||||
|
"libbase",
|
||||||
|
],
|
||||||
host_supported: true,
|
host_supported: true,
|
||||||
vendor_available: true,
|
vendor_available: true,
|
||||||
product_available: true,
|
product_available: true,
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
#include <android-base/unique_fd.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@@ -59,22 +60,22 @@ Result<MappedStorageFile*> get_mapped_file_impl(
|
|||||||
|
|
||||||
/// Map a storage file
|
/// 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);
|
android::base::unique_fd ufd(open(file.c_str(), O_CLOEXEC | O_NOFOLLOW | O_RDONLY));
|
||||||
if (fd == -1) {
|
if (ufd.get() == -1) {
|
||||||
auto result = Result<MappedStorageFile*>();
|
auto result = Result<MappedStorageFile*>();
|
||||||
result.errmsg = std::string("failed to open ") + file + ": " + strerror(errno);
|
result.errmsg = std::string("failed to open ") + file + ": " + strerror(errno);
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct stat fd_stat;
|
struct stat fd_stat;
|
||||||
if (fstat(fd, &fd_stat) < 0) {
|
if (fstat(ufd.get(), &fd_stat) < 0) {
|
||||||
auto result = Result<MappedStorageFile*>();
|
auto result = Result<MappedStorageFile*>();
|
||||||
result.errmsg = std::string("fstat failed: ") + strerror(errno);
|
result.errmsg = std::string("fstat failed: ") + strerror(errno);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
size_t file_size = fd_stat.st_size;
|
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) {
|
if (map_result == MAP_FAILED) {
|
||||||
auto result = Result<MappedStorageFile*>();
|
auto result = Result<MappedStorageFile*>();
|
||||||
result.errmsg = std::string("mmap failed: ") + strerror(errno);
|
result.errmsg = std::string("mmap failed: ") + strerror(errno);
|
||||||
|
Reference in New Issue
Block a user