You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileBackup.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // FileBackup.cpp
  2. //
  3. // Copyright (C) 2011, ARM Research Labs, LLC.
  4. // See www.armresearch.com for the copyright terms.
  5. //
  6. // This file implements the common functionality for the configuration
  7. // utilities.
  8. #include <iostream>
  9. #include <cerrno>
  10. #include <cstring>
  11. #include <cstdio>
  12. #include <sstream>
  13. #include <stdexcept>
  14. #include <fstream>
  15. #include "FileBackup.hpp"
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. const std::string FileBackup::BackupSuffix(".bak");
  20. const std::string FileBackup::FailedSuffix(".failed");
  21. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  23. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. void
  25. FileBackup::CopyFile(std::string From, std::string To) {
  26. std::ifstream Input;
  27. Input.open(From.c_str());
  28. if (!Input) {
  29. std::string Temp;
  30. Temp = "Error opening the file " + From;
  31. Temp += " to read from: ";
  32. Temp += strerror(errno);
  33. throw std::runtime_error(Temp);
  34. }
  35. std::ofstream Output;
  36. Output.open(To.c_str(), std::ios::trunc);
  37. if (!Output) {
  38. std::string Temp;
  39. Temp = "Error opening the file " + To;
  40. Temp += " to copy to: ";
  41. Temp += strerror(errno);
  42. throw std::runtime_error(Temp);
  43. }
  44. Output << Input.rdbuf();
  45. if (Output.bad()) { // Copying an empty file causes
  46. // failbit to be set.
  47. std::string Temp;
  48. Temp = "Error copying " + From;
  49. Temp += " to " + To;
  50. Temp += ": ";
  51. Temp += strerror(errno);
  52. throw std::runtime_error(Temp);
  53. }
  54. Input.close();
  55. if (!Input) {
  56. std::string Temp;
  57. Temp = "Error closing the file " + From;
  58. Temp += ": ";
  59. Temp += strerror(errno);
  60. throw std::runtime_error(Temp);
  61. }
  62. Output.close();
  63. if (Output.bad()) {
  64. std::string Temp;
  65. Temp = "Error closing the file " + To;
  66. Temp += ": ";
  67. Temp += strerror(errno);
  68. throw std::runtime_error(Temp);
  69. }
  70. }
  71. std::string
  72. FileBackup::GetBackupFileName(std::string File) {
  73. return File + BackupSuffix;
  74. }
  75. std::string
  76. FileBackup::GetFailedFileName(std::string File) {
  77. return File + FailedSuffix;
  78. }
  79. bool
  80. FileBackup::FileExists(std::string File) {
  81. bool Exists;
  82. std::ifstream Input;
  83. errno = 0;
  84. Input.open(File.c_str());
  85. if (ENOENT == errno) {
  86. Exists = false;
  87. } else {
  88. Exists = true;
  89. }
  90. Input.close();
  91. return Exists;
  92. }
  93. void
  94. FileBackup::CreateBackupFile(std::string File) {
  95. std::string FailedFileName;
  96. FailedFileName = GetFailedFileName(File);
  97. if (FileExists(FailedFileName)) { // Removed .failed file?
  98. if (0 != remove(FailedFileName.c_str())) {
  99. std::string Temp;
  100. Temp = "Unable to remove file " + FailedFileName;
  101. Temp += " created from a previous run: ";
  102. Temp += strerror(errno);
  103. throw std::runtime_error(Temp);
  104. }
  105. }
  106. bool ThisFileExists = FileExists(File);
  107. if (ThisFileExists) { // Back up if the file exists.
  108. try {
  109. CopyFile(File, GetBackupFileName(File));
  110. } catch (std::exception &e) {
  111. std::string Temp;
  112. Temp = "Error making a backup of " + File;
  113. Temp += ": ";
  114. Temp += e.what();
  115. throw std::runtime_error(Temp);
  116. }
  117. }
  118. OriginalFileExists[File] = ThisFileExists;
  119. }
  120. void
  121. FileBackup::RemoveAllBackupFiles() {
  122. bool ErrorOccurred = false;
  123. std::ostringstream ErrorMessage;
  124. for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
  125. iFile != OriginalFileExists.end();
  126. iFile++) {
  127. std::string BackupFileName;
  128. if (iFile->second) { // Original file existed?
  129. BackupFileName = GetBackupFileName(iFile->first);
  130. if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
  131. ErrorMessage << "Unable to remove backup file " << BackupFileName
  132. << ": " << strerror(errno) << "\n";
  133. ErrorOccurred = true;
  134. }
  135. }
  136. }
  137. if (ErrorOccurred) {
  138. throw std::runtime_error(ErrorMessage.str());
  139. }
  140. }
  141. void
  142. FileBackup::RestoreAllFilesFromBackup() {
  143. bool ErrorOccurred = false;
  144. std::ostringstream ErrorMessage;
  145. for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
  146. iFile != OriginalFileExists.end();
  147. iFile++) {
  148. std::string BackupFileName;
  149. std::string FailedFileName;
  150. if (FileExists(iFile->first)) {
  151. try {
  152. FailedFileName = GetFailedFileName(iFile->first);
  153. CopyFile(iFile->first, FailedFileName);
  154. } catch (std::exception &e) {
  155. ErrorMessage << "Error copying " << iFile->first << " to "
  156. << FailedFileName << ": " << e.what() << " \n";
  157. ErrorOccurred = true;
  158. }
  159. if (0 != remove(iFile->first.c_str())) { // Remove the new file.
  160. ErrorMessage << "Unable to remove backup file " << BackupFileName
  161. << ": " << strerror(errno) << "\n";
  162. ErrorOccurred = true;
  163. }
  164. }
  165. if (iFile->second) { // Original file existed?
  166. try { // Yes.
  167. BackupFileName = GetBackupFileName(iFile->first);
  168. CopyFile(BackupFileName, iFile->first);
  169. } catch (std::exception &e) {
  170. ErrorMessage << "Error restoring " << iFile->first << " from backup "
  171. << BackupFileName << ": " << e.what() << " \n";
  172. ErrorOccurred = true;
  173. }
  174. }
  175. }
  176. if (ErrorOccurred) {
  177. throw std::runtime_error(ErrorMessage.str());
  178. }
  179. }