2009-05-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Now the constructor of AbstractDiskWriter takes filename as an
	argument and filename argument is removed from openFile(),
	initAndOpenFile(), openExistingFile() interface.  storeDir
	member and its accessor functions are removed from DiskAdaptor
	because it is not used anymore.  size() member function of
	DefaultDiskWriter, DirectDiskAdaptor and MultiDiskAdaptor now
	can be called without opening file.
	* src/AbstractDiskWriter.cc
	* src/AbstractDiskWriter.h
	* src/AbstractSingleDiskAdaptor.cc
	* src/AbstractSingleDiskAdaptor.h
	* src/ByteArrayDiskWriter.cc
	* src/ByteArrayDiskWriter.h
	* src/ByteArrayDiskWriterFactory.cc
	* src/ByteArrayDiskWriterFactory.h
	* src/DefaultDiskWriter.cc
	* src/DefaultDiskWriter.h
	* src/DefaultDiskWriterFactory.cc
	* src/DefaultDiskWriterFactory.h
	* src/DefaultPieceStorage.cc
	* src/DirectDiskAdaptor.cc
	* src/DirectDiskAdaptor.h
	* src/DiskAdaptor.h
	* src/DiskWriter.h
	* src/DiskWriterFactory.h
	* src/MessageDigestHelper.cc
	* src/MultiDiskAdaptor.cc
	* src/RequestGroup.cc
	* src/UnknownLengthPieceStorage.cc
	* src/Util.cc
	* test/DefaultDiskWriterTest.cc
	* test/DirectDiskAdaptorTest.cc
	* test/FallocFileAllocationIteratorTest.cc
	* test/MessageDigestHelperTest.cc
	* test/MetalinkProcessorTest.cc
	* test/MultiDiskAdaptorTest.cc
	* test/MultiFileAllocationIteratorTest.cc
	* test/SingleFileAllocationIteratorTest.cc
	* test/UtilTest.cc
This commit is contained in:
Tatsuhiro Tsujikawa 2009-05-04 07:50:38 +00:00
parent 5c8f48dc77
commit c1aef8e2d1
33 changed files with 161 additions and 147 deletions

View file

@ -1,3 +1,45 @@
2009-05-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Now the constructor of AbstractDiskWriter takes filename as an
argument and filename argument is removed from openFile(),
initAndOpenFile(), openExistingFile() interface. storeDir member
and its accessor functions are removed from DiskAdaptor because it
is not used anymore. size() member function of DefaultDiskWriter,
DirectDiskAdaptor and MultiDiskAdaptor now can be called without
opening file.
* src/AbstractDiskWriter.cc
* src/AbstractDiskWriter.h
* src/AbstractSingleDiskAdaptor.cc
* src/AbstractSingleDiskAdaptor.h
* src/ByteArrayDiskWriter.cc
* src/ByteArrayDiskWriter.h
* src/ByteArrayDiskWriterFactory.cc
* src/ByteArrayDiskWriterFactory.h
* src/DefaultDiskWriter.cc
* src/DefaultDiskWriter.h
* src/DefaultDiskWriterFactory.cc
* src/DefaultDiskWriterFactory.h
* src/DefaultPieceStorage.cc
* src/DirectDiskAdaptor.cc
* src/DirectDiskAdaptor.h
* src/DiskAdaptor.h
* src/DiskWriter.h
* src/DiskWriterFactory.h
* src/MessageDigestHelper.cc
* src/MultiDiskAdaptor.cc
* src/RequestGroup.cc
* src/UnknownLengthPieceStorage.cc
* src/Util.cc
* test/DefaultDiskWriterTest.cc
* test/DirectDiskAdaptorTest.cc
* test/FallocFileAllocationIteratorTest.cc
* test/MessageDigestHelperTest.cc
* test/MetalinkProcessorTest.cc
* test/MultiDiskAdaptorTest.cc
* test/MultiFileAllocationIteratorTest.cc
* test/SingleFileAllocationIteratorTest.cc
* test/UtilTest.cc
2009-05-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2009-05-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Fixed the bug that aria2 aborts when --select-file is used. This Fixed the bug that aria2 aborts when --select-file is used. This

View file

