From 3e35526be841d6c5a286dff9c246e437a3d1408c Mon Sep 17 00:00:00 2001 From: David Brodsky Date: Sat, 15 Sep 2007 17:57:54 +0200 Subject: [PATCH] Save torrents' fast-resume data into another file. --- src/main/torrentmanager.cpp | 60 ++++++++++++++++++++++++++++++++++++--------- src/main/torrentmanager.h | 15 +++++++++++- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/src/main/torrentmanager.cpp b/src/main/torrentmanager.cpp index 22ad273..fdde3e0 100644 --- a/src/main/torrentmanager.cpp +++ b/src/main/torrentmanager.cpp @@ -1,6 +1,6 @@ /*************************************************************************** * * - * Copyright (C) 2006 David Brodsky * + * Copyright (C) 2006-2007 David Brodsky * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * @@ -53,6 +53,7 @@ TorrentManager::TorrentManager() makeClientID(); loadTorrents(); + loadResume(); saveTimer = new Tairon::Net::Timer(); saveTimer->timeoutSignal.connect(Tairon::Core::threadMethodDFunctor(Tairon::Core::Thread::current(), this, &TorrentManager::save)); @@ -193,6 +194,30 @@ Tairent::Core::BEncode *TorrentManager::loadMetaInfo(const String &filename) } /* }}} */ +/* {{{ TorrentManager::loadResume() */ +void TorrentManager::loadResume() +{ + TiXmlDocument document; + + if (!document.LoadFile((*Tairon::Core::Config::self())["torrents-directory"] + "/resume.xml")) + return; + + TiXmlElement *root = document.RootElement(); + if (!root) + return; + + if (root->ValueStr() != "resume") // invalid file + return; + + for (TiXmlNode *node = root->FirstChild(); node; node = node->NextSibling()) { + if (node->Type() != TiXmlNode::ELEMENT) + continue; + if (node->ValueStr() == "torrent") + loadTorrentResume(node); + } +} +/* }}} */ + /* {{{ TorrentManager::loadTorrent(TiXmlNode *) */ void TorrentManager::loadTorrent(TiXmlNode *t) { @@ -221,15 +246,29 @@ void TorrentManager::loadTorrent(TiXmlNode *t) if (!torrent->metaInfo) return; - data = element->Attribute("hash"); - if (data) - torrents[hexToBin(data)] = torrent; - else { - String hash = (*torrent->metaInfo)["info"].computeSHA1(); - torrents[hash] = torrent; + String hash = (*torrent->metaInfo)["info"].computeSHA1(); + torrents[hash] = torrent; +} +/* }}} */ + +/* {{{ TorrentManager::loadTorrentResume(TiXmlNode *) */ +void TorrentManager::loadTorrentResume(TiXmlNode *n) +{ + TiXmlElement *element = n->ToElement(); + const char *data = element->Attribute("hash"); + if (!data) + return; + + String hash = hexToBin(data); + + if (!torrents.count(hash)) { + WARNING((const char *) String(String("Unknown torrent hash: ") + data)); + return; } - // load storage, if available + TorrentStruct *torrent = torrents[hash]; + + // load storage if available for (TiXmlNode *node = element->FirstChild(); node; node = node->NextSibling()) { if (node->Type() != TiXmlNode::ELEMENT) continue; @@ -296,15 +335,14 @@ void TorrentManager::makeClientID() /* {{{ TorrentManager::save() */ void TorrentManager::save() { - TiXmlDocument document((*Tairon::Core::Config::self())["torrents-directory"] + "/torrents.xml"); + TiXmlDocument document((*Tairon::Core::Config::self())["torrents-directory"] + "/resume.xml"); - TiXmlElement *root = new TiXmlElement("torrents"); + TiXmlElement *root = new TiXmlElement("resume"); document.LinkEndChild(root); for (std::map::const_iterator it = torrents.begin(); it != torrents.end(); ++it) { TiXmlElement *element = new TiXmlElement("torrent"); element->SetAttribute(String("hash"), binToHex(it->first)); - element->SetAttribute(String("file"), it->second->torrentFile); if (it->second->storage->isComplete()) element->SetAttribute("complete", 1); element->LinkEndChild(it->second->storage->save()); diff --git a/src/main/torrentmanager.h b/src/main/torrentmanager.h index 10f88d1..a631472 100644 --- a/src/main/torrentmanager.h +++ b/src/main/torrentmanager.h @@ -1,6 +1,6 @@ /*************************************************************************** * * - * Copyright (C) 2006 David Brodsky * + * Copyright (C) 2006-2007 David Brodsky * * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * @@ -161,12 +161,25 @@ class TorrentManager */ Tairent::Core::BEncode *loadMetaInfo(const String &filename); + /** Loads informations about torrents from file resume.xml stored in a + * directory whose name is stored in Tairon::Core::Config as + * "torrents-directory". This file is created for fast-resuming + * torrents. + */ + void loadResume(); + /** Loads informations about one torrent from a node. * * \param t Element with informations about torrent. */ void loadTorrent(TiXmlNode *t); + /** Loads fast-resume data about one torrent from a node. + * + * \param n Element with fast-resume data. + */ + void loadTorrentResume(TiXmlNode *n); + /** Loads informations about torrents from file torrents.xml stored in * a directory whose name is stored in Tairon::Core::Config as * "torrents-directory". -- 2.11.4.GIT