Merge "Skip stashing source blocks in verify mode"

This commit is contained in:
Tianjie Xu 2016-03-25 22:09:34 +00:00 committed by Gerrit Code Review
commit 3123e5ad94

View file

@ -33,6 +33,7 @@
#include <unistd.h> #include <unistd.h>
#include <fec/io.h> #include <fec/io.h>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -67,6 +68,8 @@ struct RangeSet {
std::vector<size_t> pos; // Actual limit is INT_MAX. std::vector<size_t> pos; // Actual limit is INT_MAX.
}; };
static std::map<std::string, RangeSet> stash_map;
static void parse_range(const std::string& range_text, RangeSet& rs) { static void parse_range(const std::string& range_text, RangeSet& rs) {
std::vector<std::string> pieces = android::base::Split(range_text, ","); std::vector<std::string> pieces = android::base::Split(range_text, ",");
@ -522,8 +525,28 @@ static void DeleteStash(const std::string& base) {
} }
} }
static int LoadStash(const std::string& base, const std::string& id, bool verify, size_t* blocks, static int LoadStash(CommandParameters& params, const std::string& base, const std::string& id,
std::vector<uint8_t>& buffer, bool printnoent) { bool verify, size_t* blocks, std::vector<uint8_t>& buffer, bool printnoent) {
// In verify mode, if source range_set was saved for the given hash,
// check contents in the source blocks first. If the check fails,
// search for the stashed files on /cache as usual.
if (!params.canwrite) {
if (stash_map.find(id) != stash_map.end()) {
const RangeSet& src = stash_map[id];
allocate(src.size * BLOCKSIZE, buffer);
if (ReadBlocks(src, buffer, params.fd) == -1) {
fprintf(stderr, "failed to read source blocks in stash map.\n");
return -1;
}
if (VerifyBlocks(id, buffer, src.size, true) != 0) {
fprintf(stderr, "failed to verify loaded source blocks in stash map.\n");
return -1;
}
return 0;
}
}
if (base.empty()) { if (base.empty()) {
return -1; return -1;
} }
@ -722,7 +745,7 @@ static int SaveStash(CommandParameters& params, const std::string& base,
const std::string& id = params.tokens[params.cpos++]; const std::string& id = params.tokens[params.cpos++];
size_t blocks = 0; size_t blocks = 0;
if (usehash && LoadStash(base, id, true, &blocks, buffer, false) == 0) { if (usehash && LoadStash(params, base, id, true, &blocks, buffer, false) == 0) {
// Stash file already exists and has expected contents. Do not // Stash file already exists and has expected contents. Do not
// read from source again, as the source may have been already // read from source again, as the source may have been already
// overwritten during a previous attempt. // overwritten during a previous attempt.
@ -747,6 +770,12 @@ static int SaveStash(CommandParameters& params, const std::string& base,
return 0; return 0;
} }
// In verify mode, save source range_set instead of stashing blocks.
if (!params.canwrite && usehash) {
stash_map[id] = src;
return 0;
}
fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str()); fprintf(stderr, "stashing %zu blocks to %s\n", blocks, id.c_str());
return WriteStash(base, id, blocks, buffer, false, nullptr); return WriteStash(base, id, blocks, buffer, false, nullptr);
} }
@ -857,7 +886,7 @@ static int LoadSrcTgtVersion2(CommandParameters& params, RangeSet& tgt, size_t&
} }
std::vector<uint8_t> stash; std::vector<uint8_t> stash;
int res = LoadStash(stashbase, tokens[0], false, nullptr, stash, true); int res = LoadStash(params, stashbase, tokens[0], false, nullptr, stash, true);
if (res == -1) { if (res == -1) {
// These source blocks will fail verification if used later, but we // These source blocks will fail verification if used later, but we
@ -931,8 +960,9 @@ static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t&
if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) { if (VerifyBlocks(srchash, params.buffer, src_blocks, true) == 0) {
// If source and target blocks overlap, stash the source blocks so we can // If source and target blocks overlap, stash the source blocks so we can
// resume from possible write errors // resume from possible write errors. In verify mode, we can skip stashing
if (overlap) { // because the source blocks won't be overwritten.
if (overlap && params.canwrite) {
fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks, fprintf(stderr, "stashing %zu overlapping blocks to %s\n", src_blocks,
srchash.c_str()); srchash.c_str());
@ -953,7 +983,8 @@ static int LoadSrcTgtVersion3(CommandParameters& params, RangeSet& tgt, size_t&
return 0; return 0;
} }
if (overlap && LoadStash(params.stashbase, srchash, true, nullptr, params.buffer, true) == 0) { if (overlap && LoadStash(params, params.stashbase, srchash, true, nullptr, params.buffer,
true) == 0) {
// Overlapping source blocks were previously stashed, command can proceed. // Overlapping source blocks were previously stashed, command can proceed.
// We are recovering from an interrupted command, so we don't know if the // We are recovering from an interrupted command, so we don't know if the
// stash can safely be deleted after this command. // stash can safely be deleted after this command.
@ -1028,8 +1059,15 @@ static int PerformCommandFree(CommandParameters& params) {
return -1; return -1;
} }
const std::string& id = params.tokens[params.cpos++];
if (!params.canwrite && stash_map.find(id) != stash_map.end()) {
stash_map.erase(id);
return 0;
}
if (params.createdstash || params.canwrite) { if (params.createdstash || params.canwrite) {
return FreeStash(params.stashbase, params.tokens[params.cpos++]); return FreeStash(params.stashbase, id);
} }
return 0; return 0;