-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathzsmake.cpp
More file actions
376 lines (290 loc) · 12.2 KB
/
zsmake.cpp
File metadata and controls
376 lines (290 loc) · 12.2 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
// system headers
#include <arpa/inet.h>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
#include <unordered_map>
// library headers
extern "C" {
#include <rcksum.h>
#include <sha1.h>
}
// local headers
#include "config.h"
#include "zsmake.h"
#include "zsutil.h"
namespace zsync2 {
class ZSyncFileMaker::Private {
private:
typedef std::vector<char> buffer_t;
typedef std::map<std::string, std::string> headerFields_t;
public:
// path to file
std::string path;
std::string zSyncFilePath;
// header fields' values
std::string fileName;
std::string url;
std::string fileSHA1Hash;
uint32_t blockSize;
long length;
int checksumLength;
int rSumLength;
int seqMatches;
buffer_t blockSums;
headerFields_t customHeaderFields;
std::function<void(std::string)> logMessage;
public:
explicit Private(const std::string& path) : path(path),
length(0),
checksumLength(0),
blockSize(0),
rSumLength(0),
seqMatches(0)
{
// make sure to use the filename only
size_t slashPos;
if ((slashPos = path.find_last_of('/')) == std::string::npos)
fileName = path;
else
fileName = path.substr(slashPos + 1, path.size() - slashPos - 1);
// should be created in the current working directory unless specified otherwise
zSyncFilePath = fileName + ".zsync";
// by default, log to stderr
logMessage = [](std::string message) {
std::cerr << message << std::endl;
};
}
~Private() = default;
private:
bool insertMTimeHeader(headerFields_t& headerFields) {
auto mtime = readMtime(path);
if(mtime != -1) {
char buffer[32];
struct tm mtime_tm;
if(gmtime_r(&mtime, &mtime_tm) != nullptr) {
if(strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S %z", &mtime_tm) <= 0) {
logMessage("Failed to format mtime string!");
return false;
}
headerFields.insert(std::make_pair("MTime", buffer));
return true;
}
}
logMessage("Failed to read mtime for file " + path + "!");
return false;
}
public:
bool writeBlockSums(const buffer_t& buffer, size_t bytesRead) {
buffer_t checksum(CHECKSUM_SIZE);
// Create a copy of the buffer for processing, padding it to blockSize if needed
buffer_t padded_buffer(buffer.begin(), buffer.begin() + bytesRead);
if (bytesRead < blockSize) {
padded_buffer.resize(blockSize, 0); // pad with zeros to reach blockSize
}
// sometimes, the inconsistent use of char and unsigned char within the old zsync code base can get
// annoying...
auto r = rcksum_calc_rsum_block(reinterpret_cast<const unsigned char*>(padded_buffer.data()), blockSize);
rcksum_calc_checksum(
reinterpret_cast<unsigned char*>(checksum.data()),
reinterpret_cast<const unsigned char*>(padded_buffer.data()),
blockSize
);
r.a = htons(r.a);
r.b = htons(r.b);
// insert raw bytes into
auto oldSize = blockSums.size();
blockSums.resize(oldSize + sizeof(r));
memcpy(&blockSums[oldSize], &r, sizeof(r));
std::copy(checksum.begin(), checksum.end(), std::back_inserter(blockSums));
return true;
}
bool readStreamWriteBlockSums(std::ifstream& inFile, SHA1_CTX& sha1Ctx) {
buffer_t buffer(blockSize);
while (inFile.read(buffer.data(), buffer.size())) {
auto bytesRead = (size_t) inFile.gcount();
if (bytesRead > 0) {
SHA1Update(&sha1Ctx, reinterpret_cast<const uint8_t*>(buffer.data()), bytesRead);
writeBlockSums(buffer, bytesRead);
length += bytesRead;
} else {
auto error = errno;
std::string messagePrefix = "Failed to calculate block sums: ";
if (inFile.fail())
logMessage(messagePrefix + strerror(error));
else
logMessage(messagePrefix + "unknown error: " + strerror(error));
return false;
}
}
// Handle the last partial block if any data was read but stream hit EOF
if (inFile.eof()) {
auto bytesRead = (size_t) inFile.gcount();
if (bytesRead > 0) {
SHA1Update(&sha1Ctx, reinterpret_cast<const uint8_t*>(buffer.data()), bytesRead);
writeBlockSums(buffer, bytesRead);
length += bytesRead;
}
} else if (inFile.fail() || inFile.bad()) {
auto error = errno;
std::string messagePrefix = "Failed to calculate block sums: ";
logMessage(messagePrefix + strerror(error));
return false;
}
return true;
}
bool calculateBlockSums() {
// read the input file and construct the checksum of the whole file, and the per-block checksums
// create SHA1 context
SHA1_CTX sha1Ctx;
SHA1Init(&sha1Ctx);
std::ifstream ifs(path);
if (!ifs) {
return false;
}
if (blockSize == 0)
blockSize = (ifs.tellg() < 100000000) ? 2048 : 4096;
if (!readStreamWriteBlockSums(ifs, sha1Ctx))
return false;
// decide how long a rsum hash and checksum hash per block we need for this file
seqMatches = (length > blockSize) ? 2 : 1;
rSumLength = static_cast<int>(std::ceil(((log(length) + log(blockSize)) / log(2) - 8.6) / seqMatches / 8));
// apply limits
if (rSumLength > 4)
rSumLength = 4;
else if (rSumLength < 2)
rSumLength = 2;
// calculate two different checksum lengths and pick the maximum
checksumLength = static_cast<int>(std::ceil((20 + (log(length) + log(1 + length / blockSize)) / log(2)) / seqMatches / 8));
{
auto checksumLength2 = static_cast<int>((7.9 + (20 + log(1 + length / blockSize) / log(2))) / 8);
if (checksumLength < checksumLength2)
checksumLength = checksumLength2;
}
// calculate SHA-1 hash sum
{
std::vector<uint8_t> digest(SHA1_DIGEST_LENGTH);
SHA1Final(digest.data(), &sha1Ctx);
std::ostringstream oss;
for (auto& i : digest)
oss << std::setfill('0') << std::setw(2) << std::hex << (int) i;
fileSHA1Hash = oss.str();
}
return true;
}
bool dump(std::string& data) {
// prepare file header
// create copy of customHeaderFields and insert default headers
auto headerFields = customHeaderFields;
headerFields["zsync"] = VERSION;
headerFields["Filename"] = fileName;
if (!insertMTimeHeader(headerFields))
return false;
headerFields["Blocksize"] = std::to_string(blockSize);
headerFields["Length"] = std::to_string(length);
if (url.empty()) {
logMessage(
"No URL given, so I am including a relative URL in the .zsync file - you must keep the file "
"being served and the .zsync in the same public directory. Use -u " + path + " to get "
"this same result without this warning."
);
url = fileName;
}
if (!isUrlAbsolute(url)) {
logMessage(
"Warning: the given URL is relative. Please make sure the files are placed correctly on "
"the server, otherwise zsync2 won't be able to resolve the path to the target file, requiring "
"the user to specify this URL on the command line (using the -u flag)."
);
}
headerFields["URL"] = url;
headerFields["SHA-1"] = fileSHA1Hash;
std::ostringstream hashLengths;
hashLengths << seqMatches << "," << rSumLength << "," << checksumLength;
headerFields["Hash-Lengths"] = hashLengths.str();
// now, create .zsync file
std::ostringstream oss;
constexpr char endl[] = "\n";
// write header
for (const auto& pair : headerFields) {
oss << pair.first << ": " << pair.second << endl;
}
// insert separator to mark end of header
oss << endl;
// copy block hashes
{
// create buffer
buffer_t buffer(20);
std::fill(buffer.begin(), buffer.end(), 0);
auto readChunk = [&buffer, this]() {
size_t length = 0;
if (blockSums.empty())
return length;
length = buffer.size();
if (blockSums.size() < buffer.size()) {
length = blockSums.size();
std::fill(buffer.begin() + length, buffer.end(), 0);
}
std::copy(blockSums.begin(), blockSums.begin() + length, buffer.begin());
blockSums.erase(blockSums.begin(), blockSums.begin() + length);
return length;
};
while (readChunk() > 0) {
std::copy(buffer.begin() + 4 - rSumLength, buffer.begin() + 4, std::ostream_iterator<char>(oss));
std::copy(buffer.begin() + 4, buffer.begin() + 4 + checksumLength, std::ostream_iterator<char>(oss));
}
}
data = oss.str();
return true;
}
void addCustomHeaderField(const std::string& key, const std::string& value) {
customHeaderFields[key] = value;
}
};
ZSyncFileMaker::ZSyncFileMaker(const std::string& filename) {
d = new Private(filename);
}
ZSyncFileMaker::~ZSyncFileMaker() {
delete d;
}
bool ZSyncFileMaker::calculateBlockSums() {
return d->calculateBlockSums();
}
bool ZSyncFileMaker::dump(std::string& data) {
return d->dump(data);
}
void ZSyncFileMaker::setBlockSize(uint32_t blockSize) {
d->blockSize = blockSize;
}
bool ZSyncFileMaker::saveZSyncFile(std::string outFilePath) {
if (outFilePath.empty())
outFilePath = d->zSyncFilePath;
std::ofstream ofs(outFilePath);
auto error = errno;
if (!ofs) {
d->logMessage("Failed to open output file " + outFilePath + ": " + strerror(error));
return false;
}
std::string data;
if (!dump(data))
return false;
ofs << data;
return true;
}
void ZSyncFileMaker::setUrl(const std::string& url) {
d->url = url;
}
void ZSyncFileMaker::setLogMessageCallback(std::function<void(std::string)> callback) {
d->logMessage = std::move(callback);
}
void ZSyncFileMaker::addCustomHeaderField(const std::string& key, const std::string& value) {
return d->addCustomHeaderField(key, value);
}
std::map<std::string, std::string> ZSyncFileMaker::getCustomHeaderFields() {
return d->customHeaderFields;
}
}