summaryrefslogtreecommitdiffstats
path: root/apps/ktcachecheck
diff options
context:
space:
mode:
Diffstat (limited to 'apps/ktcachecheck')
-rw-r--r--apps/ktcachecheck/Makefile.am12
-rw-r--r--apps/ktcachecheck/cachecheck.cpp102
-rw-r--r--apps/ktcachecheck/cachechecker.cpp110
-rw-r--r--apps/ktcachecheck/cachechecker.h61
-rw-r--r--apps/ktcachecheck/multicachechecker.cpp143
-rw-r--r--apps/ktcachecheck/multicachechecker.h44
-rw-r--r--apps/ktcachecheck/singlecachechecker.cpp110
-rw-r--r--apps/ktcachecheck/singlecachechecker.h43
8 files changed, 625 insertions, 0 deletions
diff --git a/apps/ktcachecheck/Makefile.am b/apps/ktcachecheck/Makefile.am
new file mode 100644
index 0000000..19f89d4
--- /dev/null
+++ b/apps/ktcachecheck/Makefile.am
@@ -0,0 +1,12 @@
+INCLUDES = -I$(srcdir)/../../libktorrent $(all_includes)
+METASOURCES = AUTO
+
+bin_PROGRAMS = ktcachecheck
+
+ktcachecheck_SOURCES = cachecheck.cpp cachechecker.cpp singlecachechecker.cpp \
+ multicachechecker.cpp
+ktcachecheck_LDFLAGS = $(KDE_RPATH) $(all_libraries)
+ktcachecheck_LDADD = $(LIB_KPARTS) ../../libktorrent/libktorrent.la \
+ $(LIB_KFILE) $(LIB_KIO)
+noinst_HEADERS = cachechecker.h singlecachechecker.h multicachechecker.h
+KDE_CXXFLAGS = $(USE_EXCEPTIONS) $(USE_RTTI)
diff --git a/apps/ktcachecheck/cachecheck.cpp b/apps/ktcachecheck/cachecheck.cpp
new file mode 100644
index 0000000..0a143c5
--- /dev/null
+++ b/apps/ktcachecheck/cachecheck.cpp
@@ -0,0 +1,102 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * 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., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include <iostream>
+#include <stdlib.h>
+#include <util/log.h>
+#include <util/error.h>
+#include <util/functions.h>
+#include <torrent/globals.h>
+#include <torrent/torrent.h>
+#include "singlecachechecker.h"
+#include "multicachechecker.h"
+
+
+using namespace bt;
+using namespace ktdebug;
+
+void Help()
+{
+ Out() << "Usage : cachecheck <directory_containing_cache>" << endl;
+ Out() << "OR cachecheck <torrent> <cache_file_or_dir> <index_file>" << endl;
+ exit(0);
+}
+
+int main(int argc,char** argv)
+{
+ Globals::instance().setDebugMode(true);
+ Globals::instance().initLog("cachecheck.log");
+ CacheChecker* cc = 0;
+ try
+ {
+ Torrent tor;
+ QString tor_file,cache,index;
+
+ if (argc == 2)
+ {
+ QString cache_dir = argv[1];
+ if (!cache_dir.endsWith(bt::DirSeparator()))
+ cache_dir += bt::DirSeparator();
+
+ tor_file = cache_dir + "torrent";
+ cache = cache_dir + "cache";
+ index = cache_dir + "index";
+ }
+ else if (argc == 4)
+ {
+ tor_file = argv[1];
+ cache = argv[2];
+ index = argv[3];
+ }
+ else
+ {
+ Help();
+ }
+
+
+ Out() << "Loading torrent : " << tor_file << " ... " << endl;
+ tor.load(tor_file,false);
+ if (tor.isMultiFile())
+ cc = new MultiCacheChecker(tor);
+ else
+ cc = new SingleCacheChecker(tor);
+
+ cc->check(cache,index);
+ if (cc->foundFailedChunks())
+ {
+ std::string str;
+ std::cout << "Found failed chunks, fix index file ? (y/n) ";
+ std::cin >> str;
+ if (str == "y" || str == "Y")
+ {
+ Out() << "Fixing index file ..." << endl;
+ cc->fixIndex();
+ Out() << "Finished !" << endl;
+ }
+ }
+ }
+ catch (Error & e)
+ {
+ Out() << "Error : " << e.toString() << endl;
+ }
+
+ delete cc;
+ Globals::cleanup();
+ return 0;
+}
diff --git a/apps/ktcachecheck/cachechecker.cpp b/apps/ktcachecheck/cachechecker.cpp
new file mode 100644
index 0000000..6cdf3e5
--- /dev/null
+++ b/apps/ktcachecheck/cachechecker.cpp
@@ -0,0 +1,110 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * 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., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include <klocale.h>
+#include <util/log.h>
+#include <util/file.h>
+#include <util/error.h>
+#include <util/array.h>
+#include <torrent/globals.h>
+#include <torrent/torrent.h>
+#include <torrent/chunkmanager.h>
+#include "cachechecker.h"
+
+using namespace bt;
+
+namespace ktdebug
+{
+
+ CacheChecker::CacheChecker(bt::Torrent & tor) : tor(tor)
+ {}
+
+
+ CacheChecker::~CacheChecker()
+ {}
+
+ void CacheChecker::loadIndex(const QString & index_file)
+ {
+ this->index_file = index_file;
+ File fptr;
+ if (!fptr.open(index_file,"rb"))
+ throw Error(i18n("Cannot open index file %1 : %2").arg(index_file).arg(fptr.errorString()));
+
+ if (fptr.seek(File::END,0) != 0)
+ {
+ fptr.seek(File::BEGIN,0);
+
+ while (!fptr.eof())
+ {
+ NewChunkHeader hdr;
+ fptr.read(&hdr,sizeof(NewChunkHeader));
+ downloaded_chunks.insert(hdr.index);
+ }
+ }
+ }
+
+ struct ChunkHeader
+ {
+ unsigned int index; // the Chunks index
+ unsigned int deprecated; // offset in cache file
+ };
+
+
+ void CacheChecker::fixIndex()
+ {
+ if (failed_chunks.size() == 0 && extra_chunks.size() == 0)
+ return;
+
+ File fptr;
+ if (!fptr.open(index_file,"wb"))
+ throw Error(i18n("Cannot open index file %1 : %2").arg(index_file).arg(fptr.errorString()));
+
+ std::set<bt::Uint32>::iterator i;
+ // first remove failed chunks from downloaded
+ if (failed_chunks.size() > 0)
+ {
+ i = failed_chunks.begin();
+ while (i != failed_chunks.end())
+ {
+ downloaded_chunks.erase(*i);
+ i++;
+ }
+ }
+
+ // add extra chunks to download
+ if (extra_chunks.size() > 0)
+ {
+ i = extra_chunks.begin();
+ while (i != extra_chunks.end())
+ {
+ downloaded_chunks.insert(*i);
+ i++;
+ }
+ }
+
+ // write remaining chunks
+ i = downloaded_chunks.begin();
+ while (i != downloaded_chunks.end())
+ {
+ ChunkHeader ch = {*i,0};
+ fptr.write(&ch,sizeof(ChunkHeader));
+ i++;
+ }
+ }
+}
diff --git a/apps/ktcachecheck/cachechecker.h b/apps/ktcachecheck/cachechecker.h
new file mode 100644
index 0000000..def3e6d
--- /dev/null
+++ b/apps/ktcachecheck/cachechecker.h
@@ -0,0 +1,61 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * 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., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#ifndef DEBUGCACHECHECKER_H
+#define DEBUGCACHECHECKER_H
+
+#include <set>
+#include <qstring.h>
+#include <util/functions.h>
+
+namespace bt
+{
+ class Torrent;
+}
+
+namespace ktdebug
+{
+
+ /**
+ * @author Joris Guisson
+ */
+ class CacheChecker
+ {
+ public:
+ CacheChecker(bt::Torrent & tor);
+ virtual ~CacheChecker();
+
+ void loadIndex(const QString & index_file);
+ void fixIndex();
+ bool foundFailedChunks() const {return failed_chunks.size() > 0;}
+ bool foundExtraChunks() const {return extra_chunks.size() > 0;}
+
+ virtual void check(const QString & cache,const QString & index) = 0;
+ protected:
+ bt::Torrent & tor;
+ QString index_file;
+ std::set<bt::Uint32> downloaded_chunks;
+ std::set<bt::Uint32> failed_chunks;
+ std::set<bt::Uint32> extra_chunks;
+ };
+
+
+}
+
+#endif
diff --git a/apps/ktcachecheck/multicachechecker.cpp b/apps/ktcachecheck/multicachechecker.cpp
new file mode 100644
index 0000000..df80535
--- /dev/null
+++ b/apps/ktcachecheck/multicachechecker.cpp
@@ -0,0 +1,143 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * 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., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include <util/log.h>
+#include <util/file.h>
+#include <util/error.h>
+#include <util/array.h>
+#include <util/functions.h>
+#include <torrent/globals.h>
+#include <torrent/torrent.h>
+#include <torrent/chunkmanager.h>
+#include "multicachechecker.h"
+
+using namespace bt;
+
+namespace ktdebug
+{
+
+ MultiCacheChecker::MultiCacheChecker(bt::Torrent& tor): CacheChecker(tor)
+ {}
+
+
+ MultiCacheChecker::~MultiCacheChecker()
+ {}
+
+
+ void MultiCacheChecker::check(const QString& cache_dir, const QString& index)
+ {
+ loadIndex(index);
+ QString cache = cache_dir;
+ if (!cache.endsWith(bt::DirSeparator()))
+ cache += bt::DirSeparator();
+
+ Uint32 num_chunks = tor.getNumChunks();
+ Uint64 curr_file_off = 0;
+ Uint32 curr_file = 0;
+ Uint64 chunk_size = tor.getChunkSize();
+
+ Array<Uint8> buf((Uint32)tor.getChunkSize());
+ Uint32 num_ok = 0,num_not_ok = 0,num_not_downloaded = 0,extra_ok = 0;
+
+ for (Uint32 i = 0;i < num_chunks;i++)
+ {
+ if (i % 100 == 0)
+ Out() << "Checked " << i << " chunks" << endl;
+
+
+
+ Uint64 size = chunk_size;
+ if (i == tor.getNumChunks() - 1 && tor.getFileLength() % chunk_size != 0)
+ size = tor.getFileLength() % chunk_size;
+
+ //Out() << "Loading chunk (size = " << size << ")" << endl;
+ Uint64 bytes_offset = 0;
+ while (bytes_offset < size)
+ {
+ TorrentFile & tf = tor.getFile(curr_file);
+// Out() << "Current file : " << tf.getPath() << " (" << curr_file << ")" << endl;
+ Uint64 to_read = size - bytes_offset;
+// Out() << "to_read = " << to_read << endl;
+ if (to_read <= tf.getSize() - curr_file_off)
+ {
+ // we can read the chunk from this file
+ File fptr;
+ if (!fptr.open(cache + tf.getPath(),"rb"))
+ throw Error(QString("Cannot open %1 : %2").arg(cache + tf.getPath()).arg(fptr.errorString()));
+
+ fptr.seek(File::BEGIN,curr_file_off);
+ fptr.read(buf + bytes_offset,to_read);
+ bytes_offset += to_read;
+ curr_file_off += to_read;
+ }
+ else
+ {
+
+ // read partially the data which can be read
+ to_read = tf.getSize() - curr_file_off;
+// Out() << "Partially reading " << to_read << endl;
+ File fptr;
+ if (!fptr.open(cache + tf.getPath(),"rb"))
+ throw Error(QString("Cannot open %1 : %2").arg(cache + tf.getPath()).arg(fptr.errorString()));
+
+ fptr.seek(File::BEGIN,curr_file_off);
+ fptr.read(buf + bytes_offset,to_read);
+ bytes_offset += to_read;
+ // update curr_file and offset
+ curr_file++;
+ curr_file_off = 0;
+ }
+ } // end file reading while
+
+ // calculate hash and check it
+ SHA1Hash h = SHA1Hash::generate(buf,size);
+ if (h != tor.getHash(i))
+ {
+ if (downloaded_chunks.count(i) == 0)
+ {
+ num_not_downloaded++;
+ continue;
+ }
+ Out() << "Chunk " << i << " Failed :" << endl;
+ Out() << "\tShould be : " << tor.getHash(i).toString() << endl;
+ Out() << "\tIs : " << h.toString() << endl;
+ num_not_ok++;
+ failed_chunks.insert(i);
+ }
+ else
+ {
+ if (downloaded_chunks.count(i) == 0)
+ {
+ extra_ok++;
+ extra_chunks.insert(i);
+ continue;
+ }
+ num_ok++;
+ }
+ }
+
+ Out() << "Cache Check Summary" << endl;
+ Out() << "===================" << endl;
+ Out() << "Extra Chunks : " << extra_ok << endl;
+ Out() << "Chunks OK : " << num_ok << endl;
+ Out() << "Chunks Not Downloaded : " << num_not_downloaded << endl;
+ Out() << "Chunks Failed : " << num_not_ok << endl;
+ }
+
+}
diff --git a/apps/ktcachecheck/multicachechecker.h b/apps/ktcachecheck/multicachechecker.h
new file mode 100644
index 0000000..1df95f5
--- /dev/null
+++ b/apps/ktcachecheck/multicachechecker.h
@@ -0,0 +1,44 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * 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., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#ifndef DEBUGMULTICACHECHECKER_H
+#define DEBUGMULTICACHECHECKER_H
+
+#include "cachechecker.h"
+
+namespace ktdebug
+{
+
+ /**
+ @author Joris Guisson
+ */
+ class MultiCacheChecker : public CacheChecker
+ {
+ public:
+ MultiCacheChecker(bt::Torrent& tor);
+
+ ~MultiCacheChecker();
+
+ virtual void check(const QString& cache, const QString& index);
+
+ };
+
+}
+
+#endif
diff --git a/apps/ktcachecheck/singlecachechecker.cpp b/apps/ktcachecheck/singlecachechecker.cpp
new file mode 100644
index 0000000..822c28a
--- /dev/null
+++ b/apps/ktcachecheck/singlecachechecker.cpp
@@ -0,0 +1,110 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * 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., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#include <util/log.h>
+#include <util/file.h>
+#include <util/error.h>
+#include <util/array.h>
+#include <util/functions.h>
+#include <torrent/globals.h>
+#include <torrent/torrent.h>
+#include <torrent/chunkmanager.h>
+#include "singlecachechecker.h"
+
+using namespace bt;
+
+namespace ktdebug
+{
+
+ SingleCacheChecker::SingleCacheChecker(bt::Torrent& tor): CacheChecker(tor)
+ {}
+
+
+ SingleCacheChecker::~SingleCacheChecker()
+ {}
+
+
+ void SingleCacheChecker::check(const QString& cache, const QString& index)
+ {
+ loadIndex(index);
+ Uint32 num_chunks = tor.getNumChunks();
+ File fptr;
+ if (!fptr.open(cache,"rb"))
+ {
+ throw Error(QString("Cannot open file : %1 : %2")
+ .arg(cache).arg( fptr.errorString()));
+ }
+
+ Uint32 num_ok = 0,num_not_ok = 0,num_not_downloaded = 0,extra_ok = 0;
+
+ Array<Uint8> buf((Uint32)tor.getChunkSize());
+ for (Uint32 i = 0;i < num_chunks;i++)
+ {
+ if (i % 100 == 0)
+ Out(SYS_GEN|LOG_DEBUG) << "Checked " << i << " chunks" << endl;
+ // Out() << "Chunk " << i << " : ";
+ if (!fptr.eof())
+ {
+ Uint32 size = i == num_chunks - 1 && tor.getFileLength() % tor.getChunkSize() > 0 ?
+ tor.getFileLength() % tor.getChunkSize() : (Uint32)tor.getChunkSize();
+ fptr.seek(File::BEGIN,(Int64)i*tor.getChunkSize());
+ fptr.read(buf,size);
+ SHA1Hash h = SHA1Hash::generate(buf,size);
+ bool ok = (h == tor.getHash(i));
+ if (ok)
+ {
+ if (downloaded_chunks.count(i) == 0)
+ {
+ extra_ok++;
+ extra_chunks.insert(i);
+ continue;
+ }
+ num_ok++;
+ // Out() << "OK" << endl;
+ }
+ else
+ {
+ if (downloaded_chunks.count(i) == 0)
+ {
+ num_not_downloaded++;
+ continue;
+ }
+ Out() << "Chunk " << i << " Failed :" << endl;
+ Out() << "\tShould be : " << tor.getHash(i).toString() << endl;
+ Out() << "\tIs : " << h.toString() << endl;
+ num_not_ok++;
+ failed_chunks.insert(i);
+ }
+
+ }
+ else
+ {
+ num_not_downloaded++;
+ //Out() << "Not Downloaded" << endl;
+ }
+ }
+ Out() << "Cache Check Summary" << endl;
+ Out() << "===================" << endl;
+ Out() << "Extra Chunks : " << extra_ok << endl;
+ Out() << "Chunks OK : " << num_ok << endl;
+ Out() << "Chunks Not Downloaded : " << num_not_downloaded << endl;
+ Out() << "Chunks Failed : " << num_not_ok << endl;
+ }
+
+}
diff --git a/apps/ktcachecheck/singlecachechecker.h b/apps/ktcachecheck/singlecachechecker.h
new file mode 100644
index 0000000..2d7b178
--- /dev/null
+++ b/apps/ktcachecheck/singlecachechecker.h
@@ -0,0 +1,43 @@
+/***************************************************************************
+ * Copyright (C) 2005 by Joris Guisson *
+ * joris.guisson@gmail.com *
+ * *
+ * 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., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
+ ***************************************************************************/
+#ifndef DEBUGSINGLECACHECHECKER_H
+#define DEBUGSINGLECACHECHECKER_H
+
+#include "cachechecker.h"
+
+namespace ktdebug
+{
+
+ /**
+ @author Joris Guisson
+ */
+ class SingleCacheChecker : public CacheChecker
+ {
+ public:
+ SingleCacheChecker(bt::Torrent& tor);
+ virtual ~SingleCacheChecker();
+
+ virtual void check(const QString& cache, const QString& index);
+
+ };
+
+}
+
+#endif