forked from ton-blockchain/ton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTorrent.cpp
740 lines (677 loc) · 22.5 KB
/
Torrent.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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
/*
This file is part of TON Blockchain Library.
TON Blockchain Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
TON Blockchain Library 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
Copyright 2017-2020 Telegram Systems LLP
*/
#include "Torrent.h"
#include "td/utils/Status.h"
#include "td/utils/crypto.h"
#include "td/utils/port/Stat.h"
#include "td/utils/tl_helpers.h"
#include "td/utils/port/path.h"
namespace ton {
td::Result<Torrent> Torrent::open(Options options, td::Bits256 hash) {
Torrent res(hash);
if (!options.in_memory) {
if (options.root_dir.empty()) {
options.root_dir = ".";
}
res.set_root_dir(options.root_dir);
}
return std::move(res);
}
td::Result<Torrent> Torrent::open(Options options, TorrentMeta meta) {
Torrent res(td::Bits256(meta.info.get_hash()));
TRY_STATUS(res.init_info(std::move(meta.info)));
if (meta.header) {
TRY_STATUS(res.set_header(meta.header.unwrap()));
}
if (meta.root_proof.not_null()) {
TRY_STATUS(res.merkle_tree_.add_proof(meta.root_proof));
}
if (!options.in_memory) {
if (options.root_dir.empty()) {
options.root_dir = ".";
}
res.set_root_dir(options.root_dir);
}
if (options.validate) {
res.validate();
}
return std::move(res);
}
td::Result<Torrent> Torrent::open(Options options, td::Slice meta_str) {
TRY_RESULT(meta, TorrentMeta::deserialize(meta_str));
return open(std::move(options), std::move(meta));
}
const Torrent::Info &Torrent::get_info() const {
CHECK(inited_info_);
return info_;
}
struct IterateInfo {
td::uint64 piece_offset;
td::uint64 chunk_offset;
td::uint64 size;
};
template <class F>
td::Status Torrent::iterate_piece(Info::PieceInfo piece, F &&f) {
auto chunk_it = std::lower_bound(chunks_.begin(), chunks_.end(), piece.offset, [](auto &chunk, auto &piece_offset) {
return chunk.offset + chunk.size <= piece_offset;
});
td::uint64 size = 0;
for (; chunk_it != chunks_.end(); chunk_it++) {
if (chunk_it->offset >= piece.offset + piece.size) {
break;
}
if (chunk_it->size == 0) {
continue;
}
auto l = td::max(chunk_it->offset, piece.offset);
auto r = td::min(chunk_it->offset + chunk_it->size, piece.offset + piece.size);
CHECK(l < r);
IterateInfo info;
info.piece_offset = l - piece.offset;
info.chunk_offset = l - chunk_it->offset;
info.size = r - l;
size += info.size;
TRY_STATUS(f(chunk_it, info));
}
LOG_CHECK(size == piece.size) << size << " vs " << piece.size;
return td::Status::OK();
}
bool Torrent::is_piece_ready(td::uint64 piece_i) const {
if (!inited_info_) {
return false;
}
CHECK(piece_i < info_.pieces_count());
return piece_is_ready_[piece_i];
}
td::optional<size_t> Torrent::get_files_count() const {
if (header_) {
return header_.value().files_count;
}
return {};
}
td::CSlice Torrent::get_file_name(size_t i) const {
return chunks_.at(i + 1).name;
}
td::uint64 Torrent::get_file_size(size_t i) const {
return chunks_.at(i + 1).size;
}
td::uint64 Torrent::get_file_ready_size(size_t i) const {
return chunks_.at(i + 1).ready_size;
}
Torrent::PartsRange Torrent::get_file_parts_range(size_t i) {
auto begin = chunks_.at(i + 1).offset;
auto end = begin + chunks_.at(i + 1).size;
PartsRange res;
res.begin = begin / info_.piece_size;
res.end = (end + info_.piece_size - 1) / info_.piece_size;
return res;
}
Torrent::PartsRange Torrent::get_header_parts_range() const {
CHECK(inited_info_);
PartsRange res;
res.begin = 0;
res.end = header_pieces_count_;
return res;
}
TD_WARN_UNUSED_RESULT td::Status Torrent::ChunkState::get_piece(td::MutableSlice dest, td::uint64 offset,
Cache *cache) {
if (dest.empty()) {
return td::Status::OK();
}
if (cache != nullptr) {
auto global_offset = offset + this->offset;
if (cache->offset > global_offset || cache->offset + cache->size < global_offset + dest.size()) {
auto load_size = td::min(size - offset, (td::uint64)cache->slice.size());
cache->size = 0;
TRY_STATUS(get_piece(cache->slice.as_slice().truncate(load_size), offset));
cache->offset = global_offset;
cache->size = load_size;
}
dest.copy_from(cache->slice.as_slice().substr(global_offset - cache->offset, dest.size()));
CHECK(cache->slice.size() >= dest.size());
return td::Status::OK();
}
TRY_RESULT(size, data.view_copy(dest, offset));
if (size != dest.size()) {
return td::Status::Error("Failed to read the whole chunk");
}
return td::Status::OK();
}
std::string Torrent::get_stats_str() const {
td::StringBuilder sb;
auto o_n = get_files_count();
if (!o_n) {
return "NO HEADER YET\n";
}
for (size_t i = 0, n = o_n.unwrap(); i < n; i++) {
auto size = get_file_size(i);
auto ready_size = get_file_ready_size(i);
sb << get_file_name(i) << "\t" << 100 * ready_size / size << "%% " << td::format::as_size(ready_size) << "/"
<< td::format::as_size(size) << "\n";
}
return sb.as_cslice().str();
}
void Torrent::validate() {
if (!inited_info_ || !header_) {
return;
}
std::fill(piece_is_ready_.begin(), piece_is_ready_.end(), false);
not_ready_piece_count_ = info_.pieces_count();
included_ready_size_ = 0;
for (auto &chunk : chunks_) {
chunk.ready_size = 0;
if (root_dir_) {
if (td::stat(get_chunk_path(chunk.name)).is_error()) {
continue;
}
}
init_chunk_data(chunk);
}
std::vector<td::UInt256> hashes;
std::vector<std::pair<size_t, td::Bits256>> pieces;
auto flush = [&] {
for (size_t piece_i : merkle_tree_.add_pieces(std::move(pieces))) {
auto piece = info_.get_piece_info(piece_i);
iterate_piece(piece, [&](auto it, auto info) {
it->ready_size += info.size;
if (!it->excluded) {
included_ready_size_ += info.size;
}
return td::Status::OK();
});
piece_is_ready_[piece_i] = true;
ready_parts_count_++;
CHECK(not_ready_piece_count_);
not_ready_piece_count_--;
}
hashes.clear();
pieces.clear();
};
td::BufferSlice buf(info_.piece_size);
ChunkState::Cache cache;
cache.slice = td::BufferSlice(td::max(8u << 20, info_.piece_size));
for (size_t piece_i = 0; piece_i < info_.pieces_count(); piece_i++) {
auto piece = info_.get_piece_info(piece_i);
td::Sha256State sha256;
sha256.init();
bool skipped = false;
auto is_ok = iterate_piece(piece, [&](auto it, auto info) {
if (!it->data) {
skipped = true;
return td::Status::Error("No such file");
}
if (!it->has_piece(info.chunk_offset, info.size)) {
return td::Status::Error("Don't have piece");
}
auto dest = buf.as_slice().truncate(info.size);
TRY_STATUS(it->get_piece(dest, info.chunk_offset, &cache));
sha256.feed(dest);
return td::Status::OK();
});
if (is_ok.is_error()) {
LOG_IF(ERROR, !skipped) << "Failed: " << is_ok;
continue;
}
td::Bits256 hash;
sha256.extract(hash.as_slice());
pieces.emplace_back(piece_i, hash);
}
flush();
}
td::Result<std::string> Torrent::get_piece_data(td::uint64 piece_i) {
if (!inited_info_) {
return td::Status::Error("Torrent info not inited");
}
if (piece_i >= info_.pieces_count()) {
return td::Status::Error("Piece idx is too big");
}
if (!piece_is_ready_[piece_i]) {
return td::Status::Error("Piece is not ready");
}
auto it = pending_pieces_.find(piece_i);
if (it != pending_pieces_.end()) {
return it->second;
}
auto it2 = in_memory_pieces_.find(piece_i);
if (it2 != in_memory_pieces_.end()) {
return it2->second.data;
}
auto piece = info_.get_piece_info(piece_i);
std::string res(piece.size, '\0');
iterate_piece(piece, [&](auto it, auto info) {
return it->get_piece(td::MutableSlice(res).substr(info.piece_offset, info.size), info.chunk_offset);
});
return res;
}
td::Result<td::Ref<vm::Cell>> Torrent::get_piece_proof(td::uint64 piece_i) {
if (!inited_info_) {
return td::Status::Error("Torrent info not inited");
}
if (piece_i >= info_.pieces_count()) {
return td::Status::Error("Piece idx is too big");
}
return merkle_tree_.gen_proof(piece_i, piece_i);
}
td::Status Torrent::add_piece(td::uint64 piece_i, td::Slice data, td::Ref<vm::Cell> proof) {
if (fatal_error_.is_error()) {
return fatal_error_.clone().move_as_error_prefix("Fatal error: ");
}
if (!inited_info_) {
return td::Status::Error("Torrent info not inited");
}
if (!proof.is_null()) {
TRY_STATUS(merkle_tree_.add_proof(proof));
}
if (piece_i >= info_.pieces_count()) {
return td::Status::Error("Piece idx is too big");
}
if (piece_is_ready_[piece_i]) {
return td::Status::OK();
}
td::Bits256 hash;
td::sha256(data, hash.as_slice());
TRY_RESULT(expected_hash, merkle_tree_.get_piece_hash(piece_i));
if (expected_hash != hash) {
return td::Status::Error("Hash mismatch");
}
piece_is_ready_[piece_i] = true;
ready_parts_count_++;
if (chunks_.empty() || !enabled_write_to_files_) {
return add_pending_piece(piece_i, data);
}
return add_validated_piece(piece_i, data);
}
td::Status Torrent::add_proof(td::Ref<vm::Cell> proof) {
if (!inited_info_) {
return td::Status::Error("Torrent info not inited");
}
return merkle_tree_.add_proof(std::move(proof));
}
td::Status Torrent::add_pending_piece(td::uint64 piece_i, td::Slice data) {
pending_pieces_[piece_i] = data.str();
if (piece_i < header_pieces_count_) {
auto piece = info_.get_piece_info(piece_i);
auto dest = header_str_.as_slice().substr(piece.offset);
data.truncate(dest.size());
dest.copy_from(data);
not_ready_pending_piece_count_--;
if (not_ready_pending_piece_count_ == 0) {
TorrentHeader header;
auto S = td::unserialize(header, header_str_.as_slice());
if (S.is_ok()) {
S = set_header(std::move(header));
}
if (S.is_error()) {
S = S.move_as_error_prefix("Invalid torrent header: ");
fatal_error_ = S.clone();
return S;
}
if (enabled_write_to_files_) {
add_pending_pieces();
}
}
}
return td::Status::OK();
}
void Torrent::enable_write_to_files() {
if (enabled_write_to_files_) {
return;
}
enabled_write_to_files_ = true;
if (header_) {
add_pending_pieces();
}
}
void Torrent::add_pending_pieces() {
for (auto &p : pending_pieces_) {
td::Status S = add_validated_piece(p.first, std::move(p.second));
if (S.is_error()) {
LOG(WARNING) << "Failed to add pending piece #" << p.first << ": " << S;
}
}
pending_pieces_.clear();
}
std::string Torrent::get_chunk_path(td::Slice name) const {
return PSTRING() << root_dir_.value() << TD_DIR_SLASH << header_.value().dir_name << TD_DIR_SLASH << name;
}
std::string Torrent::get_file_path(size_t i) const {
return get_chunk_path(chunks_.at(i + 1).name);
}
td::Status Torrent::init_chunk_data(ChunkState &chunk) {
if (chunk.data) {
return td::Status::OK();
}
if (root_dir_) {
std::string path = get_chunk_path(chunk.name);
TRY_STATUS(td::mkpath(path));
TRY_RESULT(data, td::FileNoCacheBlobView::create(path, chunk.size, true));
chunk.data = std::move(data);
} else {
chunk.data = td::BufferSliceBlobView::create(td::BufferSlice(chunk.size));
}
return td::Status::OK();
}
td::Status Torrent::add_validated_piece(td::uint64 piece_i, td::Slice data) {
CHECK(!chunks_.empty());
auto piece = info_.get_piece_info(piece_i);
std::set<size_t> excluded;
TRY_STATUS(iterate_piece(piece, [&](auto it, auto info) {
if (it->excluded) {
excluded.insert(it - chunks_.begin());
return td::Status::OK();
}
TRY_STATUS(init_chunk_data(*it));
TRY_STATUS(it->write_piece(data.substr(info.piece_offset, info.size), info.chunk_offset));
return td::Status::OK();
}));
TRY_STATUS(iterate_piece(piece, [&](auto it, auto info) {
if (!it->excluded) {
it->ready_size += info.size;
included_ready_size_ += info.size;
}
return td::Status::OK();
}));
piece_is_ready_[piece_i] = true;
not_ready_piece_count_--;
if (!excluded.empty()) {
in_memory_pieces_[piece_i] = {data.str(), std::move(excluded)};
}
return td::Status::OK();
}
bool Torrent::is_completed() const {
return inited_info_ && enabled_write_to_files_ && included_ready_size_ == included_size_;
}
td::Result<td::BufferSlice> Torrent::read_file(td::Slice name) {
if (!inited_info_) {
return td::Status::Error("Torrent info not inited");
}
for (auto &chunk : chunks_) {
if (chunk.name == name) {
td::BufferSlice res(chunk.size);
TRY_STATUS(chunk.get_piece(res.as_slice(), 0));
return std::move(res);
}
}
return td::Status::Error("Unknown name");
}
Torrent::GetMetaOptions::GetMetaOptions() = default;
std::string Torrent::get_meta_str(const GetMetaOptions &options) const {
CHECK(inited_info_);
return get_meta(options).serialize();
}
TorrentMeta Torrent::get_meta(const GetMetaOptions &options) const {
CHECK(inited_info_);
TorrentMeta torrent_file;
if (options.with_header) {
torrent_file.header = header_;
}
torrent_file.info = info_;
torrent_file.info.init_cell();
if (options.with_proof) {
torrent_file.root_proof = merkle_tree_.get_root(options.proof_depth_limit);
}
return torrent_file;
}
Torrent::Torrent(td::Bits256 hash) : hash_(hash), inited_info_(false) {
}
Torrent::Torrent(Info info, td::optional<TorrentHeader> header, ton::MerkleTree tree, std::vector<ChunkState> chunks,
std::string root_dir)
: hash_(info.get_hash())
, inited_info_(true)
, info_(info)
, root_dir_(std::move(root_dir))
, header_(std::move(header))
, enabled_write_to_files_(true)
, merkle_tree_(std::move(tree))
, piece_is_ready_(info_.pieces_count(), true)
, ready_parts_count_{info_.pieces_count()}
, chunks_(std::move(chunks))
, included_size_(info_.file_size)
, included_ready_size_(info_.file_size) {
}
td::Status Torrent::set_header(TorrentHeader header) {
if (header_) {
return td::Status::OK();
}
auto header_str = header.serialize();
td::Bits256 header_hash;
td::sha256(header_str.as_slice(), header_hash.as_slice());
if (header_hash != info_.header_hash) {
return td::Status::Error("Incorrect header hash");
}
TRY_STATUS_PREFIX(header.validate(info_.file_size, info_.header_size), "Invalid torrent header: ");
auto add_chunk = [&](td::Slice name, td::uint64 offset, td::uint64 size) {
ChunkState chunk;
chunk.name = name.str();
chunk.ready_size = 0;
chunk.size = size;
chunk.offset = offset;
chunks_.push_back(std::move(chunk));
included_size_ += size;
};
add_chunk("", 0, header_str.size());
chunks_.back().data = td::BufferSliceBlobView::create(std::move(header_str));
for (size_t i = 0; i < header.files_count; i++) {
auto l = header.get_data_begin(i);
auto r = header.get_data_end(i);
add_chunk(header.get_name(i), l, r - l);
}
header_ = std::move(header);
return td::Status::OK();
}
size_t Torrent::get_ready_parts_count() const {
return ready_parts_count_;
}
std::vector<size_t> Torrent::chunks_by_piece(td::uint64 piece_id) {
CHECK(inited_info_);
std::vector<size_t> res;
auto piece = info_.get_piece_info(piece_id);
auto is_ok = iterate_piece(piece, [&](auto it, auto info) {
res.push_back(it - chunks_.begin());
return td::Status::OK();
});
return res;
}
td::Status Torrent::init_info(Info info) {
if (hash_ != info.get_hash()) {
return td::Status::Error("Hash mismatch");
}
if (inited_info_) {
return td::Status::OK();
}
auto S = info.validate();
if (S.is_error()) {
S = S.move_as_error_prefix("Invalid torrent info: ");
fatal_error_ = S.clone();
return S;
}
inited_info_ = true;
info_ = std::move(info);
merkle_tree_ = MerkleTree(info_.pieces_count(), info_.root_hash);
piece_is_ready_.resize(info_.pieces_count(), false);
not_ready_piece_count_ = piece_is_ready_.size();
header_pieces_count_ = (info_.header_size + info_.piece_size - 1) / info_.piece_size;
not_ready_pending_piece_count_ = header_pieces_count_;
header_str_ = td::BufferSlice(info_.header_size);
return td::Status::OK();
}
void Torrent::set_file_excluded(size_t i, bool excluded) {
CHECK(header_);
CHECK(i + 1 < chunks_.size());
if (!root_dir_) {
return; // All files are in-memory, nothing to do
}
size_t chunk_i = i + 1;
auto &chunk = chunks_[chunk_i];
if (chunk.excluded == excluded) {
return;
}
if (excluded) {
included_size_ -= chunk.size;
included_ready_size_ -= chunk.ready_size;
} else {
included_size_ += chunk.size;
included_ready_size_ += chunk.ready_size;
}
chunk.excluded = excluded;
if (!enabled_write_to_files_ || excluded) {
return;
}
auto range = get_file_parts_range(i);
for (auto it = in_memory_pieces_.lower_bound(range.begin); it != in_memory_pieces_.end() && it->first < range.end;) {
if (!it->second.pending_chunks.count(chunk_i)) {
++it;
continue;
}
auto piece_i = it->first;
auto piece = info_.get_piece_info(piece_i);
auto S = [&]() {
auto l = td::max(chunk.offset, piece.offset);
auto r = td::min(chunk.offset + chunk.size, piece.offset + piece.size);
TRY_STATUS(init_chunk_data(chunk));
TRY_STATUS(chunk.write_piece(it->second.data.substr(l - piece.offset, r - l), l - chunk.offset));
chunk.ready_size += r - l;
included_ready_size_ += r - l;
return td::Status::OK();
}();
if (S.is_error()) {
// Erase piece completely
piece_is_ready_[piece_i] = false;
not_ready_piece_count_++;
iterate_piece(piece, [&](auto it2, auto info) {
if (!it2->excluded) {
included_ready_size_ -= info.size;
}
if (!it2->excluded || !it->second.pending_chunks.count(it2 - chunks_.begin())) {
it2->ready_size -= info.size;
}
return td::Status::OK();
});
it = in_memory_pieces_.erase(it);
continue;
}
it->second.pending_chunks.erase(chunk_i);
if (it->second.pending_chunks.empty()) {
it = in_memory_pieces_.erase(it);
} else {
++it;
}
}
}
void Torrent::load_from_files(std::string files_path) {
CHECK(inited_header());
std::vector<td::optional<td::BlobView>> new_blobs;
new_blobs.push_back(td::BufferSliceBlobView::create(get_header().serialize()));
size_t files_count = get_files_count().unwrap();
for (size_t i = 0; i < files_count; ++i) {
std::string new_path = PSTRING() << files_path << TD_DIR_SLASH << header_.value().dir_name << TD_DIR_SLASH
<< get_file_name(i);
auto R = td::FileNoCacheBlobView::create(new_path, get_file_size(i), false);
if (R.is_error() && files_count == 1) {
R = td::FileNoCacheBlobView::create(files_path, get_file_size(i), false);
}
if (R.is_error()) {
new_blobs.emplace_back();
} else {
new_blobs.push_back(R.move_as_ok());
}
}
auto load_new_piece = [&](size_t piece_i) -> td::Result<std::string> {
auto piece = info_.get_piece_info(piece_i);
bool included = false;
TRY_STATUS(iterate_piece(piece, [&](auto it, IterateInfo info) {
if (!it->excluded) {
included = true;
}
return td::Status::OK();
}));
if (!included) {
return td::Status::Error("Piece is excluded");
}
std::string data(piece.size, '\0');
TRY_STATUS(iterate_piece(piece, [&](auto it, IterateInfo info) {
size_t chunk_i = it - chunks_.begin();
if (!new_blobs[chunk_i]) {
return td::Status::Error("No such file");
}
TRY_RESULT(s, new_blobs[chunk_i].value().view_copy(td::MutableSlice(data).substr(info.piece_offset, info.size),
info.chunk_offset));
if (s != info.size) {
return td::Status::Error("Can't read file");
}
return td::Status::OK();
}));
return data;
};
std::vector<std::pair<size_t, td::Bits256>> new_pieces;
for (size_t i = 0; i < piece_is_ready_.size(); ++i) {
if (piece_is_ready_[i]) {
continue;
}
auto r_data = load_new_piece(i);
if (r_data.is_error()) {
continue;
}
td::Bits256 hash;
td::sha256(r_data.ok(), hash.as_slice());
new_pieces.emplace_back(i, hash);
}
size_t added_cnt = 0;
for (size_t i : merkle_tree_.add_pieces(std::move(new_pieces))) {
auto r_data = load_new_piece(i);
if (r_data.is_error()) {
continue;
}
if (add_piece(i, r_data.ok(), {}).is_ok()) {
++added_cnt;
}
}
if (added_cnt > 0) {
LOG(INFO) << "Loaded " << added_cnt << " new pieces for " << get_hash().to_hex();
}
}
td::Status Torrent::copy_to(const std::string &new_root_dir) {
if (!is_completed() || included_size_ != info_.file_size) {
return td::Status::Error("Torrent::copy_to is allowed only for fully completed torrents");
}
auto get_new_chunk_path = [&](td::Slice name) -> std::string {
return PSTRING() << new_root_dir << TD_DIR_SLASH << header_.value().dir_name << TD_DIR_SLASH << name;
};
std::vector<td::BlobView> new_blobs;
for (size_t i = 1; i < chunks_.size(); ++i) {
auto &chunk = chunks_[i];
std::string new_path = get_new_chunk_path(chunk.name);
TRY_STATUS(td::mkpath(new_path));
TRY_RESULT(new_blob, td::FileNoCacheBlobView::create(new_path, chunk.size, true));
static const td::uint64 BUF_SIZE = 1 << 17;
td::BufferSlice buf(BUF_SIZE);
for (td::uint64 l = 0; l < chunk.size; l += BUF_SIZE) {
td::uint64 r = std::min(chunk.size, l + BUF_SIZE);
TRY_RESULT_PREFIX(s, chunk.data.view(buf.as_slice().substr(0, r - l), l),
PSTRING() << "Failed to read " << chunk.name << ": ");
if (s.size() != r - l) {
return td::Status::Error(PSTRING() << "Failed to read " << chunk.name);
}
TRY_STATUS_PREFIX(new_blob.write(s, l), PSTRING() << "Failed to write " << chunk.name << ": ");
}
new_blobs.push_back(std::move(new_blob));
}
root_dir_ = new_root_dir;
for (size_t i = 1; i < chunks_.size(); ++i) {
chunks_[i].data = std::move(new_blobs[i - 1]);
}
return td::Status::OK();
}
} // namespace ton