proper cmake

This commit is contained in:
2023-06-04 16:10:06 +02:00
parent 13a8b4c35d
commit fdcb0cf0c4
154 changed files with 1273 additions and 1620 deletions

View File

@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.22)
add_library(commands srcs/Command.cpp srcs/CommandDiff.cpp srcs/CommandList.cpp srcs/CommandListFiles.cpp srcs/CommandRestore.cpp srcs/CommandRun.cpp srcs/CommandsCommon.cpp srcs/Diff.cpp)
target_include_directories(commands PUBLIC includes)
target_link_libraries(commands crypto repo chunkers utils change_detectors)

View File

@@ -5,7 +5,7 @@
#ifndef SEMBACKUP_COMMAND_H
#define SEMBACKUP_COMMAND_H
#include "../Context.h"
#include "Context.h"
/// Abstract base class for some process running with some Context
class Command {

View File

@@ -7,7 +7,7 @@
#include "Command.h"
#include "../repo/objects/File.h"
#include "../../repo/includes/objects/File.h"
#include "CommandsCommon.h"

View File

@@ -0,0 +1,18 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_CONTEXT_H
#define SEMBACKUP_CONTEXT_H
#include "../../repo/includes/Repository.h"
#include "../../utils/includes/Config.h"
#include "../../utils/includes/Logger.h"
struct Context {
Logger *logger;
Repository *repo;
};
#endif//SEMBACKUP_CONTEXT_H

View File

@@ -0,0 +1,38 @@
//
// Created by Stepan Usatiuk on 06.05.2023.
//
#ifndef SEMBACKUP_DIFF_H
#define SEMBACKUP_DIFF_H
#include <sstream>
#include <string>
#include "../../change_detectors/includes/ComparableFile.h"
/// Utility class to compute difference between two ComparableFile%s
class Diff {
public:
/// Compute the difference between two ComparableFile%s
/// If the file is binary, calls diffPercent, which outputs the difference between files in bytes
/// Otherwise prints linewise difference
/// \param c1 Constant reference to the first ComparableFile
/// \param c2 Constant reference to the second ComparableFile
/// \returns Difference message
static std::string diff(const ComparableFile &c1, const ComparableFile &c2);
/// Calculates the difference between \p c1 amd \p c2 in bytes
/// \param c1 Constant reference to the first ComparableFile
/// \param c2 Constant reference to the second ComparableFile
/// \returns Difference message
static std::string diffPercent(const ComparableFile &c1, const ComparableFile &c2);
/// Checks if a file is binary
/// A file is considered binary if its first 2048 bytes contain a null byte
/// \param c1 Constant reference to the checked ComparableFile
/// \return True if the file is considered binary, false otherwise
static bool isBinary(const ComparableFile &c1);
};
#endif//SEMBACKUP_DIFF_H

View File

@@ -2,7 +2,7 @@
// Created by Stepan Usatiuk on 23.05.2023.
//
#include "Command.h"
#include "../includes/Command.h"
Command::Command(std::string name) : name(std::move(name)) {}

View File

@@ -2,20 +2,20 @@
// Created by Stepan Usatiuk on 23.05.2023.
//
#include "CommandDiff.h"
#include "../includes/CommandDiff.h"
#include "../BytesFormatter.h"
#include "../Diff.h"
#include "../Exception.h"
#include "../Progress.h"
#include "../RunningDiffAverage.h"
#include "../Signals.h"
#include "../ThreadPool.h"
#include "../change_detectors/ChangeDetectorFactory.h"
#include "../chunkers/ChunkerFactory.h"
#include "../repo/Serialize.h"
#include "../repo/objects/Archive.h"
#include "../repo/objects/Chunk.h"
#include "../../change_detectors/includes/ChangeDetectorFactory.h"
#include "../../chunkers/includes/ChunkerFactory.h"
#include "../../repo/includes/Serialize.h"
#include "../../repo/includes/objects/Archive.h"
#include "../../repo/includes/objects/Chunk.h"
#include "../../utils/includes/BytesFormatter.h"
#include "../../utils/includes/Exception.h"
#include "../../utils/includes/Progress.h"
#include "../../utils/includes/RunningDiffAverage.h"
#include "../../utils/includes/Signals.h"
#include "../../utils/includes/ThreadPool.h"
#include "../includes/Diff.h"
using namespace CommandsCommon;

View File

@@ -2,7 +2,7 @@
// Created by Stepan Usatiuk on 23.05.2023.
//
#include "CommandList.h"
#include "../includes/CommandList.h"
CommandList::CommandList() : Command("list") {
}

View File

@@ -2,13 +2,13 @@
// Created by Stepan Usatiuk on 23.05.2023.
//
#include "CommandListFiles.h"
#include "../includes/CommandListFiles.h"
#include "../BytesFormatter.h"
#include "../repo/Serialize.h"
#include "../repo/objects/Archive.h"
#include "../repo/objects/Chunk.h"
#include "../repo/objects/File.h"
#include "../../repo/includes/Serialize.h"
#include "../../repo/includes/objects/Archive.h"
#include "../../repo/includes/objects/Chunk.h"
#include "../../repo/includes/objects/File.h"
#include "../../utils/includes/BytesFormatter.h"
CommandListFiles::CommandListFiles() : Command("list-files") {
}

View File

@@ -2,21 +2,21 @@
// Created by Stepan Usatiuk on 23.05.2023.
//
#include "CommandRestore.h"
#include "../includes/CommandRestore.h"
#include <fstream>
#include <sstream>
#include "../BytesFormatter.h"
#include "../Exception.h"
#include "../Progress.h"
#include "../RunningDiffAverage.h"
#include "../Signals.h"
#include "../ThreadPool.h"
#include "../chunkers/ChunkerFactory.h"
#include "../repo/Serialize.h"
#include "../repo/objects/Archive.h"
#include "../repo/objects/Chunk.h"
#include "../../chunkers/includes/ChunkerFactory.h"
#include "../../repo/includes/Serialize.h"
#include "../../repo/includes/objects/Archive.h"
#include "../../repo/includes/objects/Chunk.h"
#include "../../utils/includes/BytesFormatter.h"
#include "../../utils/includes/Exception.h"
#include "../../utils/includes/Progress.h"
#include "../../utils/includes/RunningDiffAverage.h"
#include "../../utils/includes/Signals.h"
#include "../../utils/includes/ThreadPool.h"
using namespace CommandsCommon;

View File

@@ -2,27 +2,27 @@
// Created by Stepan Usatiuk on 23.05.2023.
//
#include "CommandRun.h"
#include "../includes/CommandRun.h"
#include <fstream>
#include <iomanip>
#include <sstream>
#include "../BytesFormatter.h"
#include "../Exception.h"
#include "../Progress.h"
#include "../RunningDiffAverage.h"
#include "../Signals.h"
#include "../ThreadPool.h"
#include "../change_detectors/ChangeDetectorFactory.h"
#include "../chunkers/ChunkerFactory.h"
#include "../crypto/MD5.h"
#include "../repo/Serialize.h"
#include "../repo/objects/Archive.h"
#include "../repo/objects/Chunk.h"
#include "../repo/objects/File.h"
#include "../../change_detectors/includes/ChangeDetectorFactory.h"
#include "../../chunkers/includes/ChunkerFactory.h"
#include "../../crypto/includes/MD5.h"
#include "../../repo/includes/Serialize.h"
#include "../../repo/includes/objects/Archive.h"
#include "../../repo/includes/objects/Chunk.h"
#include "../../repo/includes/objects/File.h"
#include "../../utils/includes/BytesFormatter.h"
#include "../../utils/includes/Exception.h"
#include "../../utils/includes/Progress.h"
#include "../../utils/includes/RunningDiffAverage.h"
#include "../../utils/includes/Signals.h"
#include "../../utils/includes/ThreadPool.h"
#include "CommandsCommon.h"
#include "../includes/CommandsCommon.h"
using namespace CommandsCommon;

View File

@@ -2,13 +2,13 @@
// Created by Stepan Usatiuk on 23.05.2023.
//
#include "CommandsCommon.h"
#include "../includes/CommandsCommon.h"
#include <fstream>
#include <regex>
#include "../Exception.h"
#include "../Signals.h"
#include "../../utils/includes/Exception.h"
#include "../../utils/includes/Signals.h"
void CommandsCommon::workerCallback(unsigned long long int bytesWritten, unsigned long long int bytesSkipped, unsigned long long int filesWritten, WorkerStats &to) {
to.bytesWritten += bytesWritten;

103
src/commands/srcs/Diff.cpp Normal file
View File

@@ -0,0 +1,103 @@
//
// Created by Stepan Usatiuk on 06.05.2023.
//
#include "../includes/Diff.h"
#include "../../chunkers/includes/BuzhashChunker.h"
#include "../../utils/includes/BytesFormatter.h"
#include "../../utils/includes/Exception.h"
#include "../../utils/includes/Signals.h"
bool Diff::isBinary(const ComparableFile &c) {
auto b = c.contents();
for (unsigned int i = 0; i < std::min(c.bytes, 2048ULL); i++) {
auto e = b->sbumpc();
if (std::streambuf::traits_type::to_char_type(e) == '\0') return true;
if (e == std::streambuf::traits_type::eof()) return false;
}
return false;
}
std::string Diff::diff(const ComparableFile &c1, const ComparableFile &c2) {
if (isBinary(c1) || isBinary(c2)) {
if (!(isBinary(c1) && isBinary(c2))) return "One of the files is binary, the other is not";
return diffPercent(c1, c2);
}
std::stringstream out;
auto b1 = c1.contents();
auto b2 = c2.contents();
std::multimap<std::string, unsigned long> f1lines;
std::multimap<std::string, unsigned long> f2diff;
std::string line;
std::istream is1(b1.get());
std::istream is2(b2.get());
int i = 0;
while (std::getline(is1, line)) {
/// Exit when asked to
if (Signals::shouldQuit) throw Exception("Quitting");
f1lines.emplace(line, ++i);
}
i = 0;
while (std::getline(is2, line)) {
/// Exit when asked to
if (Signals::shouldQuit) throw Exception("Quitting");
if (f1lines.count(line) > 0) f1lines.erase(f1lines.find(line));
else
f2diff.emplace(line, ++i);
}
out << "\nLines only in first file: " << std::endl;
for (const auto &s: f1lines) {
out << s.second << "<" << s.first << std::endl;
}
out << "Lines only in second file: " << std::endl;
for (const auto &s: f2diff) {
out << s.second << ">" << s.first << std::endl;
}
out << "^^^\n";
return out.str();
}
std::string Diff::diffPercent(const ComparableFile &c1, const ComparableFile &c2) {
auto b1 = c1.contents();
auto b2 = c2.contents();
BuzhashChunker ch1(b1.get(), 512 * 1024, 1024 * 1024, 19, 31);
BuzhashChunker ch2(b2.get(), 512 * 1024, 1024 * 1024, 19, 31);
std::multiset<std::string> ch1hashes;
std::multiset<std::string> ch2diff;
std::unordered_map<std::string, unsigned long long> hashsize;
for (auto chunkp: ch1) {
/// Exit when asked to
if (Signals::shouldQuit) throw Exception("Quitting");
if (chunkp.second.empty()) continue;
std::string md5(chunkp.first.begin(), chunkp.first.end());
ch1hashes.emplace(md5);
hashsize[md5] = chunkp.second.size();
}
for (auto chunkp: ch2) {
/// Exit when asked to
if (Signals::shouldQuit) throw Exception("Quitting");
if (chunkp.second.empty()) continue;
std::string md5(chunkp.first.begin(), chunkp.first.end());
hashsize[md5] = chunkp.second.size();
if (ch1hashes.count(md5) > 0) ch1hashes.erase(md5);
else if (ch1hashes.count(md5) == 0)
ch2diff.emplace(md5);
}
unsigned long long diff = 0;
for (const auto &c: ch1hashes) {
diff += hashsize[c];
}
for (const auto &c: ch2diff) {
diff += hashsize[c];
}
return "at most " + BytesFormatter::formatStr(diff);
}