[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/network/ktorrent
From:       Joris Guisson <joris.guisson () gmail ! com>
Date:       2005-11-03 19:30:12
Message-ID: 1131046212.559420.24457.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 477320 by guisson:

Changes : 
- IO errors should now be handled properly (115484)
- ktcachecheck can now repair the index file

BUG: 115484



 M  +13 -1     apps/ktcachecheck/cachecheck.cpp  
 M  +35 -0     apps/ktcachecheck/cachechecker.cpp  
 M  +4 -0      apps/ktcachecheck/cachechecker.h  
 M  +1 -0      apps/ktcachecheck/multicachechecker.cpp  
 M  +1 -0      apps/ktcachecheck/singlecachechecker.cpp  
 M  +22 -2     apps/ktorrent/ktorrentcore.cpp  
 M  +12 -3     apps/ktorrent/main.cpp  
 M  +2 -0      libktorrent/torrent/downloader.cpp  
 M  +7 -0      libktorrent/torrent/downloader.h  
 M  +13 -6     libktorrent/torrent/torrentcontrol.cpp  
 M  +1 -0      libktorrent/torrent/torrentcontrol.h  
 M  +2 -2      libktorrent/util/file.cpp  


--- trunk/extragear/network/ktorrent/apps/ktcachecheck/cachecheck.cpp #477319:477320
@@ -17,6 +17,7 @@
  *   Free Software Foundation, Inc.,                                       *
  *   51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA.             *
  ***************************************************************************/
+#include <iostream>
 #include <util/log.h>
 #include <util/error.h>
 #include <util/functions.h>
@@ -76,7 +77,18 @@
 			cc = new SingleCacheChecker(tor);
 
 		cc->check(cache,index);
-		
+		if (cc->foundFailedChunks())
+		{
+			std::string str;
+			std::cout << "Found failed chunks, fix index file ? [y] ";
+			std::cin >> str;
+			if (str == "y" || str == "Y")
+			{
+				Out() << "Fixing index file ..." << endl;
+				cc->fixIndex();
+				Out() << "Finished !" << endl;
+			}
+		}
 	}
 	catch (Error & e)
 	{
--- trunk/extragear/network/ktorrent/apps/ktcachecheck/cachechecker.cpp #477319:477320
@@ -40,6 +40,7 @@
 
 	void CacheChecker::loadIndex(const QString & index_file)
 	{
+		this->index_file = index_file;
 		File fptr;
 		if (!fptr.open(index_file,"rb"))
 			throw Error("Can't open index file : " + fptr.errorString());
@@ -56,4 +57,38 @@
 			}
 		}
 	}
+
+	struct ChunkHeader
+	{
+		unsigned int index; // the Chunks index
+		unsigned int deprecated; // offset in cache file
+	};
+	
+
+	void CacheChecker::fixIndex()
+	{
+		if (failed_chunks.size() == 0)
+			return;
+
+		File fptr;
+		if (!fptr.open(index_file,"wb"))
+			throw Error("Can't open index file : " + fptr.errorString());
+
+		// first remove failed chunks from downloaded
+		std::set<bt::Uint32>::iterator i = failed_chunks.begin();
+		while (i != failed_chunks.end())
+		{
+			downloaded_chunks.erase(*i);
+			i++;
+		}
+
+		// write remaining chunks
+		i = downloaded_chunks.begin();
+		while (i != downloaded_chunks.end())
+		{
+			ChunkHeader ch = {*i,0};
+			fptr.write(&ch,sizeof(ChunkHeader));
+			i++;
+		}
+	}
 }
--- trunk/extragear/network/ktorrent/apps/ktcachecheck/cachechecker.h #477319:477320
@@ -42,11 +42,15 @@
 		virtual ~CacheChecker();
 
 		void loadIndex(const QString & index_file);
+		void fixIndex();
+		bool foundFailedChunks() const {return failed_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;
 	};
 
 	
--- trunk/extragear/network/ktorrent/apps/ktcachecheck/multicachechecker.cpp #477319:477320
@@ -119,6 +119,7 @@
 					Out() << "\tShould be : " << tor.getHash(i).toString() << endl;
 					Out() << "\tIs        : " << h.toString() << endl;
 					num_not_ok++;
+					failed_chunks.insert(i);
 				}
 				else
 				{
--- trunk/extragear/network/ktorrent/apps/ktcachecheck/singlecachechecker.cpp #477319:477320
@@ -83,6 +83,7 @@
 					Out() << "\tShould be : " << tor.getHash(i).toString() << endl;
 					Out() << "\tIs        : " << h.toString() << endl;
 					num_not_ok++;
+					failed_chunks.insert(i);
 				}
 			
 			}
