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 --- applypatch/applypatch.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'applypatch') diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp index 41a8d582b..0250407a5 100644 --- a/applypatch/applypatch.cpp +++ b/applypatch/applypatch.cpp @@ -66,7 +66,7 @@ int LoadFileContents(const char* filename, FileContents* file) { } std::vector data(file->st.st_size); - std::unique_ptr f(ota_fopen(filename, "rb"), ota_fclose); + std::unique_ptr f(ota_fopen(filename, "rb"), ota_fclose); if (!f) { printf("failed to open \"%s\": %s\n", filename, strerror(errno)); return -1; @@ -118,7 +118,7 @@ static int LoadPartitionContents(const std::string& filename, FileContents* file std::sort(pairs.begin(), pairs.end()); const char* partition = pieces[1].c_str(); - std::unique_ptr dev(ota_fopen(partition, "rb"), ota_fclose); + std::unique_ptr dev(ota_fopen(partition, "rb"), ota_fclose); if (!dev) { printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno)); return -1; @@ -210,7 +210,7 @@ int SaveFileContents(const char* filename, const FileContents* file) { printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno)); return -1; } - if (ota_close(fd.release()) != 0) { + if (ota_close(fd) != 0) { printf("close of \"%s\" failed: %s\n", filename, strerror(errno)); return -1; } @@ -268,7 +268,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const std::string& t printf("failed to sync to %s: %s\n", partition, strerror(errno)); return -1; } - if (ota_close(fd.release()) != 0) { + if (ota_close(fd) != 0) { printf("failed to close %s: %s\n", partition, strerror(errno)); return -1; } @@ -287,7 +287,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const std::string& t } else { printf(" caches dropped\n"); } - ota_close(dc.release()); + ota_close(dc); sleep(1); // Verify. @@ -339,7 +339,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const std::string& t return -1; } - if (ota_close(fd.release()) == -1) { + if (ota_close(fd) == -1) { printf("error closing %s: %s\n", partition, strerror(errno)); return -1; } @@ -782,7 +782,7 @@ static int GenerateTarget(FileContents* source_file, printf("failed to fsync file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno)); result = 1; } - if (ota_close(output_fd.release()) != 0) { + if (ota_close(output_fd) != 0) { printf("failed to close file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno)); result = 1; } -- cgit v1.2.3