@ -56,7 +56,8 @@
namespace aria2 { namespace aria2 {
AbstractDiskWriter::AbstractDiskWriter(): AbstractDiskWriter::AbstractDiskWriter(const std::string& filename):
_filename(filename),
fd(-1), fd(-1),
_readOnly(false), _readOnly(false),
logger(LogFactory::getInstance()) {} logger(LogFactory::getInstance()) {}
@ -66,13 +67,12 @@ AbstractDiskWriter::~AbstractDiskWriter()
closeFile(); closeFile();
} }
void AbstractDiskWriter::openFile(const std::string& filename, uint64_t totalLength) void AbstractDiskWriter::openFile(uint64_t totalLength)
{ {
File f(filename); if(File(_filename).exists()) {
if(f.exists()) { openExistingFile(totalLength);
openExistingFile(filename, totalLength);
} else { } else {
initAndOpenFile(filename, totalLength); initAndOpenFile(totalLength);
} }
} }
@ -84,14 +84,11 @@ void AbstractDiskWriter::closeFile()
} }
} }
void AbstractDiskWriter::openExistingFile(const std::string& filename, void AbstractDiskWriter::openExistingFile(uint64_t totalLength)
uint64_t totalLength)
{ {
this->filename = filename; if(!File(_filename).exists()) {
File f(filename);
if(!f.exists()) {
throw DlAbortEx throw DlAbortEx
(StringFormat(EX_FILE_OPEN, filename.c_str(), MSG_FILE_NOT_FOUND).str()); (StringFormat(EX_FILE_OPEN, _filename.c_str(), MSG_FILE_NOT_FOUND).str());
} }
int flags = O_BINARY; int flags = O_BINARY;
@ -101,19 +98,20 @@ void AbstractDiskWriter::openExistingFile(const std::string& filename,
flags |= O_RDWR; flags |= O_RDWR;
} }
if((fd = open(filename.c_str(), flags, OPEN_MODE)) < 0) { if((fd = open(_filename.c_str(), flags, OPEN_MODE)) < 0) {
throw DlAbortEx throw DlAbortEx
(StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str()); (StringFormat(EX_FILE_OPEN, _filename.c_str(), strerror(errno)).str());
} }
} }
void AbstractDiskWriter::createFile(const std::string& filename, int addFlags) void AbstractDiskWriter::createFile(int addFlags)
{ {
this->filename = filename; assert(!_filename.empty());
assert(filename.size()); Util::mkdirs(File(_filename).getDirname());
Util::mkdirs(File(filename).getDirname()); if((fd = open(_filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags,
if((fd = open(filename.c_str(), O_CREAT|O_RDWR|O_TRUNC|O_BINARY|addFlags, OPEN_MODE)) < 0) { OPEN_MODE)) < 0) {
throw DlAbortEx(StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str()); throw DlAbortEx(StringFormat(EX_FILE_OPEN,
_filename.c_str(), strerror(errno)).str());
} }
} }
@ -142,7 +140,7 @@ void AbstractDiskWriter::seek(off_t offset)
{ {
if(lseek(fd, offset, SEEK_SET) == (off_t)-1) { if(lseek(fd, offset, SEEK_SET) == (off_t)-1) {
throw DlAbortEx throw DlAbortEx
(StringFormat(EX_FILE_SEEK, filename.c_str(), strerror(errno)).str()); (StringFormat(EX_FILE_SEEK, _filename.c_str(), strerror(errno)).str());
} }
} }
@ -154,9 +152,10 @@ void AbstractDiskWriter::writeData(const unsigned char* data, size_t len, off_t
// DownloadFailureException and abort download instantly. // DownloadFailureException and abort download instantly.
if(errno == ENOSPC) { if(errno == ENOSPC) {
throw DownloadFailureException throw DownloadFailureException
(StringFormat(EX_FILE_WRITE, filename.c_str(), strerror(errno)).str()); (StringFormat(EX_FILE_WRITE, _filename.c_str(), strerror(errno)).str());
} }
throw DlAbortEx(StringFormat(EX_FILE_WRITE, filename.c_str(), strerror(errno)).str()); throw DlAbortEx(StringFormat(EX_FILE_WRITE,
_filename.c_str(), strerror(errno)).str());
} }
} }
@ -165,7 +164,8 @@ ssize_t AbstractDiskWriter::readData(unsigned char* data, size_t len, off_t offs
ssize_t ret; ssize_t ret;
seek(offset); seek(offset);
if((ret = readDataInternal(data, len)) < 0) { if((ret = readDataInternal(data, len)) < 0) {
throw DlAbortEx(StringFormat(EX_FILE_READ, filename.c_str(), strerror(errno)).str()); throw DlAbortEx(StringFormat(EX_FILE_READ,
_filename.c_str(), strerror(errno)).str());
} }
return ret; return ret;
} }
@ -206,17 +206,9 @@ void AbstractDiskWriter::allocate(off_t offset, uint64_t length)
} }
#endif // HAVE_POSIX_FALLOCATE #endif // HAVE_POSIX_FALLOCATE
// TODO the file descriptor fd must be opened before calling this function.
uint64_t AbstractDiskWriter::size() uint64_t AbstractDiskWriter::size()
{ {
if(fd == -1) { return File(_filename).size();
throw DlAbortEx("File not opened.");
}
a2_struct_stat fileStat;
if(fstat(fd, &fileStat) < 0) {
return 0;
}
return fileStat.st_size;
} }
void AbstractDiskWriter::enableDirectIO() void AbstractDiskWriter::enableDirectIO()

View file

