forked from zbackup/zbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_restorer.cc
318 lines (274 loc) · 8.54 KB
/
backup_restorer.cc
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2012-2014 Konstantin Isakov <ikm@zbackup.org> and ZBackup contributors, see CONTRIBUTORS
// Part of ZBackup. Licensed under GNU GPLv2 or later + OpenSSL, see LICENSE
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
#include <vector>
#include <algorithm>
#include "backup_restorer.hh"
#include "chunk_id.hh"
#include "message.hh"
#include "zbackup.pb.h"
namespace BackupRestorer {
using std::vector;
using google::protobuf::io::CodedInputStream;
void restoreMap( ChunkStorage::Reader & chunkStorageReader,
ChunkMap const * chunkMap, SeekableSink *output )
{
string chunk;
size_t chunkSize;
for ( ChunkMap::const_iterator it = chunkMap->begin(); it != chunkMap->end(); it++ )
{
for ( ChunkPosition::const_iterator pi = (*it).second.begin(); pi != (*it).second.end(); pi++ )
{
if ( output )
{
// Need to emit a chunk, reading it from the store
chunkStorageReader.get( (*pi).first, chunk, chunkSize );
output->saveData( (*pi).second, chunk.data(), chunkSize );
}
}
}
}
void restore( ChunkStorage::Reader & chunkStorageReader,
std::string const & backupData,
DataSink * output, ChunkSet * chunkSet,
ChunkMap * chunkMap, SeekableSink * seekOut )
{
google::protobuf::io::ArrayInputStream is( backupData.data(),
backupData.size() );
CodedInputStream cis( &is );
CodedInputStream::Limit limit = cis.PushLimit( backupData.size() );
// The following line prevents it from barfing on large backupData.
// TODO: this disables size checks for each separate message. Figure a better
// way to do this while keeping them enabled. It seems we need to create an
// instance of CodedInputStream for each message, but it might be expensive
cis.SetTotalBytesLimit( backupData.size(), -1 );
// Used when emitting chunks
string chunk;
BackupInstruction instr;
int64_t position = 0;
while ( cis.BytesUntilLimit() > 0 )
{
Message::parse( instr, cis );
if ( instr.has_chunk_to_emit() )
{
ChunkId id( instr.chunk_to_emit() );
size_t chunkSize;
if ( output )
{
// Need to emit a chunk, reading it from the store
chunkStorageReader.get( id, chunk, chunkSize );
output->saveData( chunk.data(), chunkSize );
}
if ( chunkMap )
{
Bundle::Id const *bundleId = chunkStorageReader.getBundleId( id, chunkSize );
ChunkMap::iterator it = chunkMap->find( *bundleId );
if ( it == chunkMap->end() )
{
ChunkPosition v;
std::pair< ChunkMap::iterator, bool > r = chunkMap->insert( std::make_pair( *bundleId, v ) );
it = r.first;
}
(*it).second.push_back( std::make_pair( id, position ) );
position += chunkSize;
}
if ( chunkSet )
{
chunkSet->insert( id );
}
}
if ( ( output || chunkMap ) && instr.has_bytes_to_emit() )
{
// Need to emit the bytes directly
string const & bytes = instr.bytes_to_emit();
if ( output )
output->saveData( bytes.data(), bytes.size() );
if ( chunkMap )
{
if ( seekOut )
seekOut->saveData( position, bytes.data(), bytes.size() );
position += bytes.size();
}
}
}
cis.PopLimit( limit );
}
void restoreIterations( ChunkStorage::Reader & chunkStorageReader,
BackupInfo & backupInfo, std::string & backupData, ChunkSet * chunkSet )
{
// Perform the iterations needed to get to the actual user backup data
for ( ; ; )
{
backupData.swap( *backupInfo.mutable_backup_data() );
if ( backupInfo.iterations() )
{
struct StringWriter: public DataSink
{
string result;
virtual void saveData( void const * data, size_t size )
{
result.append( ( char const * ) data, size );
}
} stringWriter;
restore( chunkStorageReader, backupData, &stringWriter, chunkSet, NULL, NULL );
backupInfo.mutable_backup_data()->swap( stringWriter.result );
backupInfo.set_iterations( backupInfo.iterations() - 1 );
}
else
break;
}
}
// TODO: This iterator can be used in restore() function.
/// Iterator over BackupInstructions in stream
class BackupInstructionsIterator : NoCopy
{
public:
BackupInstructionsIterator( std::string const & backupData )
: is( backupData.data(), backupData.size() )
, cis( &is )
{
limit = cis.PushLimit( backupData.size() );
// The following line prevents it from barfing on large backupData.
// TODO: this disables size checks for each separate message. Figure a better
// way to do this while keeping them enabled. It seems we need to create an
// instance of CodedInputStream for each message, but it might be expensive
cis.SetTotalBytesLimit( backupData.size(), -1 );
}
~BackupInstructionsIterator()
{
cis.PopLimit( limit );
}
/// Read next BackupInstruction instruction in stream.
/// Returns true if there is any, and false if end of stream reached.
bool readNext( BackupInstruction & instr )
{
if ( cis.BytesUntilLimit() > 0 )
{
Message::parse( instr, cis );
return true;
}
else
{
return false;
}
}
private:
google::protobuf::io::ArrayInputStream is;
CodedInputStream cis;
CodedInputStream::Limit limit;
};
IndexedRestorer::IndexedRestorer( ChunkStorage::Reader & chunkStorageReader,
std::string const & backupData )
: chunkStorageReader( chunkStorageReader )
{
BackupInstructionsIterator instructionIter( backupData );
BackupInstruction instr;
int64_t position = 0;
while ( instructionIter.readNext( instr ) )
{
instructions.push_back( std::make_pair( position, instr ) );
if ( instr.has_chunk_to_emit() )
{
ChunkId id( instr.chunk_to_emit() );
size_t chunkSize;
chunkStorageReader.getBundleId( id, chunkSize );
position += chunkSize;
}
if ( instr.has_bytes_to_emit() )
{
string const & bytes = instr.bytes_to_emit();
position += bytes.size();
}
}
totalSize = position;
}
int64_t IndexedRestorer::size() const
{
return totalSize;
}
template<class PairType>
class PairFirstLess
{
public:
bool operator()( PairType const & left, PairType const & right )
{
return left.first < right.first;
}
};
void IndexedRestorer::saveData( int64_t offset, void * data, size_t size ) const
{
if ( offset < 0 || offset + size > totalSize )
throw exOutOfRange();
// Find first instruction which generates output range that starts after offset
Instructions::const_iterator it =
std::upper_bound( instructions.begin(), instructions.end(),
std::make_pair(offset, BackupInstruction()),
PairFirstLess<InstructionAtPos>() );
assert(it != instructions.begin());
// Iterator will point on instruction, which range will include byte at offset
--it;
struct Outputer
{
Outputer( int64_t offset, char * data, size_t size )
: offset(offset)
, data(data)
, size(size)
{
}
bool operator()( int64_t chunkOffset, char const * chunk, size_t chunkSize )
{
size_t start = 0;
if ( chunkOffset < offset )
{
// First chunk which begins before offset
start = offset - chunkOffset;
}
size_t end = chunkSize;
if ( chunkOffset + chunkSize > offset + size )
{
// Chunk ends beyond requested range
end = offset + size - chunkOffset;
}
size_t partSize = end - start;
memcpy( data, chunk + start, partSize );
offset += partSize;
data += partSize;
assert( size >= partSize );
size -= partSize;
return size != 0;
}
int64_t offset;
char * data;
size_t size;
};
Outputer out( offset, static_cast<char *>( data ), size );
string chunk;
int64_t position = it->first;
for ( ; it != instructions.end(); ++it)
{
assert( position == it->first );
BackupInstruction const & instr = it->second;
if ( instr.has_chunk_to_emit() )
{
ChunkId id( instr.chunk_to_emit() );
size_t chunkSize;
chunkStorageReader.get( id, chunk, chunkSize );
if ( !out( position, chunk.data(), chunkSize ) )
{
break;
}
position += chunkSize;
}
if ( instr.has_bytes_to_emit() )
{
string const & bytes = instr.bytes_to_emit();
if ( !out( position, bytes.data(), bytes.size() ) )
{
break;
}
position += bytes.size();
}
}
}
}