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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
#include "../common/presenterCache.h"
#include "../common/exceptions.h"
#include "../common/options.h"
#include <safeMapFind.h>
#include "../common/environment.h"
#include <fcntl.h>
#include <attr/xattr.h>
#include <sys/stat.h>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/bind.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <glibmm/convert.h>
#include <sys/file.h>
#include <gcrypt.h>
#include <iomanip>
SimpleSysCallException(OpenCacheFile);
SimpleSysCallException(StatCacheFile);
SimpleSysCallException(LockCacheFile);
SimpleSysCallException(TruncateCacheFile);
SimpleSysCallException(WriteCacheFile);
SimpleSysCallException(CacheFileXAttr);
template <class E>
int safesys(int n, int r) {
if (r == n) {
throw E(errno);
}
return r;
}
class FilePresenterCache : public PresenterCache {
public:
typedef std::vector<std::string> Path;
typedef std::map<std::string, VariableType> Parameters;
typedef boost::tuple<Path, Parameters> Key;
class CacheFile : public IntrusivePtrBase {
public:
CacheFile(int f) : fd(f) {
}
~CacheFile() {
close(fd);
}
operator int() const { return fd; }
const int fd;
};
typedef boost::intrusive_ptr<CacheFile> CacheFilePtr;
typedef std::map<Key, CacheFilePtr> OpenCaches;
class WriteCacheFile : public boost::iostreams::sink {
public:
WriteCacheFile(int f) : fd(f) {
gcry_md_open(&state, GCRY_MD_SHA1, 0);
}
WriteCacheFile(const WriteCacheFile & wcf) : fd(dup(wcf.fd)) {
gcry_md_copy(&state, wcf.state);
}
~WriteCacheFile() {
gcry_md_close(state);
::close(fd);
}
std::streamsize write(const char * data, std::streamsize len)
{
gcry_md_write(state, data, len);
return safesys<WriteCacheFile>(-1, ::write(fd, data, len));
}
std::string hash() const
{
int hash_len = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
unsigned char * hash = gcry_md_read(state, GCRY_MD_SHA1);
std::stringstream hashstr;
hashstr << std::hex << std::setfill('0');
for (int i = 0; i < hash_len; i++) {
hashstr << std::setw(2) << static_cast<unsigned>(hash[i]);
}
return hashstr.str();
}
const int fd;
private:
gcry_md_hd_t state;
};
typedef boost::iostreams::stream<WriteCacheFile> WriteCacheStrm;
typedef boost::shared_ptr<WriteCacheStrm> WriteCacheStrmPtr;
FilePresenterCache(ScriptNodePtr s) :
PresenterCache(s),
idProvider(VariableLoader::createNew(Provider, s)) {
}
~FilePresenterCache()
{
}
bool check(time_t scriptMtime) const
{
Key key = getCacheKey();
try {
CacheFilePtr f = defaultMapFind(openCaches, key);
if (!f) {
f = openCacheFile(key);
}
struct stat st;
safesys<StatCacheFile>(-1, fstat(*f, &st));
if (st.st_nlink == 0) {
f = openCacheFile(key);
safesys<StatCacheFile>(-1, fstat(*f, &st));
}
if (scriptMtime != 0) {
if ((st.st_mtime < (time(NULL) - FilePresenterCache::CacheLife)) || (st.st_mtime < scriptMtime)) {
unlink(getCacheFile().string().c_str());
openCaches.erase(key);
return false;
}
}
myCache = f;
return true;
}
catch (...) {
openCaches.erase(key);
myCache.reset();
return false;
}
}
Glib::ustring getEncoding() const
{
return getXAttr("user.encoding");
}
Glib::ustring getContentType() const
{
return getXAttr("user.content-type");
}
time_t getModifiedTime() const
{
struct stat st;
safesys<CacheFileXAttr>(-1, fstat(*myCache, &st));
return st.st_mtime;
}
Glib::ustring getXAttr(const char * attr) const
{
char buf[BUFSIZ];
buf[safesys<CacheFileXAttr>(-1, fgetxattr(*myCache, attr, buf, sizeof(buf)))] = 0;
return buf;
}
size_t getSizeInBytes() const
{
struct stat st;
safesys<CacheFileXAttr>(-1, fstat(*myCache, &st));
return st.st_size;
}
void writeTo(std::ostream & o) const
{
safesys<LockCacheFile>(-1, ::flock(*myCache, LOCK_SH));
boost::iostreams::stream<boost::iostreams::file_descriptor_source> cache(*myCache, boost::iostreams::never_close_handle);
cache.seekg(0);
o << cache.rdbuf();
safesys<LockCacheFile>(-1, ::flock(*myCache, LOCK_UN));
}
operator const StaticContent * () const {
return this;
}
std::ostream & writeCache(const std::string & ct, const std::string & enc)
{
int fd = safesys<OpenCacheFile>(-1, open(getCacheFile().string().c_str(),
O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
safesys<LockCacheFile>(-1, ::flock(fd, LOCK_EX));
safesys<TruncateCacheFile>(-1, ::ftruncate(fd, 0));
safesys<CacheFileXAttr>(-1, fsetxattr(fd, "user.content-type", ct.c_str(), ct.length(), 0));
safesys<CacheFileXAttr>(-1, fsetxattr(fd, "user.encoding", enc.c_str(), enc.length(), 0));
writecache = WriteCacheStrmPtr(new boost::iostreams::stream<WriteCacheFile>(fd));
return *writecache;
}
void flushCache()
{
std::string hash((*writecache)->hash());
safesys<CacheFileXAttr>(-1, fsetxattr((*writecache)->fd, "user.sha1", hash.c_str(), hash.length(), 0));
writecache.reset();
}
std::string getSHA1() const
{
return getXAttr("user.sha1");
}
WriteCacheStrmPtr writecache;
INITOPTIONS;
private:
Key getCacheKey() const
{
Key key;
key.get<0>().push_back(idProvider->value());
return key;
}
CacheFilePtr openCacheFile(const Key & key) const
{
CacheFilePtr c = new CacheFile(safesys<OpenCacheFile>(-1, open(getCacheFile().string().c_str(), O_RDONLY)));
openCaches[key] = c;
return c;
}
boost::filesystem::path getCacheFile() const
{
boost::filesystem::path cache;
cache = Store;
cache /= idProvider->value();
boost::filesystem::create_directories(cache);
cache /= FileName;
return cache;
}
static void appendParams(Parameters * cache, const std::string & n, const VariableType & v)
{
cache->insert(Parameters::value_type(n, v));
}
static void appendPath(boost::filesystem::path * cache, const std::string & n, const VariableType & v)
{
*cache /= n;
*cache /= v.operator const std::string &();
}
static OpenCaches openCaches;
mutable CacheFilePtr myCache;
Variable::VariableImplPtr idProvider;
// Config
static boost::filesystem::path Store;
static std::string FileName;
static std::string Provider;
static time_t CacheLife;
friend class FilePresenterCacheLoader;
};
FilePresenterCache::OpenCaches FilePresenterCache::openCaches;
boost::filesystem::path FilePresenterCache::Store;
std::string FilePresenterCache::FileName;
std::string FilePresenterCache::Provider;
time_t FilePresenterCache::CacheLife;
DECLARE_OPTIONS(FilePresenterCache, "File Presenter Cache options")
("pcache.file.store", Options::value(&FilePresenterCache::Store, "/tmp/project2.pcache"),
"The root folder of the cache storage area")
("pcache.file.filename", Options::value(&FilePresenterCache::FileName, "cache"),
"The filename to store the data in")
("pcache.file.life", Options::value(&FilePresenterCache::CacheLife, 3600),
"The age of cache entries after which they are removed (seconds)")
("pcache.file.idprovider", Options::value(&FilePresenterCache::Provider, "requestid"),
"The name of the component used to provide a unique request ID")
END_OPTIONS(FilePresenterCache)
class FilePresenterCacheLoader : public ElementLoader::For<FilePresenterCache> {
public:
void onIdle()
{
emptyDir(FilePresenterCache::Store);
FilePresenterCache::openCaches.clear();
}
void onConfigLoad()
{
FilePresenterCache::openCaches.clear();
}
bool emptyDir(const boost::filesystem::path & dir)
{
bool files = false;
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator itr(dir); itr != end; ++itr) {
struct stat st;
stat(itr->path().string().c_str(), &st);
if (S_ISDIR(st.st_mode)) {
if (emptyDir(*itr)) {
files = true;
}
else {
boost::filesystem::remove(*itr);
}
}
else {
if (st.st_mtime > time(NULL) - FilePresenterCache::CacheLife) {
files = true;
}
else {
boost::filesystem::remove(*itr);
}
}
}
return files;
}
};
DECLARE_CUSTOM_LOADER("filecache", FilePresenterCacheLoader);
|