Limo
A simple mod manager
Loading...
Searching...
No Matches
BackupManager Class Reference

Handles creation of, deletion of and switching between, bachups. More...

#include <backupmanager.h>

Public Member Functions

 BackupManager ()=default
 Empty default constructor.
 
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 addTarget (const std::filesystem::path &path)
 Adds a backup target which was previously managed by a BackupManager.
 
void removeTarget (int target_id)
 Removes the given target by deleting all relevant backups and config files.
 
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.
 
void removeBackup (int target_id, int backup_id, bool update_dirs=true)
 Deletes the given backup for given target.
 
void setActiveBackup (int target_id, int backup_id)
 Changes the currently active backup for the given target.
 
void setProfile (int profile)
 Sets the active profile to the new profile and changes all active backups if needed.
 
void addProfile (int source=-1)
 Adds a new profile.
 
void removeProfile (int profile)
 Removes the given profile.
 
std::vector< BackupTargetgetTargets () const
 Returns a vector containing information about all managed backup targets.
 
void reset ()
 Deletes all entries in targets_ as well as all profiles.
 
int getNumTargets ()
 Returns the number of backup targets.
 
int getNumBackups (int target_id)
 Returns the number of backups for the given target.
 
void setBackupName (int target_id, int backup_id, const std::string &name)
 Setter for the name of a backup belonging to the given target.
 
void setBackupTargetName (int target_id, const std::string &name)
 Setter for the name of a backup target.
 
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.
 
void setLog (const std::function< void(Log::LogLevel, const std::string &)> &new_log)
 Setter for log callback.
 

Private Member Functions

void updateDirectories ()
 Ensures consistency with the data on disk.
 
void updateDirectories (int target_id)
 Ensures consistency with the data on disk.
 
void updateState ()
 Updates internal state by parsing every targets state file.
 
void updateSettings ()
 Updates every targets state file with the internal state.
 
void writeSettings (const std::filesystem::path &path, const Json::Value &settings) const
 Writes the given json object to disk.
 
Json::Value readSettings (const std::filesystem::path &path) const
 Reads the given file and creates a json object from the files data.
 
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.
 
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.
 
std::filesystem::path getBackupPath (int target, int backup) const
 Returns the path to the given existing backup for the given target.
 

Private Attributes

std::vector< BackupTargettargets_ {}
 Contains all managed targets.
 
int num_profiles_ = 0
 Number of profiles.
 
int cur_profile_ = -1
 Currently active profile.
 
std::function< void(Log::LogLevel, const std::string &)> log_
 Callback for logging.
 

Static Private Attributes

static const std::string BAK_EXTENSION = ".lmmbakman"
 File extension used for backups.
 
static const std::string JSON_EXTENSION = BAK_EXTENSION + ".json"
 File extension used for the files used to store a targets state.
 

Detailed Description

Handles creation of, deletion of and switching between, bachups.

Member Function Documentation

◆ addBackup()

void BackupManager::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.

Parameters
target_idTarget for which to create a new backup.
nameDisplay name for the new backup.
sourceBackup from which to copy files to create the new backup. If -1: copy currently active backup.

◆ addProfile()

void BackupManager::addProfile ( int source = -1)

Adds a new profile.

Parameters
sourceIf this refers to an existing backup: Copy the active backups from that profile.

◆ addTarget() [1/2]

void BackupManager::addTarget ( const std::filesystem::path & path)

Adds a backup target which was previously managed by a BackupManager.

Parameters
pathPath to the target file or directory.

◆ addTarget() [2/2]

void BackupManager::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.

Parameters
pathPath to the target file or directory.
nameDisplay name for this target.
backup_namesDisplay names for initial backups. Must contain at least one.

◆ getBackupPath() [1/2]

std::filesystem::path BackupManager::getBackupPath ( const std::filesystem::path & path,
int backup ) const
private

Returns the path to the given backup for the given file or directory.

Parameters
pathPath to a backup target.
backupBackup id for the given target.
Returns
The path.

