Merge "libsnapshot: update offset functions" into main

This commit is contained in:
Daniel Zheng 2023-11-14 23:00:23 +00:00 committed by Gerrit Code Review
commit b119efef47
5 changed files with 20 additions and 27 deletions

View file

@ -232,6 +232,19 @@ static inline uint64_t GetCowOpSourceInfoData(const CowOperation& op) {
return op.source_info & kCowOpSourceInfoDataMask;
}
static constexpr off_t GetOpOffset(uint32_t op_index, const CowHeaderV3 header) {
return header.prefix.header_size + header.buffer_size +
(header.resume_point_max * sizeof(ResumePoint)) + (op_index * sizeof(CowOperationV3));
}
static constexpr off_t GetDataOffset(const CowHeaderV3 header) {
return header.prefix.header_size + header.buffer_size +
(header.resume_point_max * sizeof(ResumePoint)) +
header.op_count_max * sizeof(CowOperation);
}
static constexpr off_t GetResumeOffset(const CowHeaderV3 header) {
return header.prefix.header_size + header.buffer_size;
}
struct CowFooter {
CowFooterOperation op;
uint8_t unused[64];

View file

@ -94,18 +94,12 @@ std::optional<uint32_t> CowParserV3::FindResumeOp(const uint64_t label) {
return std::nullopt;
}
off_t CowParserV3::GetDataOffset() const {
return sizeof(CowHeaderV3) + header_.buffer_size +
header_.resume_point_max * sizeof(ResumePoint) +
header_.op_count_max * sizeof(CowOperation);
}
bool CowParserV3::ParseOps(borrowed_fd fd, const uint32_t op_index) {
ops_ = std::make_shared<std::vector<CowOperationV3>>();
ops_->resize(op_index);
const off_t offset = header_.prefix.header_size + header_.buffer_size +
header_.resume_point_max * sizeof(ResumePoint);
// read beginning of operation buffer -> so op_index = 0
const off_t offset = GetOpOffset(0, header_);
if (!android::base::ReadFullyAtOffset(fd, ops_->data(), ops_->size() * sizeof(CowOperationV3),
offset)) {
PLOG(ERROR) << "read ops failed";
@ -113,7 +107,7 @@ bool CowParserV3::ParseOps(borrowed_fd fd, const uint32_t op_index) {
}
// fill out mapping of XOR op data location
uint64_t data_pos = GetDataOffset();
uint64_t data_pos = GetDataOffset(header_);
xor_data_loc_ = std::make_shared<std::unordered_map<uint64_t, uint64_t>>();

View file

@ -50,7 +50,6 @@ class CowParserV3 final : public CowParserBase {
private:
bool ParseOps(android::base::borrowed_fd fd, const uint32_t op_index);
std::optional<uint32_t> FindResumeOp(const uint64_t label);
off_t GetDataOffset() const;
CowHeaderV3 header_ = {};
std::shared_ptr<std::vector<CowOperationV3>> ops_;
bool ReadResumeBuffer(android::base::borrowed_fd fd);

View file

@ -170,7 +170,7 @@ bool CowWriterV3::OpenForWrite() {
LOG(ERROR) << "Header sync failed";
return false;
}
next_data_pos_ = GetDataOffset();
next_data_pos_ = GetDataOffset(header_);
return true;
}
@ -192,7 +192,7 @@ bool CowWriterV3::OpenForAppend(uint64_t label) {
resume_points_ = parser.resume_points();
options_.block_size = header_.block_size;
next_data_pos_ = GetDataOffset();
next_data_pos_ = GetDataOffset(header_);
TranslatedCowOps ops;
parser.Translate(&ops);
@ -307,7 +307,7 @@ bool CowWriterV3::EmitLabel(uint64_t label) {
if (!android::base::WriteFullyAtOffset(fd_, resume_points_->data(),
resume_points_->size() * sizeof(ResumePoint),
GetResumeOffset())) {
GetResumeOffset(header_))) {
PLOG(ERROR) << "writing resume buffer failed";
return false;
}
@ -333,7 +333,7 @@ bool CowWriterV3::WriteOperation(const CowOperationV3& op, const void* data, siz
return false;
}
const off_t offset = GetOpOffset(header_.op_count);
const off_t offset = GetOpOffset(header_.op_count, header_);
if (!android::base::WriteFullyAtOffset(fd_, &op, sizeof(op), offset)) {
PLOG(ERROR) << "write failed for " << op << " at " << offset;
return false;

View file

@ -49,19 +49,6 @@ class CowWriterV3 : public CowWriterBase {
uint16_t offset, uint8_t type);
bool CompressBlocks(size_t num_blocks, const void* data);
off_t GetOpOffset(uint32_t op_index) const {
CHECK_LT(op_index, header_.op_count_max);
return header_.prefix.header_size + header_.buffer_size +
(header_.resume_point_max * sizeof(ResumePoint)) +
(op_index * sizeof(CowOperationV3));
}
off_t GetDataOffset() const {
return sizeof(CowHeaderV3) + header_.buffer_size +
(header_.resume_point_max * sizeof(ResumePoint)) +
header_.op_count_max * sizeof(CowOperation);
}
off_t GetResumeOffset() const { return sizeof(CowHeaderV3) + header_.buffer_size; }
private:
CowHeaderV3 header_{};
CowCompression compression_;