--- trunk/extragear/network/ktorrent/apps/ktorrent/ktorrentcore.cpp #477319:477320
@@ -177,7 +177,17 @@
 	if (start_tc)
 	{
 		Out() << "Starting download" << endl;
-		tc->start();
+		try
+		{
+			tc->start();
+		}
+		catch (bt::Error & err)
+		{
+			QString msg =
+				i18n("Error starting torrent %1 : %2")
+					.arg(s.torrent_name).arg(err.toString());
+			KMessageBox::error(0,msg,i18n("Error"));
+		}
 	}
 
 }
@@ -187,7 +197,17 @@
 	const TorrentStats & s = tc->getStats();
 	if (s.started && s.running)
 	{
-		tc->stop(false);
+		try
+		{
+			tc->stop(false);
+		}
+		catch (bt::Error & err)
+		{
+			QString msg =
+					i18n("Error stopping torrent %1 : %2")
+					.arg(s.torrent_name).arg(err.toString());
+			KMessageBox::error(0,msg,i18n("Error"));
+		}
 	}
 }
 
--- trunk/extragear/network/ktorrent/apps/ktorrent/main.cpp #477319:477320
@@ -31,6 +31,7 @@
 #include <qapplication.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <util/error.h>
 
 /*
 void StupidWarningMessagesFromQt( QtMsgType type, const char *msg )
@@ -93,7 +94,15 @@
 		fprintf(stderr, "ktorrent is already running!\n");
 		return 0;
 	}
-	
-	KTorrentApp app;
-	return app.exec();
+
+	try
+	{
+		KTorrentApp app;
+		return app.exec();
+	}
+	catch (bt::Error & e)
+	{
+		fprintf(stderr, "Aborted by error : %s\n",e.toString().ascii());
+	}
+	return 0;
 }
--- trunk/extragear/network/ktorrent/libktorrent/torrent/downloader.cpp #477319:477320
@@ -237,6 +237,8 @@
 			catch (Error & e)
 			{
 				Out() << "Error " << e.toString() << endl;
+				emit ioError(e.toString());
+				return false;
 			}
 		}
 		else
--- trunk/extragear/network/ktorrent/libktorrent/torrent/downloader.h #477319:477320
@@ -141,6 +141,13 @@
 		 */
 		void onExcluded(Uint32 from,Uint32 to);
 		
+	signals:
+		/**
+		 * An error occured while we we're writing or reading from disk.
+		 * @param msg Message
+		 */
+		void ioError(const QString & msg);
+		
 	private:
 		void downloadFrom(PeerDownloader* pd);
 		void normalUpdate();
--- trunk/extragear/network/ktorrent/libktorrent/torrent/torrentcontrol.cpp #477319:477320
@@ -168,15 +168,20 @@
 		}
 		catch (Error & e)
 		{
-			Out() << "Error : " << e.toString() << endl;
-			stats.stopped_by_error = true;
-			error_msg = e.toString();
-			short_error_msg = e.toString();
-			stop(false);
-			emit stoppedByError(this, error_msg);
+			onIOError(e.toString());
 		}
 	}
 
+	void TorrentControl::onIOError(const QString & msg)
+	{
+		Out() << "Error : " << msg << endl;
+		stats.stopped_by_error = true;
+		error_msg = msg;
+		short_error_msg = msg;
+		stop(false);
+		emit stoppedByError(this, error_msg);
+	}
+
 	void TorrentControl::start()
 	{
 		if (bt::Exists(datadir + "stopped"))
@@ -290,6 +295,8 @@
 
 		// create downloader,uploader and choker
 		down = new Downloader(*tor,*pman,*cman);
+		connect(down,SIGNAL(ioError(const QString& )),
+				this,SLOT(onIOError(const QString& )));
 		up = new Uploader(*cman,*pman);
 		choke = new Choker(*pman);
 
--- trunk/extragear/network/ktorrent/libktorrent/torrent/torrentcontrol.h #477319:477320
@@ -174,6 +174,7 @@
 		void onNewPeer(Peer* p);
 		void onPeerRemoved(Peer* p);
 		void doChoking();
+		void onIOError(const QString & msg);
 
 		/**
 		 * An error occured during the update of the tracker.
--- trunk/extragear/network/ktorrent/libktorrent/util/file.cpp #477319:477320
@@ -72,7 +72,7 @@
 		if (ferror(fptr))
 		{
 			clearerr(fptr);
-			throw Error(i18n("Error writing to %1").arg(file));
+			throw Error(i18n("Cannot write to %1").arg(file));
 		}
 		return ret;
 	}
@@ -86,7 +86,7 @@
 		if (ferror(fptr))
 		{
 			clearerr(fptr);
-			throw Error(i18n("Error reading from %1").arg(file));
+			throw Error(i18n("Cannot read from %1").arg(file));
 		}
 		return ret;
 	}
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic