From 3dc14cb429061e49ef76948cf55a0e3edf8b170a Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 22 Nov 2016 11:37:13 -0800 Subject: Add ota_close(unique_fd&) and ota_fclose(std::unique_ptr&). We were using the below sequence prior to the CL in [1]. unique_fd fd(ota_open(...)); ota_close(fd); fd.reset(ota_open(...)); fd.reset() may unintentionally close the newly opened FD if it has the same value as the early ota_open. The CL in [1] changed to "ota_close(fd.release())" to avoid the issue. This CL adds a new overloaded function ota_close(unique_fd&) to handle the release automatically. Similarly add ota_fclose(std::unique_ptr&). [1] commit 48cf770471ef53fbf0a1837196220862a0bdb18d. Bug: 33034669 Test: recovery_component_test passes. Change-Id: Ief91edc590e95a7426e33364b28754173efb1056 --- otafault/ota_io.cpp | 14 ++++++++++++-- otafault/ota_io.h | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'otafault') diff --git a/otafault/ota_io.cpp b/otafault/ota_io.cpp index 2efd3004a..874cb1d5c 100644 --- a/otafault/ota_io.cpp +++ b/otafault/ota_io.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include +#include "ota_io.h" #include #include @@ -22,8 +22,10 @@ #include #include +#include +#include + #include "config.h" -#include "ota_io.h" static std::map filename_cache; static std::string read_fault_file_name = ""; @@ -74,11 +76,19 @@ int ota_close(int fd) { return close(fd); } +int ota_close(unique_fd& fd) { + return ota_close(fd.release()); +} + int ota_fclose(FILE* fh) { filename_cache.erase((intptr_t)fh); return fclose(fh); } +int ota_fclose(std::unique_ptr& fh) { + return ota_fclose(fh.release()); +} + size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) { if (should_fault_inject(OTAIO_READ)) { auto cached = filename_cache.find((intptr_t)stream); diff --git a/otafault/ota_io.h b/otafault/ota_io.h index e119eef08..45d17b1bb 100644 --- a/otafault/ota_io.h +++ b/otafault/ota_io.h @@ -26,6 +26,8 @@ #include #include +#include + #include #define OTAIO_CACHE_FNAME "/cache/saved.file" @@ -60,4 +62,8 @@ struct OtaCloser { using unique_fd = android::base::unique_fd_impl; +int ota_close(unique_fd& fd); + +int ota_fclose(std::unique_ptr& fh); + #endif -- cgit v1.2.3