2008-07-12 13:35:35 +00:00
|
|
|
/* <!-- 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2008-07-12 13:35:35 +00:00
|
|
|
*
|
|
|
|
* 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 "DownloadContext.h"
|
2009-06-23 15:35:45 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2009-06-14 11:12:20 +00:00
|
|
|
#include "FileEntry.h"
|
2010-11-20 09:36:14 +00:00
|
|
|
#include "fmt.h"
|
2010-02-27 10:06:40 +00:00
|
|
|
#include "util.h"
|
2010-03-06 14:21:43 +00:00
|
|
|
#include "wallclock.h"
|
2010-06-18 14:47:09 +00:00
|
|
|
#include "DlAbortEx.h"
|
2010-11-11 07:33:43 +00:00
|
|
|
#include "a2functional.h"
|
2010-11-14 07:17:55 +00:00
|
|
|
#include "Signature.h"
|
2012-10-25 14:33:45 +00:00
|
|
|
#include "RequestGroupMan.h"
|
2008-07-12 13:35:35 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2013-11-26 01:59:51 +00:00
|
|
|
DownloadContext::DownloadContext()
|
2015-12-27 09:39:47 +00:00
|
|
|
: ownerRequestGroup_(nullptr),
|
|
|
|
attrs_(MAX_CTX_ATTR),
|
|
|
|
downloadStopTime_(Timer::zero()),
|
|
|
|
pieceLength_(0),
|
|
|
|
checksumVerified_(false),
|
|
|
|
knowsTotalLength_(true),
|
2018-11-18 10:02:13 +00:00
|
|
|
#ifdef ENABLE_METALINK
|
2015-12-27 09:39:47 +00:00
|
|
|
acceptMetalink_(true)
|
2018-11-18 10:02:13 +00:00
|
|
|
#else // !ENABLE_METALINK
|
|
|
|
acceptMetalink_(false)
|
|
|
|
#endif // !ENABLE_METALINK
|
2015-12-27 09:39:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadContext::DownloadContext(int32_t pieceLength, int64_t totalLength,
|
2014-06-04 13:43:29 +00:00
|
|
|
std::string path)
|
2015-12-27 09:39:47 +00:00
|
|
|
: ownerRequestGroup_(nullptr),
|
|
|
|
attrs_(MAX_CTX_ATTR),
|
|
|
|
downloadStopTime_(Timer::zero()),
|
|
|
|
pieceLength_(pieceLength),
|
|
|
|
checksumVerified_(false),
|
|
|
|
knowsTotalLength_(true),
|
2018-11-18 10:02:13 +00:00
|
|
|
#ifdef ENABLE_METALINK
|
2015-12-27 09:39:47 +00:00
|
|
|
acceptMetalink_(true)
|
2018-11-18 10:02:13 +00:00
|
|
|
#else // !ENABLE_METALINK
|
|
|
|
acceptMetalink_(false)
|
|
|
|
#endif // !ENABLE_METALINK
|
2008-07-12 13:35:35 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
fileEntries_.push_back(
|
|
|
|
std::make_shared<FileEntry>(std::move(path), totalLength, 0));
|
2008-07-12 13:35:35 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 00:18:43 +00:00
|
|
|
DownloadContext::~DownloadContext() = default;
|
2010-11-14 07:17:55 +00:00
|
|
|
|
2008-11-10 16:10:31 +00:00
|
|
|
void DownloadContext::resetDownloadStartTime()
|
|
|
|
{
|
2015-06-07 17:03:31 +00:00
|
|
|
downloadStopTime_ = Timer::zero();
|
2012-10-25 14:33:45 +00:00
|
|
|
netStat_.downloadStart();
|
2008-11-10 16:10:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadContext::resetDownloadStopTime()
|
|
|
|
{
|
2011-08-09 16:17:28 +00:00
|
|
|
downloadStopTime_ = global::wallclock();
|
2012-10-25 14:33:45 +00:00
|
|
|
netStat_.downloadStop();
|
2008-11-10 16:10:31 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 17:03:31 +00:00
|
|
|
Timer::Clock::duration DownloadContext::calculateSessionTime() const
|
2008-11-10 16:10:31 +00:00
|
|
|
{
|
2012-10-25 14:33:45 +00:00
|
|
|
const Timer& startTime = netStat_.getDownloadStartTime();
|
2015-06-07 17:03:31 +00:00
|
|
|
return startTime.difference(downloadStopTime_);
|
2008-11-10 16:10:31 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<FileEntry>
|
2012-06-25 14:35:24 +00:00
|
|
|
DownloadContext::findFileEntryByOffset(int64_t offset) const
|
2009-06-23 15:35:45 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (fileEntries_.empty() ||
|
|
|
|
(offset > 0 && fileEntries_.back()->getLastOffset() <= offset)) {
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2014-09-12 15:07:21 +00:00
|
|
|
auto obj = std::make_shared<FileEntry>();
|
2009-06-23 15:35:45 +00:00
|
|
|
obj->setOffset(offset);
|
2013-08-21 03:06:53 +00:00
|
|
|
auto i = std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj,
|
2015-12-27 09:39:47 +00:00
|
|
|
DerefLess<std::shared_ptr<FileEntry>>());
|
|
|
|
if (i != fileEntries_.end() && (*i)->getOffset() == offset) {
|
2009-06-23 15:35:45 +00:00
|
|
|
return *i;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2009-06-23 15:35:45 +00:00
|
|
|
return *(--i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadContext::setFilePathWithIndex(size_t index,
|
|
|
|
const std::string& path)
|
2009-06-28 10:37:15 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (0 < index && index <= fileEntries_.size()) {
|
2010-02-27 10:06:40 +00:00
|
|
|
// We don't escape path because path may come from users.
|
2015-12-27 09:39:47 +00:00
|
|
|
fileEntries_[index - 1]->setPath(path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw DL_ABORT_EX(
|
|
|
|
fmt("No such file with index=%u", static_cast<unsigned int>(index)));
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-04 14:21:01 +00:00
|
|
|
void DownloadContext::setFileFilter(SegList<int> sgl)
|
2009-06-28 10:37:15 +00:00
|
|
|
{
|
2013-06-22 08:53:39 +00:00
|
|
|
using namespace std::placeholders;
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
if (!sgl.hasNext() || fileEntries_.size() == 1) {
|
2011-10-29 14:33:48 +00:00
|
|
|
std::for_each(fileEntries_.begin(), fileEntries_.end(),
|
2013-06-22 08:53:39 +00:00
|
|
|
std::bind(&FileEntry::setRequested, _1, true));
|
2011-10-29 14:33:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(sgl.peek() >= 1);
|
2011-10-30 06:11:22 +00:00
|
|
|
size_t len = fileEntries_.size();
|
2011-10-29 14:33:48 +00:00
|
|
|
size_t i = 0;
|
2015-12-27 09:39:47 +00:00
|
|
|
for (; i < len && sgl.hasNext(); ++i) {
|
|
|
|
size_t idx = sgl.peek() - 1;
|
|
|
|
if (i == idx) {
|
2011-10-29 14:33:48 +00:00
|
|
|
fileEntries_[i]->setRequested(true);
|
|
|
|
sgl.next();
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else if (i < idx) {
|
2011-10-29 14:33:48 +00:00
|
|
|
fileEntries_[i]->setRequested(false);
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
for (; i < len; ++i) {
|
2011-10-29 14:33:48 +00:00
|
|
|
fileEntries_[i]->setRequested(false);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadContext::setAttribute(ContextAttributeType key,
|
2016-04-17 12:06:41 +00:00
|
|
|
std::shared_ptr<ContextAttribute> value)
|
2009-06-28 10:37:15 +00:00
|
|
|
{
|
2012-09-22 08:37:30 +00:00
|
|
|
assert(key < MAX_CTX_ATTR);
|
2013-06-22 15:59:55 +00:00
|
|
|
attrs_[key] = std::move(value);
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 12:06:41 +00:00
|
|
|
const std::shared_ptr<ContextAttribute>&
|
2015-12-27 09:39:47 +00:00
|
|
|
DownloadContext::getAttribute(ContextAttributeType key)
|
2009-06-28 10:37:15 +00:00
|
|
|
{
|
2012-09-22 08:37:30 +00:00
|
|
|
assert(key < MAX_CTX_ATTR);
|
2016-04-17 12:06:41 +00:00
|
|
|
const auto& attr = attrs_[key];
|
|
|
|
if (!attr) {
|
2015-12-27 09:39:47 +00:00
|
|
|
throw DL_ABORT_EX(
|
|
|
|
fmt("No attribute named %s", strContextAttributeType(key)));
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
2016-04-17 12:06:41 +00:00
|
|
|
|
|
|
|
return attr;
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2012-09-22 08:37:30 +00:00
|
|
|
bool DownloadContext::hasAttribute(ContextAttributeType key) const
|
2009-06-28 10:37:15 +00:00
|
|
|
{
|
2012-09-22 08:37:30 +00:00
|
|
|
assert(key < MAX_CTX_ATTR);
|
2013-06-21 16:10:38 +00:00
|
|
|
return attrs_[key].get();
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-17 12:06:41 +00:00
|
|
|
const std::vector<std::shared_ptr<ContextAttribute>>&
|
|
|
|
DownloadContext::getAttributes() const
|
|
|
|
{
|
|
|
|
return attrs_;
|
|
|
|
}
|
|
|
|
|
2009-07-05 09:10:08 +00:00
|
|
|
void DownloadContext::releaseRuntimeResource()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
for (std::vector<std::shared_ptr<FileEntry>>::const_iterator
|
|
|
|
i = fileEntries_.begin(),
|
|
|
|
eoi = fileEntries_.end();
|
|
|
|
i != eoi; ++i) {
|
2011-10-31 12:53:24 +00:00
|
|
|
(*i)->putBackRequest();
|
2009-07-05 09:10:08 +00:00
|
|
|
(*i)->releaseRuntimeResource();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-20 11:42:33 +00:00
|
|
|
size_t DownloadContext::getNumPieces() const
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (pieceLength_ == 0) {
|
2010-06-20 11:42:33 +00:00
|
|
|
return 0;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-21 13:51:56 +00:00
|
|
|
assert(!fileEntries_.empty());
|
2015-12-27 09:39:47 +00:00
|
|
|
return (fileEntries_.back()->getLastOffset() + pieceLength_ - 1) /
|
|
|
|
pieceLength_;
|
2010-06-20 11:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t DownloadContext::getTotalLength() const
|
2010-06-20 11:42:33 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (fileEntries_.empty()) {
|
2010-06-20 11:42:33 +00:00
|
|
|
return 0;
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-21 13:51:56 +00:00
|
|
|
return fileEntries_.back()->getLastOffset();
|
2010-06-20 11:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& DownloadContext::getBasePath() const
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (basePath_.empty()) {
|
2010-06-21 13:51:56 +00:00
|
|
|
assert(!fileEntries_.empty());
|
2010-06-20 11:42:33 +00:00
|
|
|
return getFirstFileEntry()->getPath();
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-06-21 13:51:56 +00:00
|
|
|
return basePath_;
|
2010-06-20 11:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
std::shared_ptr<FileEntry> DownloadContext::getFirstRequestedFileEntry() const
|
2010-12-04 10:00:48 +00:00
|
|
|
{
|
2013-08-21 03:06:53 +00:00
|
|
|
for (auto& e : fileEntries_) {
|
2015-12-27 09:39:47 +00:00
|
|
|
if (e->isRequested()) {
|
2013-08-21 03:06:53 +00:00
|
|
|
return e;
|
2010-12-04 10:00:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-04 12:44:09 +00:00
|
|
|
return nullptr;
|
2010-12-04 10:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DownloadContext::countRequestedFileEntry() const
|
|
|
|
{
|
|
|
|
size_t numFiles = 0;
|
2015-12-27 09:39:47 +00:00
|
|
|
for (const auto& e : fileEntries_) {
|
|
|
|
if (e->isRequested()) {
|
2010-12-04 10:00:48 +00:00
|
|
|
++numFiles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numFiles;
|
|
|
|
}
|
|
|
|
|
2010-06-22 11:57:31 +00:00
|
|
|
bool DownloadContext::isChecksumVerificationNeeded() const
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
return pieceHashType_.empty() && !digest_.empty() && !hashType_.empty() &&
|
|
|
|
!checksumVerified_;
|
2010-06-22 11:57:31 +00:00
|
|
|
}
|
|
|
|
|
2010-09-15 11:46:25 +00:00
|
|
|
bool DownloadContext::isChecksumVerificationAvailable() const
|
|
|
|
{
|
2011-07-27 13:44:30 +00:00
|
|
|
return !digest_.empty() && !hashType_.empty();
|
2010-09-15 11:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DownloadContext::isPieceHashVerificationAvailable() const
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
return !pieceHashType_.empty() && pieceHashes_.size() > 0 &&
|
|
|
|
pieceHashes_.size() == getNumPieces();
|
2010-09-15 11:46:25 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
const std::string& DownloadContext::getPieceHash(size_t index) const
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (index < pieceHashes_.size()) {
|
2010-11-14 07:17:55 +00:00
|
|
|
return pieceHashes_[index];
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2010-11-14 07:17:55 +00:00
|
|
|
return A2STR::NIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void DownloadContext::setDigest(const std::string& hashType,
|
|
|
|
const std::string& digest)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2011-07-27 13:44:30 +00:00
|
|
|
hashType_ = hashType;
|
|
|
|
digest_ = digest;
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadContext::setBasePath(const std::string& basePath)
|
|
|
|
{
|
|
|
|
basePath_ = basePath;
|
|
|
|
}
|
|
|
|
|
2013-07-12 13:59:49 +00:00
|
|
|
void DownloadContext::setSignature(std::unique_ptr<Signature> signature)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-07-12 13:59:49 +00:00
|
|
|
signature_ = std::move(signature);
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 14:54:18 +00:00
|
|
|
void DownloadContext::updateDownload(size_t bytes)
|
2012-10-25 14:33:45 +00:00
|
|
|
{
|
2016-01-04 14:54:18 +00:00
|
|
|
netStat_.updateDownload(bytes);
|
2012-10-25 14:33:45 +00:00
|
|
|
RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (rgman) {
|
2016-01-04 14:54:18 +00:00
|
|
|
rgman->getNetStat().updateDownload(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadContext::updateUploadSpeed(size_t bytes)
|
|
|
|
{
|
|
|
|
netStat_.updateUploadSpeed(bytes);
|
|
|
|
auto rgman = ownerRequestGroup_->getRequestGroupMan();
|
|
|
|
if (rgman) {
|
|
|
|
rgman->getNetStat().updateUploadSpeed(bytes);
|
2012-10-25 14:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadContext::updateUploadLength(size_t bytes)
|
|
|
|
{
|
|
|
|
netStat_.updateUploadLength(bytes);
|
2016-01-04 14:54:18 +00:00
|
|
|
auto rgman = ownerRequestGroup_->getRequestGroupMan();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (rgman) {
|
2012-10-25 14:33:45 +00:00
|
|
|
rgman->getNetStat().updateUploadLength(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-12 13:35:35 +00:00
|
|
|
} // namespace aria2
|