From deb46f31bf04f17e8a636ec2eead8f75268f64ec Mon Sep 17 00:00:00 2001 From: Jeremy Meyer Date: Wed, 8 Nov 2023 12:20:24 -0800 Subject: Handle 9patches being used for frros This adds an api that allows creation of nine patch frros. We process them the same as aapt2 does so that when they are used at runtime they work correctly. API-Coverage-Bug: 314168567 Test: manual and automatic Bug: 296324826 Change-Id: I40da020189e9ec914fbea0c17f181209347d83de --- libs/androidfw/FileStream.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'libs/androidfw/FileStream.cpp') diff --git a/libs/androidfw/FileStream.cpp b/libs/androidfw/FileStream.cpp index b86c9cb729d4..e8989490fe2c 100644 --- a/libs/androidfw/FileStream.cpp +++ b/libs/androidfw/FileStream.cpp @@ -22,6 +22,7 @@ #include "android-base/errors.h" #include "android-base/file.h" // for O_BINARY +#include "android-base/logging.h" #include "android-base/macros.h" #include "android-base/utf8.h" @@ -37,9 +38,9 @@ using ::android::base::unique_fd; namespace android { FileInputStream::FileInputStream(const std::string& path, size_t buffer_capacity) - : buffer_capacity_(buffer_capacity) { + : should_close_(true), buffer_capacity_(buffer_capacity) { int mode = O_RDONLY | O_CLOEXEC | O_BINARY; - fd_.reset(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode))); + fd_ = TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), mode)); if (fd_ == -1) { error_ = SystemErrorCodeToString(errno); } else { @@ -48,7 +49,17 @@ FileInputStream::FileInputStream(const std::string& path, size_t buffer_capacity } FileInputStream::FileInputStream(int fd, size_t buffer_capacity) - : fd_(fd), buffer_capacity_(buffer_capacity) { + : fd_(fd), should_close_(true), buffer_capacity_(buffer_capacity) { + if (fd_ < 0) { + error_ = "Bad File Descriptor"; + } else { + buffer_.reset(new uint8_t[buffer_capacity_]); + } +} + +FileInputStream::FileInputStream(android::base::borrowed_fd fd, size_t buffer_capacity) + : fd_(fd.get()), should_close_(false), buffer_capacity_(buffer_capacity) { + if (fd_ < 0) { error_ = "Bad File Descriptor"; } else { @@ -56,6 +67,7 @@ FileInputStream::FileInputStream(int fd, size_t buffer_capacity) } } + bool FileInputStream::Next(const void** data, size_t* size) { if (HadError()) { return false; @@ -73,7 +85,12 @@ bool FileInputStream::Next(const void** data, size_t* size) { ssize_t n = TEMP_FAILURE_RETRY(read(fd_, buffer_.get(), buffer_capacity_)); if (n < 0) { error_ = SystemErrorCodeToString(errno); - fd_.reset(); + if (fd_ != -1) { + if (should_close_) { + close(fd_); + } + fd_ = -1; + } buffer_.reset(); return false; } -- cgit v1.2.3-59-g8ed1b