| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- /* <!-- copyright */
- /*
- * aria2 - a simple utility for downloading files faster
- *
- * 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
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- /* copyright --> */
- #include "TorrentMan.h"
- #include "Dictionary.h"
- #include "List.h"
- #include "ShaVisitor.h"
- #include "Util.h"
- #include "MetaFileUtil.h"
- #include "DlAbortEx.h"
- #include "File.h"
- #include "message.h"
- #include "PreAllocationDiskWriter.h"
- #include <errno.h>
- #include <libgen.h>
- #include <string.h>
- TorrentMan::TorrentMan():bitfield(NULL),
- peerEntryIdCounter(0), cuidCounter(0),
- downloadedSize(0), uploadedSize(0),
- deltaDownload(0), deltaUpload(0),
- storeDir("."),
- multiFileTopDir(NULL),
- setupComplete(false),
- interval(DEFAULT_ANNOUNCE_INTERVAL),
- minInterval(DEFAULT_ANNOUNCE_MIN_INTERVAL),
- complete(0), incomplete(0),
- connections(0), diskWriter(NULL) {}
- TorrentMan::~TorrentMan() {
- if(bitfield != NULL) {
- delete bitfield;
- }
- if(multiFileTopDir != NULL) {
- delete multiFileTopDir;
- }
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- delete *itr;
- }
- if(diskWriter != NULL) {
- delete diskWriter;
- }
- }
- // TODO do not use this method in application code
- void TorrentMan::updatePeers(const Peers& peers) {
- this->peers = peers;
- }
- bool TorrentMan::addPeer(Peer* peer, bool duplicate) {
- if(duplicate) {
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer* p = *itr;
- if(p->ipaddr == peer->ipaddr && p->port == peer->port && p->error > 0) {
- return false;
- }
- }
- } else {
- if(peers.size() >= MAX_PEER_LIST_SIZE) {
- deleteOldErrorPeers(100);
- if(peers.size() >= MAX_PEER_LIST_SIZE) {
- return false;
- }
- }
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer* p = *itr;
- if(p->ipaddr == peer->ipaddr && p->port == peer->port) {
- return false;
- }
- }
- }
- ++peerEntryIdCounter;
- peer->entryId = peerEntryIdCounter;
- peers.push_back(peer);
- return true;
- }
- /*
- void TorrentMan::updatePeer(const Peer& peer) {
- for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer& p = *itr;
- if(p.eid == peer.eid) {
- p = peer;
- break;
- }
- }
- }
- */
- bool TorrentMan::isPeerAvailable() const {
- return getPeer() != Peer::nullPeer;
- }
- int TorrentMan::deleteOldErrorPeers(int maxNum) {
- int counter = 0;
- for(Peers::iterator itr = peers.begin(); itr != peers.end();) {
- Peer* p = *itr;
- if(p->error != 0 && p->cuid == 0) {
- delete p;
- itr = peers.erase(itr);
- counter++;
- if(maxNum <= counter) {
- break;
- }
- } else {
- itr++;
- }
- }
- return counter;
- }
- Peer* TorrentMan::getPeer() const {
- for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); itr++) {
- Peer* p = *itr;
- if(p->cuid == 0 && p->error == 0) {
- return p;
- }
- }
- return Peer::nullPeer;
- }
- bool TorrentMan::isEndGame() const {
- return bitfield->countMissingBlock() <= END_GAME_PIECE_NUM;
- }
- Piece TorrentMan::getMissingPiece(const Peer* peer) {
- int index = -1;
- if(isEndGame()) {
- index = bitfield->getMissingIndex(peer->getBitfield(), peer->getBitfieldLength());
- } else {
- index = bitfield->getMissingUnusedIndex(peer->getBitfield(), peer->getBitfieldLength());
- }
- if(index == -1) {
- return Piece::nullPiece;
- }
- bitfield->setUseBit(index);
- Piece piece = findUsedPiece(index);
- if(Piece::isNull(piece)) {
- Piece piece(index, bitfield->getBlockLength(index));
- addUsedPiece(piece);
- return piece;
- } else {
- return piece;
- }
- }
- int TorrentMan::deleteUsedPiecesByFillRate(int fillRate, int toDelete) {
- int deleted = 0;
- for(UsedPieces::iterator itr = usedPieces.begin();
- itr != usedPieces.end() && deleted < toDelete;) {
- Piece& piece = *itr;
- if(!bitfield->isUseBitSet(piece.getIndex()) &&
- piece.countCompleteBlock() <= piece.countBlock()*(fillRate/100.0)) {
- logger->debug("deleting used piece index=%d, fillRate(%%)=%d<=%d",
- piece.getIndex(),
- (piece.countCompleteBlock()*100)/piece.countBlock(),
- fillRate);
- itr = usedPieces.erase(itr);
- deleted++;
- } else {
- itr++;
- }
- }
- return deleted;
- }
- void TorrentMan::reduceUsedPieces(int max) {
- int toDelete = usedPieces.size()-max;
- if(toDelete <= 0) {
- return;
- }
- int fillRate = 10;
- while(fillRate < 50) {
- int deleted = deleteUsedPiecesByFillRate(fillRate, toDelete);
- if(deleted == 0) {
- break;
- }
- toDelete -= deleted;
- fillRate += 10;
- }
- }
- void TorrentMan::addUsedPiece(const Piece& piece) {
- usedPieces.push_back(piece);
- }
- Piece TorrentMan::findUsedPiece(int index) const {
- for(UsedPieces::const_iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- const Piece& piece = *itr;
- if(piece.getIndex() == index) {
- return piece;
- }
- }
- return Piece::nullPiece;
- }
- void TorrentMan::deleteUsedPiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- if(itr->getIndex() == piece.getIndex()) {
- usedPieces.erase(itr);
- break;
- }
- }
- }
- void TorrentMan::completePiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- if(!hasPiece(piece.getIndex())) {
- addDownloadedSize(piece.getLength());
- }
- bitfield->setBit(piece.getIndex());
- bitfield->unsetUseBit(piece.getIndex());
- deleteUsedPiece(piece);
- if(!isEndGame()) {
- reduceUsedPieces(100);
- }
- }
- void TorrentMan::cancelPiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- bitfield->unsetUseBit(piece.getIndex());
- if(!isEndGame()) {
- if(piece.countCompleteBlock() == 0) {
- deleteUsedPiece(piece);
- }
- }
- }
- void TorrentMan::updatePiece(const Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- if(itr->getIndex() == piece.getIndex()) {
- *itr = piece;
- break;
- }
- }
- }
- void TorrentMan::syncPiece(Piece& piece) {
- if(Piece::isNull(piece)) {
- return;
- }
- for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
- if(itr->getIndex() == piece.getIndex()) {
- piece = *itr;
- return;
- }
- }
- // hasPiece(piece.getIndex()) is true, then set all bit of
- // piece.bitfield to 1
- if(hasPiece(piece.getIndex())) {
- piece.setAllBlock();
- }
- }
- void TorrentMan::initBitfield() {
- if(bitfield != NULL) {
- delete bitfield;
- }
- bitfield = new BitfieldMan(pieceLength, totalSize);
- }
- void TorrentMan::setBitfield(unsigned char* bitfield, int bitfieldLength) {
- if(this->bitfield == NULL) {
- initBitfield();
- }
- this->bitfield->setBitfield(bitfield, bitfieldLength);
- }
- bool TorrentMan::downloadComplete() const {
- return bitfield->isAllBitSet();
- }
- void TorrentMan::setup(string metaInfoFile) {
- peerId = "-A2****-";
- for(int i = 0; i < 12; i++) {
- peerId += Util::itos((int)(((double)10)*random()/(RAND_MAX+1.0)));
- }
- uploadedSize = 0;
- downloadedSize = 0;
- Dictionary* topDic = (Dictionary*)MetaFileUtil::parseMetaFile(metaInfoFile);
- const Dictionary* infoDic = (const Dictionary*)topDic->get("info");
- ShaVisitor v;
- infoDic->accept(&v);
- unsigned char md[20];
- int len;
- v.getHash(md, len);
- setInfoHash(md);
- Data* topName = (Data*)infoDic->get("name");
- if(topName != NULL) {
- name = topName->toString();
- } else {
- char* basec = strdup(metaInfoFile.c_str());
- name = string(basename(basec))+".file";
- free(basec);
- }
- List* files = (List*)infoDic->get("files");
- if(files == NULL) {
- // single-file mode;
- setFileMode(SINGLE);
- Data* length = (Data*)infoDic->get("length");
- totalSize = length->toLLInt();
- } else {
- long long int length = 0;
- // multi-file mode
- setFileMode(MULTI);
- multiFileTopDir = new Directory(name);
- const MetaList& metaList = files->getList();
- for(MetaList::const_iterator itr = metaList.begin(); itr != metaList.end();
- itr++) {
- Dictionary* fileDic = (Dictionary*)(*itr);
- Data* lengthData = (Data*)fileDic->get("length");
- length += lengthData->toLLInt();
- List* path = (List*)fileDic->get("path");
- const MetaList& paths = path->getList();
- Directory* parentDir = multiFileTopDir;
- string filePath = name;
- for(int i = 0; i < (int)paths.size()-1; i++) {
- Data* subpath = (Data*)paths.at(i);
- Directory* dir = new Directory(subpath->toString());
- parentDir->addFile(dir);
- parentDir = dir;
- filePath.append("/").append(subpath->toString());
- }
- Data* lastpath = (Data*)paths.back();
- filePath.append("/").append(lastpath->toString());
- FileEntry fileEntry(filePath, lengthData->toLLInt());
- multiFileEntries.push_back(fileEntry);
- }
- totalSize = length;
- }
- announce = ((Data*)topDic->get("announce"))->toString();
- pieceLength = ((Data*)infoDic->get("piece length"))->toInt();
- pieces = totalSize/pieceLength+(totalSize%pieceLength ? 1 : 0);
- Data* piecesHashData = (Data*)infoDic->get("pieces");
- if(piecesHashData->getLen() != pieces*20) {
- throw new DlAbortEx("the number of pieces is wrong.");
- }
- for(int index = 0; index < pieces; index++) {
- string hex = Util::toHex((unsigned char*)&piecesHashData->getData()[index*20], 20);
- pieceHashes.push_back(hex);
- logger->debug("piece #%d, hash:%s", index, hex.c_str());
- }
- initBitfield();
- delete topDic;
- diskWriter = new PreAllocationDiskWriter(totalSize);
- if(segmentFileExists()) {
- load();
- diskWriter->openExistingFile(getTempFilePath());
- } else {
- diskWriter->initAndOpenFile(getTempFilePath());
- }
- setupComplete = true;
- }
- bool TorrentMan::hasPiece(int index) const {
- return bitfield->isBitSet(index);
- }
- string TorrentMan::getPieceHash(int index) const {
- return pieceHashes.at(index);
- }
- string TorrentMan::getFilePath() const {
- return storeDir+"/"+name;
- }
- string TorrentMan::getTempFilePath() const {
- return getFilePath()+".a2tmp";
- }
- string TorrentMan::getSegmentFilePath() const {
- return getFilePath()+".aria2";
- }
- bool TorrentMan::segmentFileExists() const {
- string segFilename = getSegmentFilePath();
- File f(segFilename);
- if(f.isFile()) {
- logger->info(MSG_SEGMENT_FILE_EXISTS, segFilename.c_str());
- return true;
- } else {
- logger->info(MSG_SEGMENT_FILE_DOES_NOT_EXIST, segFilename.c_str());
- return false;
- }
- }
- FILE* TorrentMan::openSegFile(string segFilename, string mode) const {
- FILE* segFile = fopen(segFilename.c_str(), mode.c_str());
- if(segFile == NULL) {
- throw new DlAbortEx(strerror(errno));
- }
- return segFile;
- }
- void TorrentMan::load() {
- string segFilename = getSegmentFilePath();
- logger->info(MSG_LOADING_SEGMENT_FILE, segFilename.c_str());
- FILE* segFile = openSegFile(segFilename, "r+");
- read(segFile);
- fclose(segFile);
- logger->info(MSG_LOADED_SEGMENT_FILE);
- }
- void TorrentMan::read(FILE* file) {
- assert(file != NULL);
- unsigned char savedInfoHash[INFO_HASH_LENGTH];
- if(fread(savedInfoHash, INFO_HASH_LENGTH, 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(Util::toHex(savedInfoHash, INFO_HASH_LENGTH) != Util::toHex(infoHash, INFO_HASH_LENGTH)) {
- throw new DlAbortEx("info hash mismatch");
- }
- unsigned char* savedBitfield = new unsigned char[bitfield->getBitfieldLength()];
- try {
- if(fread(savedBitfield, bitfield->getBitfieldLength(), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- setBitfield(savedBitfield, bitfield->getBitfieldLength());
- if(fread(&downloadedSize, sizeof(downloadedSize), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fread(&uploadedSize, sizeof(uploadedSize), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- delete [] savedBitfield;
- } catch(Exception* ex) {
- delete [] savedBitfield;
- throw;
- }
- }
- void TorrentMan::save() const {
- if(!setupComplete) {
- return;
- }
- string segFilename = getSegmentFilePath();
- logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str());
- FILE* file = openSegFile(segFilename, "w");
- if(fwrite(infoHash, INFO_HASH_LENGTH, 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(bitfield->getBitfield(), bitfield->getBitfieldLength(), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(&downloadedSize, sizeof(downloadedSize), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- if(fwrite(&uploadedSize, sizeof(uploadedSize), 1, file) < 1) {
- throw new DlAbortEx(strerror(errno));
- }
- fclose(file);
- logger->info(MSG_SAVED_SEGMENT_FILE);
- }
- void TorrentMan::remove() const {
- if(segmentFileExists()) {
- File f(getSegmentFilePath());
- f.remove();
- }
- }
- void TorrentMan::fixFilename() const {
- if(fileMode == SINGLE) {
- copySingleFile();
- } else {
- splitMultiFile();
- }
- }
- void TorrentMan::copySingleFile() const {
- logger->info("writing file %s", getFilePath().c_str());
- Util::fileCopy(getFilePath(), getTempFilePath());
- }
- void TorrentMan::splitMultiFile() const {
- logger->info("creating directories");
- multiFileTopDir->createDir(storeDir, true);
- long long int offset = 0;
- for(MultiFileEntries::const_iterator itr = multiFileEntries.begin();
- itr != multiFileEntries.end(); itr++) {
- string dest = storeDir+"/"+itr->path;
- logger->info("writing file %s", dest.c_str());
- Util::rangedFileCopy(dest, getTempFilePath(), offset, itr->length);
- offset += itr->length;
- }
- }
- void TorrentMan::deleteTempFile() const {
- unlink(getTempFilePath().c_str());
- }
|