Menu

[r2332]: / framework / trunk / src / session_win32_file_storage.cpp  Maximize  Restore  History

Download this file

278 lines (241 with data), 6.5 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
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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#define CPPCMS_SOURCE
#define NOMINMAX
#include "session_win32_file_storage.h"
#include <cppcms/cppcms_error.h>
#include <cppcms/config.h>
#include <booster/nowide/convert.h>
#include <booster/nowide/cstdio.h>
#include "crc32.h"
#include <memory>
#include <time.h>
#include <sstream>
#include <cppcms/cstdint.h>
#include <windows.h>
namespace cppcms {
namespace sessions {
struct session_file_storage::_data {};
session_file_storage::session_file_storage(std::string path)
{
if(path.empty()){
if(::getenv("TEMP"))
path_=std::string(::getenv("TEMP")) + "/cppcms_sessions";
else if(::getenv("TMP"))
path_=std::string(::getenv("TMP")) + "/cppcms_sessions";
else
path_ = "C:/TEMP";
}
else
path_=path;
if(!::CreateDirectory(path_.c_str(),NULL)) {
if(GetLastError()!=ERROR_ALREADY_EXISTS) {
throw cppcms_error("Failed to create a directory for session storage " + path_);
}
}
}
session_file_storage::~session_file_storage()
{
}
std::string session_file_storage::file_name(std::string const &sid)
{
return path_ + "/" + sid;
}
class session_file_storage::locked_file {
public:
locked_file(session_file_storage *object,std::string sid) :
h_(INVALID_HANDLE_VALUE)
{
name_=object->file_name(sid);
int sleep_time=0;
for(;;) {
h_=::CreateFileW(booster::nowide::convert(name_).c_str(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(h_==INVALID_HANDLE_VALUE) {
if(GetLastError()==ERROR_ACCESS_DENIED && sleep_time<1000 ) {
::Sleep(sleep_time);
sleep_time = sleep_time == 0 ? 1 : sleep_time * 2;
continue;
}
else {
std::ostringstream tmp;
tmp << "Failed to open file:" + name_ + "error code " <<std::hex << ::GetLastError();
throw cppcms_error(tmp.str());
}
}
else
break;
}
OVERLAPPED ov = OVERLAPPED();
if(!::LockFileEx(h_,LOCKFILE_EXCLUSIVE_LOCK,0,0,16,&ov)) {
::CloseHandle(h_);
h_=INVALID_HANDLE_VALUE;
throw cppcms_error("Failed to lock file:"+name_);
}
}
~locked_file()
{
if(h_==INVALID_HANDLE_VALUE)
return;
OVERLAPPED ov = OVERLAPPED();
::UnlockFileEx(h_,0,0,16,&ov);
::CloseHandle(h_);
}
HANDLE handle() { return h_; }
std::string name() { return name_; }
private:
HANDLE h_;
std::string name_;
};
void session_file_storage::save(std::string const &sid,time_t timeout,std::string const &in)
{
locked_file file(this,sid);
save_to_file(file.handle(),timeout,in);
}
bool session_file_storage::load(std::string const &sid,time_t &timeout,std::string &out)
{
locked_file file(this,sid);
if(!read_from_file(file.handle(),timeout,out)) {
::DeleteFile(file.name().c_str());
return false;
}
return true;
}
void session_file_storage::remove(std::string const &sid)
{
locked_file file(this,sid);
::DeleteFile(file.name().c_str());
}
bool session_file_storage::is_blocking()
{
return true;
}
bool session_file_storage::read_timestamp(HANDLE h)
{
int64_t stamp;
if(!read_all(h,&stamp,sizeof(stamp)) || stamp < ::time(0))
return false;
return true;
}
bool session_file_storage::read_from_file(HANDLE h,time_t &timeout,std::string &data)
{
int64_t f_timeout;
uint32_t crc;
uint32_t size;
if(!read_all(h,&f_timeout,sizeof(f_timeout)))
return false;
if(f_timeout < time(0))
return false;
if(!read_all(h,&crc,sizeof(crc)) || !read_all(h,&size,sizeof(size)))
return false;
std::vector<char> buffer(size,0);
impl::crc32_calc crc_calc;
if(size > 0) {
if(!read_all(h,&buffer.front(),size))
return false;
crc_calc.process_bytes(&buffer.front(),size);
}
uint32_t real_crc=crc_calc.checksum();
if(crc != real_crc)
return false;
timeout=f_timeout;
if(size > 0)
data.assign(&buffer.front(),size);
else
data.clear();
return true;
}
void session_file_storage::save_to_file(HANDLE h,time_t timeout,std::string const &in)
{
struct {
int64_t timeout;
uint32_t crc;
uint32_t size;
} tmp = { timeout, 0, in.size() };
impl::crc32_calc crc_calc;
crc_calc.process_bytes(in.data(),in.size());
tmp.crc=crc_calc.checksum();
if(!write_all(h,&tmp,sizeof(tmp)) || !write_all(h,in.data(),in.size()))
throw cppcms_error("Failed to write to file");
}
bool session_file_storage::write_all(HANDLE h,void const *vbuf,int n)
{
DWORD written;
if(!::WriteFile(h,vbuf,n,&written,NULL) || written!=unsigned(n))
return false;
return true;
}
bool session_file_storage::read_all(HANDLE h,void *vbuf,int n)
{
DWORD read;
if(!::ReadFile(h,vbuf,n,&read,NULL) || read!=unsigned(n))
return false;
return true;
}
void session_file_storage::gc()
{
std::auto_ptr<_WIN32_FIND_DATAW> entry(new _WIN32_FIND_DATAW);
HANDLE d=INVALID_HANDLE_VALUE;
std::string search_path = path_ + "/*";
try{
if((d=::FindFirstFileW(booster::nowide::convert(search_path).c_str(),entry.get()))==INVALID_HANDLE_VALUE) {
if(GetLastError() == ERROR_FILE_NOT_FOUND)
return;
throw cppcms_error("Failed to open directory :"+path_);
}
do {
if(entry->cFileName[32]!=0)
continue;
int i;
std::string filename=booster::nowide::convert(entry->cFileName);
for(i=0;i<32;i++) {
if(!isxdigit(filename[i]))
break;
}
if(i!=32)
continue;
{
locked_file file(this,filename);
if(!read_timestamp(file.handle()))
::DeleteFileW(booster::nowide::convert(file.name()).c_str());
}
} while(::FindNextFileW(d,entry.get()));
::FindClose(d);
}
catch(...) {
if(d!=INVALID_HANDLE_VALUE) ::FindClose(d);
throw;
}
}
struct session_file_storage_factory::_data {};
session_file_storage_factory::session_file_storage_factory(std::string path) :
storage_(new session_file_storage(path))
{
}
session_file_storage_factory::~session_file_storage_factory()
{
}
booster::shared_ptr<session_storage> session_file_storage_factory::get()
{
return storage_;
}
bool session_file_storage_factory::requires_gc()
{
return true;
}
void session_file_storage_factory::gc_job()
{
storage_->gc();
}
} // sessions
} // cppcms
MongoDB Logo MongoDB