Browse Source

Initial revision of FileBackup class. This class manages the backup

and restoring of files.


git-svn-id: https://svn.microneil.com/svn/SNFUtility/trunk@3 aa37657e-1934-4a5f-aa6d-2d8eab27ff7c
master
adeniz 12 years ago
parent
commit
cba3faee75
5 changed files with 295 additions and 52 deletions
  1. 182
    0
      Common/FileBackup.cpp
  2. 104
    0
      Common/FileBackup.hpp
  3. 9
    7
      Common/Makefile.am
  4. 0
    2
      Common/UtilityConfig.cpp
  5. 0
    43
      Common/UtilityConfig.hpp

+ 182
- 0
Common/FileBackup.cpp View File

@@ -0,0 +1,182 @@
// FileBackup.cpp
//
// Copyright (C) 2011, ARM Research Labs, LLC.
// See www.armresearch.com for the copyright terms.
//
// This file implements the common functionality for the configuration
// utilities.
#include <iostream>
#include <stdexcept>
#include <fstream>
#include <vector>
#include <cerrno>
#include <cstring>
#include <cstdio>
#include "FileBackup.hpp"
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Configuration. ////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// End of configuration. /////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
FileBackup::FileBackup(std::string Suffix) :
BackupSuffix(Suffix) {
}
void
FileBackup::CopyFile(std::string From, std::string To) {
std::ifstream Input;
Input.open(From.c_str());
if (!Input) {
std::string Temp;
Temp = "Error opening the file " + From;
Temp += " to read from: ";
Temp += strerror(errno);
throw std::runtime_error(Temp);
}
std::ofstream Output;
Output.open(To.c_str(), std::ios::trunc);
if (!Output) {
std::string Temp;
Temp = "Error opening the file " + To;
Temp += " to copy to: ";
Temp += strerror(errno);
throw std::runtime_error(Temp);
}
if (!Input.eof()) { // Copy if there are characters.
// Copying an empty file causes
Output << Input.rdbuf(); // failbit to be set.
}
if (Output.bad() || Output.fail()) {
std::string Temp;
Temp = "Error copying " + From;
Temp += " to " + To;
Temp += ": ";
Temp += strerror(errno);
throw std::runtime_error(Temp);
}
Input.close();
if (!Input) {
std::string Temp;
Temp = "Error closing the file " + From;
Temp += ": ";
Temp += strerror(errno);
throw std::runtime_error(Temp);
}
Output.close();
if (!Output) {
std::string Temp;
Temp = "Error closing the file " + To;
Temp += ": ";
Temp += strerror(errno);
throw std::runtime_error(Temp);
}
}
std::string
FileBackup::GetBackupFileName(std::string File) {
return File + BackupSuffix;
}
void
FileBackup::CreateBackupFile(std::string File) {
try {
CopyFile(File, GetBackupFileName(File));
} 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
FileBackup::RemoveAllBackupFiles() {
bool ErrorOccurred = false;
for (FilenameContainer::iterator iFile = BackedUpFile.begin();
iFile != BackedUpFile.end();
iFile++) {
std::string BackupFileName;
BackupFileName = GetBackupFileName(*iFile);
if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
std::cerr << "Unable to remove backup file " << BackupFileName
<< ": " << strerror(errno) << "\n";
ErrorOccurred = true;
}
}
return ErrorOccurred;
}
bool
FileBackup::RestoreAllFilesFromBackup() {
bool ErrorOccurred = false;
for (FilenameContainer::iterator iFile = BackedUpFile.begin();
iFile != BackedUpFile.end();
iFile++) {
std::string BackupFileName;
BackupFileName = GetBackupFileName(*iFile);
try {
CopyFile(BackupFileName, *iFile);
} catch (std::exception &e) {
std::cerr << "Error restoring " << *iFile << " from backup "
<< BackupFileName << ": " << e.what() << " \n";
ErrorOccurred = true;
}
}
return ErrorOccurred;
}

+ 104
- 0
Common/FileBackup.hpp View File

