- // 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 <sstream>
- #include <stdexcept>
- #include <fstream>
-
- #include <cerrno>
- #include <cstring>
- #include <cstdio>
-
- #include "FileBackup.hpp"
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- const std::string FileBackup::BackupSuffix(".bak");
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // End of configuration. /////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- 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;
-
- }
-
- 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) {
-
- if (OriginalFileExists[File] = FileExists(File)) { // Back up if the file exists.
-
- 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);
-
- }
-
- }
-
- }
-
- void
- FileBackup::RemoveAllBackupFiles() {
-
- bool ErrorOccurred = false;
- std::ostringstream ErrorMessage;
-
- for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
- iFile != OriginalFileExists.end();
- iFile++) {
- std::string BackupFileName;
-
- if (iFile->second) { // Original file existed?
-
- BackupFileName = GetBackupFileName(iFile->first);
-
- if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
-
- ErrorMessage << "Unable to remove backup file " << BackupFileName
- << ": " << strerror(errno) << "\n";
-
- ErrorOccurred = true;
-
- }
-
- }
-
- }
-
- if (ErrorOccurred) {
-
- throw std::runtime_error(ErrorMessage.str());
-
- }
-
- }
-
- void
- FileBackup::RestoreAllFilesFromBackup() {
-
- bool ErrorOccurred = false;
- std::ostringstream ErrorMessage;
-
- for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
- iFile != OriginalFileExists.end();
- iFile++) {
- std::string BackupFileName;
-
-
- if (iFile->second) { // Original file existed?
-
- try { // Yes.
-
- 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";
-
- ErrorOccurred = true;
-
- }
-
- }
-
- }
-
- if (ErrorOccurred) {
-
- throw std::runtime_error(ErrorMessage.str());
-
- }
-
- }
|