diff --git a/fastboot/Android.bp b/fastboot/Android.bp index 3c4269b7c..0ca8ba858 100644 --- a/fastboot/Android.bp +++ b/fastboot/Android.bp @@ -206,6 +206,7 @@ cc_defaults { "-Wextra", "-Werror", "-Wunreachable-code", + "-DANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION", ], target: { diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 402c5c5f7..783cd1974 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -230,9 +230,9 @@ static void InfoMessage(const std::string& info) { fprintf(stderr, "(bootloader) %s\n", info.c_str()); } -static int64_t get_file_size(int fd) { +static int64_t get_file_size(borrowed_fd fd) { struct stat sb; - if (fstat(fd, &sb) == -1) { + if (fstat(fd.get(), &sb) == -1) { die("could not get file size"); } return sb.st_size; @@ -657,12 +657,12 @@ static unique_fd unzip_to_file(ZipArchiveHandle zip, const char* entry_name) { fprintf(stderr, "extracting %s (%" PRIu64 " MB) to disk...", entry_name, zip_entry.uncompressed_length / 1024 / 1024); double start = now(); - int error = ExtractEntryToFile(zip, &zip_entry, fd); + int error = ExtractEntryToFile(zip, &zip_entry, fd.get()); if (error != 0) { die("\nfailed to extract '%s': %s", entry_name, ErrorCodeString(error)); } - if (lseek(fd, 0, SEEK_SET) != 0) { + if (lseek(fd.get(), 0, SEEK_SET) != 0) { die("\nlseek on extracted file '%s' failed: %s", entry_name, strerror(errno)); } @@ -902,14 +902,14 @@ static bool load_buf_fd(unique_fd fd, struct fastboot_buffer* buf) { return false; } - if (sparse_file* s = sparse_file_import(fd, false, false)) { + if (sparse_file* s = sparse_file_import(fd.get(), false, false)) { buf->image_size = sparse_file_len(s, false, false); sparse_file_destroy(s); } else { buf->image_size = sz; } - lseek(fd, 0, SEEK_SET); + lseek(fd.get(), 0, SEEK_SET); int64_t limit = get_sparse_limit(sz); buf->fd = std::move(fd); if (limit) { @@ -936,7 +936,7 @@ static bool load_buf(const char* fname, struct fastboot_buffer* buf) { } struct stat s; - if (fstat(fd, &s)) { + if (fstat(fd.get(), &s)) { return false; } if (!S_ISREG(s.st_mode)) { @@ -995,7 +995,7 @@ static void rewrite_vbmeta_buffer(struct fastboot_buffer* buf, bool vbmeta_in_bo die("Failed writing to modified vbmeta"); } buf->fd = std::move(fd); - lseek(buf->fd, 0, SEEK_SET); + lseek(buf->fd.get(), 0, SEEK_SET); } static bool has_vbmeta_partition() { @@ -1057,13 +1057,13 @@ static void copy_boot_avb_footer(const std::string& partition, struct fastboot_b if (!android::base::WriteStringToFd(data, fd)) { die("Failed writing to modified boot"); } - lseek(fd, partition_size - AVB_FOOTER_SIZE, SEEK_SET); + lseek(fd.get(), partition_size - AVB_FOOTER_SIZE, SEEK_SET); if (!android::base::WriteStringToFd(data.substr(footer_offset), fd)) { die("Failed copying AVB footer in boot"); } buf->fd = std::move(fd); buf->sz = partition_size; - lseek(buf->fd, 0, SEEK_SET); + lseek(buf->fd.get(), 0, SEEK_SET); } static void flash_buf(const std::string& partition, struct fastboot_buffer *buf) @@ -1538,7 +1538,7 @@ void FlashAllTool::FlashImage(const Image& image, const std::string& slot, fastb } void FlashAllTool::UpdateSuperPartition() { - int fd = source_.OpenFile("super_empty.img"); + unique_fd fd = source_.OpenFile("super_empty.img"); if (fd < 0) { return; } @@ -2197,7 +2197,7 @@ int FastBootTool::Main(int argc, char* argv[]) { if (!load_buf(filename.c_str(), &buf) || buf.type != FB_BUFFER_FD) { die("cannot load '%s'", filename.c_str()); } - fb->Download(filename, buf.fd, buf.sz); + fb->Download(filename, buf.fd.get(), buf.sz); } else if (command == "get_staged") { std::string filename = next_arg(&args); fb->Upload(filename); diff --git a/fastboot/fastboot_driver.cpp b/fastboot/fastboot_driver.cpp index 14ee78555..99a48734c 100644 --- a/fastboot/fastboot_driver.cpp +++ b/fastboot/fastboot_driver.cpp @@ -144,7 +144,8 @@ RetCode FastBootDriver::FlashPartition(const std::string& partition, return Flash(partition); } -RetCode FastBootDriver::FlashPartition(const std::string& partition, int fd, uint32_t size) { +RetCode FastBootDriver::FlashPartition(const std::string& partition, android::base::borrowed_fd fd, + uint32_t size) { RetCode ret; if ((ret = Download(partition, fd, size))) { return ret; @@ -182,15 +183,16 @@ RetCode FastBootDriver::Partitions(std::vector return SUCCESS; } -RetCode FastBootDriver::Download(const std::string& name, int fd, size_t size, - std::string* response, std::vector* info) { +RetCode FastBootDriver::Download(const std::string& name, android::base::borrowed_fd fd, + size_t size, std::string* response, + std::vector* info) { prolog_(StringPrintf("Sending '%s' (%zu KB)", name.c_str(), size / 1024)); auto result = Download(fd, size, response, info); epilog_(result); return result; } -RetCode FastBootDriver::Download(int fd, size_t size, std::string* response, +RetCode FastBootDriver::Download(android::base::borrowed_fd fd, size_t size, std::string* response, std::vector* info) { RetCode ret; @@ -521,7 +523,7 @@ std::string FastBootDriver::ErrnoStr(const std::string& msg) { } /******************************* PRIVATE **************************************/ -RetCode FastBootDriver::SendBuffer(int fd, size_t size) { +RetCode FastBootDriver::SendBuffer(android::base::borrowed_fd fd, size_t size) { static constexpr uint32_t MAX_MAP_SIZE = 512 * 1024 * 1024; off64_t offset = 0; uint32_t remaining = size; diff --git a/fastboot/fastboot_driver.h b/fastboot/fastboot_driver.h index f1c094f28..bccd6685f 100644 --- a/fastboot/fastboot_driver.h +++ b/fastboot/fastboot_driver.h @@ -77,9 +77,9 @@ class FastBootDriver { RetCode Continue(std::string* response = nullptr, std::vector* info = nullptr); RetCode CreatePartition(const std::string& partition, const std::string& size); RetCode DeletePartition(const std::string& partition); - RetCode Download(const std::string& name, int fd, size_t size, std::string* response = nullptr, - std::vector* info = nullptr); - RetCode Download(int fd, size_t size, std::string* response = nullptr, + RetCode Download(const std::string& name, android::base::borrowed_fd fd, size_t size, + std::string* response = nullptr, std::vector* info = nullptr); + RetCode Download(android::base::borrowed_fd fd, size_t size, std::string* response = nullptr, std::vector* info = nullptr); RetCode Download(const std::string& name, const std::vector& buf, std::string* response = nullptr, std::vector* info = nullptr); @@ -113,7 +113,8 @@ class FastBootDriver { /* HIGHER LEVEL COMMANDS -- Composed of the commands above */ RetCode FlashPartition(const std::string& partition, const std::vector& data); - RetCode FlashPartition(const std::string& partition, int fd, uint32_t sz); + RetCode FlashPartition(const std::string& partition, android::base::borrowed_fd fd, + uint32_t sz); RetCode FlashPartition(const std::string& partition, sparse_file* s, uint32_t sz, size_t current, size_t total); @@ -149,7 +150,7 @@ class FastBootDriver { Transport* transport_; private: - RetCode SendBuffer(int fd, size_t size); + RetCode SendBuffer(android::base::borrowed_fd fd, size_t size); RetCode SendBuffer(const std::vector& buf); RetCode SendBuffer(const void* buf, size_t size);