4
// $Id: //poco/1.2/Foundation/src/ArchiveStrategy.cpp#2 $
10
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
13
// Permission is hereby granted, free of charge, to any person or organization
14
// obtaining a copy of the software and accompanying documentation covered by
15
// this license (the "Software") to use, reproduce, display, distribute,
16
// execute, and transmit the Software, and to prepare derivative works of the
17
// Software, and to permit third-parties to whom the Software is furnished to
18
// do so, all subject to the following:
20
// The copyright notices in the Software and this entire statement, including
21
// the above license grant, this restriction and the following disclaimer,
22
// must be included in all copies of the Software, in whole or in part, and
23
// all derivative works of the Software, unless such copies or derivative
24
// works are solely in the form of machine-executable object code generated by
25
// a source language processor.
27
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
30
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
31
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
32
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33
// DEALINGS IN THE SOFTWARE.
37
#include "Poco/ArchiveStrategy.h"
38
#include "Poco/NumberFormatter.h"
39
#include "Poco/File.h"
40
#include "Poco/Path.h"
41
#include "Poco/DeflatingStream.h"
42
#include "Poco/StreamCopier.h"
43
#include "Poco/Exception.h"
44
#include "Poco/ActiveDispatcher.h"
45
#include "Poco/ActiveMethod.h"
46
#include "Poco/Void.h"
58
class ArchiveCompressor: public ActiveDispatcher
62
compress(this, &ArchiveCompressor::compressImpl)
70
ActiveMethod<Void, std::string, ArchiveCompressor, ActiveStarter<ActiveDispatcher> > compress;
73
Void compressImpl(const std::string& path)
75
std::string gzPath(path);
77
std::ifstream istr(Path::transcode(path).c_str(), std::ios::binary | std::ios::in);
78
if (!istr.good()) throw OpenFileException(path);
79
std::ofstream ostr(Path::transcode(gzPath).c_str(), std::ios::binary | std::ios::out);
82
DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
83
StreamCopier::copyStream(istr, deflater);
90
else throw CreateFileException(gzPath);
101
ArchiveStrategy::ArchiveStrategy():
108
ArchiveStrategy::~ArchiveStrategy()
114
void ArchiveStrategy::compress(bool flag)
120
void ArchiveStrategy::moveFile(const std::string& oldPath, const std::string& newPath)
122
bool compressed = false;
130
std::string mvPath(newPath);
131
if (_compress || compressed)
132
mvPath.append(".gz");
133
if (!_compress || compressed)
140
if (!_pCompressor) _pCompressor = new ArchiveCompressor;
141
_pCompressor->compress(newPath);
146
bool ArchiveStrategy::exists(const std::string& name)
155
std::string gzName(name);
156
gzName.append(".gz");
165
// ArchiveByNumberStrategy
169
ArchiveByNumberStrategy::ArchiveByNumberStrategy()
174
ArchiveByNumberStrategy::~ArchiveByNumberStrategy()
179
LogFile* ArchiveByNumberStrategy::archive(LogFile* pFile)
181
std::string basePath = pFile->path();
189
path.append(NumberFormatter::format(++n));
191
while (exists(path));
195
std::string oldPath = basePath;
199
oldPath.append(NumberFormatter::format(n - 1));
201
std::string newPath = basePath;
203
newPath.append(NumberFormatter::format(n));
204
moveFile(oldPath, newPath);
207
return new LogFile(basePath);