forked from owncloud/client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksums.cpp
More file actions
252 lines (223 loc) · 7.31 KB
/
Copy pathchecksums.cpp
File metadata and controls
252 lines (223 loc) · 7.31 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
/*
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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. See the GNU General Public License
* for more details.
*/
#include "config.h"
#include "filesystem.h"
#include "checksums.h"
#include "syncfileitem.h"
#include "propagatorjobs.h"
#include "account.h"
#include <qtconcurrentrun.h>
/** \file checksums.cpp
*
* \brief Computing and validating file checksums
*
* Overview
* --------
*
* Checksums are used in two distinct ways during synchronization:
*
* - to guard uploads and downloads against data corruption
* (transmission checksum)
* - to quickly check whether the content of a file has changed
* to avoid redundant uploads (content checksum)
*
* In principle both are independent and different checksumming
* algorithms can be used. To avoid redundant computations, it can
* make sense to use the same checksum algorithm though.
*
* Transmission Checksums
* ----------------------
*
* The usage of transmission checksums is currently optional and needs
* to be explicitly enabled by adding 'transmissionChecksum=TYPE' to
* the '[General]' section of the config file.
*
* When enabled, the checksum will be calculated on upload and sent to
* the server in the OC-Checksum header with the format 'TYPE:CHECKSUM'.
*
* On download, the header with the same name is read and if the
* received data does not have the expected checksum, the download is
* rejected.
*
* Transmission checksums guard a specific sync action and are not stored
* in the database.
*
* Content Checksums
* -----------------
*
* Sometimes the metadata of a local file changes while the content stays
* unchanged. Content checksums allow the sync client to avoid uploading
* the same data again by comparing the file's actual checksum to the
* checksum stored in the database.
*
* Content checksums are not sent to the server.
*
* Checksum Algorithms
* -------------------
*
* - Adler32 (requires zlib)
* - MD5
* - SHA1
*
*/
namespace OCC {
QByteArray makeChecksumHeader(const QByteArray& checksumType, const QByteArray& checksum)
{
QByteArray header = checksumType;
header.append(':');
header.append(checksum);
return header;
}
bool parseChecksumHeader(const QByteArray& header, QByteArray* type, QByteArray* checksum)
{
if (header.isEmpty()) {
type->clear();
checksum->clear();
return true;
}
const auto idx = header.indexOf(':');
if (idx < 0) {
return false;
}
*type = header.left(idx);
*checksum = header.mid(idx + 1);
return true;
}
bool uploadChecksumEnabled()
{
static bool enabled = qgetenv("OWNCLOUD_DISABLE_CHECKSUM_UPLOAD").isEmpty();
return enabled;
}
QByteArray contentChecksumType()
{
static QByteArray type = qgetenv("OWNCLOUD_CONTENT_CHECKSUM_TYPE");
if (!type.isNull()) { // can set to "" to disable checksumming
return type;
}
return "SHA1";
}
ComputeChecksum::ComputeChecksum(QObject* parent)
: QObject(parent)
{
}
void ComputeChecksum::setChecksumType(const QByteArray& type)
{
_checksumType = type;
}
QByteArray ComputeChecksum::checksumType() const
{
return _checksumType;
}
void ComputeChecksum::start(const QString& filePath)
{
// Calculate the checksum in a different thread first.
connect( &_watcher, SIGNAL(finished()),
this, SLOT(slotCalculationDone()),
Qt::UniqueConnection );
_watcher.setFuture(QtConcurrent::run(ComputeChecksum::computeNow, filePath, checksumType()));
}
QByteArray ComputeChecksum::computeNow(const QString& filePath, const QByteArray& checksumType)
{
if( checksumType == checkSumMD5C ) {
return FileSystem::calcMd5(filePath);
} else if( checksumType == checkSumSHA1C ) {
return FileSystem::calcSha1(filePath);
}
#ifdef ZLIB_FOUND
else if( checksumType == checkSumAdlerC) {
return FileSystem::calcAdler32(filePath);
}
#endif
// for an unknown checksum or no checksum, we're done right now
if( !checksumType.isEmpty() ) {
qDebug() << "Unknown checksum type:" << checksumType;
}
return QByteArray();
}
void ComputeChecksum::slotCalculationDone()
{
QByteArray checksum = _watcher.future().result();
if (!checksum.isNull()) {
emit done(_checksumType, checksum);
} else {
emit done(QByteArray(), QByteArray());
}
}
ValidateChecksumHeader::ValidateChecksumHeader(QObject *parent)
: QObject(parent)
{
}
void ValidateChecksumHeader::start(const QString& filePath, const QByteArray& checksumHeader)
{
// If the incoming header is empty no validation can happen. Just continue.
if( checksumHeader.isEmpty() ) {
emit validated(QByteArray(), QByteArray());
return;
}
if( !parseChecksumHeader(checksumHeader, &_expectedChecksumType, &_expectedChecksum) ) {
qDebug() << "Checksum header malformed:" << checksumHeader;
emit validationFailed(tr("The checksum header is malformed."));
return;
}
auto calculator = new ComputeChecksum(this);
calculator->setChecksumType(_expectedChecksumType);
connect(calculator, SIGNAL(done(QByteArray,QByteArray)),
SLOT(slotChecksumCalculated(QByteArray,QByteArray)));
calculator->start(filePath);
}
void ValidateChecksumHeader::slotChecksumCalculated(const QByteArray& checksumType,
const QByteArray& checksum)
{
if( checksumType != _expectedChecksumType ) {
emit validationFailed(tr("The checksum header contained an unknown checksum type '%1'").arg(
QString::fromLatin1(_expectedChecksumType)));
return;
}
if( checksum != _expectedChecksum ) {
emit validationFailed(tr("The downloaded file does not match the checksum, it will be resumed."));
return;
}
emit validated(checksumType, checksum);
}
CSyncChecksumHook::CSyncChecksumHook(SyncJournalDb *journal)
: _journal(journal)
{
}
const char* CSyncChecksumHook::hook(const char* path, uint32_t checksumTypeId, void *this_obj)
{
CSyncChecksumHook* checksumHook = static_cast<CSyncChecksumHook*>(this_obj);
QByteArray checksum = checksumHook->compute(QString::fromUtf8(path), checksumTypeId);
if (checksum.isNull()) {
return NULL;
}
char* result = (char*)malloc(checksum.size() + 1);
memcpy(result, checksum.constData(), checksum.size());
result[checksum.size()] = 0;
return result;
}
QByteArray CSyncChecksumHook::compute(const QString& path, int checksumTypeId)
{
QByteArray checksumType = _journal->getChecksumType(checksumTypeId);
if (checksumType.isEmpty()) {
qDebug() << "Checksum type" << checksumTypeId << "not found";
return QByteArray();
}
QByteArray checksum = ComputeChecksum::computeNow(path, checksumType);
if (checksum.isNull()) {
qDebug() << "Failed to compute checksum" << checksumType << "for" << path;
return QByteArray();
}
return checksum;
}
}