source: XMLIO_V2/external/src/POCO/Foundation/ArchiveStrategy.cpp @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1//
2// ArchiveStrategy.cpp
3//
4// $Id: //poco/1.3/Foundation/src/ArchiveStrategy.cpp#4 $
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// 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:
19//
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.
26//
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.
34//
35
36
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>
47#include <Poco/FileStream.h>
48
49
50namespace Poco {
51
52
53//
54// ArchiveCompressor
55//
56
57
58class ArchiveCompressor: public ActiveDispatcher
59{
60public:
61        ArchiveCompressor():
62                compress(this, &ArchiveCompressor::compressImpl)
63        {
64        }
65       
66        ~ArchiveCompressor()
67        {
68        }
69       
70        ActiveMethod<Void, std::string, ArchiveCompressor, ActiveStarter<ActiveDispatcher> > compress;
71
72protected:
73        Void compressImpl(const std::string& path)
74        {
75                std::string gzPath(path);
76                gzPath.append(".gz");
77                FileInputStream istr(path, std::ios::binary | std::ios::in);
78                if (!istr.good()) throw OpenFileException(path);
79                FileOutputStream ostr(gzPath, std::ios::binary | std::ios::out);
80                if (ostr.good())
81                {
82                        DeflatingOutputStream deflater(ostr, DeflatingStreamBuf::STREAM_GZIP);
83                        StreamCopier::copyStream(istr, deflater);
84                        deflater.close();
85                        ostr.close();
86                        istr.close();
87                        File f(path);
88                        f.remove();
89                }
90                else throw CreateFileException(gzPath);
91                return Void();
92        }
93};
94
95
96//
97// ArchiveStrategy
98//
99
100
101ArchiveStrategy::ArchiveStrategy(): 
102        _compress(false),
103        _pCompressor(0)
104{
105}
106
107
108ArchiveStrategy::~ArchiveStrategy()
109{
110        delete _pCompressor;
111}
112
113
114void ArchiveStrategy::compress(bool flag)
115{
116        _compress = flag;
117}
118
119
120void ArchiveStrategy::moveFile(const std::string& oldPath, const std::string& newPath)
121{
122        bool compressed = false;
123        Path p(oldPath);
124        File f(oldPath);
125        if (!f.exists())
126        {
127                f = oldPath + ".gz";
128                compressed = true;
129        }
130        std::string mvPath(newPath);
131        if (_compress || compressed)
132                mvPath.append(".gz");
133        if (!_compress || compressed)
134        {
135                f.renameTo(mvPath);
136        }
137        else
138        {
139                f.renameTo(newPath);
140                if (!_pCompressor) _pCompressor = new ArchiveCompressor;
141                _pCompressor->compress(newPath);
142        }
143}
144
145
146bool ArchiveStrategy::exists(const std::string& name)
147{
148        File f(name);
149        if (f.exists())
150        {
151                return true;
152        }
153        else if (_compress)
154        {
155                std::string gzName(name);
156                gzName.append(".gz");
157                File gzf(gzName);
158                return gzf.exists();
159        }
160        else return false;
161}
162
163
164//
165// ArchiveByNumberStrategy
166//
167
168
169ArchiveByNumberStrategy::ArchiveByNumberStrategy()
170{
171}
172
173
174ArchiveByNumberStrategy::~ArchiveByNumberStrategy()
175{
176}
177
178
179LogFile* ArchiveByNumberStrategy::archive(LogFile* pFile)
180{
181        std::string basePath = pFile->path();
182        delete pFile;
183        int n = -1;
184        std::string path;
185        do
186        {
187                path = basePath;
188                path.append(".");
189                NumberFormatter::append(path, ++n);
190        }
191        while (exists(path));
192       
193        while (n >= 0)
194        {
195                std::string oldPath = basePath;
196                if (n > 0)
197                {
198                        oldPath.append(".");
199                        NumberFormatter::append(oldPath, n - 1);
200                }
201                std::string newPath = basePath;
202                newPath.append(".");
203                NumberFormatter::append(newPath, n);
204                moveFile(oldPath, newPath);
205                --n;
206        }
207        return new LogFile(basePath);
208}
209
210
211} // namespace Poco
Note: See TracBrowser for help on using the repository browser.