Merge "libsnapshot: Add a helper around CowReader::GetRawBytes."

This commit is contained in:
David Anderson 2023-06-23 18:33:44 +00:00 committed by Gerrit Code Review
commit c6d615dd17
3 changed files with 16 additions and 3 deletions

View file

@ -133,6 +133,7 @@ class CowReader final : public ICowReader {
CowHeader& GetHeader() override { return header_; }
bool GetRawBytes(const CowOperation* op, void* buffer, size_t len, size_t* read);
bool GetRawBytes(uint64_t offset, void* buffer, size_t len, size_t* read);
// Returns the total number of data ops that should be merged. This is the

View file

@ -230,7 +230,7 @@ bool CowReader::PrepMergeOps() {
size_t seq_len = current_op.data_length / sizeof(uint32_t);
merge_op_blocks->resize(merge_op_blocks->size() + seq_len);
if (!GetRawBytes(current_op.source, &merge_op_blocks->data()[num_seqs],
if (!GetRawBytes(&current_op, &merge_op_blocks->data()[num_seqs],
current_op.data_length, &read)) {
PLOG(ERROR) << "Failed to read sequence op!";
return false;
@ -516,6 +516,18 @@ std::unique_ptr<ICowOpIter> CowReader::GetMergeOpIter(bool ignore_progress) {
ignore_progress ? 0 : merge_op_start_);
}
bool CowReader::GetRawBytes(const CowOperation* op, void* buffer, size_t len, size_t* read) {
switch (op->type) {
case kCowSequenceOp:
case kCowReplaceOp:
case kCowXorOp:
return GetRawBytes(op->source, buffer, len, read);
default:
LOG(ERROR) << "Cannot get raw bytes of non-data op: " << *op;
return false;
}
}
bool CowReader::GetRawBytes(uint64_t offset, void* buffer, size_t len, size_t* read) {
// Validate the offset, taking care to acknowledge possible overflow of offset+len.
if (offset < header_.prefix.header_size || offset >= fd_size_ - sizeof(CowFooter) ||

View file

@ -57,7 +57,7 @@ static void ShowBad(CowReader& reader, const struct CowOperation* op) {
size_t count;
auto buffer = std::make_unique<uint8_t[]>(op->data_length);
if (!reader.GetRawBytes(op->source, buffer.get(), op->data_length, &count)) {
if (!reader.GetRawBytes(op, buffer.get(), op->data_length, &count)) {
std::cerr << "Failed to read at all!\n";
} else {
std::cout << "The Block data is:\n";
@ -199,7 +199,7 @@ static bool Inspect(const std::string& path) {
std::vector<uint32_t> merge_op_blocks;
size_t seq_len = op->data_length / sizeof(uint32_t);
merge_op_blocks.resize(seq_len);
if (!reader.GetRawBytes(op->source, merge_op_blocks.data(), op->data_length, &read)) {
if (!reader.GetRawBytes(op, merge_op_blocks.data(), op->data_length, &read)) {
PLOG(ERROR) << "Failed to read sequence op!";
return false;
}