libsnapshot: Remove dependence between CowWriterV2 and CowReader.
The plan of record for the new CowOperation layout is to automatically
translate from CowOperationV2. However, CowWriterV2 currently imports
directly from CowReader, which means we'd need two translation steps.
Luckily we now have CowParserV2, which means we can directly import into
CowWriterV2.
Bug: 280529365
Test: vts_libsnapshot_test
Test: cow_api_test
Change-Id: I11863dcb483f0448686d6c492d8eb128840ce18c
diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v2.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v2.cpp
index b6603da..c549969 100644
--- a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v2.cpp
+++ b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v2.cpp
@@ -14,6 +14,8 @@
// limitations under the License.
//
+#include "writer_v2.h"
+
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
@@ -37,7 +39,7 @@
#include <sys/ioctl.h>
#include <unistd.h>
-#include "writer_v2.h"
+#include "parser_v2.h"
// The info messages here are spammy, but as useful for update_engine. Disable
// them when running on the host.
@@ -252,14 +254,20 @@
}
bool CowWriterV2::OpenForAppend(uint64_t label) {
- auto reader = std::make_unique<CowReader>();
- std::queue<CowOperation> toAdd;
-
- if (!reader->Parse(fd_, {label})) {
+ if (!ReadCowHeader(fd_, &header_)) {
return false;
}
- header_ = reader->GetHeader();
+ CowParserV2 parser;
+ if (!parser.Parse(fd_, header_, {label})) {
+ return false;
+ }
+ if (header_.prefix.major_version > 2) {
+ LOG(ERROR) << "CowWriterV2 tried to open incompatible version "
+ << header_.prefix.major_version;
+ return false;
+ }
+
options_.block_size = header_.block_size;
options_.cluster_ops = header_.cluster_ops;
@@ -267,16 +275,10 @@
footer_.op.num_ops = 0;
InitPos();
- auto iter = reader->GetOpIter();
-
- while (!iter->AtEnd()) {
- AddOperation(*iter->Get());
- iter->Next();
+ for (const auto& op : *parser.ops()) {
+ AddOperation(op);
}
- // Free reader so we own the descriptor position again.
- reader = nullptr;
-
if (lseek(fd_.get(), next_op_pos_, SEEK_SET) < 0) {
PLOG(ERROR) << "lseek failed";
return false;