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
|
#include <core.h>
#include <session.h>
#include <fcntl.h>
#include <fileUtils.h>
#include <sys.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <factory.impl.h>
#include <filesystem>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <Ice/OutputStream.h>
#include <Ice/InputStream.h>
#include "Ice/Initialize.h"
namespace IceSpider {
class FileSessions : public Plugin, public SessionManager {
public:
FileSessions(Ice::CommunicatorPtr c, const Ice::PropertiesPtr & p) :
ic(std::move(c)),
root(p->getProperty("IceSpider.FileSessions.Path")),
duration(p->getPropertyAsIntWithDefault("IceSpider.FileSessions.Duration", 3600))
{
if (!root.empty() && !std::filesystem::exists(root)) {
std::filesystem::create_directories(root);
}
}
FileSessions(const FileSessions &) = delete;
FileSessions(FileSessions &&) = delete;
~FileSessions() override
{
try {
removeExpired();
}
catch (...) {
// Meh :)
}
}
void operator=(const FileSessions &) = delete;
void operator=(FileSessions &&) = delete;
SessionPtr createSession(const ::Ice::Current &) override
{
auto s = std::make_shared<Session>();
// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
s->id = boost::lexical_cast<std::string>(boost::uuids::random_generator()());
s->duration = duration;
save(s);
return s;
}
SessionPtr getSession(const ::std::string id, const ::Ice::Current & current) override
{
auto s = load(id);
if (s && isExpired(s)) {
destroySession(id, current);
return nullptr;
}
return s;
}
void updateSession(const SessionPtr s, const ::Ice::Current &) override
{
save(s);
}
void destroySession(const ::std::string id, const ::Ice::Current &) override
{
try {
std::filesystem::remove(root / id);
}
catch (const std::exception & e) {
throw SessionError(e.what());
}
}
private:
void save(const SessionPtr & s)
{
s->lastUsed = time(nullptr);
Ice::OutputStream buf(ic);
buf.write(s);
auto range = buf.finished();
// NOLINTNEXTLINE(hicpp-signed-bitwise)
AdHoc::FileUtils::FileHandle f(root / s->id, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
sysassert(flock(f.fh, LOCK_EX), -1);
sysassert(pwrite(f.fh, range.first, range.second - range.first, 0), -1);
sysassert(ftruncate(f.fh, range.second - range.first), -1);
sysassert(flock(f.fh, LOCK_UN), -1);
}
SessionPtr load(const std::string & id)
{
auto path = root / id;
if (!std::filesystem::exists(path)) {
return nullptr;
}
try {
AdHoc::FileUtils::MemMap f(path);
sysassert(flock(f.fh, LOCK_SH), -1);
auto fbuf = (Ice::Byte *)f.data;
Ice::InputStream buf(ic, std::make_pair(fbuf, fbuf + f.getStat().st_size));
SessionPtr s;
buf.read(s);
sysassert(flock(f.fh, LOCK_UN), -1);
return s;
}
catch (const AdHoc::SystemException & e) {
if (e.errNo == ENOENT) {
return nullptr;
}
throw;
}
}
void removeExpired()
{
if (root.empty() || !std::filesystem::exists(root)) {
return;
}
std::filesystem::directory_iterator di(root);
while (di != std::filesystem::directory_iterator()) {
auto s = load(di->path());
if (s && isExpired(s)) {
FileSessions::destroySession(s->id, Ice::Current());
}
di++;
}
}
bool isExpired(const SessionPtr & s)
{
return (s->lastUsed + s->duration < time(nullptr));
}
template<typename R, typename ER>
R sysassert(R rtn, ER ertn)
{
if (rtn == ertn) {
throw SessionError(strerror(errno));
}
return rtn;
}
Ice::CommunicatorPtr ic;
const std::filesystem::path root;
const Ice::Int duration;
};
}
NAMEDFACTORY("IceSpider-FileSessions", IceSpider::FileSessions, IceSpider::PluginFactory);
|