From 04e4c2a6c2ad0dd8d8380877cb5adf1fa9628293 Mon Sep 17 00:00:00 2001 From: Daniel Zheng Date: Tue, 12 Sep 2023 15:35:31 -0700 Subject: [PATCH] Add v3 writer Adding v3 writer that works off of Cow Operation v3. Adding test file that will test this new writer. Adding in stub implementations to v3 writer. None of these functions have to work yet, we just need the implementations here to compile. Test: m libsnapshot Change-Id: If86437d5ceb2c33520d4ca26dea5193984f86546 --- fs_mgr/libsnapshot/Android.bp | 2 + .../libsnapshot/libsnapshot_cow/test_v3.cpp | 53 ++++++++ .../libsnapshot/libsnapshot_cow/writer_v3.cpp | 117 ++++++++++++++++++ .../libsnapshot/libsnapshot_cow/writer_v3.h | 43 +++++++ 4 files changed, 215 insertions(+) create mode 100644 fs_mgr/libsnapshot/libsnapshot_cow/test_v3.cpp create mode 100644 fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp create mode 100644 fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h diff --git a/fs_mgr/libsnapshot/Android.bp b/fs_mgr/libsnapshot/Android.bp index fe47801f3f72..6fad6626655e 100644 --- a/fs_mgr/libsnapshot/Android.bp +++ b/fs_mgr/libsnapshot/Android.bp @@ -201,6 +201,7 @@ cc_library_static { "libsnapshot_cow/snapshot_reader.cpp", "libsnapshot_cow/writer_base.cpp", "libsnapshot_cow/writer_v2.cpp", + "libsnapshot_cow/writer_v3.cpp", ], export_include_dirs: ["include"], host_supported: true, @@ -392,6 +393,7 @@ cc_test { srcs: [ "libsnapshot_cow/snapshot_reader_test.cpp", "libsnapshot_cow/test_v2.cpp", + "libsnapshot_cow/test_v3.cpp", ], cflags: [ "-D_FILE_OFFSET_BITS=64", diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/test_v3.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/test_v3.cpp new file mode 100644 index 000000000000..2373d4df5bf3 --- /dev/null +++ b/fs_mgr/libsnapshot/libsnapshot_cow/test_v3.cpp @@ -0,0 +1,53 @@ +// Copyright (C) 2023 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include "cow_decompress.h" +#include "libsnapshot/cow_format.h" +#include "writer_v3.h" +using android::base::unique_fd; +using testing::AssertionFailure; +using testing::AssertionResult; +using testing::AssertionSuccess; + +namespace android { +namespace snapshot { + +class CowOperationV3Test : public ::testing::Test { + protected: + virtual void SetUp() override { + cow_ = std::make_unique(); + ASSERT_GE(cow_->fd, 0) << strerror(errno); + } + + virtual void TearDown() override { cow_ = nullptr; } + + unique_fd GetCowFd() { return unique_fd{dup(cow_->fd)}; } + + std::unique_ptr cow_; +}; + +} // namespace snapshot +} // namespace android \ No newline at end of file diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp new file mode 100644 index 000000000000..2b9867ef1b0c --- /dev/null +++ b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.cpp @@ -0,0 +1,117 @@ +// +// Copyright (C) 2020 The Android Open Source_info Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#include "writer_v3.h" + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +// The info messages here are spammy, but as useful for update_engine. Disable +// them when running on the host. +#ifdef __ANDROID__ +#define LOG_INFO LOG(INFO) +#else +#define LOG_INFO LOG(VERBOSE) +#endif + +namespace android { +namespace snapshot { + +static_assert(sizeof(off_t) == sizeof(uint64_t)); + +using android::base::unique_fd; + +CowWriterV3::CowWriterV3(const CowOptions& options, unique_fd&& fd) + : CowWriterBase(options, std::move(fd)) {} + +CowWriterV3::~CowWriterV3() {} + +bool CowWriterV3::Initialize(std::optional label) { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + if (label) return false; + return false; +} + +bool CowWriterV3::EmitCopy(uint64_t new_block, uint64_t old_block, uint64_t num_blocks) { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + if (new_block || old_block || num_blocks) return false; + return false; +} + +bool CowWriterV3::EmitRawBlocks(uint64_t new_block_start, const void* data, size_t size) { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + + if (new_block_start || data || size) return false; + return false; +} + +bool CowWriterV3::EmitXorBlocks(uint32_t new_block_start, const void* data, size_t size, + uint32_t old_block, uint16_t offset) { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + if (new_block_start || old_block || offset || data || size) return false; + return false; +} + +bool CowWriterV3::EmitZeroBlocks(uint64_t new_block_start, uint64_t num_blocks) { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + if (new_block_start && num_blocks) return false; + return false; +} + +bool CowWriterV3::EmitLabel(uint64_t label) { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + if (label) return false; + return false; +} + +bool CowWriterV3::EmitSequenceData(size_t num_ops, const uint32_t* data) { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + if (num_ops && data) return false; + return false; +} + +bool CowWriterV3::Finalize() { + LOG(ERROR) << __LINE__ << " " << __FILE__ << " <- function here should never be called"; + return false; +} + +uint64_t CowWriterV3::GetCowSize() { + LOG(ERROR) << __LINE__ << " " << __FILE__ + << " <- Get Cow Size function here should never be called"; + return 0; +} + +} // namespace snapshot +} // namespace android diff --git a/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h new file mode 100644 index 000000000000..ddd72873c1b8 --- /dev/null +++ b/fs_mgr/libsnapshot/libsnapshot_cow/writer_v3.h @@ -0,0 +1,43 @@ +// Copyright (C) 2023 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include +#include "writer_base.h" + +namespace android { +namespace snapshot { + +class CowWriterV3 : public CowWriterBase { + public: + explicit CowWriterV3(const CowOptions& options, android::base::unique_fd&& fd); + ~CowWriterV3() override; + + bool Initialize(std::optional label = {}) override; + bool Finalize() override; + uint64_t GetCowSize() override; + + protected: + virtual bool EmitCopy(uint64_t new_block, uint64_t old_block, uint64_t num_blocks = 1) override; + virtual bool EmitRawBlocks(uint64_t new_block_start, const void* data, size_t size) override; + virtual bool EmitXorBlocks(uint32_t new_block_start, const void* data, size_t size, + uint32_t old_block, uint16_t offset) override; + virtual bool EmitZeroBlocks(uint64_t new_block_start, uint64_t num_blocks) override; + virtual bool EmitLabel(uint64_t label) override; + virtual bool EmitSequenceData(size_t num_ops, const uint32_t* data) override; +}; + +} // namespace snapshot +} // namespace android