From 22f11205a1f0d534c10e88e40f09b92c9faa161c Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Mon, 27 Aug 2018 10:50:31 -0700 Subject: Remove otafault Now it's less beneficial to inject I/O faults since we don't see many of them. Remove the library that mocks I/O failures. And switch to android::base I/O when possible. Bug: 113032079 Test: unit tests pass Change-Id: I9f2a92b7ba80f4da6ff9e2abc27f2680138f942c --- updater/blockimg.cpp | 116 ++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 70 deletions(-) (limited to 'updater/blockimg.cpp') diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp index 96b2d9feb..823a1cbbd 100644 --- a/updater/blockimg.cpp +++ b/updater/blockimg.cpp @@ -53,7 +53,6 @@ #include #include "edify/expr.h" -#include "otafault/ota_io.h" #include "otautil/error_code.h" #include "otautil/paths.h" #include "otautil/print_sha1.h" @@ -119,15 +118,14 @@ static bool ParseLastCommandFile(size_t* last_command_index) { } static bool FsyncDir(const std::string& dirname) { - android::base::unique_fd dfd( - TEMP_FAILURE_RETRY(ota_open(dirname.c_str(), O_RDONLY | O_DIRECTORY))); + android::base::unique_fd dfd(TEMP_FAILURE_RETRY(open(dirname.c_str(), O_RDONLY | O_DIRECTORY))); if (dfd == -1) { - failure_type = kFileOpenFailure; + failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; PLOG(ERROR) << "Failed to open " << dirname; return false; } if (fsync(dfd) == -1) { - failure_type = kFsyncFailure; + failure_type = errno == EIO ? kEioFailure : kFsyncFailure; PLOG(ERROR) << "Failed to fsync " << dirname; return false; } @@ -180,47 +178,6 @@ static bool SetPartitionUpdatedMarker(const std::string& marker) { return true; } -static int read_all(int fd, uint8_t* data, size_t size) { - size_t so_far = 0; - while (so_far < size) { - ssize_t r = TEMP_FAILURE_RETRY(ota_read(fd, data+so_far, size-so_far)); - if (r == -1) { - failure_type = kFreadFailure; - PLOG(ERROR) << "read failed"; - return -1; - } else if (r == 0) { - failure_type = kFreadFailure; - LOG(ERROR) << "read reached unexpected EOF."; - return -1; - } - so_far += r; - } - return 0; -} - -static int read_all(int fd, std::vector* buffer, size_t size) { - return read_all(fd, buffer->data(), size); -} - -static int write_all(int fd, const uint8_t* data, size_t size) { - size_t written = 0; - while (written < size) { - ssize_t w = TEMP_FAILURE_RETRY(ota_write(fd, data+written, size-written)); - if (w == -1) { - failure_type = kFwriteFailure; - PLOG(ERROR) << "write failed"; - return -1; - } - written += w; - } - - return 0; -} - -static int write_all(int fd, const std::vector& buffer, size_t size) { - return write_all(fd, buffer.data(), size); -} - static bool discard_blocks(int fd, off64_t offset, uint64_t size) { // Don't discard blocks unless the update is a retry run. if (!is_retry) { @@ -293,7 +250,9 @@ class RangeSinkWriter { write_now = current_range_left_; } - if (write_all(fd_, data, write_now) == -1) { + if (!android::base::WriteFully(fd_, data, write_now)) { + failure_type = errno == EIO ? kEioFailure : kFwriteFailure; + PLOG(ERROR) << "Failed to write " << write_now << " bytes of data"; break; } @@ -510,7 +469,9 @@ static int ReadBlocks(const RangeSet& src, std::vector* buffer, int fd) } size_t size = (range.second - range.first) * BLOCKSIZE; - if (read_all(fd, buffer->data() + p, size) == -1) { + if (!android::base::ReadFully(fd, buffer->data() + p, size)) { + failure_type = errno == EIO ? kEioFailure : kFreadFailure; + PLOG(ERROR) << "Failed to read " << size << " bytes of data"; return -1; } @@ -533,7 +494,9 @@ static int WriteBlocks(const RangeSet& tgt, const std::vector& buffer, return -1; } - if (write_all(fd, buffer.data() + written, size) == -1) { + if (!android::base::WriteFully(fd, buffer.data() + written, size)) { + failure_type = errno == EIO ? kEioFailure : kFwriteFailure; + PLOG(ERROR) << "Failed to write " << size << " bytes of data"; return -1; } @@ -793,15 +756,18 @@ static int LoadStash(const CommandParameters& params, const std::string& id, boo return -1; } - android::base::unique_fd fd(TEMP_FAILURE_RETRY(ota_open(fn.c_str(), O_RDONLY))); + android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(fn.c_str(), O_RDONLY))); if (fd == -1) { + failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; PLOG(ERROR) << "open \"" << fn << "\" failed"; return -1; } allocate(sb.st_size, buffer); - if (read_all(fd, buffer, sb.st_size) == -1) { + if (!android::base::ReadFully(fd, buffer->data(), sb.st_size)) { + failure_type = errno == EIO ? kEioFailure : kFreadFailure; + PLOG(ERROR) << "Failed to read " << sb.st_size << " bytes of data"; return -1; } @@ -855,8 +821,9 @@ static int WriteStash(const std::string& base, const std::string& id, int blocks LOG(INFO) << " writing " << blocks << " blocks to " << cn; android::base::unique_fd fd( - TEMP_FAILURE_RETRY(ota_open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE))); + TEMP_FAILURE_RETRY(open(fn.c_str(), O_WRONLY | O_CREAT | O_TRUNC, STASH_FILE_MODE))); if (fd == -1) { + failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; PLOG(ERROR) << "failed to create \"" << fn << "\""; return -1; } @@ -866,12 +833,14 @@ static int WriteStash(const std::string& base, const std::string& id, int blocks return -1; } - if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) { + if (!android::base::WriteFully(fd, buffer.data(), blocks * BLOCKSIZE)) { + failure_type = errno == EIO ? kEioFailure : kFwriteFailure; + PLOG(ERROR) << "Failed to write " << blocks * BLOCKSIZE << " bytes of data"; return -1; } - if (ota_fsync(fd) == -1) { - failure_type = kFsyncFailure; + if (fsync(fd) == -1) { + failure_type = errno == EIO ? kEioFailure : kFsyncFailure; PLOG(ERROR) << "fsync \"" << fn << "\" failed"; return -1; } @@ -883,7 +852,6 @@ static int WriteStash(const std::string& base, const std::string& id, int blocks std::string dname = GetStashFileName(base, "", ""); if (!FsyncDir(dname)) { - failure_type = kFsyncFailure; return -1; } @@ -1313,7 +1281,9 @@ static int PerformCommandZero(CommandParameters& params) { } for (size_t j = range.first; j < range.second; ++j) { - if (write_all(params.fd, params.buffer, BLOCKSIZE) == -1) { + if (!android::base::WriteFully(params.fd, params.buffer.data(), BLOCKSIZE)) { + failure_type = errno == EIO ? kEioFailure : kFwriteFailure; + PLOG(ERROR) << "Failed to write " << BLOCKSIZE << " bytes of data"; return -1; } } @@ -1560,7 +1530,8 @@ static int PerformCommandComputeHashTree(CommandParameters& params) { } for (size_t i = range.first; i < range.second; i++) { - if (read_all(params.fd, buffer, BLOCKSIZE) == -1) { + if (!android::base::ReadFully(params.fd, buffer, BLOCKSIZE)) { + failure_type = errno == EIO ? kEioFailure : kFreadFailure; LOG(ERROR) << "Failed to read data in " << range.first << ":" << range.second; return -1; } @@ -1676,8 +1647,9 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, return StringValue(""); } - params.fd.reset(TEMP_FAILURE_RETRY(ota_open(blockdev_filename->data.c_str(), O_RDWR))); + params.fd.reset(TEMP_FAILURE_RETRY(open(blockdev_filename->data.c_str(), O_RDWR))); if (params.fd == -1) { + failure_type = errno == EIO ? kEioFailure : kFileOpenFailure; PLOG(ERROR) << "open \"" << blockdev_filename->data << "\" failed"; return StringValue(""); } @@ -1859,8 +1831,8 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, } if (params.canwrite) { - if (ota_fsync(params.fd) == -1) { - failure_type = kFsyncFailure; + if (fsync(params.fd) == -1) { + failure_type = errno == EIO ? kEioFailure : kFsyncFailure; PLOG(ERROR) << "fsync failed"; goto pbiudone; } @@ -1920,8 +1892,8 @@ pbiudone: LOG(INFO) << "verified partition contents; update may be resumed"; } - if (ota_fsync(params.fd) == -1) { - failure_type = kFsyncFailure; + if (fsync(params.fd) == -1) { + failure_type = errno == EIO ? kEioFailure : kFsyncFailure; PLOG(ERROR) << "fsync failed"; } // params.fd will be automatically closed because it's a unique_fd. @@ -2059,9 +2031,10 @@ Value* RangeSha1Fn(const char* name, State* state, const std::vectordata.c_str(), O_RDWR)); + android::base::unique_fd fd(open(blockdev_filename->data.c_str(), O_RDWR)); if (fd == -1) { - ErrorAbort(state, kFileOpenFailure, "open \"%s\" failed: %s", blockdev_filename->data.c_str(), + CauseCode cause_code = errno == EIO ? kEioFailure : kFileOpenFailure; + ErrorAbort(state, cause_code, "open \"%s\" failed: %s", blockdev_filename->data.c_str(), strerror(errno)); return StringValue(""); } @@ -2081,8 +2054,9 @@ Value* RangeSha1Fn(const char* name, State* state, const std::vectordata.c_str(), + if (!android::base::ReadFully(fd, buffer.data(), BLOCKSIZE)) { + CauseCode cause_code = errno == EIO ? kEioFailure : kFreadFailure; + ErrorAbort(state, cause_code, "failed to read %s: %s", blockdev_filename->data.c_str(), strerror(errno)); return StringValue(""); } @@ -2121,9 +2095,10 @@ Value* CheckFirstBlockFn(const char* name, State* state, return StringValue(""); } - android::base::unique_fd fd(ota_open(arg_filename->data.c_str(), O_RDONLY)); + android::base::unique_fd fd(open(arg_filename->data.c_str(), O_RDONLY)); if (fd == -1) { - ErrorAbort(state, kFileOpenFailure, "open \"%s\" failed: %s", arg_filename->data.c_str(), + CauseCode cause_code = errno == EIO ? kEioFailure : kFileOpenFailure; + ErrorAbort(state, cause_code, "open \"%s\" failed: %s", arg_filename->data.c_str(), strerror(errno)); return StringValue(""); } @@ -2132,7 +2107,8 @@ Value* CheckFirstBlockFn(const char* name, State* state, std::vector block0_buffer(BLOCKSIZE); if (ReadBlocks(blk0, &block0_buffer, fd) == -1) { - ErrorAbort(state, kFreadFailure, "failed to read %s: %s", arg_filename->data.c_str(), + CauseCode cause_code = errno == EIO ? kEioFailure : kFreadFailure; + ErrorAbort(state, cause_code, "failed to read %s: %s", arg_filename->data.c_str(), strerror(errno)); return StringValue(""); } -- cgit v1.2.3