/* */ #include "AdaptiveFileAllocationIterator.h" #include "BinaryStream.h" #ifdef HAVE_FALLOCATE # include "FallocFileAllocationIterator.h" #endif // HAVE_FALLOCATE #include "SingleFileAllocationIterator.h" #include "RecoverableException.h" #include "LogFactory.h" #include "Logger.h" namespace aria2 { AdaptiveFileAllocationIterator::AdaptiveFileAllocationIterator (BinaryStream* stream, off_t offset, uint64_t totalLength): stream_(stream), offset_(offset), totalLength_(totalLength), logger_(LogFactory::getInstance()) {} AdaptiveFileAllocationIterator::~AdaptiveFileAllocationIterator() {} void AdaptiveFileAllocationIterator::allocateChunk() { if(allocator_.isNull()) { #ifdef HAVE_FALLOCATE try { if(logger_->debug()) { logger_->debug("Testing file system supports fallocate."); } if(static_cast(offset_) < totalLength_) { off_t len = std::min(totalLength_-offset_, static_cast(4096)); stream_->allocate(offset_, len); offset_ += len; } if(logger_->debug()) { logger_->debug("File system supports fallocate."); } allocator_.reset (new FallocFileAllocationIterator(stream_, offset_, totalLength_)); } catch(RecoverableException& e) { if(logger_->debug()) { logger_->debug("File system does not support fallocate."); } SharedHandle salloc (new SingleFileAllocationIterator(stream_, offset_, totalLength_)); salloc->init(); allocator_ = salloc; } #else // !HAVE_FALLOCATE SharedHandle salloc (new SingleFileAllocationIterator(stream_, offset_, totalLength_)); salloc->init(); allocator_ = salloc; #endif // !HAVE_FALLOCATE allocator_->allocateChunk(); } else { allocator_->allocateChunk(); } } bool AdaptiveFileAllocationIterator::finished() { if(allocator_.isNull()) { return (uint64_t)offset_ >= totalLength_; } else { return allocator_->finished(); } } } // namespace aria2