Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 76622d44 authored by David Anderson's avatar David Anderson
Browse files

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
parent 5eae2cb4
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
@@ -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::OpenForWrite() {
}

bool CowWriterV2::OpenForAppend(uint64_t label) {
    auto reader = std::make_unique<CowReader>();
    std::queue<CowOperation> toAdd;
    if (!ReadCowHeader(fd_, &header_)) {
        return false;
    }

    if (!reader->Parse(fd_, {label})) {
    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;
    }

    header_ = reader->GetHeader();
    options_.block_size = header_.block_size;
    options_.cluster_ops = header_.cluster_ops;

@@ -267,16 +275,10 @@ bool CowWriterV2::OpenForAppend(uint64_t label) {
    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;