◆ getBackupPath() [2/2]

sfs::path BackupManager::getBackupPath ( int target,
int backup ) const
private

Returns the path to the given existing backup for the given target.

Parameters
pathtarget Target for which to find the path.
backupBackup id for the given target.
Returns
The path.

◆ getConfigPath()

sfs::path BackupManager::getConfigPath ( const std::filesystem::path & path) const
private

Returns the path to the file which contains state data for the given file or directory.

Parameters
pathFile or directory for which to generate the path.
Returns
The path.

◆ getNumBackups()

int BackupManager::getNumBackups ( int target_id)

Returns the number of backups for the given target.

Parameters
target_idBackup target.
Returns
The number of backups.

◆ getTargets()

std::vector< BackupTarget > BackupManager::getTargets ( ) const

Returns a vector containing information about all managed backup targets.

Returns
The vector.

◆ overwriteBackup()

void BackupManager::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.

Parameters
target_idBackup target.
source_backupBackup from which to copy files.
dest_backupTarget for data deletion.

◆ readSettings()

Json::Value BackupManager::readSettings ( const std::filesystem::path & path) const
private

Reads the given file and creates a json object from the files data.

Parameters
pathFile to read.
Returns
The json object created from the file.

◆ removeBackup()

void BackupManager::removeBackup ( int target_id,
int backup_id,
bool update_dirs = true )

Deletes the given backup for given target.

Parameters
target_idTarget from which to delete a backup.
backup_idBackup to remove.
update_dirsIf true: Repair the target if it is in an invalid state, e.g. if a backup has been manually deleted.

◆ removeProfile()

void BackupManager::removeProfile ( int profile)

Removes the given profile.

Parameters
profileProfile to be removed.

◆ removeTarget()

void BackupManager::removeTarget ( int target_id)

Removes the given target by deleting all relevant backups and config files.

Parameters
target_idTarget to remove.

◆ setActiveBackup()

void BackupManager::setActiveBackup ( int target_id,
int backup_id )

Changes the currently active backup for the given target.

Parameters
target_idTarget for which to change the active backup.
backup_idNew active backup.

◆ setBackupName()

void BackupManager::setBackupName ( int target_id,
int backup_id,
const std::string & name )

Setter for the name of a backup belonging to the given target.

Parameters
target_idBackup target.
backup_idBackup to be edited.
nameThe new name.

◆ setBackupTargetName()

void BackupManager::setBackupTargetName ( int target_id,
const std::string & name )

Setter for the name of a backup target.

Parameters
target_idBackup target.
nameThe new name.

◆ setLog()

void BackupManager::setLog ( const std::function< void(Log::LogLevel, const std::string &)> & new_log)

Setter for log callback.

Parameters
new_logNew log callback

◆ setProfile()

void BackupManager::setProfile ( int profile)

Sets the active profile to the new profile and changes all active backups if needed.

Parameters
profileNew active profile.

◆ updateDirectories() [1/2]

void BackupManager::updateDirectories ( )
private

Ensures consistency with the data on disk.

This is accomplished by deleting backups for which no file exists and files on disk which should by filename and extension be a backup but have an invalid id. This is done for all files matching the filename and path of any target.

◆ updateDirectories() [2/2]

void BackupManager::updateDirectories ( int target_id)
private

Ensures consistency with the data on disk.

This is accomplished by deleting backups for which no file exists and renaming files on disk which should by filename and extension be a backup but have an invalid id. This is done for all files matching the filename and path of the given target.

Parameters
target_idTarget to check.

◆ writeSettings()

void BackupManager::writeSettings ( const std::filesystem::path & path,
const Json::Value & settings ) const
private

Writes the given json object to disk.

Parameters
pathPath to write to.
settingsThe json object.

Member Data Documentation

◆ log_

std::function<void(Log::LogLevel, const std::string&)> BackupManager::log_
private
Initial value:
const std::string& b) {}
LogLevel
Represents the importance of a log message.
Definition log.h:19

Callback for logging.


The documentation for this class was generated from the following files: