summaryrefslogtreecommitdiff
path: root/icespider/fileSessions/fileSessions.cpp
blob: da9fdea581feda210328877521a8b83cafcb4a1c (plain)
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
#include <Ice/Communicator.h>
#include <Ice/Config.h>
#include <Ice/Current.h>
#include <Ice/InputStream.h>
#include <Ice/OutputStream.h>
#include <Ice/Properties.h>
#include <Ice/PropertiesF.h>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <cerrno>
#include <core.h>
#include <cstring>
#include <ctime>
#include <exception>
#include <factory.impl.h>
#include <fileUtils.h>
#include <memory>
#include <session.h>
#include <string>
#include <string_view>
#include <sys.h>
#include <sys/file.h>
#include <unistd.h>
#include <utility>

namespace IceSpider {
	class FileSessions : public Plugin, public SessionManager {
	public:
		FileSessions(Ice::CommunicatorPtr com, const Ice::PropertiesPtr & props) :
			ic(std::move(com)), root(props->getProperty("IceSpider.FileSessions.Path")),
			duration(static_cast<Ice::Short>(
					props->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 (...) { // NOLINT(bugprone-empty-catch) - Meh :)
			}
		}

		void operator=(const FileSessions &) = delete;
		void operator=(FileSessions &&) = delete;

		SessionPtr
		createSession(const ::Ice::Current &) override
		{
			auto session = std::make_shared<Session>();
			// NOLINTNEXTLINE(clang-analyzer-optin.cplusplus.VirtualCall)
			session->id = boost::lexical_cast<std::string>(boost::uuids::random_generator()());
			session->duration = duration;
			save(session);
			return session;
		}

		SessionPtr
		getSession(const ::std::string sessionId, const ::Ice::Current & current) override
		{
			auto session = load(sessionId);
			if (session && isExpired(session)) {
				destroySession(sessionId, current);
				return nullptr;
			}
			return session;
		}

		void
		updateSession(const SessionPtr session, const ::Ice::Current &) override
		{
			save(session);
		}

		void
		destroySession(const ::std::string sessionId, const ::Ice::Current &) override
		{
			try {
				std::filesystem::remove(root / sessionId);
			}
			catch (const std::exception & e) {
				throw SessionError(e.what());
			}
		}

	private:
		void
		save(const SessionPtr & session)
		{
			session->lastUsed = time(nullptr);
			Ice::OutputStream buf(ic);
			buf.write(session);
			const auto range = buf.finished();
			// NOLINTNEXTLINE(hicpp-signed-bitwise)
			AdHoc::FileUtils::FileHandle sessionFile(root / session->id, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
			sysassert(flock(sessionFile.fh, LOCK_EX), -1);
			sysassert(pwrite(sessionFile.fh, range.first, static_cast<size_t>(range.second - range.first), 0), -1);
			sysassert(ftruncate(sessionFile.fh, range.second - range.first), -1);
			sysassert(flock(sessionFile.fh, LOCK_UN), -1);
		}

		SessionPtr
		load(const std::string & sessionId)
		{
			auto path = root / sessionId;
			if (!std::filesystem::exists(path)) {
				return nullptr;
			}
			try {
				AdHoc::FileUtils::MemMap sessionFile(path);
				sysassert(flock(sessionFile.fh, LOCK_SH), -1);
				auto fbuf = sessionFile.sv<Ice::Byte>();
				Ice::InputStream buf(ic, std::make_pair(fbuf.begin(), fbuf.end()));
				SessionPtr session;
				buf.read(session);
				sysassert(flock(sessionFile.fh, LOCK_UN), -1);
				return session;
			}
			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 dirIter(root);
			while (dirIter != std::filesystem::directory_iterator()) {
				auto session = load(dirIter->path());
				if (session && isExpired(session)) {
					FileSessions::destroySession(session->id, Ice::Current());
				}
				dirIter++;
			}
		}

		[[nodiscard]]
		static bool
		isExpired(const SessionPtr & session)
		{
			return (session->lastUsed + session->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::Short duration;
	};
}

NAMEDFACTORY("IceSpider-FileSessions", IceSpider::FileSessions, IceSpider::PluginFactory);