您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FileBackup.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <sstream>
  9. #include <stdexcept>
  10. #include <fstream>
  11. #include <cerrno>
  12. #include <cstring>
  13. #include <cstdio>
  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. if (OriginalFileExists[File] = FileExists(File)) { // Back up if the file exists.
  92. try {
  93. CopyFile(File, GetBackupFileName(File));
  94. } catch (std::exception &e) {
  95. std::string Temp;
  96. Temp = "Error making a backup of " + File;
  97. Temp += ": ";
  98. Temp += e.what();
  99. throw std::runtime_error(Temp);
  100. }
  101. }
  102. }
  103. void
  104. FileBackup::RemoveAllBackupFiles() {
  105. bool ErrorOccurred = false;
  106. std::ostringstream ErrorMessage;
  107. for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
  108. iFile != OriginalFileExists.end();
  109. iFile++) {
  110. std::string BackupFileName;
  111. if (iFile->second) { // Original file existed?
  112. BackupFileName = GetBackupFileName(iFile->first);
  113. if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
  114. ErrorMessage << "Unable to remove backup file " << BackupFileName
  115. << ": " << strerror(errno) << "\n";
  116. ErrorOccurred = true;
  117. }
  118. }
  119. }
  120. if (ErrorOccurred) {
  121. throw std::runtime_error(ErrorMessage.str());
  122. }
  123. }
  124. void
  125. FileBackup::RestoreAllFilesFromBackup() {
  126. bool ErrorOccurred = false;
  127. std::ostringstream ErrorMessage;
  128. for (FilenameContainer::iterator iFile = OriginalFileExists.begin();
  129. iFile != OriginalFileExists.end();
  130. iFile++) {
  131. std::string BackupFileName;
  132. if (iFile->second) { // Original file existed?
  133. try { // Yes.
  134. BackupFileName = GetBackupFileName(iFile->first);
  135. CopyFile(BackupFileName, iFile->first);
  136. } catch (std::exception &e) {
  137. ErrorMessage << "Error restoring " << iFile->first << " from backup "
  138. << BackupFileName << ": " << e.what() << " \n";
  139. ErrorOccurred = true;
  140. }
  141. } else { // No.
  142. if (0 != remove(iFile->first.c_str())) {
  143. ErrorMessage << "Unable to remove backup file " << BackupFileName
  144. << ": " << strerror(errno) << "\n";
  145. ErrorOccurred = true;
  146. }
  147. }
  148. }
  149. if (ErrorOccurred) {
  150. throw std::runtime_error(ErrorMessage.str());
  151. }
  152. }