forked from aria2/aria2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBtDependency.cc
179 lines (172 loc) · 6.55 KB
/
BtDependency.cc
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
/* <!-- 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 "BtDependency.h"
#include "RequestGroup.h"
#include "Option.h"
#include "LogFactory.h"
#include "Logger.h"
#include "DownloadContext.h"
#include "RecoverableException.h"
#include "message.h"
#include "prefs.h"
#include "util.h"
#include "PieceStorage.h"
#include "DiskAdaptor.h"
#include "File.h"
#include "bittorrent_helper.h"
#include "DlAbortEx.h"
#include "fmt.h"
#include "FileEntry.h"
#include "SimpleRandomizer.h"
namespace aria2 {
BtDependency::BtDependency
(RequestGroup* dependant,
const std::shared_ptr<RequestGroup>& dependee)
: dependant_(dependant),
dependee_(dependee)
{}
BtDependency::~BtDependency() {}
namespace {
void copyValues(const std::shared_ptr<FileEntry>& d,
const std::shared_ptr<FileEntry>& s)
{
d->setRequested(true);
d->setPath(s->getPath());
d->addUris(std::begin(s->getRemainingUris()),
std::end(s->getRemainingUris()));
d->setMaxConnectionPerServer(s->getMaxConnectionPerServer());
d->setUniqueProtocol(s->isUniqueProtocol());
}
} // namespace
namespace {
struct EntryCmp {
bool operator()
(const std::shared_ptr<FileEntry>& lhs,
const std::shared_ptr<FileEntry>& rhs) const
{
return lhs->getOriginalName() < rhs->getOriginalName();
}
};
} // namespace
bool BtDependency::resolve()
{
if(!dependee_) {
return true;
}
if(dependee_->getNumCommand() == 0 && dependee_->downloadFinished()) {
std::shared_ptr<RequestGroup> dependee = dependee_;
// cut reference here
dependee_.reset();
auto context = std::make_shared<DownloadContext>();
try {
std::shared_ptr<DiskAdaptor> diskAdaptor =
dependee->getPieceStorage()->getDiskAdaptor();
diskAdaptor->openExistingFile();
std::string content = util::toString(diskAdaptor);
if(dependee->getDownloadContext()->hasAttribute(CTX_ATTR_BT)) {
auto attrs =
bittorrent::getTorrentAttrs(dependee->getDownloadContext());
bittorrent::loadFromMemory
(bittorrent::metadata2Torrent(content, attrs), context,
dependant_->getOption(), "default");
// We don't call bittorrent::adjustAnnounceUri() because it
// has already been called with attrs.
} else {
bittorrent::loadFromMemory
(content, context, dependant_->getOption(),
File(dependee->getFirstFilePath()).getBasename());
bittorrent::adjustAnnounceUri(bittorrent::getTorrentAttrs(context),
dependant_->getOption());
}
const std::vector<std::shared_ptr<FileEntry> >& fileEntries =
context->getFileEntries();
for (auto &fe : fileEntries) {
auto &uri = fe->getRemainingUris();
std::shuffle(std::begin(uri), std::end(uri),
*SimpleRandomizer::getInstance());
}
const std::vector<std::shared_ptr<FileEntry> >& dependantFileEntries =
dependant_->getDownloadContext()->getFileEntries();
// If dependant's FileEntry::getOriginalName() is empty, we
// assume that torrent is single file. In Metalink3, this is
// always assumed.
if(fileEntries.size() == 1 && dependantFileEntries.size() == 1 &&
dependantFileEntries[0]->getOriginalName().empty()) {
copyValues(fileEntries[0], dependantFileEntries[0]);
} else {
std::vector<std::shared_ptr<FileEntry> > destFiles;
destFiles.reserve(fileEntries.size());
for(auto & e : fileEntries) {
e->setRequested(false);
destFiles.push_back(e);
}
std::sort(std::begin(destFiles), std::end(destFiles), EntryCmp());
// Copy file path in dependant_'s FileEntries to newly created
// context's FileEntries to endorse the path structure of
// dependant_. URIs and singleHostMultiConnection are also copied.
for(const auto& e: dependantFileEntries){
const auto d = std::lower_bound(std::begin(destFiles),
std::end(destFiles), e, EntryCmp());
if(d == std::end(destFiles) ||
(*d)->getOriginalName() != e->getOriginalName()) {
throw DL_ABORT_EX
(fmt("No entry %s in torrent file", e->getOriginalName().c_str()));
} else {
copyValues(*d, e);
}
}
}
} catch(RecoverableException& e) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, e);
A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
GroupId::toHex(dependant_->getGID()).c_str()));
return true;
}
A2_LOG_INFO(fmt("Dependency resolved for GID#%s",
GroupId::toHex(dependant_->getGID()).c_str()));
dependant_->setDownloadContext(context);
return true;
} else if(dependee_->getNumCommand() == 0) {
// dependee_'s download failed.
// cut reference here
dependee_.reset();
A2_LOG_INFO(fmt("BtDependency for GID#%s failed. Go without Bt.",
GroupId::toHex(dependant_->getGID()).c_str()));
return true;
} else {
return false;
}
}
} // namespace aria2