Browse Source

Modified FileBackup to handle backups of files that don't exist. Ran

test program to check old functionality; need to check new
functionality.


git-svn-id: https://svn.microneil.com/svn/SNFUtility/trunk@5 aa37657e-1934-4a5f-aa6d-2d8eab27ff7c
master
adeniz 13 years ago
parent
commit
e4897dcf78
4 changed files with 128 additions and 31 deletions
  1. 73
    27
      Common/FileBackup.cpp
  2. 12
    3
      Common/FileBackup.hpp
  3. 1
    1
      CommonTests/Makefile.am
  4. 42
    0
      CommonTests/TestFileBackup.cpp

+ 73
- 27
Common/FileBackup.cpp View File

@@ -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;
}
}

+ 12
- 3
Common/FileBackup.hpp View File

@@ -10,7 +10,7 @@
#define FileBackuphpp_included
#include <string>
#include <vector>
#include <map>
/// FileBackup class.
//
@@ -66,6 +66,12 @@ public:
//
static std::string GetBackupFileName(std::string File);
/// Check if a file exists.
//
// \returns true if the file exists, false otherwise.
//
static bool FileExists(std::string File);
private:
/// Copy a file.
@@ -83,10 +89,13 @@ private:
static const std::string BackupSuffix;
/// Typedef for container of names of files.
typedef std::vector<std::string> FilenameContainer;
//
// The key is the name of the file to back up. The value is true
// if the file to back up exists, false otherwise.
typedef std::map<std::string, bool> FilenameContainer;
/// Container of files that have backups.
FilenameContainer BackedUpFile;
FilenameContainer OriginalFileExists;
};

+ 1
- 1
CommonTests/Makefile.am View File

@@ -11,7 +11,7 @@
## See www.armresearch.com for the copyright terms.
##

AM_CXXFLAGS = -I@top_srcdir@/SNFUtility/Common
CXXFLAGS = $(SNF_CXXFLAGS) -I@top_srcdir@/SNFUtility/Common

TESTS = \
TestFileBackup

+ 42
- 0
CommonTests/TestFileBackup.cpp View File

@@ -240,6 +240,43 @@ TestRemoveAllBackupFiles() {
}
bool
TestFileExists() {
bool ResultIsPass = true;
for (int i = 0; i < FileName.size(); i++) { // Check that files don't exist.
std::string BackupFileName;
BackupFileName = FileBackup::GetBackupFileName(FileName[i]);
if (FileBackup::FileExists(BackupFileName)) {
std::string Temp;
Temp = "***Error--File " + BackupFileName;
Temp += " incorrectly determined to exist.\n";
Error(Temp);
ResultIsPass = false;
}
if (!FileBackup::FileExists(FileName[i])) {
std::string Temp;
Temp = "***Error--File " + FileName[i];
Temp += " incorrectly determined to not exist.\n";
Error(Temp);
ResultIsPass = false;
}
}
return ResultIsPass;
}
void
Finalize() {
@@ -273,6 +310,11 @@ int main(int argc, char* argv[]) {
ErrorExit("TestRemoveAllBackupFiles() failure.\n");
}
// Test FileExists.
if (!TestFileExists()) {
ErrorExit("TestFileExists() failure.\n");
}
Finalize(); // Remove test files.
} // That's all folks.

Loading…
Cancel
Save