From d7c55110956b329b5d314aa851c923e7f1fe3e1e Mon Sep 17 00:00:00 2001 From: Daniel Zheng Date: Tue, 26 Sep 2023 12:27:33 -0700 Subject: [PATCH] Add binary to write basic COW Adding small binary to write a simple COW for version 2. We will then use updated reader to make sure we can read this version 2 cow. Think it would be a good idea to keep the binary here to see how exactly we generated the cow (useful for debugging and if we ever need to recreate this cow) Test: m basic_v2_cow_writer Change-Id: I28435025b7a8280fc5c4574876cc9110b391cb0e --- fs_mgr/libsnapshot/tools/Android.bp | 23 +++++++++ .../libsnapshot/tools/basic_v2_cow_writer.cpp | 48 ++++++++++++++++++ fs_mgr/libsnapshot/tools/testdata/cow_v2 | Bin 0 -> 2105826 bytes 3 files changed, 71 insertions(+) create mode 100644 fs_mgr/libsnapshot/tools/basic_v2_cow_writer.cpp create mode 100644 fs_mgr/libsnapshot/tools/testdata/cow_v2 diff --git a/fs_mgr/libsnapshot/tools/Android.bp b/fs_mgr/libsnapshot/tools/Android.bp index cfa0cefe7..0f0828699 100644 --- a/fs_mgr/libsnapshot/tools/Android.bp +++ b/fs_mgr/libsnapshot/tools/Android.bp @@ -20,3 +20,26 @@ cc_binary { cflags: ["-Werror"], } + + +cc_binary { + name: "basic_v2_cow_writer", + host_supported: true, + defaults: [ + "fs_mgr_defaults", + "libsnapshot_cow_defaults", + ], + + srcs: ["basic_v2_cow_writer.cpp"], + + static_libs: [ + "libsnapshot_cow", + ], + + shared_libs: [ + "libbase", + "liblog", + ], + + cflags: ["-Werror"], +} diff --git a/fs_mgr/libsnapshot/tools/basic_v2_cow_writer.cpp b/fs_mgr/libsnapshot/tools/basic_v2_cow_writer.cpp new file mode 100644 index 000000000..59d35399a --- /dev/null +++ b/fs_mgr/libsnapshot/tools/basic_v2_cow_writer.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include +#include +#include + +#include "android-base/unique_fd.h" + +using namespace android::snapshot; + +// This writes a simple cow v2 file in the current directory. This file will serve as testdata for +// ensuring our v3 cow reader will be able to read a cow file created by the v2 writer. +// +// WARNING: We should not be overriding this test file, as it will serve as historic marker for what +// a device with old writer_v2 will write as a cow. +void write_cow_v2() { + CowOptions options; + options.cluster_ops = 5; + options.num_merge_ops = 1; + std::string data = "This is some data, believe it"; + data.resize(options.block_size, '\0'); + + char cwd_buffer[1024]; + size_t cwd_buffer_size = sizeof(cwd_buffer); + + // Get the current working directory path. + char* err = getcwd(cwd_buffer, cwd_buffer_size); + if (!err) { + LOG(ERROR) << "Couldn't get current directory"; + } + android::base::unique_fd fd(open(strcat(cwd_buffer, "/cow_v2"), O_CREAT | O_RDWR, 0666)); + if (fd.get() == -1) { + LOG(FATAL) << "couldn't open tmp_cow"; + } + std::unique_ptr writer = CreateCowWriter(2, options, std::move(fd)); + writer->AddCopy(0, 5); + writer->AddRawBlocks(2, data.data(), data.size()); + writer->AddLabel(1); + writer->AddXorBlocks(50, data.data(), data.size(), 24, 10); + writer->AddZeroBlocks(5, 10); + writer->AddLabel(2); + writer->Finalize(); +} + +int main() { + write_cow_v2(); +} diff --git a/fs_mgr/libsnapshot/tools/testdata/cow_v2 b/fs_mgr/libsnapshot/tools/testdata/cow_v2 new file mode 100644 index 0000000000000000000000000000000000000000..9f37dfc50c5550a2ed796d046ab52b3f6960d05b GIT binary patch literal 2105826 zcmeI&yGjE=6b9gvXuQM=L41HsV`b$N2tI&Kz&h$e0!oSmZLNK2oxMRaL=YPZe8X;L z=if6s%ePfv#^ZnuoP$(% z)8@mECheY4$3=5+O|Of`@?~DWmBoD3&estjK!5-N0t5&UAV7cs z0Rp=%;Q#+_e?MSiN_6`uzT!4!?Z%0Vtp2aVZ!rKH(EtDd literal 0 HcmV?d00001