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 12 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

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

+ 12
- 3
Common/FileBackup.hpp View File

#define FileBackuphpp_included #define FileBackuphpp_included
#include <string> #include <string>
#include <vector>
#include <map>
/// FileBackup class. /// FileBackup class.
// //
// //
static std::string GetBackupFileName(std::string File); 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: private:
/// Copy a file. /// Copy a file.
static const std::string BackupSuffix; static const std::string BackupSuffix;
/// Typedef for container of names of files. /// 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. /// Container of files that have backups.
FilenameContainer BackedUpFile;
FilenameContainer OriginalFileExists;
}; };

+ 1
- 1
CommonTests/Makefile.am View File

## See www.armresearch.com for the copyright terms. ## See www.armresearch.com for the copyright terms.
## ##


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


TESTS = \ TESTS = \
TestFileBackup TestFileBackup

+ 42
- 0
CommonTests/TestFileBackup.cpp View File

} }
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 void
Finalize() { Finalize() {
ErrorExit("TestRemoveAllBackupFiles() failure.\n"); ErrorExit("TestRemoveAllBackupFiles() failure.\n");
} }
// Test FileExists.
if (!TestFileExists()) {
ErrorExit("TestFileExists() failure.\n");
}
Finalize(); // Remove test files. Finalize(); // Remove test files.
} // That's all folks. } // That's all folks.

Loading…
Cancel
Save