-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0314c07
commit c335892
Showing
10 changed files
with
259 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
#include "transform.h" | ||
|
||
#include "archive.h" | ||
|
||
namespace zpods | ||
{ | ||
class ArchiveTransform : public Transform | ||
{ | ||
public: | ||
ArchiveTransform(const char* src_dir, const char* dest_dir, | ||
const BackupConfig& config) | ||
: src_dir_(src_dir), dest_dir_(dest_dir), config_(config) | ||
{ | ||
} | ||
|
||
auto execute(std::string&& bytes) -> Transform::ResType override | ||
{ | ||
let_mut[status, buf] = | ||
make_archive(src_dir_.c_str(), dest_dir_.c_str(), config_); | ||
if (status == Status::NO_NEW_TO_ARCHIVE) | ||
{ | ||
spdlog::info("no new pea to archive"); | ||
status = Status::OK; | ||
} | ||
return {status, std::move(buf)}; | ||
} | ||
|
||
private: | ||
std::string src_dir_; | ||
std::string dest_dir_; | ||
const BackupConfig& config_; | ||
}; | ||
|
||
} // namespace zpods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
#include "compress.h" | ||
#include "transform.h" | ||
|
||
namespace zpods | ||
{ | ||
class CompressTransform : public Transform | ||
{ | ||
public: | ||
auto execute(std::string&& bytes) -> Transform::ResType override | ||
{ | ||
let[size, ptr] = zpods::compress({(byte*)bytes.data(), bytes.size()}); | ||
return {Status::OK, std::string{(const char*)ptr.get(), size}}; | ||
} | ||
|
||
private: | ||
}; // namespace zpods | ||
} // namespace zpods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "base/crypto.h" | ||
#include "transform.h" | ||
|
||
namespace zpods | ||
{ | ||
class EncryptTransform : public Transform | ||
{ | ||
public: | ||
EncryptTransform(std::string_view key, std::string_view iv) | ||
: key_(key), iv_(iv) | ||
{ | ||
} | ||
auto execute(std::string&& bytes) -> Transform::ResType override | ||
{ | ||
auto ciphertext = zpods::encrypt(bytes, key_, iv_); | ||
if (!ciphertext) | ||
{ | ||
spdlog::error("encrypt failed!"); | ||
return {zpods::Status::ERROR, {}}; | ||
} | ||
return {zpods::Status::OK, std::move(*ciphertext)}; | ||
} | ||
|
||
private: | ||
std::string key_; | ||
std::string iv_; | ||
}; | ||
} // namespace zpods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#include "pch.h" | ||
|
||
namespace zpods | ||
{ | ||
|
||
class Transform | ||
{ | ||
public: | ||
using ResType = std::pair<zpods::Status, std::string>; | ||
virtual auto execute(std::string&& bytes) -> ResType = 0; | ||
virtual ~Transform() {} | ||
}; | ||
} // namespace zpods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include "transform.h" | ||
|
||
namespace zpods | ||
{ | ||
|
||
class TransformPipeline : public Transform | ||
{ | ||
public: | ||
auto execute(std::string&& _bytes) -> Transform::ResType override | ||
{ | ||
Status status; | ||
std::string bytes = std::move(_bytes); | ||
for (auto& transform : transforms_) | ||
{ | ||
std::tie(status, bytes) = transform->execute(std::move(bytes)); | ||
if (status != Status::OK) | ||
{ | ||
return {status, {}}; | ||
} | ||
} | ||
return {Status::OK, std::move(bytes)}; | ||
} | ||
|
||
template <typename TransType, typename... Param> | ||
requires std::derived_from<TransType, Transform> | ||
void add_transform(Param&&... param) | ||
{ | ||
transforms_.push_back(new TransType(std::forward<Param>(param)...)); | ||
} | ||
|
||
~TransformPipeline() override | ||
{ | ||
for (auto& transform : transforms_) | ||
{ | ||
delete transform; | ||
transform = nullptr; | ||
} | ||
} | ||
|
||
private: | ||
std::vector<Transform*> transforms_; | ||
}; | ||
} // namespace zpods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
|
||
#include "transform/archive_transform.h" | ||
#include "transform/compress_transform.h" | ||
#include "transform/encrypt_transform.h" | ||
#include "transform_pipeline.h" | ||
|
||
namespace zpods | ||
{ | ||
class TransformPipelineBuilder | ||
{ | ||
public: | ||
TransformPipelineBuilder() | ||
: pipeline_(std::make_unique<TransformPipeline>()) | ||
{ | ||
} | ||
|
||
TransformPipelineBuilder& enable_archive(const char* src_dir, | ||
const char* dest_dir, | ||
const BackupConfig& config) | ||
{ | ||
pipeline_->add_transform<ArchiveTransform>(src_dir, dest_dir, config); | ||
return *this; | ||
} | ||
TransformPipelineBuilder& enable_compress() | ||
{ | ||
pipeline_->add_transform<CompressTransform>(); | ||
return *this; | ||
} | ||
TransformPipelineBuilder& enable_encrypt(std::string_view key, | ||
std::string_view iv) | ||
{ | ||
pipeline_->add_transform<EncryptTransform>(key, iv); | ||
return *this; | ||
} | ||
|
||
auto build() -> std::unique_ptr<TransformPipeline> | ||
{ | ||
return std::move(pipeline_); | ||
} | ||
|
||
private: | ||
std::unique_ptr<TransformPipeline> pipeline_; | ||
}; | ||
} // namespace zpods |