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,28 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_COMMAND_H
#define SEMBACKUP_COMMAND_H
#include "Context.h"
/// Abstract base class for some process running with some Context
class Command {
public:
/// Runs the command with Context \p ctx
virtual void run(Context ctx) = 0;
/// Default virtual destructor
virtual ~Command() = 0;
/// The name of the command
const std::string name;
protected:
/// Constructs a command with name \p name
Command(std::string name);
};
#endif//SEMBACKUP_COMMAND_H

View File

@@ -0,0 +1,23 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_COMMANDDIFF_H
#define SEMBACKUP_COMMANDDIFF_H
#include "Command.h"
#include "CommandsCommon.h"
/// Run the diff between:
/// 1. The latest archive and the `from` directory
/// 2. if `aid` is set the aid archive and the `from` directory
/// 3. if `aid` and `aid2` are set between `aid` and `aid2`
class CommandDiff : public Command {
public:
CommandDiff();
void run(Context ctx) override;
};
#endif//SEMBACKUP_COMMANDDIFF_H

View File

@@ -0,0 +1,20 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_COMMANDLIST_H
#define SEMBACKUP_COMMANDLIST_H
#include "Command.h"
#include "CommandsCommon.h"
/// Lists available archives in a repository
class CommandList : public Command {
public:
CommandList();
void run(Context ctx) override;
};
#endif//SEMBACKUP_COMMANDLIST_H

View File

@@ -0,0 +1,20 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_COMMANDLISTFILES_H
#define SEMBACKUP_COMMANDLISTFILES_H
#include "Command.h"
#include "CommandsCommon.h"
/// Lists files in the selected Archive
class CommandListFiles : public Command {
public:
CommandListFiles();
void run(Context ctx) override;
};
#endif//SEMBACKUP_COMMANDLISTFILES_H

View File

@@ -0,0 +1,30 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_COMMANDRESTORE_H
#define SEMBACKUP_COMMANDRESTORE_H
#include "Command.h"
#include "../../repo/includes/objects/File.h"
#include "CommandsCommon.h"
/// Restores the archive with id \aid to path \p to (from config)
class CommandRestore : public Command {
public:
CommandRestore();
void run(Context ctx) override;
private:
/// Internal function to restore a file
/// \param file Constant reference to the File object
/// \param base Base directory to restore to
/// \param callback Stats callback
/// \return Name of the restored file
std::string backupRestoreFile(const File &file, const std::filesystem::path &base, CommandsCommon::workerStatsFunction &callback, Context ctx);
};
#endif//SEMBACKUP_COMMANDRESTORE_H

View File

@@ -0,0 +1,28 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_COMMANDRUN_H
#define SEMBACKUP_COMMANDRUN_H
#include "Command.h"
#include "CommandsCommon.h"
/// Runs the backup according to the config in the Repository
class CommandRun : public Command {
public:
CommandRun();
void run(Context ctx) override;
private:
/// Internal function to chunk the file and save it
/// \param orig Absolute path to the file
/// \param saveAs UTF-8 encoded file name to save as
/// \param callback Stats callback
/// \return ID of the saved file
Object::idType backupChunkFile(const std::filesystem::path &orig, const std::string &saveAs, CommandsCommon::workerStatsFunction &callback, Context ctx);
};
#endif//SEMBACKUP_COMMANDRUN_H

View File

@@ -0,0 +1,48 @@
//
// Created by Stepan Usatiuk on 23.05.2023.
//
#ifndef SEMBACKUP_COMMANDSCOMMON_H
#define SEMBACKUP_COMMANDSCOMMON_H
#include <atomic>
#include <filesystem>
#include <functional>
namespace CommandsCommon {
// Bytes written, bytes skipped, files written
using workerStatsFunction = std::function<void(unsigned long long, unsigned long long, unsigned long long)>;
/// Internat function for recursive directory processing, taking into account ".ignore" and ".nobackup" files
/// \param dir Const reference to the path of directory to iterate through
/// \param ignore List of files to ignore
/// \param spawner Function to spawn other tasks
/// \param processFile Task to spawn on found files
void processDirWithIgnore(const std::filesystem::path &dir, std::vector<std::string> ignore, std::function<void(std::function<void()>)> spawner, std::function<void(std::filesystem::directory_entry)> processFile);
struct WorkerStats {
public:
std::atomic<unsigned long long> bytesWritten = 0;
std::atomic<unsigned long long> bytesSkipped = 0;
std::atomic<unsigned long long> filesWritten = 0;
};
struct RunnerStats {
public:
std::atomic<unsigned long long> bytesToSave = 0;
std::atomic<unsigned long long> filesToSaveCount = 0;
std::atomic<unsigned long long> filesSkipped = 0;
};
/// Checks if \p p has \p prefix as prefix
/// \param prefix Constant reference to the prefix path
/// \param p Constant reference to the checked path
/// \return True if \p p contains \p prefix at its prefix, False otherwise
bool isSubpath(const std::filesystem::path &prefix, const std::filesystem::path &p);
void workerCallback(unsigned long long bytesWritten, unsigned long long bytesSkipped, unsigned long long filesWritten, WorkerStats &to);
};// namespace CommandsCommon
#endif//SEMBACKUP_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