@@ -0,0 +1,104 @@
// FileBackup.hpp
//
// Copyright (C) 2011 ARM Research Labs, LLC.
// See www.armresearch.com for the copyright terms.
//
// This file defines the interface used by the FileBackup class.
//
#ifndef FileBackuphpp_included
#define FileBackuphpp_included
#include <string>
#include <vector>
/// FileBackup class.
//
// This class provides capability to manage the backup and restore of
// disk files.
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class FileBackup {
public:
/// Constructor.
//
// \param[in] Suffix is the suffix to append to create the name of
// the backup file.
//
FileBackup(std::string Suffix);
/// Create a backup of the specified file.
//
// This method creates a backup of the specified file The name of
// the link is the name of the file appended by the BackupSuffix
// data member.
//
// The file can be restored from the backup by RestoreFromBackup.
//
// \param[in] File is the file name to create a backup for.
//
// \throws std::runtime_error in case of error.
//
// \see RestoreAllFilesFromBackup().
//
void CreateBackupFile(std::string File);
/// Remove the backups of all the specified files.
//
// This method removes the backup of the files specified by the
// CreateBackup() method.
//
// If an error is encountered, a message is output to std::cerr.
//
// \returns true if all backups were removed with no error, false otherwise.
//
bool RemoveAllBackupFiles();
/// Restore the all the specified files from the backup.
//
// This method restores the backup of the files specified by the
// CreateBackupFile() method.
//
// If an error is encountered, a message is output to std::cerr.
//
// \returns true if all files were restored with no error, false otherwise.
//
// \see CreateBackupFile().
//
bool RestoreAllFilesFromBackup();
private:
/// Get the name of the backup file.
//
// \param[in] File is the name of the file to back up.
//
// \returns the name of the backup file.
//
std::string GetBackupFileName(std::string File);
/// Copy a file.
//
// \param[in] From is the name of the file to copy from.
//
// \param[in] To is the name of the file to copy to.
//
// \throws runtime_error in case of error.
//
void CopyFile(std::string From, std::string To);
/// Suffix to append to the file name to obtain the backup file
/// name.
std::string BackupSuffix;
/// Typedef for container of names of files.
typedef std::vector<std::string> FilenameContainer;
/// Container of files that have backups.
FilenameContainer BackedUpFile;
};
#endif

+ 9
- 7
Common/Makefile.am View File

@@ -12,17 +12,19 @@ CXXFLAGS = $(SNF_CXXFLAGS) -I@top_srcdir@/SNFMulti \
-DSNF_OSTYPE=\"$(SNF_OSTYPE)\" -DDEFAULT_DATA_DIR=\"@datadir@/$(PACKAGE_NAME)\" \
-DSBIN_DIR=\"$(DESTDIR)$(sbindir)\"

noinst_LIBRARIES = \
noinst_LIBRARIES = \
libUtilityCommon.a

libUtilityCommon_a_SOURCES = \
@top_srcdir@/SNFUtility/Common/UtilityConfig.cpp
libUtilityCommon_a_SOURCES = \
@top_srcdir@/SNFUtility/Common/UtilityConfig.cpp \
@top_srcdir@/SNFUtility/Common/FileBackup.cpp

noinst_HEADERS = \
@top_srcdir@/SNFUtility/Common/UtilityConfig.hpp
noinst_HEADERS = \
@top_srcdir@/SNFUtility/Common/UtilityConfig.hpp \
@top_srcdir@/SNFUtility/Common/FileBackup.hpp

EXTRA_DIST = \
Makefile.am \
EXTRA_DIST = \
Makefile.am \
ChangeLog

clean-local:

+ 0
- 2
Common/UtilityConfig.cpp View File

@@ -99,8 +99,6 @@ const string ExplainKey("-explain");
/// Help command-line input.
const string HelpKey("-h");
const std::string UtilityConfig::BackupSuffix("_snf_backup");
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// End of configuration. /////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////

+ 0
- 43
Common/UtilityConfig.hpp View File

@@ -143,47 +143,6 @@ public:
//
void MkDir(std::string &Dir);
/// Create a backup link of the specified file.
//
// This method creates a backup of the specified file by creating
// a link to the file. The name of the link is the name of the
// file appended by the BackupSuffix data member.
//
// The file can be restored from the backup link by
// RestoreFromBackupLink.
//
// \param[in] File is the file name to create a backup link for.
//
// \throws std::runtime_error in case of error.
//
// \see RestoreFromBackupLink.
//
void CreateBackupLink(std::string File);
/// Remove the backup link of the specified file.
//
// This method removes the backup link of the specified file by
// creating a link to the file. The name of the link is the name
// of the file appended by the BackupSuffix data member.
//
// \param[in] File is the file name whose backup link is to be
// removed.
//
void RemoveBackupLink(std::string File);
/// Restore the specified file from the backup link.
//
// This method restores the specified from the backup link created
// by CreateBackupLink().
//
// \param[in] File is the file name whose backup link is to be
// removed.
//
// \see CreateBackupLink.
//
void RestoreFromBackupLink(std::string File);
/// Restart the MTA.
//
// This function starts or restarts the MTA.
@@ -291,8 +250,6 @@ private:
static const std::string SampleIgnoreListFile; ///< Sample ignore list file.
static const std::string SampleRulebaseScriptFile; ///< Sample rulebase script file.
static const std::string BackupSuffix; ///< Suffix appended to create backup file name.
snfCFGData CFGData; ///< Configuration data.
/// Operating system type.

Loading…
Cancel
Save