/* */ #include "FileEntry.h" #include #include "Util.h" #include "URISelector.h" namespace aria2 { FileEntry::FileEntry(const std::string& path, uint64_t length, off_t offset, const std::deque& uris): path(path), _uris(uris), length(length), offset(offset), extracted(false), requested(true) {} FileEntry::~FileEntry() {} void FileEntry::setupDir() { Util::mkdirs(File(path).getDirname()); } FileEntry& FileEntry::operator=(const FileEntry& entry) { if(this != &entry) { path = entry.path; length = entry.length; offset = entry.offset; extracted = entry.extracted; requested = entry.requested; } return *this; } bool FileEntry::operator<(const FileEntry& fileEntry) const { return offset < fileEntry.offset; } bool FileEntry::exists() const { return File(getPath()).exists(); } off_t FileEntry::gtoloff(off_t goff) const { assert(offset <= goff); return goff-offset; } void FileEntry::getUris(std::deque& uris) const { uris.insert(uris.end(), _spentUris.begin(), _spentUris.end()); uris.insert(uris.end(), _uris.begin(), _uris.end()); } std::string FileEntry::selectUri(const SharedHandle& uriSelector) { return uriSelector->select(_uris); } SharedHandle FileEntry::getRequest(const SharedHandle& selector) { SharedHandle req; if(_requestPool.empty()) { while(1) { std::string uri = selector->select(_uris); if(uri.empty()) { return req; } req.reset(new Request()); if(req->setUrl(uri)) { _spentUris.push_back(uri); _inFlightRequests.push_back(req); return req; } else { req.reset(); } } } else { req = _requestPool.back(); _requestPool.pop_back(); _inFlightRequests.push_back(req); return req; } } void FileEntry::poolRequest(const SharedHandle& request) { removeRequest(request); _requestPool.push_back(request); } bool FileEntry::removeRequest(const SharedHandle& request) { for(std::deque >::iterator i = _inFlightRequests.begin(); i != _inFlightRequests.end(); ++i) { if((*i).get() == request.get()) { _inFlightRequests.erase(i); return true; } } return false; } } // namespace aria2