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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
|
/* <!-- copyright */
/*
* aria2 - The high speed download utility
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*/
/* copyright --> */
#include "RequestGroup.h"
#include <cassert>
#include <algorithm>
#include "PostDownloadHandler.h"
#include "DownloadEngine.h"
#include "SegmentMan.h"
#include "NullProgressInfoFile.h"
#include "Dependency.h"
#include "prefs.h"
#include "CreateRequestCommand.h"
#include "File.h"
#include "message.h"
#include "util.h"
#include "LogFactory.h"
#include "Logger.h"
#include "DiskAdaptor.h"
#include "DiskWriterFactory.h"
#include "RecoverableException.h"
#include "StreamCheckIntegrityEntry.h"
#include "CheckIntegrityCommand.h"
#include "UnknownLengthPieceStorage.h"
#include "DownloadContext.h"
#include "DlAbortEx.h"
#include "DownloadFailureException.h"
#include "RequestGroupMan.h"
#include "DefaultBtProgressInfoFile.h"
#include "DefaultPieceStorage.h"
#include "DownloadHandlerFactory.h"
#include "MemoryBufferPreDownloadHandler.h"
#include "DownloadHandlerConstants.h"
#include "Option.h"
#include "FileEntry.h"
#include "Request.h"
#include "FileAllocationIterator.h"
#include "fmt.h"
#include "A2STR.h"
#include "URISelector.h"
#include "InOrderURISelector.h"
#include "PieceSelector.h"
#include "a2functional.h"
#include "SocketCore.h"
#include "SimpleRandomizer.h"
#include "Segment.h"
#ifdef ENABLE_MESSAGE_DIGEST
# include "CheckIntegrityCommand.h"
# include "ChecksumCheckIntegrityEntry.h"
#endif // ENABLE_MESSAGE_DIGEST
#ifdef ENABLE_BITTORRENT
# include "bittorrent_helper.h"
# include "BtRegistry.h"
# include "BtCheckIntegrityEntry.h"
# include "DefaultPeerStorage.h"
# include "DefaultBtAnnounce.h"
# include "BtRuntime.h"
# include "BtSetup.h"
# include "BtPostDownloadHandler.h"
# include "DHTSetup.h"
# include "DHTRegistry.h"
# include "DHTNode.h"
# include "DHTRoutingTable.h"
# include "DHTTaskQueue.h"
# include "DHTTaskFactory.h"
# include "DHTTokenTracker.h"
# include "DHTMessageDispatcher.h"
# include "DHTMessageReceiver.h"
# include "DHTMessageFactory.h"
# include "DHTMessageCallback.h"
# include "BtMessageFactory.h"
# include "BtRequestFactory.h"
# include "BtMessageDispatcher.h"
# include "BtMessageReceiver.h"
# include "PeerConnection.h"
# include "ExtensionMessageFactory.h"
# include "DHTPeerAnnounceStorage.h"
# include "DHTEntryPointNameResolveCommand.h"
# include "LongestSequencePieceSelector.h"
# include "PriorityPieceSelector.h"
# include "bittorrent_helper.h"
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
# include "MetalinkPostDownloadHandler.h"
#endif // ENABLE_METALINK
namespace aria2 {
gid_t RequestGroup::gidCounter_ = 0;
RequestGroup::RequestGroup(const SharedHandle<Option>& option)
: gid_(newGID()),
option_(new Option(*option.get())),
numConcurrentCommand_(option->getAsInt(PREF_SPLIT)),
numStreamConnection_(0),
numStreamCommand_(0),
numCommand_(0),
saveControlFile_(true),
progressInfoFile_(new NullProgressInfoFile()),
preLocalFileCheckEnabled_(true),
haltRequested_(false),
forceHaltRequested_(false),
haltReason_(RequestGroup::NONE),
pauseRequested_(false),
uriSelector_(new InOrderURISelector()),
lastModifiedTime_(Time::null()),
fileNotFoundCount_(0),
timeout_(option->getAsInt(PREF_TIMEOUT)),
#ifdef ENABLE_BITTORRENT
btRuntime_(0),
peerStorage_(0),
#endif // ENABLE_BITTORRENT
inMemoryDownload_(false),
maxDownloadSpeedLimit_(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT)),
maxUploadSpeedLimit_(option->getAsInt(PREF_MAX_UPLOAD_LIMIT)),
lastErrorCode_(error_code::UNDEFINED),
belongsToGID_(0),
requestGroupMan_(0),
resumeFailureCount_(0)
{
fileAllocationEnabled_ = option_->get(PREF_FILE_ALLOCATION) != V_NONE;
// Add types to be sent as a Accept header value here.
// It would be good to put this value in Option so that user can tweak
// and add this list.
// The mime types of Metalink is used for `transparent metalink'.
addAcceptType(DownloadHandlerConstants::getMetalinkContentTypes().begin(),
DownloadHandlerConstants::getMetalinkContentTypes().end());
if(!option_->getAsBool(PREF_DRY_RUN)) {
initializePreDownloadHandler();
initializePostDownloadHandler();
}
}
RequestGroup::~RequestGroup() {}
bool RequestGroup::isCheckIntegrityReady() const
{
return option_->getAsBool(PREF_CHECK_INTEGRITY) &&
(downloadContext_->isChecksumVerificationAvailable() ||
downloadContext_->isPieceHashVerificationAvailable());
}
bool RequestGroup::downloadFinished() const
{
if(!pieceStorage_) {
return false;
} else {
return pieceStorage_->downloadFinished();
}
}
bool RequestGroup::allDownloadFinished() const
{
if(!pieceStorage_) {
return false;
} else {
return pieceStorage_->allDownloadFinished();
}
}
error_code::Value RequestGroup::downloadResult() const
{
if(downloadFinished() && !downloadContext_->isChecksumVerificationNeeded())
return error_code::FINISHED;
else {
if(lastErrorCode_ == error_code::UNDEFINED) {
if(haltReason_ == RequestGroup::USER_REQUEST ||
haltReason_ == RequestGroup::SHUTDOWN_SIGNAL) {
return error_code::IN_PROGRESS;
} else {
return error_code::UNKNOWN_ERROR;
}
} else {
return lastErrorCode_;
}
}
}
void RequestGroup::closeFile()
{
if(pieceStorage_) {
pieceStorage_->getDiskAdaptor()->closeFile();
}
}
// TODO The function name is not intuitive at all.. it does not convey
// that this function open file.
SharedHandle<CheckIntegrityEntry> RequestGroup::createCheckIntegrityEntry()
{
BtProgressInfoFileHandle infoFile
(new DefaultBtProgressInfoFile(downloadContext_, pieceStorage_,
option_.get()));
SharedHandle<CheckIntegrityEntry> checkEntry;
if(option_->getAsBool(PREF_CHECK_INTEGRITY) &&
downloadContext_->isPieceHashVerificationAvailable()) {
// When checking piece hash, we don't care file is downloaded and
// infoFile exists.
loadAndOpenFile(infoFile);
checkEntry.reset(new StreamCheckIntegrityEntry(this));
} else if(infoFile->exists()) {
loadAndOpenFile(infoFile);
if(downloadFinished()) {
#ifdef ENABLE_MESSAGE_DIGEST
if(downloadContext_->isChecksumVerificationNeeded()) {
A2_LOG_INFO(MSG_HASH_CHECK_NOT_DONE);
ChecksumCheckIntegrityEntry* tempEntry =
new ChecksumCheckIntegrityEntry(this);
tempEntry->setRedownload(true);
checkEntry.reset(tempEntry);
} else
#endif // ENABLE_MESSAGE_DIGEST
{
downloadContext_->setChecksumVerified(true);
A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
util::itos(gid_).c_str(),
downloadContext_->getBasePath().c_str()));
}
} else {
checkEntry.reset(new StreamCheckIntegrityEntry(this));
}
} else if(downloadFinishedByFileLength()) {
pieceStorage_->markAllPiecesDone();
#ifdef ENABLE_MESSAGE_DIGEST
if(option_->getAsBool(PREF_CHECK_INTEGRITY) &&
downloadContext_->isChecksumVerificationAvailable()) {
loadAndOpenFile(infoFile);
ChecksumCheckIntegrityEntry* tempEntry =
new ChecksumCheckIntegrityEntry(this);
tempEntry->setRedownload(true);
checkEntry.reset(tempEntry);
} else
#endif // ENABLE_MESSAGE_DIGEST
{
downloadContext_->setChecksumVerified(true);
A2_LOG_NOTICE(fmt(MSG_DOWNLOAD_ALREADY_COMPLETED,
util::itos(gid_).c_str(),
downloadContext_->getBasePath().c_str()));
}
} else {
loadAndOpenFile(infoFile);
checkEntry.reset(new StreamCheckIntegrityEntry(this));
}
return checkEntry;
}
void RequestGroup::createInitialCommand
(std::vector<Command*>& commands, DownloadEngine* e)
{
// Start session timer here. When file size becomes known, it will
// be reset again in *FileAllocationEntry, because hash check and
// file allocation takes a time. For downloads in which file size
// is unknown, session timer will not be reset.
downloadContext_->resetDownloadStartTime();
#ifdef ENABLE_BITTORRENT
{
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
SharedHandle<TorrentAttribute> torrentAttrs =
bittorrent::getTorrentAttrs(downloadContext_);
bool metadataGetMode = torrentAttrs->metadata.empty();
if(option_->getAsBool(PREF_DRY_RUN)) {
throw DOWNLOAD_FAILURE_EXCEPTION
("Cancel BitTorrent download in dry-run context.");
}
SharedHandle<BtRegistry> btRegistry = e->getBtRegistry();
if(btRegistry->getDownloadContext(torrentAttrs->infoHash)) {
// TODO If metadataGetMode == false and each FileEntry has
// URI, then go without BT.
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt("InfoHash %s is already registered.",
bittorrent::getInfoHashString(downloadContext_).c_str()),
error_code::DUPLICATE_INFO_HASH);
}
if(metadataGetMode) {
// Use UnknownLengthPieceStorage.
initPieceStorage();
} else {
if(e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) {
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
downloadContext_->getBasePath().c_str()),
error_code::DUPLICATE_DOWNLOAD);
}
initPieceStorage();
if(downloadContext_->getFileEntries().size() > 1) {
pieceStorage_->setupFileFilter();
}
}
SharedHandle<DefaultBtProgressInfoFile> progressInfoFile;
if(!metadataGetMode) {
progressInfoFile.reset(new DefaultBtProgressInfoFile(downloadContext_,
pieceStorage_,
option_.get()));
}
BtRuntimeHandle btRuntime(new BtRuntime());
btRuntime->setMaxPeers(option_->getAsInt(PREF_BT_MAX_PEERS));
btRuntime_ = btRuntime.get();
if(progressInfoFile) {
progressInfoFile->setBtRuntime(btRuntime);
}
SharedHandle<DefaultPeerStorage> peerStorage(new DefaultPeerStorage());
peerStorage->setBtRuntime(btRuntime);
peerStorage->setPieceStorage(pieceStorage_);
peerStorage_ = peerStorage.get();
if(progressInfoFile) {
progressInfoFile->setPeerStorage(peerStorage);
}
SharedHandle<DefaultBtAnnounce> btAnnounce
(new DefaultBtAnnounce(downloadContext_, option_.get()));
btAnnounce->setBtRuntime(btRuntime);
btAnnounce->setPieceStorage(pieceStorage_);
btAnnounce->setPeerStorage(peerStorage);
btAnnounce->setUserDefinedInterval
(option_->getAsInt(PREF_BT_TRACKER_INTERVAL));
btAnnounce->shuffleAnnounce();
assert(btRegistry->get(gid_).isNull());
btRegistry->put
(gid_, BtObject
(downloadContext_,
pieceStorage_,
peerStorage,
btAnnounce,
btRuntime,
(progressInfoFile ?
SharedHandle<BtProgressInfoFile>(progressInfoFile) :
progressInfoFile_)));
if(metadataGetMode) {
if(option_->getAsBool(PREF_ENABLE_DHT) ||
(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6))) {
if(option_->getAsBool(PREF_ENABLE_DHT)) {
std::vector<Command*> dhtCommands;
DHTSetup().setup(dhtCommands, e, AF_INET);
e->addCommand(dhtCommands);
}
if(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6)) {
std::vector<Command*> dhtCommands;
DHTSetup().setup(dhtCommands, e, AF_INET6);
e->addCommand(dhtCommands);
}
} else {
A2_LOG_NOTICE("For BitTorrent Magnet URI, enabling DHT is strongly"
" recommended. See --enable-dht option.");
}
SharedHandle<CheckIntegrityEntry> entry
(new BtCheckIntegrityEntry(this));
entry->onDownloadIncomplete(commands, e);
return;
}
removeDefunctControlFile(progressInfoFile);
{
uint64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size();
if(actualFileSize == downloadContext_->getTotalLength()) {
// First, make DiskAdaptor read-only mode to allow the
// program to seed file in read-only media.
pieceStorage_->getDiskAdaptor()->enableReadOnly();
} else {
// Open file in writable mode to allow the program
// truncate the file to downloadContext_->getTotalLength()
A2_LOG_DEBUG
(fmt("File size not match. File is opened in writable"
" mode. Expected:%s Actual:%s",
util::uitos(downloadContext_->getTotalLength()).c_str(),
util::uitos(actualFileSize).c_str()));
}
}
// Call Load, Save and file allocation command here
if(progressInfoFile->exists()) {
// load .aria2 file if it exists.
progressInfoFile->load();
pieceStorage_->getDiskAdaptor()->openFile();
} else {
if(pieceStorage_->getDiskAdaptor()->fileExists()) {
if(!option_->getAsBool(PREF_CHECK_INTEGRITY) &&
!option_->getAsBool(PREF_ALLOW_OVERWRITE) &&
!option_->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
// TODO we need this->haltRequested = true?
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt(MSG_FILE_ALREADY_EXISTS,
downloadContext_->getBasePath().c_str()),
error_code::FILE_ALREADY_EXISTS);
} else {
pieceStorage_->getDiskAdaptor()->openFile();
}
if(option_->getAsBool(PREF_BT_SEED_UNVERIFIED)) {
pieceStorage_->markAllPiecesDone();
}
} else {
pieceStorage_->getDiskAdaptor()->openFile();
}
}
progressInfoFile_ = progressInfoFile;
if(!torrentAttrs->privateTorrent &&
(option_->getAsBool(PREF_ENABLE_DHT) ||
(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6)))) {
if(option_->getAsBool(PREF_ENABLE_DHT)) {
std::vector<Command*> dhtCommands;
DHTSetup().setup(dhtCommands, e, AF_INET);
e->addCommand(dhtCommands);
}
if(!e->getOption()->getAsBool(PREF_DISABLE_IPV6) &&
option_->getAsBool(PREF_ENABLE_DHT6)) {
std::vector<Command*> dhtCommands;
DHTSetup().setup(dhtCommands, e, AF_INET6);
e->addCommand(dhtCommands);
}
const std::vector<std::pair<std::string, uint16_t> >& nodes =
torrentAttrs->nodes;
// TODO Are nodes in torrent IPv4 only?
if(!nodes.empty() && DHTRegistry::isInitialized()) {
DHTEntryPointNameResolveCommand* command =
new DHTEntryPointNameResolveCommand(e->newCUID(), e, nodes);
command->setTaskQueue(DHTRegistry::getData().taskQueue);
command->setTaskFactory(DHTRegistry::getData().taskFactory);
command->setRoutingTable(DHTRegistry::getData().routingTable);
command->setLocalNode(DHTRegistry::getData().localNode);
e->addCommand(command);
}
}
SharedHandle<CheckIntegrityEntry> entry(new BtCheckIntegrityEntry(this));
// --bt-seed-unverified=true is given and download has completed, skip
// validation for piece hashes.
if(option_->getAsBool(PREF_BT_SEED_UNVERIFIED) &&
pieceStorage_->downloadFinished()) {
entry->onDownloadFinished(commands, e);
} else {
processCheckIntegrityEntry(commands, entry, e);
}
return;
}
}
#endif // ENABLE_BITTORRENT
if(downloadContext_->getFileEntries().size() == 1) {
// TODO I assume here when totallength is set to DownloadContext and it is
// not 0, then filepath is also set DownloadContext correctly....
if(option_->getAsBool(PREF_DRY_RUN) ||
downloadContext_->getTotalLength() == 0) {
createNextCommand(commands, e, 1);
} else {
if(e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) {
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
downloadContext_->getBasePath().c_str()),
error_code::DUPLICATE_DOWNLOAD);
}
SharedHandle<BtProgressInfoFile> progressInfoFile
(new DefaultBtProgressInfoFile
(downloadContext_, SharedHandle<PieceStorage>(), option_.get()));
adjustFilename(progressInfoFile);
initPieceStorage();
SharedHandle<CheckIntegrityEntry> checkEntry =
createCheckIntegrityEntry();
if(checkEntry) {
processCheckIntegrityEntry(commands, checkEntry, e);
}
}
} else {
// TODO --dry-run is not supported for multifile download for now.
if(option_->getAsBool(PREF_DRY_RUN)) {
throw DOWNLOAD_FAILURE_EXCEPTION
("--dry-run in multi-file download is not supported yet.");
}
// TODO file size is known in this context?
// In this context, multiple FileEntry objects are in
// DownloadContext.
if(e->getRequestGroupMan()->isSameFileBeingDownloaded(this)) {
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt(EX_DUPLICATE_FILE_DOWNLOAD,
downloadContext_->getBasePath().c_str()),
error_code::DUPLICATE_DOWNLOAD);
}
initPieceStorage();
if(downloadContext_->getFileEntries().size() > 1) {
pieceStorage_->setupFileFilter();
}
SharedHandle<DefaultBtProgressInfoFile> progressInfoFile
(new DefaultBtProgressInfoFile(downloadContext_,
pieceStorage_,
option_.get()));
removeDefunctControlFile(progressInfoFile);
// Call Load, Save and file allocation command here
if(progressInfoFile->exists()) {
// load .aria2 file if it exists.
progressInfoFile->load();
pieceStorage_->getDiskAdaptor()->openFile();
} else {
if(pieceStorage_->getDiskAdaptor()->fileExists()) {
if(!isCheckIntegrityReady() &&
!option_->getAsBool(PREF_ALLOW_OVERWRITE)) {
// TODO we need this->haltRequested = true?
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt(MSG_FILE_ALREADY_EXISTS,
downloadContext_->getBasePath().c_str()),
error_code::FILE_ALREADY_EXISTS);
} else {
pieceStorage_->getDiskAdaptor()->openFile();
}
} else {
pieceStorage_->getDiskAdaptor()->openFile();
}
}
progressInfoFile_ = progressInfoFile;
SharedHandle<CheckIntegrityEntry> checkIntegrityEntry
(new StreamCheckIntegrityEntry(this));
processCheckIntegrityEntry(commands, checkIntegrityEntry, e);
}
}
void RequestGroup::processCheckIntegrityEntry
(std::vector<Command*>& commands,
const SharedHandle<CheckIntegrityEntry>& entry,
DownloadEngine* e)
{
uint64_t actualFileSize = pieceStorage_->getDiskAdaptor()->size();
if(actualFileSize > downloadContext_->getTotalLength()) {
entry->cutTrailingGarbage();
}
#ifdef ENABLE_MESSAGE_DIGEST
if((option_->getAsBool(PREF_CHECK_INTEGRITY) ||
downloadContext_->isChecksumVerificationNeeded()) &&
entry->isValidationReady()) {
entry->initValidator();
// Don't save control file(.aria2 file) when user presses
// control-c key while aria2 is checking hashes. If control file
// doesn't exist when aria2 launched, the completed length in
// saved control file will be 0 byte and this confuses user.
// enableSaveControlFile() will be called after hash checking is
// done. See CheckIntegrityCommand.
disableSaveControlFile();
e->getCheckIntegrityMan()->pushEntry(entry);
} else
#endif // ENABLE_MESSAGE_DIGEST
{
entry->onDownloadIncomplete(commands, e);
}
}
void RequestGroup::initPieceStorage()
{
SharedHandle<PieceStorage> tempPieceStorage;
if(downloadContext_->knowsTotalLength() &&
// Following conditions are needed for chunked encoding with
// content-length = 0. Google's dl server used this before.
(downloadContext_->getTotalLength() > 0
#ifdef ENABLE_BITTORRENT
|| downloadContext_->hasAttribute(bittorrent::BITTORRENT)
#endif // ENABLE_BITTORRENT
)) {
#ifdef ENABLE_BITTORRENT
DefaultPieceStorage* ps =
new DefaultPieceStorage(downloadContext_, option_.get());
SharedHandle<PieceStorage> psHolder(ps);
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
if(isUriSuppliedForRequsetFileEntry
(downloadContext_->getFileEntries().begin(),
downloadContext_->getFileEntries().end())) {
// Use LongestSequencePieceSelector when HTTP/FTP/BitTorrent
// integrated downloads. Currently multi-file integrated
// download is not supported.
A2_LOG_DEBUG("Using LongestSequencePieceSelector");
SharedHandle<PieceSelector> longestPieceSelector
(new LongestSequencePieceSelector());
ps->setPieceSelector(longestPieceSelector);
}
if(option_->defined(PREF_BT_PRIORITIZE_PIECE)) {
std::vector<size_t> result;
util::parsePrioritizePieceRange
(result, option_->get(PREF_BT_PRIORITIZE_PIECE),
downloadContext_->getFileEntries(),
downloadContext_->getPieceLength());
if(!result.empty()) {
std::random_shuffle(result.begin(), result.end(),
*(SimpleRandomizer::getInstance().get()));
SharedHandle<PriorityPieceSelector> priSelector
(new PriorityPieceSelector(ps->getPieceSelector()));
priSelector->setPriorityPiece(result.begin(), result.end());
ps->setPieceSelector(priSelector);
}
}
}
#else // !ENABLE_BITTORRENT
DefaultPieceStorage* ps =
new DefaultPieceStorage(downloadContext_, option_.get());
SharedHandle<PieceStorage> psHolder(ps);
#endif // !ENABLE_BITTORRENT
if(diskWriterFactory_) {
ps->setDiskWriterFactory(diskWriterFactory_);
}
tempPieceStorage.swap(psHolder);
} else {
UnknownLengthPieceStorage* ps =
new UnknownLengthPieceStorage(downloadContext_, option_.get());
SharedHandle<PieceStorage> psHolder(ps);
if(diskWriterFactory_) {
ps->setDiskWriterFactory(diskWriterFactory_);
}
tempPieceStorage.swap(psHolder);
}
tempPieceStorage->initStorage();
SharedHandle<SegmentMan> tempSegmentMan
(new SegmentMan(option_.get(), downloadContext_, tempPieceStorage));
pieceStorage_.swap(tempPieceStorage);
segmentMan_.swap(tempSegmentMan);
}
void RequestGroup::dropPieceStorage()
{
segmentMan_.reset();
pieceStorage_.reset();
}
bool RequestGroup::downloadFinishedByFileLength()
{
// assuming that a control file doesn't exist.
if(!isPreLocalFileCheckEnabled() ||
option_->getAsBool(PREF_ALLOW_OVERWRITE)) {
return false;
}
if(!downloadContext_->knowsTotalLength()) {
return false;
}
File outfile(getFirstFilePath());
if(outfile.exists() && downloadContext_->getTotalLength() == outfile.size()) {
return true;
} else {
return false;
}
}
void RequestGroup::adjustFilename
(const SharedHandle<BtProgressInfoFile>& infoFile)
{
if(!isPreLocalFileCheckEnabled()) {
// OK, no need to care about filename.
return;
}
if(!option_->getAsBool(PREF_DRY_RUN) &&
option_->getAsBool(PREF_REMOVE_CONTROL_FILE) &&
infoFile->exists()) {
infoFile->removeFile();
A2_LOG_NOTICE(fmt("Removed control file for %s because it is requested by"
" user.",
infoFile->getFilename().c_str()));
}
if(infoFile->exists()) {
// Use current filename
} else if(downloadFinishedByFileLength()) {
// File was downloaded already, no need to change file name.
} else {
File outfile(getFirstFilePath());
if(outfile.exists() && option_->getAsBool(PREF_CONTINUE) &&
outfile.size() <= downloadContext_->getTotalLength()) {
// File exists but user decided to resume it.
} else {
#ifdef ENABLE_MESSAGE_DIGEST
if(outfile.exists() && isCheckIntegrityReady()) {
// check-integrity existing file
} else {
#endif // ENABLE_MESSAGE_DIGEST
shouldCancelDownloadForSafety();
#ifdef ENABLE_MESSAGE_DIGEST
}
#endif // ENABLE_MESSAGE_DIGEST
}
}
}
void RequestGroup::removeDefunctControlFile
(const SharedHandle<BtProgressInfoFile>& progressInfoFile)
{
// Remove the control file if download file doesn't exist
if(progressInfoFile->exists() &&
!pieceStorage_->getDiskAdaptor()->fileExists()) {
progressInfoFile->removeFile();
A2_LOG_NOTICE(fmt(MSG_REMOVED_DEFUNCT_CONTROL_FILE,
progressInfoFile->getFilename().c_str(),
downloadContext_->getBasePath().c_str()));
}
}
void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoFile)
{
try {
if(!isPreLocalFileCheckEnabled()) {
pieceStorage_->getDiskAdaptor()->initAndOpenFile();
return;
}
removeDefunctControlFile(progressInfoFile);
if(progressInfoFile->exists()) {
progressInfoFile->load();
pieceStorage_->getDiskAdaptor()->openExistingFile();
} else {
File outfile(getFirstFilePath());
if(outfile.exists() && option_->getAsBool(PREF_CONTINUE) &&
outfile.size() <= getTotalLength()) {
pieceStorage_->getDiskAdaptor()->openExistingFile();
pieceStorage_->markPiecesDone(outfile.size());
} else {
#ifdef ENABLE_MESSAGE_DIGEST
if(outfile.exists() && isCheckIntegrityReady()) {
pieceStorage_->getDiskAdaptor()->openExistingFile();
} else {
#endif // ENABLE_MESSAGE_DIGEST
pieceStorage_->getDiskAdaptor()->initAndOpenFile();
#ifdef ENABLE_MESSAGE_DIGEST
}
#endif // ENABLE_MESSAGE_DIGEST
}
}
setProgressInfoFile(progressInfoFile);
} catch(RecoverableException& e) {
throw DOWNLOAD_FAILURE_EXCEPTION2(fmt(EX_DOWNLOAD_ABORTED), e);
}
}
// assuming that a control file does not exist
void RequestGroup::shouldCancelDownloadForSafety()
{
if(option_->getAsBool(PREF_ALLOW_OVERWRITE)) {
return;
}
File outfile(getFirstFilePath());
if(outfile.exists()) {
if(option_->getAsBool(PREF_AUTO_FILE_RENAMING)) {
if(tryAutoFileRenaming()) {
A2_LOG_NOTICE(fmt(MSG_FILE_RENAMED, getFirstFilePath().c_str()));
} else {
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt("File renaming failed: %s",
getFirstFilePath().c_str()),
error_code::FILE_RENAMING_FAILED);
}
} else {
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt(MSG_FILE_ALREADY_EXISTS,
getFirstFilePath().c_str()),
error_code::FILE_ALREADY_EXISTS);
}
}
}
bool RequestGroup::tryAutoFileRenaming()
{
std::string filepath = getFirstFilePath();
if(filepath.empty()) {
return false;
}
for(unsigned int i = 1; i < 10000; ++i) {
File newfile(strconcat(filepath, A2STR::DOT_C, util::uitos(i)));
File ctrlfile(newfile.getPath()+DefaultBtProgressInfoFile::getSuffix());
if(!newfile.exists() || (newfile.exists() && ctrlfile.exists())) {
downloadContext_->getFirstFileEntry()->setPath(newfile.getPath());
return true;
}
}
return false;
}
void RequestGroup::createNextCommandWithAdj(std::vector<Command*>& commands,
DownloadEngine* e, int numAdj)
{
int numCommand;
if(getTotalLength() == 0) {
numCommand = 1+numAdj;
} else {
numCommand = std::min(downloadContext_->getNumPieces(),
numConcurrentCommand_);
numCommand += numAdj;
}
if(numCommand > 0) {
createNextCommand(commands, e, numCommand);
}
}
void RequestGroup::createNextCommand(std::vector<Command*>& commands,
DownloadEngine* e)
{
int numCommand;
if(getTotalLength() == 0) {
if(numStreamCommand_ > 0) {
numCommand = 0;
} else {
numCommand = 1;
}
} else {
if(numStreamCommand_ >= numConcurrentCommand_) {
numCommand = 0;
} else {
numCommand = std::min(downloadContext_->getNumPieces(),
numConcurrentCommand_-numStreamCommand_);
}
}
if(numCommand > 0) {
createNextCommand(commands, e, numCommand);
}
}
void RequestGroup::createNextCommand(std::vector<Command*>& commands,
DownloadEngine* e,
unsigned int numCommand)
{
for(; numCommand--; ) {
Command* command = new CreateRequestCommand(e->newCUID(), this, e);
commands.push_back(command);
}
if(!commands.empty()) {
e->setNoWait(true);
}
}
std::string RequestGroup::getFirstFilePath() const
{
assert(downloadContext_);
if(inMemoryDownload()) {
static const std::string DIR_MEMORY("[MEMORY]");
return DIR_MEMORY+File(downloadContext_->getFirstFileEntry()->getPath()).getBasename();
} else {
return downloadContext_->getFirstFileEntry()->getPath();
}
}
uint64_t RequestGroup::getTotalLength() const
{
if(!pieceStorage_) {
return 0;
} else {
if(pieceStorage_->isSelectiveDownloadingMode()) {
return pieceStorage_->getFilteredTotalLength();
} else {
return pieceStorage_->getTotalLength();
}
}
}
uint64_t RequestGroup::getCompletedLength() const
{
if(!pieceStorage_) {
return 0;
} else {
if(pieceStorage_->isSelectiveDownloadingMode()) {
return pieceStorage_->getFilteredCompletedLength();
} else {
return pieceStorage_->getCompletedLength();
}
}
}
void RequestGroup::validateFilename(const std::string& expectedFilename,
const std::string& actualFilename) const
{
if(expectedFilename.empty()) {
return;
}
if(expectedFilename != actualFilename) {
throw DL_ABORT_EX(fmt(EX_FILENAME_MISMATCH,
expectedFilename.c_str(),
actualFilename.c_str()));
}
}
void RequestGroup::validateTotalLength(uint64_t expectedTotalLength,
uint64_t actualTotalLength) const
{
if(expectedTotalLength <= 0) {
return;
}
if(expectedTotalLength != actualTotalLength) {
throw DL_ABORT_EX
(fmt(EX_SIZE_MISMATCH,
util::itos(expectedTotalLength, true).c_str(),
util::itos(actualTotalLength, true).c_str()));
}
}
void RequestGroup::validateFilename(const std::string& actualFilename) const
{
validateFilename(downloadContext_->getFileEntries().front()->getBasename(), actualFilename);
}
void RequestGroup::validateTotalLength(uint64_t actualTotalLength) const
{
validateTotalLength(getTotalLength(), actualTotalLength);
}
void RequestGroup::increaseStreamCommand()
{
++numStreamCommand_;
}
void RequestGroup::decreaseStreamCommand()
{
--numStreamCommand_;
}
void RequestGroup::increaseStreamConnection()
{
++numStreamConnection_;
}
void RequestGroup::decreaseStreamConnection()
{
--numStreamConnection_;
}
unsigned int RequestGroup::getNumConnection() const
{
unsigned int numConnection = numStreamConnection_;
#ifdef ENABLE_BITTORRENT
if(btRuntime_) {
numConnection += btRuntime_->getConnections();
}
#endif // ENABLE_BITTORRENT
return numConnection;
}
void RequestGroup::increaseNumCommand()
{
++numCommand_;
}
void RequestGroup::decreaseNumCommand()
{
--numCommand_;
if(!numCommand_ && requestGroupMan_) {
A2_LOG_DEBUG(fmt("GID#%s - Request queue check", util::itos(gid_).c_str()));
requestGroupMan_->requestQueueCheck();
}
}
TransferStat RequestGroup::calculateStat() const
{
TransferStat stat;
#ifdef ENABLE_BITTORRENT
if(peerStorage_) {
stat = peerStorage_->calculateStat();
}
#endif // ENABLE_BITTORRENT
if(segmentMan_) {
stat.setDownloadSpeed
(stat.getDownloadSpeed()+segmentMan_->calculateDownloadSpeed());
stat.setSessionDownloadLength
(stat.getSessionDownloadLength()+
segmentMan_->calculateSessionDownloadLength());
}
return stat;
}
void RequestGroup::setHaltRequested(bool f, HaltReason haltReason)
{
haltRequested_ = f;
if(haltRequested_) {
pauseRequested_ = false;
haltReason_ = haltReason;
}
#ifdef ENABLE_BITTORRENT
if(btRuntime_) {
btRuntime_->setHalt(f);
}
#endif // ENABLE_BITTORRENT
}
void RequestGroup::setForceHaltRequested(bool f, HaltReason haltReason)
{
setHaltRequested(f, haltReason);
forceHaltRequested_ = f;
}
void RequestGroup::setPauseRequested(bool f)
{
pauseRequested_ = f;
}
void RequestGroup::releaseRuntimeResource(DownloadEngine* e)
{
#ifdef ENABLE_BITTORRENT
e->getBtRegistry()->remove(gid_);
#endif // ENABLE_BITTORRENT
if(pieceStorage_) {
pieceStorage_->removeAdvertisedPiece(0);
}
// Don't reset segmentMan_ and pieceStorage_ here to provide
// progress information via XML-RPC
progressInfoFile_.reset();
downloadContext_->releaseRuntimeResource();
}
void RequestGroup::preDownloadProcessing()
{
A2_LOG_DEBUG(fmt("Finding PreDownloadHandler for path %s.",
getFirstFilePath().c_str()));
try {
for(std::vector<SharedHandle<PreDownloadHandler> >::const_iterator itr =
preDownloadHandlers_.begin(), eoi = preDownloadHandlers_.end();
itr != eoi; ++itr) {
if((*itr)->canHandle(this)) {
(*itr)->execute(this);
return;
}
}
} catch(RecoverableException& ex) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
return;
}
A2_LOG_DEBUG("No PreDownloadHandler found.");
return;
}
void RequestGroup::postDownloadProcessing
(std::vector<SharedHandle<RequestGroup> >& groups)
{
A2_LOG_DEBUG(fmt("Finding PostDownloadHandler for path %s.",
getFirstFilePath().c_str()));
try {
for(std::vector<SharedHandle<PostDownloadHandler> >::const_iterator itr =
postDownloadHandlers_.begin(), eoi = postDownloadHandlers_.end();
itr != eoi; ++itr) {
if((*itr)->canHandle(this)) {
(*itr)->getNextRequestGroups(groups, this);
return;
}
}
} catch(RecoverableException& ex) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
}
A2_LOG_DEBUG("No PostDownloadHandler found.");
}
void RequestGroup::initializePreDownloadHandler()
{
#ifdef ENABLE_BITTORRENT
if(option_->get(PREF_FOLLOW_TORRENT) == V_MEM) {
preDownloadHandlers_.push_back(DownloadHandlerFactory::getBtPreDownloadHandler());
}
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(option_->get(PREF_FOLLOW_METALINK) == V_MEM) {
preDownloadHandlers_.push_back(DownloadHandlerFactory::getMetalinkPreDownloadHandler());
}
#endif // ENABLE_METALINK
}
void RequestGroup::initializePostDownloadHandler()
{
#ifdef ENABLE_BITTORRENT
if(option_->getAsBool(PREF_FOLLOW_TORRENT) ||
option_->get(PREF_FOLLOW_TORRENT) == V_MEM) {
postDownloadHandlers_.push_back(DownloadHandlerFactory::getBtPostDownloadHandler());
}
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_METALINK
if(option_->getAsBool(PREF_FOLLOW_METALINK) ||
option_->get(PREF_FOLLOW_METALINK) == V_MEM) {
postDownloadHandlers_.push_back(DownloadHandlerFactory::getMetalinkPostDownloadHandler());
}
#endif // ENABLE_METALINK
}
bool RequestGroup::isDependencyResolved()
{
if(!dependency_) {
return true;
}
return dependency_->resolve();
}
void RequestGroup::dependsOn(const DependencyHandle& dep)
{
dependency_ = dep;
}
void RequestGroup::setDiskWriterFactory(const DiskWriterFactoryHandle& diskWriterFactory)
{
diskWriterFactory_ = diskWriterFactory;
}
void RequestGroup::addPostDownloadHandler
(const SharedHandle<PostDownloadHandler>& handler)
{
postDownloadHandlers_.push_back(handler);
}
void RequestGroup::addPreDownloadHandler
(const SharedHandle<PreDownloadHandler>& handler)
{
preDownloadHandlers_.push_back(handler);
}
void RequestGroup::clearPostDownloadHandler()
{
postDownloadHandlers_.clear();
}
void RequestGroup::clearPreDownloadHandler()
{
preDownloadHandlers_.clear();
}
void RequestGroup::setPieceStorage(const PieceStorageHandle& pieceStorage)
{
pieceStorage_ = pieceStorage;
}
void RequestGroup::setProgressInfoFile(const BtProgressInfoFileHandle& progressInfoFile)
{
progressInfoFile_ = progressInfoFile;
}
bool RequestGroup::needsFileAllocation() const
{
return isFileAllocationEnabled() &&
(uint64_t)option_->getAsLLInt(PREF_NO_FILE_ALLOCATION_LIMIT) <= getTotalLength() &&
!pieceStorage_->getDiskAdaptor()->fileAllocationIterator()->finished();
}
DownloadResultHandle RequestGroup::createDownloadResult() const
{
A2_LOG_DEBUG(fmt("GID#%s - Creating DownloadResult.",
util::itos(gid_).c_str()));
TransferStat st = calculateStat();
SharedHandle<DownloadResult> res(new DownloadResult());
res->gid = gid_;
res->fileEntries = downloadContext_->getFileEntries();
res->inMemoryDownload = inMemoryDownload_;
res->sessionDownloadLength = st.getSessionDownloadLength();
res->sessionTime = downloadContext_->calculateSessionTime();
res->result = downloadResult();
res->followedBy = followedByGIDs_;
res->belongsTo = belongsToGID_;
res->option = option_;
res->metadataInfo = metadataInfo_;
res->totalLength = getTotalLength();
res->completedLength = getCompletedLength();
res->uploadLength = st.getAllTimeUploadLength();
if(pieceStorage_) {
if(pieceStorage_->getBitfieldLength() > 0) {
res->bitfieldStr = util::toHex(pieceStorage_->getBitfield(),
pieceStorage_->getBitfieldLength());
}
}
#ifdef ENABLE_BITTORRENT
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
res->infoHashStr = bittorrent::getInfoHashString(downloadContext_);
}
#endif // ENABLE_BITTORRENT
res->pieceLength = downloadContext_->getPieceLength();
res->numPieces = downloadContext_->getNumPieces();
res->dir = option_->get(PREF_DIR);
return res;
}
void RequestGroup::reportDownloadFinished()
{
A2_LOG_NOTICE(fmt(MSG_FILE_DOWNLOAD_COMPLETED,
downloadContext_->getBasePath().c_str()));
uriSelector_->resetCounters();
#ifdef ENABLE_BITTORRENT
if(downloadContext_->hasAttribute(bittorrent::BITTORRENT)) {
TransferStat stat = calculateStat();
uint64_t completedLength = getCompletedLength();
double shareRatio = completedLength == 0 ? 0.0 :
1.0*stat.getAllTimeUploadLength()/completedLength;
SharedHandle<TorrentAttribute> attrs =
bittorrent::getTorrentAttrs(downloadContext_);
if(!attrs->metadata.empty()) {
A2_LOG_NOTICE(fmt(MSG_SHARE_RATIO_REPORT,
shareRatio,
util::abbrevSize(stat.getAllTimeUploadLength()).c_str(),
util::abbrevSize(completedLength).c_str()));
}
}
#endif // ENABLE_BITTORRENT
}
void RequestGroup::addAcceptType(const std::string& type)
{
if(std::find(acceptTypes_.begin(), acceptTypes_.end(), type) == acceptTypes_.end()) {
acceptTypes_.push_back(type);
}
}
void RequestGroup::removeAcceptType(const std::string& type)
{
acceptTypes_.erase(std::remove(acceptTypes_.begin(), acceptTypes_.end(), type),
acceptTypes_.end());
}
void RequestGroup::setURISelector(const SharedHandle<URISelector>& uriSelector)
{
uriSelector_ = uriSelector;
}
void RequestGroup::applyLastModifiedTimeToLocalFiles()
{
if(pieceStorage_ && lastModifiedTime_.good()) {
A2_LOG_INFO(fmt("Applying Last-Modified time: %s",
lastModifiedTime_.toHTTPDate().c_str()));
size_t n =
pieceStorage_->getDiskAdaptor()->utime(Time(), lastModifiedTime_);
A2_LOG_INFO(fmt("Last-Modified attrs of %lu files were updated.",
static_cast<unsigned long>(n)));
}
}
void RequestGroup::updateLastModifiedTime(const Time& time)
{
if(time.good() && lastModifiedTime_ < time) {
lastModifiedTime_ = time;
}
}
void RequestGroup::increaseAndValidateFileNotFoundCount()
{
++fileNotFoundCount_;
const unsigned int maxCount = option_->getAsInt(PREF_MAX_FILE_NOT_FOUND);
if(maxCount > 0 && fileNotFoundCount_ >= maxCount &&
(!segmentMan_ ||
segmentMan_->calculateSessionDownloadLength() == 0)) {
throw DOWNLOAD_FAILURE_EXCEPTION2
(fmt("Reached max-file-not-found count=%u", maxCount),
error_code::MAX_FILE_NOT_FOUND);
}
}
void RequestGroup::markInMemoryDownload()
{
inMemoryDownload_ = true;
}
void RequestGroup::setTimeout(time_t timeout)
{
timeout_ = timeout;
}
bool RequestGroup::doesDownloadSpeedExceed()
{
return maxDownloadSpeedLimit_ > 0 &&
maxDownloadSpeedLimit_ < calculateStat().getDownloadSpeed();
}
bool RequestGroup::doesUploadSpeedExceed()
{
return maxUploadSpeedLimit_ > 0 &&
maxUploadSpeedLimit_ < calculateStat().getUploadSpeed();
}
void RequestGroup::saveControlFile() const
{
if(saveControlFile_) {
progressInfoFile_->save();
}
}
void RequestGroup::removeControlFile() const
{
progressInfoFile_->removeFile();
}
void RequestGroup::setDownloadContext
(const SharedHandle<DownloadContext>& downloadContext)
{
downloadContext_ = downloadContext;
if(downloadContext_) {
downloadContext_->setOwnerRequestGroup(this);
}
}
bool RequestGroup::p2pInvolved() const
{
#ifdef ENABLE_BITTORRENT
return downloadContext_->hasAttribute(bittorrent::BITTORRENT);
#else // !ENABLE_BITTORRENT
return false;
#endif // !ENABLE_BITTORRENT
}
gid_t RequestGroup::newGID()
{
if(gidCounter_ == INT64_MAX) {
gidCounter_ = 0;
}
return ++gidCounter_;
}
} // namespace aria2
|