ArchiveStrategy.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // ArchiveStrategy.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/ArchiveStrategy.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Logging
  8. // Module: FileChannel
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/ArchiveStrategy.h"
  16. #include "Poco/NumberFormatter.h"
  17. #include "Poco/File.h"
  18. #include "Poco/Path.h"
  19. #include "Poco/DeflatingStream.h"
  20. #include "Poco/StreamCopier.h"
  21. #include "Poco/Exception.h"
  22. #include "Poco/ActiveDispatcher.h"
  23. #include "Poco/ActiveMethod.h"
  24. #include "Poco/Void.h"
  25. #include "Poco/FileStream.h"
  26. namespace Poco {
  27. //
  28. // ArchiveCompressor
  29. //
  30. class ArchiveCompressor: public ActiveDispatcher
  31. {
  32. public:
  33. ArchiveCompressor():
  34. compress(this, &ArchiveCompressor::compressImpl)
  35. {
  36. }
  37. ~ArchiveCompressor()
  38. {
  39. }
  40. ActiveMethod<Void, std::string, ArchiveCompressor, ActiveStarter<ActiveDispatcher> > compress;
  41. protected:
  42. Void compressImpl(const std::string& path)
  43. {
  44. std::string gzPath(path);
  45. gzPath.append(".gz");
  46. FileInputStream istr(path, std::ios::binary | std::ios::in);
  47. if (!istr.good()) throw OpenFileException(path);
  48. FileOutputStream ostr(gzPath, std::ios::binary | std::ios::out);
  49. if (ostr.good())
  50. {
  51. DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
  52. StreamCopier::copyStream(istr, deflater);
  53. deflater.close();
  54. ostr.close();
  55. istr.close();
  56. File f(path);
  57. f.remove();
  58. }
  59. else throw CreateFileException(gzPath);
  60. return Void();
  61. }
  62. };
  63. //
  64. // ArchiveStrategy
  65. //
  66. ArchiveStrategy::ArchiveStrategy():
  67. _compress(false),
  68. _pCompressor(0)
  69. {
  70. }
  71. ArchiveStrategy::~ArchiveStrategy()
  72. {
  73. delete _pCompressor;
  74. }
  75. void ArchiveStrategy::compress(bool flag)
  76. {
  77. _compress = flag;
  78. }
  79. void ArchiveStrategy::moveFile(const std::string& oldPath, const std::string& newPath)
  80. {
  81. bool compressed = false;
  82. Path p(oldPath);
  83. File f(oldPath);
  84. if (!f.exists())
  85. {
  86. f = oldPath + ".gz";
  87. compressed = true;
  88. }
  89. std::string mvPath(newPath);
  90. if (_compress || compressed)
  91. mvPath.append(".gz");
  92. if (!_compress || compressed)
  93. {
  94. f.renameTo(mvPath);
  95. }
  96. else
  97. {
  98. f.renameTo(newPath);
  99. if (!_pCompressor) _pCompressor = new ArchiveCompressor;
  100. _pCompressor->compress(newPath);
  101. }
  102. }
  103. bool ArchiveStrategy::exists(const std::string& name)
  104. {
  105. File f(name);
  106. if (f.exists())
  107. {
  108. return true;
  109. }
  110. else if (_compress)
  111. {
  112. std::string gzName(name);
  113. gzName.append(".gz");
  114. File gzf(gzName);
  115. return gzf.exists();
  116. }
  117. else return false;
  118. }
  119. //
  120. // ArchiveByNumberStrategy
  121. //
  122. ArchiveByNumberStrategy::ArchiveByNumberStrategy()
  123. {
  124. }
  125. ArchiveByNumberStrategy::~ArchiveByNumberStrategy()
  126. {
  127. }
  128. LogFile* ArchiveByNumberStrategy::archive(LogFile* pFile)
  129. {
  130. std::string basePath = pFile->path();
  131. delete pFile;
  132. int n = -1;
  133. std::string path;
  134. do
  135. {
  136. path = basePath;
  137. path.append(".");
  138. NumberFormatter::append(path, ++n);
  139. }
  140. while (exists(path));
  141. while (n >= 0)
  142. {
  143. std::string oldPath = basePath;
  144. if (n > 0)
  145. {
  146. oldPath.append(".");
  147. NumberFormatter::append(oldPath, n - 1);
  148. }
  149. std::string newPath = basePath;
  150. newPath.append(".");
  151. NumberFormatter::append(newPath, n);
  152. moveFile(oldPath, newPath);
  153. --n;
  154. }
  155. return new LogFile(basePath);
  156. }
  157. } // namespace Poco