@ -44,14 +44,14 @@ class Logger;
class AbstractDiskWriter : public DiskWriter { class AbstractDiskWriter : public DiskWriter {
protected: protected:
std::string filename; std::string _filename;
int fd; int fd;
bool _readOnly; bool _readOnly;
Logger* logger; Logger* logger;
void createFile(const std::string& filename, int addFlags = 0); void createFile(int addFlags = 0);
private: private:
ssize_t writeDataInternal(const unsigned char* data, size_t len); ssize_t writeDataInternal(const unsigned char* data, size_t len);
@ -59,14 +59,14 @@ private:
void seek(off_t offset); void seek(off_t offset);
public: public:
AbstractDiskWriter(); AbstractDiskWriter(const std::string& filename);
virtual ~AbstractDiskWriter(); virtual ~AbstractDiskWriter();
virtual void openFile(const std::string& filename, uint64_t totalLength = 0); virtual void openFile(uint64_t totalLength = 0);
virtual void closeFile(); virtual void closeFile();
virtual void openExistingFile(const std::string& filename, uint64_t totalLength = 0); virtual void openExistingFile(uint64_t totalLength = 0);
virtual void writeData(const unsigned char* data, size_t len, off_t offset); virtual void writeData(const unsigned char* data, size_t len, off_t offset);

View file

@ -49,12 +49,12 @@ AbstractSingleDiskAdaptor::~AbstractSingleDiskAdaptor() {}
void AbstractSingleDiskAdaptor::initAndOpenFile() void AbstractSingleDiskAdaptor::initAndOpenFile()
{ {
diskWriter->initAndOpenFile(getFilePath(), totalLength); diskWriter->initAndOpenFile(totalLength);
} }
void AbstractSingleDiskAdaptor::openFile() void AbstractSingleDiskAdaptor::openFile()
{ {
diskWriter->openFile(getFilePath(), totalLength); diskWriter->openFile(totalLength);
} }
void AbstractSingleDiskAdaptor::closeFile() void AbstractSingleDiskAdaptor::closeFile()
@ -64,7 +64,7 @@ void AbstractSingleDiskAdaptor::closeFile()
void AbstractSingleDiskAdaptor::openExistingFile() void AbstractSingleDiskAdaptor::openExistingFile()
{ {
diskWriter->openExistingFile(getFilePath(), totalLength); diskWriter->openExistingFile(totalLength);
} }
void AbstractSingleDiskAdaptor::writeData(const unsigned char* data, size_t len, off_t offset) void AbstractSingleDiskAdaptor::writeData(const unsigned char* data, size_t len, off_t offset)
@ -84,7 +84,7 @@ bool AbstractSingleDiskAdaptor::fileExists()
uint64_t AbstractSingleDiskAdaptor::size() uint64_t AbstractSingleDiskAdaptor::size()
{ {
return diskWriter->size(); return File(getFilePath()).size();
} }
void AbstractSingleDiskAdaptor::truncate(uint64_t length) void AbstractSingleDiskAdaptor::truncate(uint64_t length)

View file

@ -89,7 +89,7 @@ public:
virtual void cutTrailingGarbage(); virtual void cutTrailingGarbage();
virtual std::string getFilePath() = 0; virtual const std::string& getFilePath() = 0;
void setDiskWriter(const SharedHandle<DiskWriter>& diskWriter); void setDiskWriter(const SharedHandle<DiskWriter>& diskWriter);

View file

@ -47,21 +47,18 @@ void ByteArrayDiskWriter::clear()
buf.str(A2STR::NIL); buf.str(A2STR::NIL);
} }
void ByteArrayDiskWriter::initAndOpenFile(const std::string& filename, void ByteArrayDiskWriter::initAndOpenFile(uint64_t totalLength)
uint64_t totalLength)
{ {
clear(); clear();
} }
void ByteArrayDiskWriter::openFile(const std::string& filename, void ByteArrayDiskWriter::openFile(uint64_t totalLength) {}
uint64_t totalLength) {}
void ByteArrayDiskWriter::closeFile() {} void ByteArrayDiskWriter::closeFile() {}
void ByteArrayDiskWriter::openExistingFile(const std::string& filename, void ByteArrayDiskWriter::openExistingFile(uint64_t totalLength)
uint64_t totalLength)
{ {
openFile(filename); openFile();
} }
void ByteArrayDiskWriter::writeData(const unsigned char* data, size_t dataLength, off_t position) void ByteArrayDiskWriter::writeData(const unsigned char* data, size_t dataLength, off_t position)

View file

@ -49,13 +49,13 @@ public:
ByteArrayDiskWriter(); ByteArrayDiskWriter();
virtual ~ByteArrayDiskWriter(); virtual ~ByteArrayDiskWriter();
virtual void initAndOpenFile(const std::string& filename, uint64_t totalLength = 0); virtual void initAndOpenFile(uint64_t totalLength = 0);
virtual void openFile(const std::string& filename, uint64_t totalLength = 0); virtual void openFile(uint64_t totalLength = 0);
virtual void closeFile(); virtual void closeFile();
virtual void openExistingFile(const std::string& filename, uint64_t totalLength = 0); virtual void openExistingFile(uint64_t totalLength = 0);
virtual void writeData(const unsigned char* data, size_t len, off_t position); virtual void writeData(const unsigned char* data, size_t len, off_t position);
virtual ssize_t readData(unsigned char* data, size_t len, off_t position); virtual ssize_t readData(unsigned char* data, size_t len, off_t position);

View file

