forked from uroni/urbackup_backend
/
PersistentOpenFiles.cpp
292 lines (229 loc) · 6.36 KB
/
PersistentOpenFiles.cpp
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
290
291
292
#include "PersistentOpenFiles.h"
#include "../urbackupcommon/os_functions.h"
#include "../stringtools.h"
#include "file_permissions.h"
const char* persistent_open_files_fn = "urbackup\\open_files.dat";
const char* persistent_open_files_new_fn = "urbackup\\open_files.dat.new";
bool PersistentOpenFiles::cycle()
{
Server->destroy(persistf);
persistf = Server->openFile(persistent_open_files_new_fn, MODE_WRITE);
#ifndef _DEBUG
change_file_permissions_admin_only(persistent_open_files_new_fn);
#endif
if(!persistf) return false;
bytes_deleted = 0;
bytes_written = 0;
for(std::map<std::wstring, unsigned int>::iterator it=open_files.begin();it!=open_files.end();++it)
{
addf(it->first, it->second);
}
flushf_int(false);
Server->destroy(persistf);
if(!os_rename_file(widen(persistent_open_files_new_fn), widen(persistent_open_files_fn)))
{
return false;
}
persistf = Server->openFile(widen(persistent_open_files_fn), MODE_RW);
if(!persistf) return false;
return true;
}
bool PersistentOpenFiles::flushf_int(bool allow_cycle)
{
if(!persistf) return false;
if(wdata.getDataSize()==0) return true;
if(bytes_deleted>bytes_written*2 && allow_cycle)
{
wdata.clear();
return cycle();
}
_u32 w = persistf->Write(wdata.getDataPtr(), wdata.getDataSize());
if(w!=wdata.getDataSize())
{
Server->Log("Error persisting open files to disk", LL_ERROR);
wdata.clear();
return false;
}
wdata.clear();
if(persistf->Size()>20*1024*1024 && allow_cycle)
{
return cycle();
}
return true;
}
bool PersistentOpenFiles::flushf()
{
return flushf_int(true);
}
void PersistentOpenFiles::removef( unsigned int id, size_t fn_size )
{
wdata.addChar(PERSIST_REMOVE);
wdata.addInt64(Server->getTimeMS());
wdata.addUInt(id);
bytes_deleted+=1+sizeof(int64)+sizeof(id);
bytes_deleted+=1+sizeof(int64)+sizeof(_u32)+fn_size+sizeof(id);
}
void PersistentOpenFiles::addf( const std::wstring& fn, unsigned int id )
{
if(!persistf) return;
wdata.addChar(PERSIST_ADD);
wdata.addInt64(Server->getTimeMS());
wdata.addString(Server->ConvertToUTF8(fn));
wdata.addUInt(id);
bytes_written+=1+sizeof(int64)+sizeof(_u32)+fn.size()+sizeof(id);
}
bool PersistentOpenFiles::load()
{
if(!persistf)
{
Server->Log("No persisted open files list", LL_INFO);
return false;
}
bytes_written=0;
bytes_deleted=0;
std::vector<char> data;
data.resize(persistf->Size());
size_t read=0;
while(read<data.size())
{
read+=persistf->Read(data.data()+read, static_cast<_u32>(data.size()-read));
}
CRData pdata(data.data(), data.size());
std::map<std::wstring, unsigned int> new_open_files;
int64 max_time=0;
int64 ctime = Server->getTimeMS();
char id;
while(pdata.getChar(&id))
{
if(id==PERSIST_ADD)
{
int64 rec_time;
if(!pdata.getInt64(&rec_time))
{
Server->Log("Error reading rec_time from persisted open files", LL_ERROR);
return false;
}
std::string fn;
if(!pdata.getStr(&fn))
{
Server->Log("Error reading fn from persisted open files", LL_ERROR);
return false;
}
unsigned int id;
if(!pdata.getUInt(&id))
{
Server->Log("Error reading id from persisted open files", LL_ERROR);
return false;
}
new_open_files[Server->ConvertToUnicode(fn)]=id;
curr_id=(std::max)(curr_id, id);
max_time=(std::max)(max_time, rec_time);
if(max_time>ctime)
{
Server->Log("Persisted open files list is obsolete (restart occured) (1)", LL_INFO);
return false;
}
bytes_written+=1+sizeof(int64)+sizeof(_u32)+fn.size()+sizeof(id);
}
else if(id==PERSIST_REMOVE)
{
int64 rec_time;
if(!pdata.getInt64(&rec_time))
{
Server->Log("Error reading rec_time from persisted open files (remove)", LL_ERROR);
return false;
}
unsigned int id;
if(!pdata.getUInt(&id))
{
Server->Log("Error reading id from persisted open files (remove)", LL_ERROR);
return false;
}
bool found=false;
for(std::map<std::wstring, unsigned int>::iterator it=new_open_files.begin();it!=new_open_files.end();++it)
{
if(it->second==id)
{
bytes_deleted+=1+sizeof(int64)+sizeof(id);
bytes_deleted+=1+sizeof(int64)+sizeof(_u32)+it->first.size()+sizeof(id);
found=true;
new_open_files.erase(it);
break;
}
}
if(!found) return false;
max_time=(std::max)(max_time, rec_time);
if(max_time>ctime)
{
Server->Log("Persisted open files list is obsolete (restart occured) (2)", LL_INFO);
return false;
}
}
else
{
Server->Log("Unknown id while reading persisted open files", LL_ERROR);
return false;
}
}
if(max_time>Server->getTimeMS())
{
Server->Log("Persisted open files list is obsolete (restart occured)", LL_INFO);
return false;
}
open_files = new_open_files;
return true;
}
std::vector<std::wstring> PersistentOpenFiles::get()
{
std::vector<std::wstring> ret;
for(std::map<std::wstring, unsigned int>::iterator it=open_files.begin();it!=open_files.end();++it)
{
ret.push_back(it->first);
}
return ret;
}
void PersistentOpenFiles::remove( const std::wstring& fn )
{
std::map<std::wstring, unsigned int>::iterator it=open_files.find(fn);
if(it!=open_files.end())
{
removef(it->second, it->first.size());
open_files.erase(it);
}
}
void PersistentOpenFiles::add( const std::wstring& fn )
{
if(open_files.find(fn)==open_files.end())
{
++curr_id;
open_files[fn]=curr_id;
addf(fn, curr_id);
}
}
bool PersistentOpenFiles::is_present( const std::wstring& fn )
{
return (open_files.find(fn)!=open_files.end());
}
PersistentOpenFiles::PersistentOpenFiles() : curr_id(0), bytes_written(0), bytes_deleted(0)
{
persistf = Server->openFile(persistent_open_files_fn, MODE_RW_CREATE);
#ifndef _DEBUG
change_file_permissions_admin_only(persistent_open_files_fn);
#endif
if(!load())
{
Server->destroy(persistf);
persistf = Server->openFile(persistent_open_files_fn, MODE_WRITE);
#ifndef _DEBUG
change_file_permissions_admin_only(persistent_open_files_fn);
#endif
if(persistf==NULL)
{
Server->Log(std::string("Error opening open write file at ")+persistent_open_files_fn, LL_ERROR);
}
}
}
PersistentOpenFiles::~PersistentOpenFiles()
{
Server->destroy(persistf);
}