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 4.2KB

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