// 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 #include #include #include #include #include #include #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()); } }