Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <cerrno>
  9. #include <cstring>
  10. #include <cstdio>
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <fstream>
  14. #include "FileBackup.hpp"
  15. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  17. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  18. const std::string FileBackup::BackupSuffix(".bak");
  19. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  20. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  21. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. void
  23. FileBackup::CopyFile(std::string From, std::string To) {
  24. std::ifstream Input;
  25. Input.open(From.c_str());
  26. if (!Input) {
  27. std::string Temp;
  28. Temp = "Error opening the file " + From;
  29. Temp += " to read from: ";
  30. Temp += strerror(errno);
  31. throw std::runtime_error(Temp);
  32. }
  33. std::ofstream Output;
  34. Output.open(To.c_str(), std::ios::trunc);
  35. if (!Output) {
  36. std::string Temp;
  37. Temp = "Error opening the file " + To;
  38. Temp += " to copy to: ";
  39. Temp += strerror(errno);
  40. throw std::runtime_error(Temp);
  41. }
  42. if (!Input.eof()) { // Copy if there are characters.
  43. // Copying an empty file causes
  44. Output << Input.rdbuf(); // failbit to be set.
  45. }
  46. if (Output.bad() || Output.fail()) {
  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) {
  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. bool
  76. FileBackup::FileExists(std::string File) {
  77. bool Exists;
  78. std::ifstream Input;
  79. errno = 0;
  80. Input.open(File.c_str());
  81. if (ENOENT == errno) {
  82. Exists = false;
  83. } else {
  84. Exists = true;
  85. }
  86. Input.close();
  87. return Exists;
  88. }
  89. void
  90. FileBackup::CreateBackupFile(std::string File) {
  91. bool ThisFileExists = FileExists(File);
  92. if (ThisFileExists) { // Back up if the file exists.
  93. try {
  94. CopyFile(File, GetBackupFileName(File));
  95. } catch (std::exception &e) {
  96. std::string Temp;
  97. Temp = "Error making a backup of " + File;
  98. Temp += ": ";
  99. Temp += e.what();
  100. throw std::runtime_error(Temp);
  101. }
  102. }
  103. OriginalFileExists[File] = ThisFileExists;
  104. }
  105. void
  106. FileBackup::RemoveAllBackupFiles() {
  107. bool ErrorOccurred = false;
  108. std::ostringstream ErrorMessage;
  109. for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
  110. iFile != OriginalFileExists.end();
  111. iFile++) {
  112. std::string BackupFileName;
  113. if (iFile->second) { // Original file existed?
  114. BackupFileName = GetBackupFileName(iFile->first);
  115. if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
  116. ErrorMessage << "Unable to remove backup file " << BackupFileName
  117. << ": " << strerror(errno) << "\n";
  118. ErrorOccurred = true;
  119. }
  120. }
  121. }
  122. if (ErrorOccurred) {
  123. throw std::runtime_error(ErrorMessage.str());
  124. }
  125. }
  126. void
  127. FileBackup::RestoreAllFilesFromBackup() {
  128. bool ErrorOccurred = false;
  129. std::ostringstream ErrorMessage;
  130. for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
  131. iFile != OriginalFileExists.end();
  132. iFile++) {
  133. std::string BackupFileName;
  134. if (iFile->second) { // Original file existed?
  135. try { // Yes.
  136. BackupFileName = GetBackupFileName(iFile->first);
  137. CopyFile(BackupFileName, iFile->first);
  138. } catch (std::exception &e) {
  139. ErrorMessage << "Error restoring " << iFile->first << " from backup "
  140. << BackupFileName << ": " << e.what() << " \n";
  141. ErrorOccurred = true;
  142. }
  143. } else { // No.
  144. if (FileExists(iFile->first)) {
  145. if (0 != remove(iFile->first.c_str())) {
  146. ErrorMessage << "Unable to remove backup file " << BackupFileName
  147. << ": " << strerror(errno) << "\n";
  148. ErrorOccurred = true;
  149. }
  150. }
  151. }
  152. }
  153. if (ErrorOccurred) {
  154. throw std::runtime_error(ErrorMessage.str());
  155. }
  156. }