Merge "Add ota_close(unique_fd&) and ota_fclose(std::unique_ptr<FILE>&)." am: eed7f607cf am: 839fbbdad3

am: 6a8e0eb164

Change-Id: I481327e831b2748d6dfa1b347ede324fdd2b9629
This commit is contained in:
Tao Bao
2016-11-29 05:31:05 +00:00
committed by android-build-merger
3 changed files with 25 additions and 9 deletions
+7 -7
View File
@@ -66,7 +66,7 @@ int LoadFileContents(const char* filename, FileContents* file) {
} }
std::vector<unsigned char> data(file->st.st_size); std::vector<unsigned char> data(file->st.st_size);
std::unique_ptr<FILE, decltype(&ota_fclose)> f(ota_fopen(filename, "rb"), ota_fclose); std::unique_ptr<FILE, int (*)(FILE*)> f(ota_fopen(filename, "rb"), ota_fclose);
if (!f) { if (!f) {
printf("failed to open \"%s\": %s\n", filename, strerror(errno)); printf("failed to open \"%s\": %s\n", filename, strerror(errno));
return -1; return -1;
@@ -118,7 +118,7 @@ static int LoadPartitionContents(const std::string& filename, FileContents* file
std::sort(pairs.begin(), pairs.end()); std::sort(pairs.begin(), pairs.end());
const char* partition = pieces[1].c_str(); const char* partition = pieces[1].c_str();
std::unique_ptr<FILE, decltype(&ota_fclose)> dev(ota_fopen(partition, "rb"), ota_fclose); std::unique_ptr<FILE, int (*)(FILE*)> dev(ota_fopen(partition, "rb"), ota_fclose);
if (!dev) { if (!dev) {
printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno)); printf("failed to open emmc partition \"%s\": %s\n", partition, strerror(errno));
return -1; return -1;
@@ -210,7 +210,7 @@ int SaveFileContents(const char* filename, const FileContents* file) {
printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno)); printf("fsync of \"%s\" failed: %s\n", filename, strerror(errno));
return -1; return -1;
} }
if (ota_close(fd.release()) != 0) { if (ota_close(fd) != 0) {
printf("close of \"%s\" failed: %s\n", filename, strerror(errno)); printf("close of \"%s\" failed: %s\n", filename, strerror(errno));
return -1; 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)); printf("failed to sync to %s: %s\n", partition, strerror(errno));
return -1; return -1;
} }
if (ota_close(fd.release()) != 0) { if (ota_close(fd) != 0) {
printf("failed to close %s: %s\n", partition, strerror(errno)); printf("failed to close %s: %s\n", partition, strerror(errno));
return -1; return -1;
} }
@@ -287,7 +287,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const std::string& t
} else { } else {
printf(" caches dropped\n"); printf(" caches dropped\n");
} }
ota_close(dc.release()); ota_close(dc);
sleep(1); sleep(1);
// Verify. // Verify.
@@ -339,7 +339,7 @@ int WriteToPartition(const unsigned char* data, size_t len, const std::string& t
return -1; return -1;
} }
if (ota_close(fd.release()) == -1) { if (ota_close(fd) == -1) {
printf("error closing %s: %s\n", partition, strerror(errno)); printf("error closing %s: %s\n", partition, strerror(errno));
return -1; 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)); printf("failed to fsync file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
result = 1; 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)); printf("failed to close file \"%s\": %s\n", tmp_target_filename.c_str(), strerror(errno));
result = 1; result = 1;
} }
+12 -2
View File
@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
#include <map> #include "ota_io.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@@ -22,8 +22,10 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <map>
#include <memory>
#include "config.h" #include "config.h"
#include "ota_io.h"
static std::map<intptr_t, const char*> filename_cache; static std::map<intptr_t, const char*> filename_cache;
static std::string read_fault_file_name = ""; static std::string read_fault_file_name = "";
@@ -74,11 +76,19 @@ int ota_close(int fd) {
return close(fd); return close(fd);
} }
int ota_close(unique_fd& fd) {
return ota_close(fd.release());
}
int ota_fclose(FILE* fh) { int ota_fclose(FILE* fh) {
filename_cache.erase((intptr_t)fh); filename_cache.erase((intptr_t)fh);
return fclose(fh); return fclose(fh);
} }
int ota_fclose(std::unique_ptr<FILE, int (*)(FILE*)>& fh) {
return ota_fclose(fh.release());
}
size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) { size_t ota_fread(void* ptr, size_t size, size_t nitems, FILE* stream) {
if (should_fault_inject(OTAIO_READ)) { if (should_fault_inject(OTAIO_READ)) {
auto cached = filename_cache.find((intptr_t)stream); auto cached = filename_cache.find((intptr_t)stream);
+6
View File
@@ -26,6 +26,8 @@
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <memory>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#define OTAIO_CACHE_FNAME "/cache/saved.file" #define OTAIO_CACHE_FNAME "/cache/saved.file"
@@ -60,4 +62,8 @@ struct OtaCloser {
using unique_fd = android::base::unique_fd_impl<OtaCloser>; using unique_fd = android::base::unique_fd_impl<OtaCloser>;
int ota_close(unique_fd& fd);
int ota_fclose(std::unique_ptr<FILE, int (*)(FILE*)>& fh);
#endif #endif