forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilefingerprint.cpp
More file actions
448 lines (368 loc) · 10.3 KB
/
filefingerprint.cpp
File metadata and controls
448 lines (368 loc) · 10.3 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
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/**
* @file filefingerprint.cpp
* @brief Sparse file fingerprint
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega/filefingerprint.h"
#include "mega/serialize64.h"
#include "mega/base64.h"
#include "mega/logging.h"
#include "mega/utils.h"
namespace {
constexpr int MAXFULL = 8192;
} // anonymous
namespace mega {
bool operator==(const FileFingerprint& lhs, const FileFingerprint& rhs)
{
// size differs - cannot be equal
if (lhs.size != rhs.size)
{
return false;
}
#ifndef __ANDROID__
// mtime check disabled on Android due to this bug:
// https://code.google.com/p/android/issues/detail?id=18624
#ifndef WINDOWS_PHONE
// disabled on Windows Phone too because SetFileTime() isn't available
// mtime differs - cannot be equal
if (abs(lhs.mtime-rhs.mtime) > 2)
{
return false;
}
#endif
#endif
// FileFingerprints not fully available - give it the benefit of the doubt
if (!lhs.isvalid || !rhs.isvalid)
{
return true;
}
return !memcmp(lhs.crc.data(), rhs.crc.data(), sizeof lhs.crc);
}
bool FileFingerprint::serialize(string *d)
{
d->append((const char*)&size, sizeof(size));
d->append((const char*)&mtime, sizeof(mtime));
d->append((const char*)crc.data(), sizeof(crc));
d->append((const char*)&isvalid, sizeof(isvalid));
return true;
}
FileFingerprint *FileFingerprint::unserialize(string *d)
{
const char* ptr = d->data();
const char* end = ptr + d->size();
if (ptr + sizeof(m_off_t) + sizeof(m_time_t) + 4 * sizeof(int32_t) + sizeof(bool) > end)
{
LOG_err << "FileFingerprint unserialization failed - serialized string too short";
return NULL;
}
FileFingerprint *fp = new FileFingerprint();
fp->size = MemAccess::get<m_off_t>(ptr);
ptr += sizeof(m_off_t);
fp->mtime = MemAccess::get<m_time_t>(ptr);
ptr += sizeof(m_time_t);
memcpy(fp->crc.data(), ptr, sizeof(fp->crc));
ptr += sizeof(fp->crc);
fp->isvalid = MemAccess::get<bool>(ptr);
ptr += sizeof(bool);
d->erase(0, ptr - d->data());
return fp;
}
FileFingerprint::FileFingerprint(const FileFingerprint& other)
: size{other.size}
, mtime{other.mtime}
, crc(other.crc)
, isvalid{other.isvalid}
{}
FileFingerprint& FileFingerprint::operator=(const FileFingerprint& other)
{
assert(this != &other);
size = other.size;
mtime = other.mtime;
crc = other.crc;
isvalid = other.isvalid;
return *this;
}
bool FileFingerprint::genfingerprint(FileAccess* fa, bool ignoremtime)
{
bool changed = false;
decltype(crc) newcrc;
int32_t crcval;
if (mtime != fa->mtime)
{
mtime = fa->mtime;
changed = !ignoremtime;
}
if (size != fa->size)
{
size = fa->size;
changed = true;
}
if (!fa->openf())
{
size = -1;
return true;
}
if (size <= (m_off_t)sizeof crc)
{
// tiny file: read verbatim, NUL pad
if (!fa->frawread((byte*)newcrc.data(), static_cast<unsigned>(size), 0, true))
{
size = -1;
fa->closef();
return true;
}
if (size < (m_off_t)sizeof(crc))
{
memset((byte*)newcrc.data() + size, 0, size_t(sizeof(crc) - size));
}
}
else if (size <= MAXFULL)
{
// small file: full coverage, four full CRC32s
HashCRC32 crc32;
byte buf[MAXFULL];
if (!fa->frawread(buf, static_cast<unsigned>(size), 0, true))
{
size = -1;
fa->closef();
return true;
}
for (unsigned i = 0; i < crc.size(); i++)
{
int begin = int(i * size / crc.size());
int end = int((i + 1) * size / crc.size());
crc32.add(buf + begin, end - begin);
crc32.get((byte*)&crcval);
newcrc[i] = htonl(crcval);
}
}
else
{
// large file: sparse coverage, four sparse CRC32s
HashCRC32 crc32;
byte block[4 * sizeof crc];
const unsigned blocks = MAXFULL / unsigned(sizeof block * crc.size());
for (unsigned i = 0; i < crc.size(); i++)
{
for (unsigned j = 0; j < blocks; j++)
{
if (!fa->frawread(block, sizeof block,
(size - sizeof block)
* (i * blocks + j)
/ (crc.size() * blocks - 1), true))
{
size = -1;
fa->closef();
return true;
}
crc32.add(block, sizeof block);
}
crc32.get((byte*)&crcval);
newcrc[i] = htonl(crcval);
}
}
if (crc != newcrc)
{
crc = newcrc;
changed = true;
}
if (!isvalid)
{
isvalid = true;
changed = true;
}
fa->closef();
return changed;
}
bool FileFingerprint::genfingerprint(InputStreamAccess *is, m_time_t cmtime, bool ignoremtime)
{
bool changed = false;
decltype(crc) newcrc;
int32_t crcval;
if (mtime != cmtime)
{
mtime = cmtime;
changed = !ignoremtime;
}
if (size != is->size())
{
size = is->size();
changed = true;
}
if (size < 0)
{
size = -1;
return true;
}
if (size <= (m_off_t)sizeof crc)
{
// tiny file: read verbatim, NUL pad
if (!is->read((byte*)newcrc.data(), (unsigned int)size))
{
size = -1;
return true;
}
if (size < (m_off_t)sizeof(crc))
{
memset((byte*)newcrc.data() + size, 0, size_t(sizeof(crc) - size));
}
}
else if (size <= MAXFULL)
{
// small file: full coverage, four full CRC32s
HashCRC32 crc32;
byte buf[MAXFULL];
if (!is->read(buf, int(size)))
{
size = -1;
return true;
}
for (unsigned i = 0; i < crc.size(); i++)
{
int begin = int(i * size / crc.size());
int end = int((i + 1) * size / crc.size());
crc32.add(buf + begin, end - begin);
crc32.get((byte*)&crcval);
newcrc[i] = htonl(crcval);
}
}
else
{
// large file: sparse coverage, four sparse CRC32s
HashCRC32 crc32;
byte block[4 * sizeof crc];
const unsigned blocks = MAXFULL / unsigned(sizeof block * crc.size());
m_off_t current = 0;
for (unsigned i = 0; i < crc.size(); i++)
{
for (unsigned j = 0; j < blocks; j++)
{
m_off_t offset = (size - sizeof block)
* (i * blocks + j)
/ (crc.size() * blocks - 1);
//Seek
for (m_off_t fullstep = offset - current; fullstep > 0; ) // 500G or more and the step doesn't fit in 32 bits
{
unsigned step = fullstep > UINT_MAX ? UINT_MAX : unsigned(fullstep);
if (!is->read(NULL, step))
{
size = -1;
return true;
}
fullstep -= (uint64_t)step;
}
current += (offset - current);
if (!is->read(block, sizeof block))
{
size = -1;
return true;
}
current += sizeof block;
crc32.add(block, sizeof block);
}
crc32.get((byte*)&crcval);
newcrc[i] = htonl(crcval);
}
}
if (crc != newcrc)
{
crc = newcrc;
changed = true;
}
if (!isvalid)
{
isvalid = true;
changed = true;
}
return changed;
}
// convert this FileFingerprint to string
void FileFingerprint::serializefingerprint(string* d) const
{
byte buf[sizeof crc + 1 + sizeof mtime];
int l;
memcpy(buf, crc.data(), sizeof crc);
l = Serialize64::serialize(buf + sizeof crc, mtime);
d->resize((sizeof crc + l) * 4 / 3 + 4);
d->resize(Base64::btoa(buf, sizeof crc + l, (char*)d->c_str()));
}
// decode and set base64-encoded fingerprint
int FileFingerprint::unserializefingerprint(string* d)
{
byte buf[sizeof crc + sizeof mtime + 1];
unsigned l;
uint64_t t;
if ((l = Base64::atob(d->c_str(), buf, sizeof buf)) < sizeof crc + 1)
{
return 0;
}
if (Serialize64::unserialize(buf + sizeof crc, l - sizeof crc, &t) < 0)
{
return 0;
}
memcpy(crc.data(), buf, sizeof crc);
mtime = t;
isvalid = true;
return 1;
}
bool FileFingerprintCmp::operator()(const FileFingerprint* a, const FileFingerprint* b) const
{
if (a->size < b->size)
{
return true;
}
if (a->size > b->size)
{
return false;
}
if (a->mtime < b->mtime)
{
return true;
}
if (a->mtime > b->mtime)
{
return false;
}
return memcmp(a->crc.data(), b->crc.data(), sizeof a->crc) < 0;
}
bool LightFileFingerprint::genfingerprint(const m_off_t filesize, const m_time_t filemtime)
{
bool changed = false;
if (mtime != filemtime)
{
mtime = filemtime;
changed = true;
}
if (size != filesize)
{
size = filesize;
changed = true;
}
return changed;
}
bool LightFileFingerprintCmp::operator()(const LightFileFingerprint* a, const LightFileFingerprint* b) const
{
assert(a);
assert(b);
return std::tie(a->mtime, a->size) < std::tie(b->mtime, b->size);
}
bool operator==(const LightFileFingerprint& lhs, const LightFileFingerprint& rhs)
{
return std::tie(lhs.mtime, lhs.size) == std::tie(rhs.mtime, rhs.size);
}
} // mega