|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
#include <sstream>
|
|
|
#include <stdexcept>
|
|
|
#include <stdexcept>
|
|
|
#include <fstream>
|
|
|
#include <fstream>
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
#include <cerrno>
|
|
|
#include <cerrno>
|
|
|
#include <cstring>
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
void
|
|
|
FileBackup::CreateBackupFile(std::string File) {
|
|
|
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.
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ErrorOccurred = false;
|
|
|
bool ErrorOccurred = false;
|
|
|
std::ostringstream ErrorMessage;
|
|
|
std::ostringstream ErrorMessage;
|
|
|
|
|
|
|
|
|
for (FilenameContainer::iterator iFile = BackedUpFile.begin();
|
|
|
|
|
|
iFile != BackedUpFile.end();
|
|
|
|
|
|
|
|
|
for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
|
|
|
|
|
|
iFile != OriginalFileExists.end();
|
|
|
iFile++) {
|
|
|
iFile++) {
|
|
|
std::string BackupFileName;
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool ErrorOccurred = false;
|
|
|
bool ErrorOccurred = false;
|
|
|
std::ostringstream ErrorMessage;
|
|
|
std::ostringstream ErrorMessage;
|
|
|
|
|
|
|
|
|
for (FilenameContainer::iterator iFile = BackedUpFile.begin();
|
|
|
|
|
|
iFile != BackedUpFile.end();
|
|
|
|
|
|
|
|
|
for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
|
|
|
|
|
|
iFile != OriginalFileExists.end();
|
|
|
iFile++) {
|
|
|
iFile++) {
|
|
|
std::string BackupFileName;
|
|
|
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;
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|