@ -37,7 +37,8 @@
namespace aria2 { namespace aria2 {
DiskWriterHandle ByteArrayDiskWriterFactory::newDiskWriter() DiskWriterHandle ByteArrayDiskWriterFactory::newDiskWriter
(const std::string& filename)
{ {
return SharedHandle<DiskWriter>(new ByteArrayDiskWriter()); return SharedHandle<DiskWriter>(new ByteArrayDiskWriter());
} }

View file

@ -44,7 +44,7 @@ class ByteArrayDiskWriter;
class ByteArrayDiskWriterFactory:public DiskWriterFactory class ByteArrayDiskWriterFactory:public DiskWriterFactory
{ {
public: public:
SharedHandle<DiskWriter> newDiskWriter(); SharedHandle<DiskWriter> newDiskWriter(const std::string& filename);
}; };
typedef SharedHandle<ByteArrayDiskWriterFactory> ByteArrayDiskWriterFactoryHandle; typedef SharedHandle<ByteArrayDiskWriterFactory> ByteArrayDiskWriterFactoryHandle;

View file

@ -36,14 +36,14 @@
namespace aria2 { namespace aria2 {
DefaultDiskWriter::DefaultDiskWriter():AbstractDiskWriter() {} DefaultDiskWriter::DefaultDiskWriter(const std::string& filename):
AbstractDiskWriter(filename) {}
DefaultDiskWriter::~DefaultDiskWriter() {} DefaultDiskWriter::~DefaultDiskWriter() {}
void DefaultDiskWriter::initAndOpenFile(const std::string& filename, void DefaultDiskWriter::initAndOpenFile(uint64_t totalLength)
uint64_t totalLength)
{ {
createFile(filename); createFile();
} }
} // namespace aria2 } // namespace aria2

View file

@ -41,11 +41,11 @@ namespace aria2 {
class DefaultDiskWriter:public AbstractDiskWriter { class DefaultDiskWriter:public AbstractDiskWriter {
public: public:
DefaultDiskWriter(); DefaultDiskWriter(const std::string& filename);
virtual ~DefaultDiskWriter(); virtual ~DefaultDiskWriter();
virtual void initAndOpenFile(const std::string& filename, uint64_t totalLength = 0); virtual void initAndOpenFile(uint64_t totalLength = 0);
}; };
typedef SharedHandle<DefaultDiskWriter> DefaultDiskWriterHandle; typedef SharedHandle<DefaultDiskWriter> DefaultDiskWriterHandle;

View file

@ -37,9 +37,10 @@
namespace aria2 { namespace aria2 {
SharedHandle<DiskWriter> DefaultDiskWriterFactory::newDiskWriter() SharedHandle<DiskWriter> DefaultDiskWriterFactory::newDiskWriter
(const std::string& filename)
{ {
return SharedHandle<DiskWriter>(new DefaultDiskWriter()); return SharedHandle<DiskWriter>(new DefaultDiskWriter(filename));
} }
} // namespace aria2 } // namespace aria2

View file

@ -44,7 +44,7 @@ class DiskWriter;
class DefaultDiskWriterFactory:public DiskWriterFactory class DefaultDiskWriterFactory:public DiskWriterFactory
{ {
public: public:
virtual SharedHandle<DiskWriter> newDiskWriter(); virtual SharedHandle<DiskWriter> newDiskWriter(const std::string& filename);
}; };
typedef SharedHandle<DefaultDiskWriterFactory> DefaultDiskWriterFactoryHandle; typedef SharedHandle<DefaultDiskWriterFactory> DefaultDiskWriterFactoryHandle;

View file

@ -485,23 +485,26 @@ void DefaultPieceStorage::initStorage()
{ {
if(downloadContext->getFileMode() == DownloadContext::SINGLE) { if(downloadContext->getFileMode() == DownloadContext::SINGLE) {
logger->debug("Instantiating DirectDiskAdaptor"); logger->debug("Instantiating DirectDiskAdaptor");
DiskWriterHandle writer = _diskWriterFactory->newDiskWriter();
writer->setDirectIOAllowed(option->getAsBool(PREF_ENABLE_DIRECT_IO));
DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor()); DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
directDiskAdaptor->setDiskWriter(writer);
directDiskAdaptor->setTotalLength(downloadContext->getTotalLength()); directDiskAdaptor->setTotalLength(downloadContext->getTotalLength());
directDiskAdaptor->setFileEntries(downloadContext->getFileEntries());
DiskWriterHandle writer =
_diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
writer->setDirectIOAllowed(option->getAsBool(PREF_ENABLE_DIRECT_IO));
directDiskAdaptor->setDiskWriter(writer);
this->diskAdaptor = directDiskAdaptor; this->diskAdaptor = directDiskAdaptor;
} else { } else {
// file mode == DownloadContext::MULTI // file mode == DownloadContext::MULTI
logger->debug("Instantiating MultiDiskAdaptor"); logger->debug("Instantiating MultiDiskAdaptor");
MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor()); MultiDiskAdaptorHandle multiDiskAdaptor(new MultiDiskAdaptor());
multiDiskAdaptor->setFileEntries(downloadContext->getFileEntries());
multiDiskAdaptor->setDirectIOAllowed(option->getAsBool(PREF_ENABLE_DIRECT_IO)); multiDiskAdaptor->setDirectIOAllowed(option->getAsBool(PREF_ENABLE_DIRECT_IO));
multiDiskAdaptor->setPieceLength(downloadContext->getPieceLength()); multiDiskAdaptor->setPieceLength(downloadContext->getPieceLength());
multiDiskAdaptor->setMaxOpenFiles(option->getAsInt(PREF_BT_MAX_OPEN_FILES)); multiDiskAdaptor->setMaxOpenFiles(option->getAsInt(PREF_BT_MAX_OPEN_FILES));
this->diskAdaptor = multiDiskAdaptor; this->diskAdaptor = multiDiskAdaptor;
} }
diskAdaptor->setStoreDir(downloadContext->getDir());
diskAdaptor->setFileEntries(downloadContext->getFileEntries());
#ifdef HAVE_POSIX_FALLOCATE #ifdef HAVE_POSIX_FALLOCATE
if(option->get(PREF_FILE_ALLOCATION) == V_FALLOC) { if(option->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
diskAdaptor->enableFallocate(); diskAdaptor->enableFallocate();

View file

@ -38,7 +38,7 @@
namespace aria2 { namespace aria2 {
std::string DirectDiskAdaptor::getFilePath() const std::string& DirectDiskAdaptor::getFilePath()
{ {
return fileEntries.front()->getPath(); return fileEntries.front()->getPath();
} }

View file

@ -44,7 +44,7 @@ public:
DirectDiskAdaptor() {}; DirectDiskAdaptor() {};
virtual ~DirectDiskAdaptor() {}; virtual ~DirectDiskAdaptor() {};
virtual std::string getFilePath(); virtual const std::string& getFilePath();
virtual void onDownloadComplete(); virtual void onDownloadComplete();

View file

@ -48,7 +48,6 @@ class FileAllocationIterator;
class DiskAdaptor:public BinaryStream { class DiskAdaptor:public BinaryStream {
protected: protected:
std::string storeDir;
std::deque<SharedHandle<FileEntry> > fileEntries; std::deque<SharedHandle<FileEntry> > fileEntries;
#ifdef HAVE_POSIX_FALLOCATE #ifdef HAVE_POSIX_FALLOCATE
bool _fallocate; bool _fallocate;
@ -86,10 +85,6 @@ public:
void removeAllDownloadEntry(); void removeAllDownloadEntry();
void setStoreDir(const std::string& storeDir) { this->storeDir = storeDir; }
const std::string& getStoreDir() const { return this->storeDir; }
virtual SharedHandle<FileAllocationIterator> fileAllocationIterator() = 0; virtual SharedHandle<FileAllocationIterator> fileAllocationIterator() = 0;
virtual void enableDirectIO() {} virtual void enableDirectIO() {}

View file

@ -52,13 +52,11 @@ public:
virtual ~DiskWriter() {} virtual ~DiskWriter() {}
/** /**
* Creates a file output stream to write to the file with the specified name. * Opens file. If the file exists, then it is truncated to 0 length.
* If the file exists, then it is truncated to 0 length.
* @param filename the file name to be opened.
*/ */
virtual void initAndOpenFile(const std::string& filename, uint64_t totalLength = 0) = 0; virtual void initAndOpenFile(uint64_t totalLength = 0) = 0;
virtual void openFile(const std::string& filename, uint64_t totalLength = 0) = 0; virtual void openFile(uint64_t totalLength = 0) = 0;
/** /**
* Closes this output stream. * Closes this output stream.
@ -67,12 +65,10 @@ public:
virtual void closeFile() = 0; virtual void closeFile() = 0;
/** /**
* Opens a file output stream to write to the file with the specified name. * Opens a file. If the file doesnot exists, an exception may be
* If the file doesnot exists, an exception may be throwed. * thrown.
*
* @param filename the file name to be opened.
*/ */
virtual void openExistingFile(const std::string& filename, uint64_t totalLength = 0) = 0; virtual void openExistingFile(uint64_t totalLength = 0) = 0;
// Returns file length // Returns file length
virtual uint64_t size() = 0; virtual uint64_t size() = 0;

View file

@ -46,7 +46,7 @@ class DiskWriterFactory {
public: public:
virtual ~DiskWriterFactory() {} virtual ~DiskWriterFactory() {}
virtual SharedHandle<DiskWriter> newDiskWriter() = 0; virtual SharedHandle<DiskWriter> newDiskWriter(const std::string& filename)=0;
}; };
typedef SharedHandle<DiskWriterFactory> DiskWriterFactoryHandle; typedef SharedHandle<DiskWriterFactory> DiskWriterFactoryHandle;

View file

@ -111,8 +111,8 @@ std::string MessageDigestHelper::digest(MessageDigestContext* ctx,
std::string MessageDigestHelper::digest(const std::string& algo, const std::string& filename) std::string MessageDigestHelper::digest(const std::string& algo, const std::string& filename)
{ {
DiskWriterHandle writer(new DefaultDiskWriter()); DiskWriterHandle writer(new DefaultDiskWriter(filename));
writer->openExistingFile(filename); writer->openExistingFile();
return digest(algo, writer, 0, writer->size()); return digest(algo, writer, 0, writer->size());
} }

View file

@ -66,7 +66,7 @@ const std::string& DiskWriterEntry::getFilePath() const
void DiskWriterEntry::initAndOpenFile() void DiskWriterEntry::initAndOpenFile()
{ {
if(!diskWriter.isNull()) { if(!diskWriter.isNull()) {
diskWriter->initAndOpenFile(getFilePath(), fileEntry->getLength()); diskWriter->initAndOpenFile(fileEntry->getLength());
if(_directIO) { if(_directIO) {
diskWriter->enableDirectIO(); diskWriter->enableDirectIO();
} }
@ -77,7 +77,7 @@ void DiskWriterEntry::initAndOpenFile()
void DiskWriterEntry::openFile() void DiskWriterEntry::openFile()
{ {
if(!diskWriter.isNull()) { if(!diskWriter.isNull()) {
diskWriter->openFile(getFilePath(), fileEntry->getLength()); diskWriter->openFile(fileEntry->getLength());
if(_directIO) { if(_directIO) {
diskWriter->enableDirectIO(); diskWriter->enableDirectIO();
} }
@ -88,7 +88,7 @@ void DiskWriterEntry::openFile()
void DiskWriterEntry::openExistingFile() void DiskWriterEntry::openExistingFile()
{ {
if(!diskWriter.isNull()) { if(!diskWriter.isNull()) {
diskWriter->openExistingFile(getFilePath(), fileEntry->getLength()); diskWriter->openExistingFile(fileEntry->getLength());
if(_directIO) { if(_directIO) {
diskWriter->enableDirectIO(); diskWriter->enableDirectIO();
} }
@ -116,11 +116,7 @@ bool DiskWriterEntry::fileExists()
uint64_t DiskWriterEntry::size() const uint64_t DiskWriterEntry::size() const
{ {
if(diskWriter.isNull()) { return File(getFilePath()).size();
return File(getFilePath()).size();
} else {
return diskWriter->size();
}
} }
SharedHandle<FileEntry> DiskWriterEntry::getFileEntry() const SharedHandle<FileEntry> DiskWriterEntry::getFileEntry() const
@ -276,7 +272,7 @@ void MultiDiskAdaptor::resetDiskWriterEntries()
(*i)->fileExists()) { (*i)->fileExists()) {
logger->debug("Creating DiskWriter for filename=%s", logger->debug("Creating DiskWriter for filename=%s",
(*i)->getFilePath().c_str()); (*i)->getFilePath().c_str());
(*i)->setDiskWriter(dwFactory.newDiskWriter()); (*i)->setDiskWriter(dwFactory.newDiskWriter((*i)->getFilePath()));
(*i)->getDiskWriter()->setDirectIOAllowed(_directIOAllowed); (*i)->getDiskWriter()->setDirectIOAllowed(_directIOAllowed);
if(_readOnly) { if(_readOnly) {
(*i)->getDiskWriter()->enableReadOnly(); (*i)->getDiskWriter()->enableReadOnly();
@ -479,7 +475,6 @@ bool MultiDiskAdaptor::fileExists()
return false; return false;
} }
// TODO call DiskWriter::openFile() before calling this function.
uint64_t MultiDiskAdaptor::size() uint64_t MultiDiskAdaptor::size()
{ {
uint64_t size = 0; uint64_t size = 0;

View file

@ -282,9 +282,21 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
progressInfoFile->getFilename().c_str(), progressInfoFile->getFilename().c_str(),
getFilePath().c_str()); getFilePath().c_str());
} }
// First, make DiskAdaptor read-only mode. {
_pieceStorage->getDiskAdaptor()->enableReadOnly(); uint64_t actualFileSize = _pieceStorage->getDiskAdaptor()->size();
if(actualFileSize == btContext->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 btContext->getTotalLength()
_logger->debug("File size not match. File is opened in writable mode."
" Expected:%s Actual:%s",
Util::uitos(btContext->getTotalLength()).c_str(),
Util::uitos(actualFileSize).c_str());
}
}
// Call Load, Save and file allocation command here // Call Load, Save and file allocation command here
if(progressInfoFile->exists()) { if(progressInfoFile->exists()) {
// load .aria2 file if it exists. // load .aria2 file if it exists.
@ -311,20 +323,7 @@ void RequestGroup::createInitialCommand(std::deque<Command*>& commands,
} }
} }
_progressInfoFile = progressInfoFile; _progressInfoFile = progressInfoFile;
{
uint64_t actualFileSize = _pieceStorage->getDiskAdaptor()->size();
if(actualFileSize != btContext->getTotalLength()) {
// Re-open file in writable mode to allow the program
// truncate the file to the specified length
_logger->debug("File size not match. Re-open file in writable mode."
" Expected:%s Actual:%s",
Util::uitos(btContext->getTotalLength()).c_str(),
Util::uitos(actualFileSize).c_str());
_pieceStorage->getDiskAdaptor()->closeFile();
_pieceStorage->getDiskAdaptor()->disableReadOnly();
_pieceStorage->getDiskAdaptor()->openFile();
}
}
if(!btContext->isPrivate() && _option->getAsBool(PREF_ENABLE_DHT)) { if(!btContext->isPrivate() && _option->getAsBool(PREF_ENABLE_DHT)) {
std::deque<Command*> commands; std::deque<Command*> commands;
DHTSetup().setup(commands, e, _option); DHTSetup().setup(commands, e, _option);

View file

@ -56,17 +56,15 @@ UnknownLengthPieceStorage::~UnknownLengthPieceStorage() {}
void UnknownLengthPieceStorage::initStorage() void UnknownLengthPieceStorage::initStorage()
{ {
DiskWriterHandle writer = _diskWriterFactory->newDiskWriter();
DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor()); DirectDiskAdaptorHandle directDiskAdaptor(new DirectDiskAdaptor());
directDiskAdaptor->setDiskWriter(writer);
directDiskAdaptor->setTotalLength(_downloadContext->getTotalLength()); directDiskAdaptor->setTotalLength(_downloadContext->getTotalLength());
directDiskAdaptor->setFileEntries(_downloadContext->getFileEntries());
DiskWriterHandle writer =
_diskWriterFactory->newDiskWriter(directDiskAdaptor->getFilePath());
directDiskAdaptor->setDiskWriter(writer);
_diskAdaptor = directDiskAdaptor; _diskAdaptor = directDiskAdaptor;
std::string storeDir = _downloadContext->getDir();
// if(storeDir == "") {
// storeDir = ".";
// }
_diskAdaptor->setStoreDir(storeDir);
_diskAdaptor->setFileEntries(_downloadContext->getFileEntries());
} }
bool UnknownLengthPieceStorage::hasMissingPiece(const SharedHandle<Peer>& peer) bool UnknownLengthPieceStorage::hasMissingPiece(const SharedHandle<Peer>& peer)

View file

@ -303,11 +303,11 @@ void Util::rangedFileCopy(const std::string& dest, const std::string& src, off_t
{ {
size_t bufSize = 4096; size_t bufSize = 4096;
unsigned char buf[bufSize]; unsigned char buf[bufSize];
DefaultDiskWriter srcdw; DefaultDiskWriter srcdw(src);
DefaultDiskWriter destdw; DefaultDiskWriter destdw(dest);
srcdw.openExistingFile(src); srcdw.openExistingFile();
destdw.initAndOpenFile(dest); destdw.initAndOpenFile();
lldiv_t res = lldiv(length, bufSize); lldiv_t res = lldiv(length, bufSize);
unsigned int x = res.quot; unsigned int x = res.quot;

View file

@ -22,8 +22,8 @@ CPPUNIT_TEST_SUITE_REGISTRATION( DefaultDiskWriterTest );
void DefaultDiskWriterTest::testSize() void DefaultDiskWriterTest::testSize()
{ {
DefaultDiskWriter dw; DefaultDiskWriter dw("4096chunk.txt");
dw.openExistingFile("4096chunk.txt"); dw.openExistingFile();
CPPUNIT_ASSERT_EQUAL((uint64_t)4096ULL, dw.size()); CPPUNIT_ASSERT_EQUAL((uint64_t)4096ULL, dw.size());
} }

View file

@ -37,9 +37,9 @@ void DirectDiskAdaptorTest::testCutTrailingGarbage()
fileEntries.push_back(entry); fileEntries.push_back(entry);
DirectDiskAdaptor adaptor; DirectDiskAdaptor adaptor;
adaptor.setDiskWriter(SharedHandle<DiskWriter>(new DefaultDiskWriter())); adaptor.setDiskWriter
(SharedHandle<DiskWriter>(new DefaultDiskWriter(entry->getPath())));
adaptor.setTotalLength(entry->getLength()); adaptor.setTotalLength(entry->getLength());
adaptor.setStoreDir(dir);
adaptor.setFileEntries(fileEntries); adaptor.setFileEntries(fileEntries);
adaptor.openFile(); adaptor.openFile();

View file

@ -36,12 +36,12 @@ void FallocFileAllocationIteratorTest::testAllocate()
File f(fn); File f(fn);
CPPUNIT_ASSERT_EQUAL((uint64_t)10, f.size()); CPPUNIT_ASSERT_EQUAL((uint64_t)10, f.size());
DefaultDiskWriter writer; DefaultDiskWriter writer(fn);
int64_t offset = 10; int64_t offset = 10;
int64_t totalLength = 40960; int64_t totalLength = 40960;
// we have to open file first. // we have to open file first.
writer.openExistingFile(fn); writer.openExistingFile();
FallocFileAllocationIterator itr(&writer, offset, totalLength); FallocFileAllocationIterator itr(&writer, offset, totalLength);
itr.allocateChunk(); itr.allocateChunk();

View file

@ -29,8 +29,9 @@ public:
CPPUNIT_TEST_SUITE_REGISTRATION( MessageDigestHelperTest ); CPPUNIT_TEST_SUITE_REGISTRATION( MessageDigestHelperTest );
void MessageDigestHelperTest::testDigestDiskWriter() { void MessageDigestHelperTest::testDigestDiskWriter() {
SharedHandle<DefaultDiskWriter> diskio(new DefaultDiskWriter()); SharedHandle<DefaultDiskWriter> diskio
diskio->openExistingFile("4096chunk.txt"); (new DefaultDiskWriter("4096chunk.txt"));
diskio->openExistingFile();
CPPUNIT_ASSERT_EQUAL(std::string("608cabc0f2fa18c260cafd974516865c772363d5"), CPPUNIT_ASSERT_EQUAL(std::string("608cabc0f2fa18c260cafd974516865c772363d5"),
MessageDigestHelper::digest("sha1", diskio, 0, 4096)); MessageDigestHelper::digest("sha1", diskio, 0, 4096));

View file

@ -175,8 +175,8 @@ void MetalinkProcessorTest::testParseFile()
void MetalinkProcessorTest::testParseFromBinaryStream() void MetalinkProcessorTest::testParseFromBinaryStream()
{ {
MetalinkProcessor proc; MetalinkProcessor proc;
DefaultDiskWriterHandle dw(new DefaultDiskWriter()); DefaultDiskWriterHandle dw(new DefaultDiskWriter("test.xml"));
dw->openExistingFile("test.xml"); dw->openExistingFile();
try { try {
SharedHandle<Metalinker> m = proc.parseFromBinaryStream(dw); SharedHandle<Metalinker> m = proc.parseFromBinaryStream(dw);

View file

@ -31,7 +31,6 @@ public:
void setUp() { void setUp() {
adaptor.reset(new MultiDiskAdaptor()); adaptor.reset(new MultiDiskAdaptor());
adaptor->setPieceLength(2); adaptor->setPieceLength(2);
adaptor->setStoreDir(".");
} }
void testWriteData(); void testWriteData();
@ -384,7 +383,6 @@ void MultiDiskAdaptorTest::testCutTrailingGarbage()
(&entries[0], &entries[arrayLength(entries)]); (&entries[0], &entries[arrayLength(entries)]);
MultiDiskAdaptor adaptor; MultiDiskAdaptor adaptor;
adaptor.setStoreDir(dir);
adaptor.setFileEntries(fileEntries); adaptor.setFileEntries(fileEntries);
adaptor.setMaxOpenFiles(1); adaptor.setMaxOpenFiles(1);
adaptor.setPieceLength(1); adaptor.setPieceLength(1);
@ -414,7 +412,6 @@ void MultiDiskAdaptorTest::testSize()
(&entries[0], &entries[arrayLength(entries)]); (&entries[0], &entries[arrayLength(entries)]);
MultiDiskAdaptor adaptor; MultiDiskAdaptor adaptor;
adaptor.setStoreDir(dir);
adaptor.setFileEntries(fileEntries); adaptor.setFileEntries(fileEntries);
adaptor.setMaxOpenFiles(1); adaptor.setMaxOpenFiles(1);
adaptor.setPieceLength(1); adaptor.setPieceLength(1);
@ -444,7 +441,6 @@ void MultiDiskAdaptorTest::testUtime()
std::deque<SharedHandle<FileEntry> > fileEntries std::deque<SharedHandle<FileEntry> > fileEntries
(&entries[0], &entries[arrayLength(entries)]); (&entries[0], &entries[arrayLength(entries)]);
MultiDiskAdaptor adaptor; MultiDiskAdaptor adaptor;
adaptor.setStoreDir(storeDir);
adaptor.setFileEntries(fileEntries); adaptor.setFileEntries(fileEntries);
time_t atime = (time_t) 100000; time_t atime = (time_t) 100000;

View file

@ -67,7 +67,6 @@ void MultiFileAllocationIteratorTest::testMakeDiskWriterEntries()
diskAdaptor->setFileEntries diskAdaptor->setFileEntries
(std::deque<SharedHandle<FileEntry> >(&fs[0], &fs[arrayLength(fs)])); (std::deque<SharedHandle<FileEntry> >(&fs[0], &fs[arrayLength(fs)]));
diskAdaptor->setPieceLength(1024); diskAdaptor->setPieceLength(1024);
diskAdaptor->setStoreDir(storeDir);
diskAdaptor->openFile(); diskAdaptor->openFile();
SharedHandle<MultiFileAllocationIterator> itr SharedHandle<MultiFileAllocationIterator> itr
@ -155,7 +154,6 @@ void MultiFileAllocationIteratorTest::testAllocate()
try { try {
SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor()); SharedHandle<MultiDiskAdaptor> diskAdaptor(new MultiDiskAdaptor());
diskAdaptor->setStoreDir(storeDir);
diskAdaptor->setPieceLength(1); diskAdaptor->setPieceLength(1);
int64_t offset = 0; int64_t offset = 0;

View file

@ -34,12 +34,12 @@ void SingleFileAllocationIteratorTest::testAllocate()
File x(fn); File x(fn);
CPPUNIT_ASSERT_EQUAL((uint64_t)10, x.size()); CPPUNIT_ASSERT_EQUAL((uint64_t)10, x.size());
DefaultDiskWriter writer; DefaultDiskWriter writer(fn);
int64_t offset = 10; int64_t offset = 10;
int64_t totalLength = 16*1024*2+8*1024; int64_t totalLength = 16*1024*2+8*1024;
// we have to open file first. // we have to open file first.
writer.openExistingFile(fn); writer.openExistingFile();
SingleFileAllocationIterator itr(&writer, offset, totalLength); SingleFileAllocationIterator itr(&writer, offset, totalLength);
itr.init(); itr.init();

View file

@ -638,7 +638,7 @@ void UtilTest::testToString_binaryStream()
{ {
SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter()); SharedHandle<DiskWriter> dw(new ByteArrayDiskWriter());
std::string data(16*1024+256, 'a'); std::string data(16*1024+256, 'a');
dw->initAndOpenFile("dummy"); dw->initAndOpenFile();
dw->writeData((const unsigned char*)data.c_str(), data.size(), 0); dw->writeData((const unsigned char*)data.c_str(), data.size(), 0);
std::string readData = Util::toString(dw); std::string readData = Util::toString(dw);