KTorrent – BitTorrent client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

111 lines
3.2 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Joris Guisson *
  3. * joris.guisson@gmail.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
  19. ***************************************************************************/
  20. #include <tdelocale.h>
  21. #include <util/log.h>
  22. #include <util/file.h>
  23. #include <util/error.h>
  24. #include <util/array.h>
  25. #include <torrent/globals.h>
  26. #include <torrent/torrent.h>
  27. #include <torrent/chunkmanager.h>
  28. #include "cachechecker.h"
  29. using namespace bt;
  30. namespace ktdebug
  31. {
  32. CacheChecker::CacheChecker(bt::Torrent & tor) : tor(tor)
  33. {}
  34. CacheChecker::~CacheChecker()
  35. {}
  36. void CacheChecker::loadIndex(const TQString & index_file)
  37. {
  38. this->index_file = index_file;
  39. File fptr;
  40. if (!fptr.open(index_file,"rb"))
  41. throw Error(i18n("Cannot open index file %1 : %2").arg(index_file).arg(fptr.errorString()));
  42. if (fptr.seek(File::END,0) != 0)
  43. {
  44. fptr.seek(File::BEGIN,0);
  45. while (!fptr.eof())
  46. {
  47. NewChunkHeader hdr;
  48. fptr.read(&hdr,sizeof(NewChunkHeader));
  49. downloaded_chunks.insert(hdr.index);
  50. }
  51. }
  52. }
  53. struct ChunkHeader
  54. {
  55. unsigned int index; // the Chunks index
  56. unsigned int deprecated; // offset in cache file
  57. };
  58. void CacheChecker::fixIndex()
  59. {
  60. if (failed_chunks.size() == 0 && extra_chunks.size() == 0)
  61. return;
  62. File fptr;
  63. if (!fptr.open(index_file,"wb"))
  64. throw Error(i18n("Cannot open index file %1 : %2").arg(index_file).arg(fptr.errorString()));
  65. std::set<bt::Uint32>::iterator i;
  66. // first remove failed chunks from downloaded
  67. if (failed_chunks.size() > 0)
  68. {
  69. i = failed_chunks.begin();
  70. while (i != failed_chunks.end())
  71. {
  72. downloaded_chunks.erase(*i);
  73. i++;
  74. }
  75. }
  76. // add extra chunks to download
  77. if (extra_chunks.size() > 0)
  78. {
  79. i = extra_chunks.begin();
  80. while (i != extra_chunks.end())
  81. {
  82. downloaded_chunks.insert(*i);
  83. i++;
  84. }
  85. }
  86. // write remaining chunks
  87. i = downloaded_chunks.begin();
  88. while (i != downloaded_chunks.end())
  89. {
  90. ChunkHeader ch = {*i,0};
  91. fptr.write(&ch,sizeof(ChunkHeader));
  92. i++;
  93. }
  94. }
  95. }