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