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 {
|
|
|
|
|
2008-11-10 16:10:31 +00:00
|
|
|
DownloadContext::DownloadContext():
|
2010-06-21 13:51:56 +00:00
|
|
|
pieceLength_(0),
|
2010-06-22 11:57:31 +00:00
|
|
|
checksumVerified_(false),
|
2010-06-21 13:51:56 +00:00
|
|
|
knowsTotalLength_(true),
|
|
|
|
ownerRequestGroup_(0),
|
2012-09-22 08:37:30 +00:00
|
|
|
attrs_(MAX_CTX_ATTR),
|
2012-10-25 14:33:45 +00:00
|
|
|
downloadStopTime_(0),
|
2012-09-22 14:19:41 +00:00
|
|
|
acceptMetalink_(true) {}
|
2008-07-12 13:35:35 +00:00
|
|
|
|
2011-12-07 15:03:25 +00:00
|
|
|
DownloadContext::DownloadContext(int32_t pieceLength,
|
2012-06-25 14:35:24 +00:00
|
|
|
int64_t totalLength,
|
2010-01-05 16:01:46 +00:00
|
|
|
const std::string& path):
|
2010-06-21 13:51:56 +00:00
|
|
|
pieceLength_(pieceLength),
|
2010-06-22 11:57:31 +00:00
|
|
|
checksumVerified_(false),
|
2010-06-21 13:51:56 +00:00
|
|
|
knowsTotalLength_(true),
|
|
|
|
ownerRequestGroup_(0),
|
2012-09-22 08:37:30 +00:00
|
|
|
attrs_(MAX_CTX_ATTR),
|
2011-07-26 14:41:21 +00:00
|
|
|
downloadStopTime_(0),
|
2012-09-22 14:19:41 +00:00
|
|
|
acceptMetalink_(true)
|
2008-07-12 13:35:35 +00:00
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<FileEntry> fileEntry(new FileEntry(path, totalLength, 0));
|
2010-06-21 13:51:56 +00:00
|
|
|
fileEntries_.push_back(fileEntry);
|
2008-07-12 13:35:35 +00:00
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
DownloadContext::~DownloadContext() {}
|
|
|
|
|
2008-11-10 16:10:31 +00:00
|
|
|
void DownloadContext::resetDownloadStartTime()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
downloadStopTime_.reset(0);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int64_t DownloadContext::calculateSessionTime() const
|
|
|
|
{
|
2012-10-25 14:33:45 +00:00
|
|
|
const Timer& startTime = netStat_.getDownloadStartTime();
|
|
|
|
return startTime.differenceInMillis(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
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(fileEntries_.empty() ||
|
2011-12-07 13:57:34 +00:00
|
|
|
(offset > 0 && fileEntries_.back()->getLastOffset() <= offset)) {
|
2013-06-21 16:10:38 +00:00
|
|
|
return std::shared_ptr<FileEntry>();
|
2009-06-23 15:35:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<FileEntry> obj(new FileEntry());
|
2009-06-23 15:35:45 +00:00
|
|
|
obj->setOffset(offset);
|
2013-06-21 16:10:38 +00:00
|
|
|
std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
2010-11-11 07:33:43 +00:00
|
|
|
std::upper_bound(fileEntries_.begin(), fileEntries_.end(), obj,
|
2013-06-21 16:10:38 +00:00
|
|
|
DerefLess<std::shared_ptr<FileEntry> >());
|
2010-06-21 13:51:56 +00:00
|
|
|
if(i != fileEntries_.end() && (*i)->getOffset() == offset) {
|
2009-06-23 15:35:45 +00:00
|
|
|
return *i;
|
|
|
|
} else {
|
|
|
|
return *(--i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
void DownloadContext::setFilePathWithIndex
|
|
|
|
(size_t index, const std::string& path)
|
|
|
|
{
|
2010-06-21 13:51:56 +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.
|
2010-06-21 13:51:56 +00:00
|
|
|
fileEntries_[index-1]->setPath(path);
|
2009-06-28 10:37:15 +00:00
|
|
|
} else {
|
2010-11-20 09:36:14 +00:00
|
|
|
throw DL_ABORT_EX(fmt("No such file with index=%u",
|
|
|
|
static_cast<unsigned int>(index)));
|
2009-06-28 10:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-29 14:33:48 +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;
|
|
|
|
|
2011-10-29 14:33:48 +00:00
|
|
|
if(!sgl.hasNext() || fileEntries_.size() == 1) {
|
|
|
|
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;
|
2011-10-30 06:11:22 +00:00
|
|
|
for(; i < len && sgl.hasNext(); ++i) {
|
2011-10-29 14:33:48 +00:00
|
|
|
size_t idx = sgl.peek()-1;
|
|
|
|
if(i == idx) {
|
|
|
|
fileEntries_[i]->setRequested(true);
|
|
|
|
sgl.next();
|
|
|
|
} else if(i < idx) {
|
|
|
|
fileEntries_[i]->setRequested(false);
|
|
|
|
}
|
|
|
|
}
|
2011-10-30 06:11:22 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-18 14:47:09 +00:00
|
|
|
void DownloadContext::setAttribute
|
2013-06-22 15:59:55 +00:00
|
|
|
(ContextAttributeType key, std::unique_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
|
|
|
}
|
|
|
|
|
2013-06-22 15:59:55 +00:00
|
|
|
ContextAttribute* 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);
|
2013-06-22 15:59:55 +00:00
|
|
|
const std::unique_ptr<ContextAttribute>& attr = attrs_[key];
|
2012-09-22 08:37:30 +00:00
|
|
|
if(attr) {
|
2013-06-22 15:59:55 +00:00
|
|
|
return attr.get();
|
2010-06-18 14:47:09 +00:00
|
|
|
} else {
|
2012-09-22 08:37:30 +00:00
|
|
|
throw DL_ABORT_EX(fmt("No attribute named %s",
|
|
|
|
strContextAttributeType(key)));
|
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
|
|
|
}
|
|
|
|
|
2009-07-05 09:10:08 +00:00
|
|
|
void DownloadContext::releaseRuntimeResource()
|
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
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
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(pieceLength_ == 0) {
|
2010-06-20 11:42:33 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
assert(!fileEntries_.empty());
|
|
|
|
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
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(fileEntries_.empty()) {
|
2010-06-20 11:42:33 +00:00
|
|
|
return 0;
|
|
|
|
} 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
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(basePath_.empty()) {
|
|
|
|
assert(!fileEntries_.empty());
|
2010-06-20 11:42:33 +00:00
|
|
|
return getFirstFileEntry()->getPath();
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
return basePath_;
|
2010-06-20 11:42:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-21 16:10:38 +00:00
|
|
|
std::shared_ptr<FileEntry>
|
2010-12-04 10:00:48 +00:00
|
|
|
DownloadContext::getFirstRequestedFileEntry() const
|
|
|
|
{
|
2013-06-21 16:10:38 +00:00
|
|
|
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
2010-12-04 10:00:48 +00:00
|
|
|
fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
|
|
|
|
if((*i)->isRequested()) {
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 16:10:38 +00:00
|
|
|
return std::shared_ptr<FileEntry>();
|
2010-12-04 10:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DownloadContext::countRequestedFileEntry() const
|
|
|
|
{
|
|
|
|
size_t numFiles = 0;
|
2013-06-21 16:10:38 +00:00
|
|
|
for(std::vector<std::shared_ptr<FileEntry> >::const_iterator i =
|
2010-12-04 10:00:48 +00:00
|
|
|
fileEntries_.begin(), eoi = fileEntries_.end(); i != eoi; ++i) {
|
|
|
|
if((*i)->isRequested()) {
|
|
|
|
++numFiles;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numFiles;
|
|
|
|
}
|
|
|
|
|
2010-06-22 11:57:31 +00:00
|
|
|
bool DownloadContext::isChecksumVerificationNeeded() const
|
|
|
|
{
|
2011-07-27 13:44:30 +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
|
|
|
|
{
|
2011-07-27 13:44:30 +00:00
|
|
|
return !pieceHashType_.empty() &&
|
2010-09-15 11:46:25 +00:00
|
|
|
pieceHashes_.size() > 0 && pieceHashes_.size() == getNumPieces();
|
|
|
|
}
|
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
const std::string& DownloadContext::getPieceHash(size_t index) const
|
|
|
|
{
|
|
|
|
if(index < pieceHashes_.size()) {
|
|
|
|
return pieceHashes_[index];
|
|
|
|
} else {
|
|
|
|
return A2STR::NIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 13:44:30 +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-06-21 16:10:38 +00:00
|
|
|
void DownloadContext::setSignature(const std::shared_ptr<Signature>& signature)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
|
|
|
signature_ = signature;
|
|
|
|
}
|
|
|
|
|
2012-10-25 14:33:45 +00:00
|
|
|
void DownloadContext::updateDownloadLength(size_t bytes)
|
|
|
|
{
|
|
|
|
netStat_.updateDownloadLength(bytes);
|
|
|
|
RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan();
|
|
|
|
if(rgman) {
|
|
|
|
rgman->getNetStat().updateDownloadLength(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadContext::updateUploadLength(size_t bytes)
|
|
|
|
{
|
|
|
|
netStat_.updateUploadLength(bytes);
|
|
|
|
RequestGroupMan* rgman = ownerRequestGroup_->getRequestGroupMan();
|
|
|
|
if(rgman) {
|
|
|
|
rgman->getNetStat().updateUploadLength(bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-12 13:35:35 +00:00
|
|
|
} // namespace aria2
|