TorrentMan.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - a simple utility for downloading files faster
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /* copyright --> */
  22. #include "TorrentMan.h"
  23. #include "Dictionary.h"
  24. #include "List.h"
  25. #include "ShaVisitor.h"
  26. #include "Util.h"
  27. #include "MetaFileUtil.h"
  28. #include "DlAbortEx.h"
  29. #include "File.h"
  30. #include "message.h"
  31. #include "PreAllocationDiskWriter.h"
  32. #include "DefaultDiskWriter.h"
  33. #include "MultiDiskWriter.h"
  34. #include "prefs.h"
  35. #include <errno.h>
  36. #include <libgen.h>
  37. #include <string.h>
  38. TorrentMan::TorrentMan():bitfield(NULL),
  39. peerEntryIdCounter(0), cuidCounter(0),
  40. downloadLength(0), uploadLength(0),
  41. preDownloadLength(0), preUploadLength(0),
  42. deltaDownloadLength(0), deltaUploadLength(0),
  43. storeDir("."),
  44. multiFileTopDir(NULL),
  45. setupComplete(false),
  46. interval(DEFAULT_ANNOUNCE_INTERVAL),
  47. minInterval(DEFAULT_ANNOUNCE_MIN_INTERVAL),
  48. complete(0), incomplete(0),
  49. connections(0), diskWriter(NULL) {}
  50. TorrentMan::~TorrentMan() {
  51. if(bitfield != NULL) {
  52. delete bitfield;
  53. }
  54. if(multiFileTopDir != NULL) {
  55. delete multiFileTopDir;
  56. }
  57. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  58. delete *itr;
  59. }
  60. if(diskWriter != NULL) {
  61. delete diskWriter;
  62. }
  63. }
  64. // TODO do not use this method in application code
  65. void TorrentMan::updatePeers(const Peers& peers) {
  66. this->peers = peers;
  67. }
  68. bool TorrentMan::addPeer(Peer* peer, bool duplicate) {
  69. if(duplicate) {
  70. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  71. Peer* p = *itr;
  72. if(p->ipaddr == peer->ipaddr && p->port == peer->port && p->error > 0) {
  73. return false;
  74. }
  75. }
  76. } else {
  77. deleteOldErrorPeers(MAX_PEER_LIST_SIZE);
  78. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  79. Peer* p = *itr;
  80. if(p->ipaddr == peer->ipaddr && p->port == peer->port) {
  81. return false;
  82. }
  83. }
  84. }
  85. ++peerEntryIdCounter;
  86. peer->entryId = peerEntryIdCounter;
  87. peers.push_back(peer);
  88. return true;
  89. }
  90. /*
  91. void TorrentMan::updatePeer(const Peer& peer) {
  92. for(Peers::iterator itr = peers.begin(); itr != peers.end(); itr++) {
  93. Peer& p = *itr;
  94. if(p.eid == peer.eid) {
  95. p = peer;
  96. break;
  97. }
  98. }
  99. }
  100. */
  101. bool TorrentMan::isPeerAvailable() const {
  102. return getPeer() != Peer::nullPeer;
  103. }
  104. int TorrentMan::deleteOldErrorPeers(int maxNum) {
  105. int counter = 0;
  106. for(Peers::iterator itr = peers.begin(); itr != peers.end();) {
  107. Peer* p = *itr;
  108. if(p->error >= MAX_PEER_ERROR && p->cuid == 0) {
  109. delete p;
  110. itr = peers.erase(itr);
  111. counter++;
  112. if(maxNum <= counter) {
  113. break;
  114. }
  115. } else {
  116. itr++;
  117. }
  118. }
  119. return counter;
  120. }
  121. Peer* TorrentMan::getPeer() const {
  122. for(Peers::const_iterator itr = peers.begin(); itr != peers.end(); itr++) {
  123. Peer* p = *itr;
  124. if(p->cuid == 0 && p->error < MAX_PEER_ERROR) {
  125. return p;
  126. }
  127. }
  128. return Peer::nullPeer;
  129. }
  130. bool TorrentMan::isEndGame() const {
  131. return bitfield->countMissingBlock() <= END_GAME_PIECE_NUM;
  132. }
  133. Piece TorrentMan::getMissingPiece(const Peer* peer) {
  134. int index = -1;
  135. if(isEndGame()) {
  136. index = bitfield->getMissingIndex(peer->getBitfield(), peer->getBitfieldLength());
  137. } else {
  138. index = bitfield->getMissingUnusedIndex(peer->getBitfield(), peer->getBitfieldLength());
  139. }
  140. if(index == -1) {
  141. return Piece::nullPiece;
  142. }
  143. bitfield->setUseBit(index);
  144. Piece piece = findUsedPiece(index);
  145. if(Piece::isNull(piece)) {
  146. Piece piece(index, bitfield->getBlockLength(index));
  147. addUsedPiece(piece);
  148. return piece;
  149. } else {
  150. return piece;
  151. }
  152. }
  153. int TorrentMan::deleteUsedPiecesByFillRate(int fillRate, int toDelete) {
  154. int deleted = 0;
  155. for(UsedPieces::iterator itr = usedPieces.begin();
  156. itr != usedPieces.end() && deleted < toDelete;) {
  157. Piece& piece = *itr;
  158. if(!bitfield->isUseBitSet(piece.getIndex()) &&
  159. piece.countCompleteBlock() <= piece.countBlock()*(fillRate/100.0)) {
  160. logger->debug("deleting used piece index=%d, fillRate(%%)=%d<=%d",
  161. piece.getIndex(),
  162. (piece.countCompleteBlock()*100)/piece.countBlock(),
  163. fillRate);
  164. itr = usedPieces.erase(itr);
  165. deleted++;
  166. } else {
  167. itr++;
  168. }
  169. }
  170. return deleted;
  171. }
  172. void TorrentMan::reduceUsedPieces(int max) {
  173. int toDelete = usedPieces.size()-max;
  174. if(toDelete <= 0) {
  175. return;
  176. }
  177. int fillRate = 10;
  178. while(fillRate < 50) {
  179. int deleted = deleteUsedPiecesByFillRate(fillRate, toDelete);
  180. if(deleted == 0) {
  181. break;
  182. }
  183. toDelete -= deleted;
  184. fillRate += 10;
  185. }
  186. }
  187. void TorrentMan::addUsedPiece(const Piece& piece) {
  188. usedPieces.push_back(piece);
  189. }
  190. Piece TorrentMan::findUsedPiece(int index) const {
  191. for(UsedPieces::const_iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
  192. const Piece& piece = *itr;
  193. if(piece.getIndex() == index) {
  194. return piece;
  195. }
  196. }
  197. return Piece::nullPiece;
  198. }
  199. void TorrentMan::deleteUsedPiece(const Piece& piece) {
  200. if(Piece::isNull(piece)) {
  201. return;
  202. }
  203. for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
  204. if(itr->getIndex() == piece.getIndex()) {
  205. usedPieces.erase(itr);
  206. break;
  207. }
  208. }
  209. }
  210. void TorrentMan::completePiece(const Piece& piece) {
  211. if(Piece::isNull(piece)) {
  212. return;
  213. }
  214. if(!hasPiece(piece.getIndex())) {
  215. addDownloadLength(piece.getLength());
  216. }
  217. bitfield->setBit(piece.getIndex());
  218. bitfield->unsetUseBit(piece.getIndex());
  219. deleteUsedPiece(piece);
  220. if(!isEndGame()) {
  221. reduceUsedPieces(100);
  222. }
  223. }
  224. void TorrentMan::cancelPiece(const Piece& piece) {
  225. if(Piece::isNull(piece)) {
  226. return;
  227. }
  228. bitfield->unsetUseBit(piece.getIndex());
  229. if(!isEndGame()) {
  230. if(piece.countCompleteBlock() == 0) {
  231. deleteUsedPiece(piece);
  232. }
  233. }
  234. }
  235. void TorrentMan::updatePiece(const Piece& piece) {
  236. if(Piece::isNull(piece)) {
  237. return;
  238. }
  239. for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
  240. if(itr->getIndex() == piece.getIndex()) {
  241. *itr = piece;
  242. break;
  243. }
  244. }
  245. }
  246. void TorrentMan::syncPiece(Piece& piece) {
  247. if(Piece::isNull(piece)) {
  248. return;
  249. }
  250. for(UsedPieces::iterator itr = usedPieces.begin(); itr != usedPieces.end(); itr++) {
  251. if(itr->getIndex() == piece.getIndex()) {
  252. piece = *itr;
  253. return;
  254. }
  255. }
  256. // hasPiece(piece.getIndex()) is true, then set all bit of
  257. // piece.bitfield to 1
  258. if(hasPiece(piece.getIndex())) {
  259. piece.setAllBlock();
  260. }
  261. }
  262. void TorrentMan::initBitfield() {
  263. if(bitfield != NULL) {
  264. delete bitfield;
  265. }
  266. bitfield = new BitfieldMan(pieceLength, totalLength);
  267. }
  268. void TorrentMan::setBitfield(unsigned char* bitfield, int bitfieldLength) {
  269. if(this->bitfield == NULL) {
  270. initBitfield();
  271. }
  272. this->bitfield->setBitfield(bitfield, bitfieldLength);
  273. }
  274. bool TorrentMan::downloadComplete() const {
  275. return bitfield->isAllBitSet();
  276. }
  277. void TorrentMan::readFileEntry(const Dictionary* infoDic, const string& defaultName) {
  278. Data* topName = (Data*)infoDic->get("name");
  279. if(topName != NULL) {
  280. name = topName->toString();
  281. } else {
  282. char* basec = strdup(defaultName.c_str());
  283. name = string(basename(basec))+".file";
  284. free(basec);
  285. }
  286. List* files = (List*)infoDic->get("files");
  287. if(files == NULL) {
  288. // single-file mode;
  289. setFileMode(SINGLE);
  290. Data* length = (Data*)infoDic->get("length");
  291. totalLength = length->toLLInt();
  292. } else {
  293. long long int length = 0;
  294. long long int offset = 0;
  295. // multi-file mode
  296. setFileMode(MULTI);
  297. multiFileTopDir = new Directory(name);
  298. const MetaList& metaList = files->getList();
  299. for(MetaList::const_iterator itr = metaList.begin(); itr != metaList.end();
  300. itr++) {
  301. Dictionary* fileDic = (Dictionary*)(*itr);
  302. Data* lengthData = (Data*)fileDic->get("length");
  303. length += lengthData->toLLInt();
  304. List* path = (List*)fileDic->get("path");
  305. const MetaList& paths = path->getList();
  306. Directory* parentDir = multiFileTopDir;
  307. string filePath = name;
  308. for(int i = 0; i < (int)paths.size()-1; i++) {
  309. Data* subpath = (Data*)paths.at(i);
  310. Directory* dir = new Directory(subpath->toString());
  311. parentDir->addFile(dir);
  312. parentDir = dir;
  313. filePath.append("/").append(subpath->toString());
  314. }
  315. Data* lastpath = (Data*)paths.back();
  316. filePath.append("/").append(lastpath->toString());
  317. FileEntry fileEntry(filePath, lengthData->toLLInt(), offset);
  318. multiFileEntries.push_back(fileEntry);
  319. offset += fileEntry.length;
  320. }
  321. totalLength = length;
  322. }
  323. }
  324. void TorrentMan::setup(string metaInfoFile) {
  325. peerId = "-A2****-";
  326. for(int i = 0; i < 12; i++) {
  327. peerId += Util::itos((int)(((double)10)*random()/(RAND_MAX+1.0)));
  328. }
  329. uploadLength = 0;
  330. downloadLength = 0;
  331. Dictionary* topDic = (Dictionary*)MetaFileUtil::parseMetaFile(metaInfoFile);
  332. const Dictionary* infoDic = (const Dictionary*)topDic->get("info");
  333. ShaVisitor v;
  334. infoDic->accept(&v);
  335. unsigned char md[20];
  336. int len;
  337. v.getHash(md, len);
  338. setInfoHash(md);
  339. readFileEntry(infoDic, metaInfoFile);
  340. announce = ((Data*)topDic->get("announce"))->toString();
  341. pieceLength = ((Data*)infoDic->get("piece length"))->toInt();
  342. pieces = totalLength/pieceLength+(totalLength%pieceLength ? 1 : 0);
  343. Data* piecesHashData = (Data*)infoDic->get("pieces");
  344. if(piecesHashData->getLen() != pieces*20) {
  345. throw new DlAbortEx("the number of pieces is wrong.");
  346. }
  347. for(int index = 0; index < pieces; index++) {
  348. string hex = Util::toHex((unsigned char*)&piecesHashData->getData()[index*20], 20);
  349. pieceHashes.push_back(hex);
  350. logger->debug("piece #%d, hash:%s", index, hex.c_str());
  351. }
  352. initBitfield();
  353. delete topDic;
  354. }
  355. void TorrentMan::setupDiskWriter() {
  356. if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE) {
  357. if(segmentFileExists()) {
  358. load();
  359. }
  360. if(fileMode == SINGLE) {
  361. diskWriter = new DefaultDiskWriter();
  362. } else {
  363. diskWriter = new MultiDiskWriter();
  364. ((MultiDiskWriter*)diskWriter)->setMultiFileEntries(multiFileEntries, pieceLength);
  365. multiFileTopDir->createDir(storeDir, true);
  366. }
  367. diskWriter->openFile(getFilePath());
  368. } else {
  369. if(option->get(PREF_NO_PREALLOCATION) == V_TRUE) {
  370. diskWriter = new DefaultDiskWriter();
  371. } else {
  372. diskWriter = new PreAllocationDiskWriter(totalLength);
  373. }
  374. if(segmentFileExists()) {
  375. load();
  376. diskWriter->openExistingFile(getTempFilePath());
  377. } else {
  378. diskWriter->initAndOpenFile(getTempFilePath());
  379. }
  380. }
  381. setupComplete = true;
  382. }
  383. const MultiFileEntries& TorrentMan::getMultiFileEntries() const {
  384. return multiFileEntries;
  385. }
  386. void TorrentMan::readFileEntryFromMetaInfoFile(const string& metaInfoFile) {
  387. Dictionary* topDic = (Dictionary*)MetaFileUtil::parseMetaFile(metaInfoFile);
  388. const Dictionary* infoDic = (const Dictionary*)topDic->get("info");
  389. readFileEntry(infoDic, metaInfoFile);
  390. }
  391. string TorrentMan::getName() const {
  392. return name;
  393. }
  394. bool TorrentMan::hasPiece(int index) const {
  395. return bitfield->isBitSet(index);
  396. }
  397. string TorrentMan::getPieceHash(int index) const {
  398. return pieceHashes.at(index);
  399. }
  400. string TorrentMan::getFilePath() const {
  401. if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE && fileMode == MULTI) {
  402. return storeDir;
  403. } else {
  404. return storeDir+"/"+name;
  405. }
  406. }
  407. string TorrentMan::getTempFilePath() const {
  408. if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE) {
  409. return getFilePath();
  410. } else {
  411. return getFilePath()+".a2tmp";
  412. }
  413. }
  414. string TorrentMan::getSegmentFilePath() const {
  415. if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE && fileMode == MULTI) {
  416. return storeDir+"/"+name+".aria2";
  417. } else {
  418. return getFilePath()+".aria2";
  419. }
  420. }
  421. bool TorrentMan::segmentFileExists() const {
  422. string segFilename = getSegmentFilePath();
  423. File f(segFilename);
  424. if(f.isFile()) {
  425. logger->info(MSG_SEGMENT_FILE_EXISTS, segFilename.c_str());
  426. return true;
  427. } else {
  428. logger->info(MSG_SEGMENT_FILE_DOES_NOT_EXIST, segFilename.c_str());
  429. return false;
  430. }
  431. }
  432. FILE* TorrentMan::openSegFile(string segFilename, string mode) const {
  433. FILE* segFile = fopen(segFilename.c_str(), mode.c_str());
  434. if(segFile == NULL) {
  435. throw new DlAbortEx(strerror(errno));
  436. }
  437. return segFile;
  438. }
  439. void TorrentMan::load() {
  440. string segFilename = getSegmentFilePath();
  441. logger->info(MSG_LOADING_SEGMENT_FILE, segFilename.c_str());
  442. FILE* segFile = openSegFile(segFilename, "r+");
  443. read(segFile);
  444. fclose(segFile);
  445. logger->info(MSG_LOADED_SEGMENT_FILE);
  446. }
  447. void TorrentMan::read(FILE* file) {
  448. assert(file != NULL);
  449. unsigned char savedInfoHash[INFO_HASH_LENGTH];
  450. if(fread(savedInfoHash, INFO_HASH_LENGTH, 1, file) < 1) {
  451. throw new DlAbortEx(strerror(errno));
  452. }
  453. if(Util::toHex(savedInfoHash, INFO_HASH_LENGTH) != Util::toHex(infoHash, INFO_HASH_LENGTH)) {
  454. throw new DlAbortEx("info hash mismatch");
  455. }
  456. unsigned char* savedBitfield = new unsigned char[bitfield->getBitfieldLength()];
  457. try {
  458. if(fread(savedBitfield, bitfield->getBitfieldLength(), 1, file) < 1) {
  459. throw new DlAbortEx(strerror(errno));
  460. }
  461. setBitfield(savedBitfield, bitfield->getBitfieldLength());
  462. if(fread(&downloadLength, sizeof(downloadLength), 1, file) < 1) {
  463. throw new DlAbortEx(strerror(errno));
  464. }
  465. if(fread(&uploadLength, sizeof(uploadLength), 1, file) < 1) {
  466. throw new DlAbortEx(strerror(errno));
  467. }
  468. preDownloadLength = downloadLength;
  469. preUploadLength = uploadLength;
  470. delete [] savedBitfield;
  471. } catch(Exception* ex) {
  472. delete [] savedBitfield;
  473. throw;
  474. }
  475. }
  476. void TorrentMan::save() const {
  477. if(!setupComplete) {
  478. return;
  479. }
  480. string segFilename = getSegmentFilePath();
  481. logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str());
  482. FILE* file = openSegFile(segFilename, "w");
  483. if(fwrite(infoHash, INFO_HASH_LENGTH, 1, file) < 1) {
  484. throw new DlAbortEx(strerror(errno));
  485. }
  486. if(fwrite(bitfield->getBitfield(), bitfield->getBitfieldLength(), 1, file) < 1) {
  487. throw new DlAbortEx(strerror(errno));
  488. }
  489. if(fwrite(&downloadLength, sizeof(downloadLength), 1, file) < 1) {
  490. throw new DlAbortEx(strerror(errno));
  491. }
  492. if(fwrite(&uploadLength, sizeof(uploadLength), 1, file) < 1) {
  493. throw new DlAbortEx(strerror(errno));
  494. }
  495. fclose(file);
  496. logger->info(MSG_SAVED_SEGMENT_FILE);
  497. }
  498. void TorrentMan::remove() const {
  499. if(segmentFileExists()) {
  500. File f(getSegmentFilePath());
  501. f.remove();
  502. }
  503. }
  504. void TorrentMan::fixFilename() {
  505. if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE) {
  506. // nothing to do here
  507. } else {
  508. if(fileMode == SINGLE) {
  509. copySingleFile();
  510. } else {
  511. splitMultiFile();
  512. }
  513. }
  514. }
  515. void TorrentMan::copySingleFile() const {
  516. logger->info("writing file %s", getFilePath().c_str());
  517. Util::fileCopy(getFilePath(), getTempFilePath());
  518. }
  519. void TorrentMan::splitMultiFile() {
  520. logger->info("creating directories");
  521. multiFileTopDir->createDir(storeDir, true);
  522. long long int offset = 0;
  523. for(MultiFileEntries::iterator itr = multiFileEntries.begin();
  524. itr != multiFileEntries.end(); itr++) {
  525. if(!itr->extracted && itr->requested) {
  526. string dest = storeDir+"/"+itr->path;
  527. logger->info("writing file %s", dest.c_str());
  528. Util::rangedFileCopy(dest, getTempFilePath(), offset, itr->length);
  529. itr->extracted = true;
  530. }
  531. offset += itr->length;
  532. }
  533. }
  534. void TorrentMan::deleteTempFile() const {
  535. if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE) {
  536. // nothing to do here
  537. } else {
  538. unlink(getTempFilePath().c_str());
  539. }
  540. }
  541. // bool TorrentMan::unextractedFileEntryExists() const {
  542. // if(fileMode == SINGLE) {
  543. // return false;
  544. // }
  545. // for(MultiFileEntries::const_iterator itr = multiFileEntries.begin();
  546. // itr != multiFileEntries.end(); itr++) {
  547. // if(!itr->extracted) {
  548. // return true;
  549. // }
  550. // }
  551. // }
  552. void TorrentMan::setFileEntriesToDownload(const Strings& filePaths) {
  553. if(fileMode != MULTI) {
  554. throw new DlAbortEx("only multi-mode supports partial downloading mode.");
  555. }
  556. // clear all requested flags in multiFileEntries.
  557. setAllMultiFileRequestedState(false);
  558. for(Strings::const_iterator pitr = filePaths.begin();
  559. pitr != filePaths.end(); pitr++) {
  560. bool found = false;
  561. for(MultiFileEntries::iterator itr = multiFileEntries.begin();
  562. itr != multiFileEntries.end(); itr++) {
  563. if(*pitr == itr->path) {
  564. itr->requested = true;
  565. found = true;
  566. bitfield->addFilter(itr->offset, itr->length);
  567. break;
  568. }
  569. }
  570. if(!found) {
  571. throw new DlAbortEx("no such file entry <%s>", (*pitr).c_str());
  572. }
  573. }
  574. bitfield->enableFilter();
  575. }
  576. bool TorrentMan::isPartialDownloadingMode() const {
  577. return bitfield->isFilterEnabled();
  578. }
  579. void TorrentMan::setAllMultiFileRequestedState(bool state) {
  580. for(MultiFileEntries::iterator itr = multiFileEntries.begin();
  581. itr != multiFileEntries.end(); itr++) {
  582. itr->requested = state;
  583. }
  584. }
  585. void TorrentMan::finishPartialDownloadingMode() {
  586. bitfield->clearFilter();
  587. setAllMultiFileRequestedState(true);
  588. if(option->get(PREF_DIRECT_FILE_MAPPING) == V_TRUE && fileMode == MULTI) {
  589. ((MultiDiskWriter*)diskWriter)->setMultiFileEntries(multiFileEntries, pieceLength);
  590. }
  591. }
  592. long long int TorrentMan::getCompletedLength() const {
  593. return bitfield->getCompletedLength();
  594. }
  595. long long int TorrentMan::getPartialTotalLength() const {
  596. return bitfield->getFilteredTotalLength();
  597. }
  598. void TorrentMan::onDownloadComplete() {
  599. diskWriter->closeFile();
  600. save();
  601. fixFilename();
  602. if(isPartialDownloadingMode()) {
  603. finishPartialDownloadingMode();
  604. }
  605. diskWriter->openFile(getTempFilePath());
  606. }