123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- // 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 <vector>
-
- #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;
-
- }
-
- 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.
-
- }
-
- void
- FileBackup::RemoveAllBackupFiles() {
-
- bool ErrorOccurred = false;
- std::ostringstream ErrorMessage;
-
- 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.
-
- 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 = BackedUpFile.begin();
- iFile != BackedUpFile.end();
- iFile++) {
- std::string BackupFileName;
-
- BackupFileName = GetBackupFileName(*iFile);
-
- try {
-
- CopyFile(BackupFileName, *iFile);
-
- } catch (std::exception &e) {
-
- ErrorMessage << "Error restoring " << *iFile << " from backup "
- << BackupFileName << ": " << e.what() << " \n";
-
- ErrorOccurred = true;
-
- }
-
- }
-
- if (ErrorOccurred) {
-
- throw std::runtime_error(ErrorMessage.str());
-
- }
-
- }
|