Limo
A simple mod manager
Loading...
Searching...
No Matches
backupmanager.h
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "backuptarget.h"
9#include "log.h"
10#include <filesystem>
11#include <functional>
12#include <json/json.h>
13#include <vector>
14
15
20{
21public:
23 BackupManager() = default;
24
31 void addTarget(const std::filesystem::path& path,
32 const std::string& name,
33 const std::vector<std::string>& backup_names);
38 void addTarget(const std::filesystem::path& path);
43 void removeTarget(int target_id);
51 void addBackup(int target_id, const std::string& name, int source = -1);
59 void removeBackup(int target_id, int backup_id, bool update_dirs = true);
65 void setActiveBackup(int target_id, int backup_id);
71 void setProfile(int profile);
77 void addProfile(int source = -1);
82 void removeProfile(int profile);
87 std::vector<BackupTarget> getTargets() const;
89 void reset();
91 int getNumTargets();
97 int getNumBackups(int target_id);
104 void setBackupName(int target_id, int backup_id, const std::string& name);
110 void setBackupTargetName(int target_id, const std::string& name);
118 void overwriteBackup(int target_id, int source_backup, int dest_backup);
123 void setLog(const std::function<void(Log::LogLevel, const std::string&)>& new_log);
124
125private:
127 static inline const std::string BAK_EXTENSION = ".lmmbakman";
129 static inline const std::string JSON_EXTENSION = BAK_EXTENSION + ".json";
131 std::vector<BackupTarget> targets_{};
135 int cur_profile_ = -1;
137 std::function<void(Log::LogLevel, const std::string&)> log_ = [](Log::LogLevel a,
138 const std::string& b) {};
139
148 void updateDirectories();
158 void updateDirectories(int target_id);
160 void updateState();
162 void updateSettings();
168 void writeSettings(const std::filesystem::path& path, const Json::Value& settings) const;
174 Json::Value readSettings(const std::filesystem::path& path) const;
181 std::filesystem::path getConfigPath(const std::filesystem::path& path) const;
188 std::filesystem::path getBackupPath(const std::filesystem::path& path, int backup) const;
195 std::filesystem::path getBackupPath(int target, int backup) const;
196};
Header for the BackupTarget struct.
void setActiveBackup(int target_id, int backup_id)
Changes the currently active backup for the given target.
Definition backupmanager.cpp:124
std::filesystem::path getConfigPath(const std::filesystem::path &path) const
Returns the path to the file which contains state data for the given file or directory.
Definition backupmanager.cpp:389
void setBackupName(int target_id, int backup_id, const std::string &name)
Setter for the name of a backup belonging to the given target.
Definition backupmanager.cpp:222
int cur_profile_
Currently active profile.
Definition backupmanager.h:135
void updateState()
Updates internal state by parsing every targets state file.
Definition backupmanager.cpp:311
void addTarget(const std::filesystem::path &path, const std::string &name, const std::vector< std::string > &backup_names)
Adds a new target file or directory to be managed.
void setProfile(int profile)
Sets the active profile to the new profile and changes all active backups if needed.
Definition backupmanager.cpp:143
int getNumTargets()
Returns the number of backup targets.
Definition backupmanager.cpp:212
BackupManager()=default
Empty default constructor.
void setLog(const std::function< void(Log::LogLevel, const std::string &)> &new_log)
Setter for log callback.
Definition backupmanager.cpp:245
static const std::string BAK_EXTENSION
File extension used for backups.
Definition backupmanager.h:127
void setBackupTargetName(int target_id, const std::string &name)
Setter for the name of a backup target.
Definition backupmanager.cpp:228
void updateDirectories()
Ensures consistency with the data on disk.
Definition backupmanager.cpp:304
void writeSettings(const std::filesystem::path &path, const Json::Value &settings) const
Writes the given json object to disk.
Definition backupmanager.cpp:369
void overwriteBackup(int target_id, int source_backup, int dest_backup)
Deletes all files in the dest backup and replaces them with the files from the source backup.
Definition backupmanager.cpp:234
void addBackup(int target_id, const std::string &name, int source=-1)
Adds a new backup for the given target by copying the currently active backup.
Definition backupmanager.cpp:61
void removeTarget(int target_id)
Removes the given target by deleting all relevant backups and config files.
Definition backupmanager.cpp:79
std::vector< BackupTarget > targets_
Contains all managed targets.
Definition backupmanager.h:131
void reset()
Deletes all entries in targets_ as well as all profiles.
Definition backupmanager.cpp:206
void removeBackup(int target_id, int backup_id, bool update_dirs=true)
Deletes the given backup for given target.
Definition backupmanager.cpp:93
void addTarget(const std::filesystem::path &path)
Adds a backup target which was previously managed by a BackupManager.
std::vector< BackupTarget > getTargets() const
Returns a vector containing information about all managed backup targets.
Definition backupmanager.cpp:198
void updateSettings()
Updates every targets state file with the internal state.
Definition backupmanager.cpp:354
Json::Value readSettings(const std::filesystem::path &path) const
Reads the given file and creates a json object from the files data.
Definition backupmanager.cpp:378
void addProfile(int source=-1)
Adds a new profile.
Definition backupmanager.cpp:167
static const std::string JSON_EXTENSION
File extension used for the files used to store a targets state.
Definition backupmanager.h:129
void removeProfile(int profile)
Removes the given profile.
Definition backupmanager.cpp:180
std::filesystem::path getBackupPath(const std::filesystem::path &path, int backup) const
Returns the path to the given backup for the given file or directory.
int getNumBackups(int target_id)
Returns the number of backups for the given target.
Definition backupmanager.cpp:217
std::function< void(Log::LogLevel, const std::string &)> log_
Callback for logging.
Definition backupmanager.h:137
int num_profiles_
Number of profiles.
Definition backupmanager.h:133
Header for the Log namespace.
char path[256]
Path to which to extract the file.
Definition lspakfilelistentry.h:1
LogLevel
Represents the importance of a log message.
Definition log.h:19