123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- // 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 <cerrno>
- #include <cstring>
- #include <cstdio>
-
- #include <sstream>
- #include <stdexcept>
- #include <fstream>
-
- #include "FileBackup.hpp"
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- const std::string FileBackup::BackupSuffix(".bak");
-
- const std::string FileBackup::FailedSuffix(".failed");
-
- //////////////////////////////////////////////////////////////////////////////////////////////////////////
- // 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);
- }
-
- Output << Input.rdbuf();
-
- if (Output.bad()) { // Copying an empty file causes
- // failbit to be set.
- 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.bad()) {
- 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;
-
- }
-
- std::string
- FileBackup::GetFailedFileName(std::string File) {
-
- return File + FailedSuffix;
-
- }
-
- 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) {
-
- std::string FailedFileName;
-
- FailedFileName = GetFailedFileName(File);
-
- if (FileExists(FailedFileName)) { // Removed .failed file?
-
- if (0 != remove(FailedFileName.c_str())) {
- std::string Temp;
-
- Temp = "Unable to remove file " + FailedFileName;
- Temp += " created from a previous run: ";
- Temp += strerror(errno);
-
- throw std::runtime_error(Temp);
-
- }
-
- }
-
- bool ThisFileExists = FileExists(File);
-
- if (ThisFileExists) { // 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);
-
- }
-
- }
-
- OriginalFileExists[File] = ThisFileExists;
-
- }
-
- 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;
- std::string FailedFileName;
-
- if (FileExists(iFile->first)) {
-
- try {
-
- FailedFileName = GetFailedFileName(iFile->first);
-
- CopyFile(iFile->first, FailedFileName);
-
- } catch (std::exception &e) {
-
- ErrorMessage << "Error copying " << iFile->first << " to "
- << FailedFileName << ": " << e.what() << " \n";
-
- ErrorOccurred = true;
-
- }
-
- if (0 != remove(iFile->first.c_str())) { // Remove the new file.
-
- ErrorMessage << "Unable to remove backup file " << BackupFileName
- << ": " << strerror(errno) << "\n";
-
- ErrorOccurred = true;
-
- }
-
- }
-
- 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;
-
- }
-
- }
-
- }
-
- if (ErrorOccurred) {
-
- throw std::runtime_error(ErrorMessage.str());
-
- }
-
- }
|