|
|
@@ -9,7 +9,6 @@ |
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
@@ -99,24 +98,51 @@ FileBackup::GetBackupFileName(std::string File) { |
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
FileBackup::FileExists(std::string File) {
|
|
|
|
|
|
|
|
bool Exists;
|
|
|
|
std::ifstream Input;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
Input.open(File.c_str());
|
|
|
|
|
|
|
|
if (ENOENT == errno) {
|
|
|
|
|
|
|
|
Exists = false;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Exists = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Input.close();
|
|
|
|
|
|
|
|
return Exists;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FileBackup::CreateBackupFile(std::string File) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (OriginalFileExists[File] = FileExists(File)) { // Back up if the file exists.
|
|
|
|
|
|
|
|
CopyFile(File, GetBackupFileName(File));
|
|
|
|
try {
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
std::string Temp;
|
|
|
|
CopyFile(File, GetBackupFileName(File));
|
|
|
|
|
|
|
|
Temp = "Error making a backup of " + File;
|
|
|
|
Temp += ": ";
|
|
|
|
Temp += e.what();
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
std::string Temp;
|
|
|
|
|
|
|
|
}
|
|
|
|
Temp = "Error making a backup of " + File;
|
|
|
|
Temp += ": ";
|
|
|
|
Temp += e.what();
|
|
|
|
throw std::runtime_error(Temp);
|
|
|
|
|
|
|
|
BackedUpFile.push_back(File); // Save the name.
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
@@ -126,19 +152,23 @@ FileBackup::RemoveAllBackupFiles() { |
|
|
|
bool ErrorOccurred = false;
|
|
|
|
std::ostringstream ErrorMessage;
|
|
|
|
|
|
|
|
for (FilenameContainer::iterator iFile = BackedUpFile.begin();
|
|
|
|
iFile != BackedUpFile.end();
|
|
|
|
for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
|
|
|
|
iFile != OriginalFileExists.end();
|
|
|
|
iFile++) {
|
|
|
|
std::string BackupFileName;
|
|
|
|
|
|
|
|
BackupFileName = GetBackupFileName(*iFile);
|
|
|
|
if (iFile->second) { // Original file existed?
|
|
|
|
|
|
|
|
BackupFileName = GetBackupFileName(iFile->first);
|
|
|
|
|
|
|
|
if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
|
|
|
|
if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
|
|
|
|
|
|
|
|
ErrorMessage << "Unable to remove backup file " << BackupFileName
|
|
|
|
<< ": " << strerror(errno) << "\n";
|
|
|
|
ErrorMessage << "Unable to remove backup file " << BackupFileName
|
|
|
|
<< ": " << strerror(errno) << "\n";
|
|
|
|
|
|
|
|
ErrorOccurred = true;
|
|
|
|
ErrorOccurred = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
@@ -158,23 +188,39 @@ FileBackup::RestoreAllFilesFromBackup() { |
|
|
|
bool ErrorOccurred = false;
|
|
|
|
std::ostringstream ErrorMessage;
|
|
|
|
|
|
|
|
for (FilenameContainer::iterator iFile = BackedUpFile.begin();
|
|
|
|
iFile != BackedUpFile.end();
|
|
|
|
for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
|
|
|
|
iFile != OriginalFileExists.end();
|
|
|
|
iFile++) {
|
|
|
|
std::string BackupFileName;
|
|
|
|
|
|
|
|
BackupFileName = GetBackupFileName(*iFile);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (iFile->second) { // Original file existed?
|
|
|
|
|
|
|
|
CopyFile(BackupFileName, *iFile);
|
|
|
|
try { // Yes.
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
BackupFileName = GetBackupFileName(iFile->first);
|
|
|
|
|
|
|
|
CopyFile(BackupFileName, iFile->first);
|
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
|
|
|
|
ErrorMessage << "Error restoring " << iFile->first << " from backup "
|
|
|
|
<< BackupFileName << ": " << e.what() << " \n";
|
|
|
|
|
|
|
|
ErrorOccurred = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // No.
|
|
|
|
|
|
|
|
if (0 != remove(iFile->first.c_str())) {
|
|
|
|
|
|
|
|
ErrorMessage << "Unable to remove backup file " << BackupFileName
|
|
|
|
<< ": " << strerror(errno) << "\n";
|
|
|
|
|
|
|
|
ErrorMessage << "Error restoring " << *iFile << " from backup "
|
|
|
|
<< BackupFileName << ": " << e.what() << " \n";
|
|
|
|
ErrorOccurred = true;
|
|
|
|
|
|
|
|
ErrorOccurred = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|