mirror of
https://github.com/usatiuk/backup.git
synced 2025-10-27 01:37:49 +01:00
proper cmake
This commit is contained in:
28
src/commands/includes/Command.h
Normal file
28
src/commands/includes/Command.h
Normal 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
|
||||
23
src/commands/includes/CommandDiff.h
Normal file
23
src/commands/includes/CommandDiff.h
Normal 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
|
||||
20
src/commands/includes/CommandList.h
Normal file
20
src/commands/includes/CommandList.h
Normal 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
|
||||
20
src/commands/includes/CommandListFiles.h
Normal file
20
src/commands/includes/CommandListFiles.h
Normal 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
|
||||
30
src/commands/includes/CommandRestore.h
Normal file
30
src/commands/includes/CommandRestore.h
Normal 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
|
||||
28
src/commands/includes/CommandRun.h
Normal file
28
src/commands/includes/CommandRun.h
Normal 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
|
||||
48
src/commands/includes/CommandsCommon.h
Normal file
48
src/commands/includes/CommandsCommon.h
Normal 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
|
||||
18
src/commands/includes/Context.h
Normal file
18
src/commands/includes/Context.h
Normal 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
|
||||
38
src/commands/includes/Diff.h
Normal file
38
src/commands/includes/Diff.h
Normal 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
|
||||
Reference in New Issue
Block a user