Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FileBackup.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <vector>
  12. #include <cerrno>
  13. #include <cstring>
  14. #include <cstdio>
  15. #include "FileBackup.hpp"
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Configuration. ////////////////////////////////////////////////////////////////////////////////////////
  18. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. const std::string FileBackup::BackupSuffix(".bak");
  20. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. // End of configuration. /////////////////////////////////////////////////////////////////////////////////
  22. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. void
  24. FileBackup::CopyFile(std::string From, std::string To) {
  25. std::ifstream Input;
  26. Input.open(From.c_str());
  27. if (!Input) {
  28. std::string Temp;
  29. Temp = "Error opening the file " + From;
  30. Temp += " to read from: ";
  31. Temp += strerror(errno);
  32. throw std::runtime_error(Temp);
  33. }
  34. std::ofstream Output;
  35. Output.open(To.c_str(), std::ios::trunc);
  36. if (!Output) {
  37. std::string Temp;
  38. Temp = "Error opening the file " + To;
  39. Temp += " to copy to: ";
  40. Temp += strerror(errno);
  41. throw std::runtime_error(Temp);
  42. }
  43. if (!Input.eof()) { // Copy if there are characters.
  44. // Copying an empty file causes
  45. Output << Input.rdbuf(); // failbit to be set.
  46. }
  47. if (Output.bad() || Output.fail()) {
  48. std::string Temp;
  49. Temp = "Error copying " + From;
  50. Temp += " to " + To;
  51. Temp += ": ";
  52. Temp += strerror(errno);
  53. throw std::runtime_error(Temp);
  54. }
  55. Input.close();
  56. if (!Input) {
  57. std::string Temp;
  58. Temp = "Error closing the file " + From;
  59. Temp += ": ";
  60. Temp += strerror(errno);
  61. throw std::runtime_error(Temp);
  62. }
  63. Output.close();
  64. if (!Output) {
  65. std::string Temp;
  66. Temp = "Error closing the file " + To;
  67. Temp += ": ";
  68. Temp += strerror(errno);
  69. throw std::runtime_error(Temp);
  70. }
  71. }
  72. std::string
  73. FileBackup::GetBackupFileName(std::string File) {
  74. return File + BackupSuffix;
  75. }
  76. void
  77. FileBackup::CreateBackupFile(std::string File) {
  78. try {
  79. CopyFile(File, GetBackupFileName(File));
  80. } catch (std::exception &e) {
  81. std::string Temp;
  82. Temp = "Error making a backup of " + File;
  83. Temp += ": ";
  84. Temp += e.what();
  85. throw std::runtime_error(Temp);
  86. }
  87. BackedUpFile.push_back(File); // Save the name.
  88. }
  89. void
  90. FileBackup::RemoveAllBackupFiles() {
  91. bool ErrorOccurred = false;
  92. std::ostringstream ErrorMessage;
  93. for (FilenameContainer::iterator iFile = BackedUpFile.begin();
  94. iFile != BackedUpFile.end();
  95. iFile++) {
  96. std::string BackupFileName;
  97. BackupFileName = GetBackupFileName(*iFile);
  98. if (0 != remove(BackupFileName.c_str())) { // Delete the backup file.
  99. ErrorMessage << "Unable to remove backup file " << BackupFileName
  100. << ": " << strerror(errno) << "\n";
  101. ErrorOccurred = true;
  102. }
  103. }
  104. if (ErrorOccurred) {
  105. throw std::runtime_error(ErrorMessage.str());
  106. }
  107. }
  108. void
  109. FileBackup::RestoreAllFilesFromBackup() {
  110. bool ErrorOccurred = false;
  111. std::ostringstream ErrorMessage;
  112. for (FilenameContainer::iterator iFile = BackedUpFile.begin();
  113. iFile != BackedUpFile.end();
  114. iFile++) {
  115. std::string BackupFileName;
  116. BackupFileName = GetBackupFileName(*iFile);
  117. try {
  118. CopyFile(BackupFileName, *iFile);
  119. } catch (std::exception &e) {
  120. ErrorMessage << "Error restoring " << *iFile << " from backup "
  121. << BackupFileName << ": " << e.what() << " \n";
  122. ErrorOccurred = true;
  123. }
  124. }
  125. if (ErrorOccurred) {
  126. throw std::runtime_error(ErrorMessage.str());
  127. }
  128. }