From 59ce7a45d19f4de1cf3ace2e6a6c6858effbf891 Mon Sep 17 00:00:00 2001 From: Daniel Zheng Date: Tue, 14 Nov 2023 10:15:28 -0800 Subject: [PATCH] libsnapshot: update offset functions Since these functions are used across both parser and writer, updating it as inline functions in cow_format. Test: cow_api_test Change-Id: I9824684e3b9b48947accce935335d4019d745ae0 --- fs_mgr/libsnapshot/include/libsnapshot/cow_format.h | 13 +++++++++++++ fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp | 12 +++--------- fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h | 1 - fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp | 8 ++++---- fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h | 13 ------------- 5 files changed, 20 insertions(+), 27 deletions(-) diff --git a/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h b/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h index 2079609c0..75467cb79 100644 --- a/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h +++ b/fs_mgr/libsnapshot/include/libsnapshot/cow_format.h @@ -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]; diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp index 778ba6270..52c6348b6 100644 --- a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp +++ b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.cpp @@ -94,18 +94,12 @@ std::optional 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>(); 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>(); diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h index fe3a2fb18..afc01afcc 100644 --- a/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h +++ b/fs_mgr/libsnapshot/libsnapshot_cow/parser_v3.h @@ -50,7 +50,6 @@ class CowParserV3 final : public CowParserBase { private: bool ParseOps(android::base::borrowed_fd fd, const uint32_t op_index); std::optional FindResumeOp(const uint64_t label); - off_t GetDataOffset() const; CowHeaderV3 header_ = {}; std::shared_ptr> ops_; bool ReadResumeBuffer(android::base::borrowed_fd fd); diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp index e8dad922a..6883c5e93 100644 --- a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp +++ b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp @@ -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; diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h index e928d3557..3dfc33cea 100644 --- a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h +++ b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h @@ -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_;