2006-04-16 14:42:26 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-04-16 14:42:26 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-09-21 15:31:24 +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.
|
2006-04-16 14:42:26 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "MultiDiskAdaptor.h"
|
2008-11-08 08:36:40 +00:00
|
|
|
|
|
|
|
#include <cassert>
|
2009-02-11 07:41:15 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2008-11-08 08:36:40 +00:00
|
|
|
|
2007-01-08 00:13:25 +00:00
|
|
|
#include "DefaultDiskWriter.h"
|
|
|
|
#include "message.h"
|
2009-10-22 15:35:33 +00:00
|
|
|
#include "util.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "FileEntry.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "MultiFileAllocationIterator.h"
|
|
|
|
#include "DefaultDiskWriterFactory.h"
|
2007-11-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Don't connect server before checking file integrity at startup,
if
filesize and output file path are known.
* src/AbstractCommand.cc
* src/StreamFileAllocationEntry.cc
* src/Metalink2RequestGroup.cc
* src/RequestGroup.{h, cc}
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
Added DownloadFailureException. If it is thrown, RequestGroup
should
halt.
* src/AbstractCommand.cc
* src/DownloadFailureException.h
* src/RequestGroup.cc
Catch RecoverableException, instead of DlAbortEx.
* src/RequestGroupMan.cc
* src/FillRequestGroupCommand.cc
* src/MetaFileUtil.cc
* src/IteratableChunkChecksumValidator.cc
Now first parameter of MSG_DOWNLOAD_ABORTED is
gid(RequestGroup::
getGID())
* src/CheckIntegrityCommand.cc
* src/message.h
Print gid instead of idx.
* src/RequestGroupMan.cc
Removed exception throwers declaration.
* src/DirectDiskAdaptor.{h, cc}
* src/SocketCore.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpConnection.{h, cc}
* src/HttpResponse.{h, cc}
* src/DiskAdaptor.{h, cc}
* src/CopyDiskAdaptor.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpHeaderProcessor.{h, cc}
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/Util.{h, cc}
* test/UtilTest.cc
* src/DefaultDiskWriter.{h, cc}
* src/FtpConnection.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Removed duplicate code.
* src/StreamCheckIntegrityEntry.cc
Removed unnecessary include.
* src/DiskWriter.h
Included Exception.h
* src/option_processing.cc
Included 2 files and added doc
* src/TrackerWatcherCommand.cc
* src/SocketCore.cc (writeData): Fixed send error with GnuTLS.
2007-11-09 18:01:12 +00:00
|
|
|
#include "DlAbortEx.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "File.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2008-06-08 10:47:00 +00:00
|
|
|
#include "Logger.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "LogFactory.h"
|
2008-06-08 10:47:00 +00:00
|
|
|
#include "SimpleRandomizer.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
DiskWriterEntry::DiskWriterEntry(const SharedHandle<FileEntry>& fileEntry)
|
|
|
|
: fileEntry_(fileEntry),
|
|
|
|
open_(false),
|
|
|
|
needsFileAllocation_(false)
|
|
|
|
{}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2009-03-09 15:10:26 +00:00
|
|
|
const std::string& DiskWriterEntry::getFilePath() const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return fileEntry_->getPath();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-09 15:10:26 +00:00
|
|
|
void DiskWriterEntry::initAndOpenFile()
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
if(diskWriter_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriter_->initAndOpenFile(fileEntry_->getLength());
|
|
|
|
open_ = true;
|
2008-06-08 10:47:00 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-09 15:10:26 +00:00
|
|
|
void DiskWriterEntry::openFile()
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
if(diskWriter_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriter_->openFile(fileEntry_->getLength());
|
|
|
|
open_ = true;
|
2008-06-08 10:47:00 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-09 15:10:26 +00:00
|
|
|
void DiskWriterEntry::openExistingFile()
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-11-12 12:48:48 +00:00
|
|
|
if(diskWriter_) {
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriter_->openExistingFile(fileEntry_->getLength());
|
|
|
|
open_ = true;
|
2008-06-08 10:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
void DiskWriterEntry::closeFile()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
if(open_) {
|
|
|
|
diskWriter_->closeFile();
|
|
|
|
open_ = false;
|
2008-06-08 10:47:00 +00:00
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2009-03-09 15:10:26 +00:00
|
|
|
bool DiskWriterEntry::fileExists()
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
return fileEntry_->exists();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
uint64_t DiskWriterEntry::size() const
|
2008-02-08 15:53:45 +00:00
|
|
|
{
|
2009-05-04 07:50:38 +00:00
|
|
|
return File(getFilePath()).size();
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DiskWriterEntry::setDiskWriter(const SharedHandle<DiskWriter>& diskWriter)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriter_ = diskWriter;
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DiskWriterEntry::operator<(const DiskWriterEntry& entry) const
|
|
|
|
{
|
2010-11-11 07:33:43 +00:00
|
|
|
return *fileEntry_ < *entry.fileEntry_;
|
2008-02-08 15:53:45 +00:00
|
|
|
}
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
MultiDiskAdaptor::MultiDiskAdaptor()
|
|
|
|
: pieceLength_(0),
|
|
|
|
maxOpenFiles_(DEFAULT_MAX_OPEN_FILES),
|
|
|
|
readOnly_(false)
|
|
|
|
{}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2010-11-14 07:17:55 +00:00
|
|
|
MultiDiskAdaptor::~MultiDiskAdaptor() {}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
SharedHandle<DiskWriterEntry> createDiskWriterEntry
|
2008-06-15 16:19:06 +00:00
|
|
|
(const SharedHandle<FileEntry>& fileEntry,
|
2008-09-07 11:37:15 +00:00
|
|
|
bool needsFileAllocation)
|
2008-06-15 16:19:06 +00:00
|
|
|
{
|
|
|
|
SharedHandle<DiskWriterEntry> entry(new DiskWriterEntry(fileEntry));
|
2008-09-07 11:37:15 +00:00
|
|
|
entry->needsFileAllocation(needsFileAllocation);
|
2008-06-15 16:19:06 +00:00
|
|
|
return entry;
|
2010-06-11 12:49:14 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2008-06-15 16:19:06 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void MultiDiskAdaptor::resetDiskWriterEntries()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriterEntries_.clear();
|
2008-06-15 16:19:06 +00:00
|
|
|
|
2010-06-11 12:49:14 +00:00
|
|
|
if(getFileEntries().empty()) {
|
2008-06-15 16:19:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-06-28 10:37:15 +00:00
|
|
|
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
|
2010-06-11 12:49:14 +00:00
|
|
|
getFileEntries().begin(), eoi = getFileEntries().end(); i != eoi; ++i) {
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriterEntries_.push_back
|
2008-09-07 11:37:15 +00:00
|
|
|
(createDiskWriterEntry(*i, (*i)->isRequested()));
|
|
|
|
}
|
2009-02-11 07:41:15 +00:00
|
|
|
std::map<std::string, bool> dwreq;
|
2010-06-21 13:51:56 +00:00
|
|
|
// TODO Currently, pieceLength_ == 0 is used for unit testing only.
|
|
|
|
if(pieceLength_ > 0) {
|
2010-02-28 16:04:52 +00:00
|
|
|
std::vector<SharedHandle<DiskWriterEntry> >::const_iterator done =
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriterEntries_.begin();
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<SharedHandle<DiskWriterEntry> >::const_iterator itr =
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriterEntries_.begin(), eoi = diskWriterEntries_.end();
|
2010-02-28 16:04:52 +00:00
|
|
|
itr != eoi;) {
|
2009-02-11 07:41:15 +00:00
|
|
|
const SharedHandle<FileEntry>& fileEntry = (*itr)->getFileEntry();
|
|
|
|
|
|
|
|
if(!fileEntry->isRequested()) {
|
2010-01-05 16:01:46 +00:00
|
|
|
++itr;
|
|
|
|
continue;
|
2008-06-15 16:19:06 +00:00
|
|
|
}
|
2008-09-07 11:37:15 +00:00
|
|
|
off_t pieceStartOffset =
|
2010-06-21 13:51:56 +00:00
|
|
|
(fileEntry->getOffset()/pieceLength_)*pieceLength_;
|
|
|
|
if(itr != diskWriterEntries_.begin()) {
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<SharedHandle<DiskWriterEntry> >::const_iterator i =
|
2010-01-05 16:01:46 +00:00
|
|
|
itr-1; true; --i) {
|
|
|
|
const SharedHandle<FileEntry>& fileEntry = (*i)->getFileEntry();
|
|
|
|
if(pieceStartOffset <= fileEntry->getOffset() ||
|
|
|
|
(uint64_t)pieceStartOffset <
|
|
|
|
fileEntry->getOffset()+fileEntry->getLength()) {
|
|
|
|
(*i)->needsFileAllocation(true);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(i == done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-06-15 16:19:06 +00:00
|
|
|
}
|
|
|
|
|
2009-02-11 07:41:15 +00:00
|
|
|
if(fileEntry->getLength() > 0) {
|
2010-01-05 16:01:46 +00:00
|
|
|
off_t lastPieceStartOffset =
|
2010-06-11 12:49:14 +00:00
|
|
|
(fileEntry->getOffset()+fileEntry->getLength()-1)/
|
2010-06-21 13:51:56 +00:00
|
|
|
pieceLength_*pieceLength_;
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Checking adjacent backward file to %s"
|
|
|
|
" whose lastPieceStartOffset+pieceLength_=%lld",
|
2011-08-08 12:46:10 +00:00
|
|
|
fileEntry->getPath().c_str(),
|
2010-11-20 08:21:36 +00:00
|
|
|
static_cast<long long int>
|
|
|
|
(lastPieceStartOffset+pieceLength_)));
|
2010-01-05 16:01:46 +00:00
|
|
|
++itr;
|
|
|
|
// adjacent backward files are not needed to be allocated. They
|
|
|
|
// just requre DiskWriter
|
2010-02-28 16:04:52 +00:00
|
|
|
for(; itr != eoi &&
|
2010-01-05 16:01:46 +00:00
|
|
|
(!(*itr)->getFileEntry()->isRequested() ||
|
|
|
|
(*itr)->getFileEntry()->getLength() == 0); ++itr) {
|
2011-08-04 12:43:02 +00:00
|
|
|
A2_LOG_DEBUG
|
|
|
|
(fmt("file=%s, offset=%lld",
|
2011-08-08 12:46:10 +00:00
|
|
|
(*itr)->getFileEntry()->getPath().c_str(),
|
2011-08-04 12:43:02 +00:00
|
|
|
static_cast<long long int>
|
|
|
|
((*itr)->getFileEntry()->getOffset())));
|
2010-01-05 16:01:46 +00:00
|
|
|
if((*itr)->getFileEntry()->getOffset() <
|
2010-06-21 13:51:56 +00:00
|
|
|
static_cast<off_t>(lastPieceStartOffset+pieceLength_)) {
|
2011-08-04 12:43:02 +00:00
|
|
|
A2_LOG_DEBUG
|
|
|
|
(fmt("%s needs diskwriter",
|
2011-08-08 12:46:10 +00:00
|
|
|
(*itr)->getFileEntry()->getPath().c_str()));
|
2010-01-05 16:01:46 +00:00
|
|
|
dwreq[(*itr)->getFileEntry()->getPath()] = true;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done = itr-1;
|
2009-02-11 07:41:15 +00:00
|
|
|
} else {
|
2010-01-05 16:01:46 +00:00
|
|
|
done = itr;
|
|
|
|
++itr;
|
2008-06-15 16:19:06 +00:00
|
|
|
}
|
2007-01-08 00:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
2008-09-07 11:37:15 +00:00
|
|
|
DefaultDiskWriterFactory dwFactory;
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<SharedHandle<DiskWriterEntry> >::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriterEntries_.begin(), eoi = diskWriterEntries_.end();
|
2010-02-28 16:04:52 +00:00
|
|
|
i != eoi; ++i) {
|
2009-02-11 07:41:15 +00:00
|
|
|
if((*i)->needsFileAllocation() ||
|
|
|
|
dwreq.find((*i)->getFileEntry()->getPath()) != dwreq.end() ||
|
2009-03-09 15:10:26 +00:00
|
|
|
(*i)->fileExists()) {
|
2010-11-20 08:21:36 +00:00
|
|
|
A2_LOG_DEBUG(fmt("Creating DiskWriter for filename=%s",
|
2011-08-08 12:46:10 +00:00
|
|
|
(*i)->getFilePath().c_str()));
|
2009-05-04 07:50:38 +00:00
|
|
|
(*i)->setDiskWriter(dwFactory.newDiskWriter((*i)->getFilePath()));
|
2010-06-21 13:51:56 +00:00
|
|
|
if(readOnly_) {
|
2010-01-05 16:01:46 +00:00
|
|
|
(*i)->getDiskWriter()->enableReadOnly();
|
2009-02-23 13:27:08 +00:00
|
|
|
}
|
2008-09-07 11:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
2006-04-16 14:42:26 +00:00
|
|
|
}
|
|
|
|
|
2008-06-08 10:47:00 +00:00
|
|
|
void MultiDiskAdaptor::openIfNot
|
2009-03-09 15:10:26 +00:00
|
|
|
(const SharedHandle<DiskWriterEntry>& entry, void (DiskWriterEntry::*open)())
|
2008-06-08 10:47:00 +00:00
|
|
|
{
|
|
|
|
if(!entry->isOpen()) {
|
2010-11-20 08:21:36 +00:00
|
|
|
// A2_LOG_DEBUG(fmt("DiskWriterEntry: Cache MISS. offset=%s",
|
|
|
|
// util::itos(entry->getFileEntry()->getOffset()).c_str()));
|
2008-06-08 10:47:00 +00:00
|
|
|
|
2010-06-21 13:51:56 +00:00
|
|
|
size_t numOpened = openedDiskWriterEntries_.size();
|
2009-03-09 15:10:26 +00:00
|
|
|
(entry.get()->*open)();
|
2010-06-21 13:51:56 +00:00
|
|
|
if(numOpened >= maxOpenFiles_) {
|
2008-06-08 10:47:00 +00:00
|
|
|
// Cache is full.
|
|
|
|
// Choose one DiskWriterEntry randomly and close it.
|
2010-06-11 12:49:14 +00:00
|
|
|
size_t index =
|
|
|
|
SimpleRandomizer::getInstance()->getRandomNumber(numOpened);
|
2009-06-28 10:37:15 +00:00
|
|
|
std::vector<SharedHandle<DiskWriterEntry> >::iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
openedDiskWriterEntries_.begin();
|
2008-06-08 10:47:00 +00:00
|
|
|
std::advance(i, index);
|
|
|
|
(*i)->closeFile();
|
|
|
|
(*i) = entry;
|
|
|
|
} else {
|
2010-06-21 13:51:56 +00:00
|
|
|
openedDiskWriterEntries_.push_back(entry);
|
2008-06-08 10:47:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2010-11-20 08:21:36 +00:00
|
|
|
// A2_LOG_DEBUG(fmt("DiskWriterEntry: Cache HIT. offset=%s",
|
|
|
|
// util::itos(entry->getFileEntry()->getOffset()).c_str()));
|
2008-06-08 10:47:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void MultiDiskAdaptor::openFile()
|
|
|
|
{
|
2007-01-08 00:13:25 +00:00
|
|
|
resetDiskWriterEntries();
|
2010-11-25 12:38:57 +00:00
|
|
|
// util::mkdir() is called in AbstractDiskWriter::createFile(), so
|
|
|
|
// we don't need to call it here.
|
|
|
|
|
2008-11-08 08:36:40 +00:00
|
|
|
// Call DiskWriterEntry::openFile to make sure that zero-length files are
|
|
|
|
// created.
|
2010-06-21 13:51:56 +00:00
|
|
|
for(DiskWriterEntries::const_iterator itr = diskWriterEntries_.begin(),
|
|
|
|
eoi = diskWriterEntries_.end(); itr != eoi; ++itr) {
|
2009-03-09 15:10:26 +00:00
|
|
|
openIfNot(*itr, &DiskWriterEntry::openFile);
|
2008-11-08 08:36:40 +00:00
|
|
|
}
|
2006-04-16 14:42:26 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void MultiDiskAdaptor::initAndOpenFile()
|
|
|
|
{
|
2007-01-08 00:13:25 +00:00
|
|
|
resetDiskWriterEntries();
|
2010-11-25 12:38:57 +00:00
|
|
|
// util::mkdir() is called in AbstractDiskWriter::createFile(), so
|
|
|
|
// we don't need to call it here.
|
|
|
|
|
2008-06-08 10:47:00 +00:00
|
|
|
// Call DiskWriterEntry::initAndOpenFile to make files truncated.
|
2010-06-21 13:51:56 +00:00
|
|
|
for(DiskWriterEntries::const_iterator itr = diskWriterEntries_.begin(),
|
|
|
|
eoi = diskWriterEntries_.end(); itr != eoi; ++itr) {
|
2009-03-09 15:10:26 +00:00
|
|
|
openIfNot(*itr, &DiskWriterEntry::initAndOpenFile);
|
2007-01-08 00:13:25 +00:00
|
|
|
}
|
2006-04-16 14:42:26 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void MultiDiskAdaptor::openExistingFile()
|
|
|
|
{
|
2007-01-08 00:13:25 +00:00
|
|
|
resetDiskWriterEntries();
|
2008-06-08 10:47:00 +00:00
|
|
|
// Not need to call openIfNot here.
|
2007-01-08 00:13:25 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void MultiDiskAdaptor::closeFile()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
std::for_each(diskWriterEntries_.begin(), diskWriterEntries_.end(),
|
2010-06-11 12:49:14 +00:00
|
|
|
mem_fun_sh(&DiskWriterEntry::closeFile));
|
2006-04-16 14:42:26 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
bool isInRange(const DiskWriterEntryHandle entry, off_t offset)
|
2007-01-08 00:13:25 +00:00
|
|
|
{
|
2007-01-11 16:32:31 +00:00
|
|
|
return entry->getFileEntry()->getOffset() <= offset &&
|
2010-06-11 12:49:14 +00:00
|
|
|
(uint64_t)offset <
|
|
|
|
entry->getFileEntry()->getOffset()+entry->getFileEntry()->getLength();
|
2007-01-08 00:13:25 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2007-01-08 00:13:25 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
size_t calculateLength(const DiskWriterEntryHandle entry,
|
|
|
|
off_t fileOffset, size_t rem)
|
2007-01-08 00:13:25 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t length;
|
|
|
|
if(entry->getFileEntry()->getLength() < (uint64_t)fileOffset+rem) {
|
2007-01-11 16:32:31 +00:00
|
|
|
length = entry->getFileEntry()->getLength()-fileOffset;
|
2007-01-08 00:13:25 +00:00
|
|
|
} else {
|
|
|
|
length = rem;
|
|
|
|
}
|
|
|
|
return length;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2007-01-08 00:13:25 +00:00
|
|
|
|
2010-08-31 13:33:05 +00:00
|
|
|
namespace {
|
2008-05-11 01:13:22 +00:00
|
|
|
class OffsetCompare {
|
|
|
|
public:
|
|
|
|
bool operator()(off_t offset, const SharedHandle<DiskWriterEntry>& dwe)
|
|
|
|
{
|
|
|
|
return offset < dwe->getFileEntry()->getOffset();
|
|
|
|
}
|
|
|
|
};
|
2010-10-30 14:53:40 +00:00
|
|
|
} // namespace
|
2008-05-11 01:13:22 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
DiskWriterEntries::const_iterator findFirstDiskWriterEntry
|
2010-06-11 12:49:14 +00:00
|
|
|
(const DiskWriterEntries& diskWriterEntries, off_t offset)
|
2008-05-11 01:13:22 +00:00
|
|
|
{
|
|
|
|
DiskWriterEntries::const_iterator first =
|
|
|
|
std::upper_bound(diskWriterEntries.begin(), diskWriterEntries.end(),
|
2010-01-05 16:01:46 +00:00
|
|
|
offset, OffsetCompare());
|
2008-05-11 01:13:22 +00:00
|
|
|
|
|
|
|
--first;
|
|
|
|
|
|
|
|
// In case when offset is out-of-range
|
|
|
|
if(!isInRange(*first, offset)) {
|
2009-05-18 15:07:15 +00:00
|
|
|
throw DL_ABORT_EX
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt(EX_FILE_OFFSET_OUT_OF_RANGE,
|
|
|
|
util::itos(offset, true).c_str()));
|
2008-05-11 01:13:22 +00:00
|
|
|
}
|
|
|
|
return first;
|
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2008-05-11 01:13:22 +00:00
|
|
|
|
2010-10-30 16:02:15 +00:00
|
|
|
namespace {
|
|
|
|
void throwOnDiskWriterNotOpened(const SharedHandle<DiskWriterEntry>& e,
|
|
|
|
off_t offset)
|
2008-10-21 15:34:56 +00:00
|
|
|
{
|
2009-05-18 15:07:15 +00:00
|
|
|
throw DL_ABORT_EX
|
2010-11-20 09:36:14 +00:00
|
|
|
(fmt("DiskWriter for offset=%s, filename=%s is not opened.",
|
|
|
|
util::itos(offset).c_str(),
|
|
|
|
e->getFilePath().c_str()));
|
2008-10-21 15:34:56 +00:00
|
|
|
}
|
2010-10-30 16:02:15 +00:00
|
|
|
} // namespace
|
2008-10-21 15:34:56 +00:00
|
|
|
|
2008-05-11 01:13:22 +00:00
|
|
|
void MultiDiskAdaptor::writeData(const unsigned char* data, size_t len,
|
2010-01-05 16:01:46 +00:00
|
|
|
off_t offset)
|
2008-05-11 01:13:22 +00:00
|
|
|
{
|
2010-06-11 12:49:14 +00:00
|
|
|
DiskWriterEntries::const_iterator first =
|
2010-06-21 13:51:56 +00:00
|
|
|
findFirstDiskWriterEntry(diskWriterEntries_, offset);
|
2008-05-11 01:13:22 +00:00
|
|
|
|
|
|
|
size_t rem = len;
|
|
|
|
off_t fileOffset = offset-(*first)->getFileEntry()->getOffset();
|
2010-02-28 16:04:52 +00:00
|
|
|
for(DiskWriterEntries::const_iterator i = first,
|
2010-06-21 13:51:56 +00:00
|
|
|
eoi = diskWriterEntries_.end(); i != eoi; ++i) {
|
2008-05-11 01:13:22 +00:00
|
|
|
size_t writeLength = calculateLength(*i, fileOffset, rem);
|
2008-06-08 10:47:00 +00:00
|
|
|
|
2009-03-09 15:10:26 +00:00
|
|
|
openIfNot(*i, &DiskWriterEntry::openFile);
|
2008-06-08 10:47:00 +00:00
|
|
|
|
2008-10-21 15:34:56 +00:00
|
|
|
if(!(*i)->isOpen()) {
|
2009-03-09 15:10:26 +00:00
|
|
|
throwOnDiskWriterNotOpened(*i, offset+(len-rem));
|
2008-10-21 15:34:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 01:13:22 +00:00
|
|
|
(*i)->getDiskWriter()->writeData(data+(len-rem), writeLength, fileOffset);
|
|
|
|
rem -= writeLength;
|
|
|
|
fileOffset = 0;
|
|
|
|
if(rem == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-11 12:49:14 +00:00
|
|
|
ssize_t MultiDiskAdaptor::readData
|
|
|
|
(unsigned char* data, size_t len, off_t offset)
|
2007-01-08 00:13:25 +00:00
|
|
|
{
|
2010-02-28 16:04:52 +00:00
|
|
|
DiskWriterEntries::const_iterator first =
|
2010-06-21 13:51:56 +00:00
|
|
|
findFirstDiskWriterEntry(diskWriterEntries_, offset);
|
2008-05-11 01:13:22 +00:00
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
size_t rem = len;
|
|
|
|
size_t totalReadLength = 0;
|
2008-05-11 01:13:22 +00:00
|
|
|
off_t fileOffset = offset-(*first)->getFileEntry()->getOffset();
|
2010-02-28 16:04:52 +00:00
|
|
|
for(DiskWriterEntries::const_iterator i = first,
|
2010-06-21 13:51:56 +00:00
|
|
|
eoi = diskWriterEntries_.end(); i != eoi; ++i) {
|
2008-05-11 01:13:22 +00:00
|
|
|
size_t readLength = calculateLength(*i, fileOffset, rem);
|
2008-06-08 10:47:00 +00:00
|
|
|
|
2009-03-09 15:10:26 +00:00
|
|
|
openIfNot(*i, &DiskWriterEntry::openFile);
|
2008-06-08 10:47:00 +00:00
|
|
|
|
2008-10-21 15:34:56 +00:00
|
|
|
if(!(*i)->isOpen()) {
|
2009-03-09 15:10:26 +00:00
|
|
|
throwOnDiskWriterNotOpened(*i, offset+(len-rem));
|
2008-10-21 15:34:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 01:13:22 +00:00
|
|
|
totalReadLength +=
|
|
|
|
(*i)->getDiskWriter()->readData(data+(len-rem), readLength, fileOffset);
|
|
|
|
rem -= readLength;
|
|
|
|
fileOffset = 0;
|
|
|
|
if(rem == 0) {
|
|
|
|
break;
|
2007-01-08 00:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalReadLength;
|
|
|
|
}
|
|
|
|
|
2007-01-11 16:32:31 +00:00
|
|
|
bool MultiDiskAdaptor::fileExists()
|
|
|
|
{
|
2010-06-11 12:49:14 +00:00
|
|
|
return std::find_if(getFileEntries().begin(), getFileEntries().end(),
|
|
|
|
mem_fun_sh(&FileEntry::exists)) !=
|
|
|
|
getFileEntries().end();
|
2007-01-11 16:32:31 +00:00
|
|
|
}
|
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
MessageDigestHelper is introduced in order to simplify the use
of message digest. Removed repeated code.
The message digest algorithm is now specified by string, like
"sha1",
"md5".
* src/messageDigest.{h, cc}
* src/MessageDigestHelper.{h, cc}: New class.
* src/DefaultPieceStorage.cc
* src/DefaultBtContext.{h, cc}
(computeFastSet): New function.
(setInfoHash): Added for unit testing.
(setNumPieces): Added for unit testing.
* src/DefaultBtInteractive.cc
* src/BtPieceMessage.cc
* src/Peer.cc
* src/Checksum.h
* src/message.h
* src/IteratableChecksumValidator.h
* src/ChunkChecksumValidator.{h, cc}: Use
IteratableChecksumValidator
inside it.
* src/SegmentMan.{h, cc}
(checkIntegrity): Removed.
* src/IteratableChunkChecksumValidator.{h, cc}
* src/Util.h
(sha1Sum): Removed.
(simpleMessageDigest): Removed.
(fileChecksum): Removed.
(computeFastSet): Removed.
* src/ShaVisitor.cc
* src/ChunkChecksum.h
* src/DownloadCommand.cc
Removed messageDigest virtual functions.
* src/MultiDiskAdaptor.{h, cc}
* src/DiskAdaptor.h
* src/ByteArrayDiskWriter.h
* src/DiskWriter.h
* src/DiskAdaptorWriter.h
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Fixed comilation error when message digest is disabled.
* src/MetalinkEntry.{h, cc}
* src/MetalinkRequestInfo.cc
Removed srandom and random.
* src/SimpleRandomizer.h
Added size() virtual function to DiskAdaptor
* src/MultiDiskAdaptor.h
Fixed the bug that causes that files are not opened correctly in
multi-file torrent.
* src/TorrentRequestInfo.cc
* src/MultiDiskAdaptor.cc
Added SHA256 support
* src/messageDigest.cc
* src/Xml2MetalinkProcessor.cc
Show supported message digest algorithms
* src/main.cc
Updated contact info.
* src/main.cc
2007-08-08 14:40:11 +00:00
|
|
|
|
2008-09-07 08:29:09 +00:00
|
|
|
uint64_t MultiDiskAdaptor::size()
|
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
MessageDigestHelper is introduced in order to simplify the use
of message digest. Removed repeated code.
The message digest algorithm is now specified by string, like
"sha1",
"md5".
* src/messageDigest.{h, cc}
* src/MessageDigestHelper.{h, cc}: New class.
* src/DefaultPieceStorage.cc
* src/DefaultBtContext.{h, cc}
(computeFastSet): New function.
(setInfoHash): Added for unit testing.
(setNumPieces): Added for unit testing.
* src/DefaultBtInteractive.cc
* src/BtPieceMessage.cc
* src/Peer.cc
* src/Checksum.h
* src/message.h
* src/IteratableChecksumValidator.h
* src/ChunkChecksumValidator.{h, cc}: Use
IteratableChecksumValidator
inside it.
* src/SegmentMan.{h, cc}
(checkIntegrity): Removed.
* src/IteratableChunkChecksumValidator.{h, cc}
* src/Util.h
(sha1Sum): Removed.
(simpleMessageDigest): Removed.
(fileChecksum): Removed.
(computeFastSet): Removed.
* src/ShaVisitor.cc
* src/ChunkChecksum.h
* src/DownloadCommand.cc
Removed messageDigest virtual functions.
* src/MultiDiskAdaptor.{h, cc}
* src/DiskAdaptor.h
* src/ByteArrayDiskWriter.h
* src/DiskWriter.h
* src/DiskAdaptorWriter.h
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Fixed comilation error when message digest is disabled.
* src/MetalinkEntry.{h, cc}
* src/MetalinkRequestInfo.cc
Removed srandom and random.
* src/SimpleRandomizer.h
Added size() virtual function to DiskAdaptor
* src/MultiDiskAdaptor.h
Fixed the bug that causes that files are not opened correctly in
multi-file torrent.
* src/TorrentRequestInfo.cc
* src/MultiDiskAdaptor.cc
Added SHA256 support
* src/messageDigest.cc
* src/Xml2MetalinkProcessor.cc
Show supported message digest algorithms
* src/main.cc
Updated contact info.
* src/main.cc
2007-08-08 14:40:11 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
uint64_t size = 0;
|
2010-02-28 16:04:52 +00:00
|
|
|
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
|
2010-06-11 12:49:14 +00:00
|
|
|
getFileEntries().begin(), eoi = getFileEntries().end(); i != eoi; ++i) {
|
2009-06-28 10:37:15 +00:00
|
|
|
size += File((*i)->getPath()).size();
|
2007-08-08 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
MessageDigestHelper is introduced in order to simplify the use
of message digest. Removed repeated code.
The message digest algorithm is now specified by string, like
"sha1",
"md5".
* src/messageDigest.{h, cc}
* src/MessageDigestHelper.{h, cc}: New class.
* src/DefaultPieceStorage.cc
* src/DefaultBtContext.{h, cc}
(computeFastSet): New function.
(setInfoHash): Added for unit testing.
(setNumPieces): Added for unit testing.
* src/DefaultBtInteractive.cc
* src/BtPieceMessage.cc
* src/Peer.cc
* src/Checksum.h
* src/message.h
* src/IteratableChecksumValidator.h
* src/ChunkChecksumValidator.{h, cc}: Use
IteratableChecksumValidator
inside it.
* src/SegmentMan.{h, cc}
(checkIntegrity): Removed.
* src/IteratableChunkChecksumValidator.{h, cc}
* src/Util.h
(sha1Sum): Removed.
(simpleMessageDigest): Removed.
(fileChecksum): Removed.
(computeFastSet): Removed.
* src/ShaVisitor.cc
* src/ChunkChecksum.h
* src/DownloadCommand.cc
Removed messageDigest virtual functions.
* src/MultiDiskAdaptor.{h, cc}
* src/DiskAdaptor.h
* src/ByteArrayDiskWriter.h
* src/DiskWriter.h
* src/DiskAdaptorWriter.h
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Fixed comilation error when message digest is disabled.
* src/MetalinkEntry.{h, cc}
* src/MetalinkRequestInfo.cc
Removed srandom and random.
* src/SimpleRandomizer.h
Added size() virtual function to DiskAdaptor
* src/MultiDiskAdaptor.h
Fixed the bug that causes that files are not opened correctly in
multi-file torrent.
* src/TorrentRequestInfo.cc
* src/MultiDiskAdaptor.cc
Added SHA256 support
* src/messageDigest.cc
* src/Xml2MetalinkProcessor.cc
Show supported message digest algorithms
* src/main.cc
Updated contact info.
* src/main.cc
2007-08-08 14:40:11 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
|
2010-07-10 08:13:01 +00:00
|
|
|
SharedHandle<FileAllocationIterator> MultiDiskAdaptor::fileAllocationIterator()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2010-06-11 12:49:14 +00:00
|
|
|
return SharedHandle<FileAllocationIterator>
|
|
|
|
(new MultiFileAllocationIterator(this));
|
2007-10-11 16:58:24 +00:00
|
|
|
}
|
2007-11-28 14:22:28 +00:00
|
|
|
|
2009-02-23 13:27:08 +00:00
|
|
|
void MultiDiskAdaptor::enableReadOnly()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
readOnly_ = true;
|
2009-02-23 13:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MultiDiskAdaptor::disableReadOnly()
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
readOnly_ = false;
|
2009-02-23 13:27:08 +00:00
|
|
|
}
|
|
|
|
|
2008-08-26 12:39:07 +00:00
|
|
|
void MultiDiskAdaptor::cutTrailingGarbage()
|
|
|
|
{
|
2009-06-28 10:37:15 +00:00
|
|
|
for(std::vector<SharedHandle<DiskWriterEntry> >::const_iterator i =
|
2010-06-21 13:51:56 +00:00
|
|
|
diskWriterEntries_.begin(), eoi = diskWriterEntries_.end();
|
2010-02-28 16:04:52 +00:00
|
|
|
i != eoi; ++i) {
|
2008-08-26 12:39:07 +00:00
|
|
|
uint64_t length = (*i)->getFileEntry()->getLength();
|
2009-03-09 15:10:26 +00:00
|
|
|
if(File((*i)->getFilePath()).size() > length) {
|
2008-08-26 12:39:07 +00:00
|
|
|
// We need open file before calling DiskWriter::truncate(uint64_t)
|
2009-03-09 15:10:26 +00:00
|
|
|
openIfNot(*i, &DiskWriterEntry::openFile);
|
2008-08-26 12:39:07 +00:00
|
|
|
(*i)->getDiskWriter()->truncate(length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-08 12:34:36 +00:00
|
|
|
void MultiDiskAdaptor::setMaxOpenFiles(size_t maxOpenFiles)
|
|
|
|
{
|
2010-06-21 13:51:56 +00:00
|
|
|
maxOpenFiles_ = maxOpenFiles;
|
2008-06-08 12:34:36 +00:00
|
|
|
}
|
|
|
|
|
2008-09-07 14:38:26 +00:00
|
|
|
size_t MultiDiskAdaptor::utime(const Time& actime, const Time& modtime)
|
|
|
|
{
|
|
|
|
size_t numOK = 0;
|
2009-06-28 10:37:15 +00:00
|
|
|
for(std::vector<SharedHandle<FileEntry> >::const_iterator i =
|
2010-06-11 12:49:14 +00:00
|
|
|
getFileEntries().begin(), eoi = getFileEntries().end(); i != eoi; ++i) {
|
2008-09-07 14:38:26 +00:00
|
|
|
if((*i)->isRequested()) {
|
2009-03-09 15:10:26 +00:00
|
|
|
File f((*i)->getPath());
|
2008-09-07 14:38:26 +00:00
|
|
|
if(f.isFile() && f.utime(actime, modtime)) {
|
2010-01-05 16:01:46 +00:00
|
|
|
++numOK;
|
2008-09-07 14:38:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numOK;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|