MultiDiskWriter.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef _D_MULTI_DISK_WRITER_H_
  23. #define _D_MULTI_DISK_WRITER_H_
  24. #include "DefaultDiskWriter.h"
  25. #include "TorrentMan.h"
  26. #include "messageDigest.h"
  27. class DiskWriterEntry {
  28. public:
  29. FileEntry fileEntry;
  30. DiskWriter* diskWriter;
  31. bool enabled;
  32. public:
  33. DiskWriterEntry(const FileEntry& fileEntry, bool enabled):fileEntry(fileEntry), enabled(enabled) {
  34. if(enabled) {
  35. diskWriter = new DefaultDiskWriter();
  36. }
  37. }
  38. ~DiskWriterEntry() {
  39. if(enabled) {
  40. delete diskWriter;
  41. }
  42. }
  43. };
  44. typedef deque<DiskWriterEntry*> DiskWriterEntries;
  45. class MultiDiskWriter : public DiskWriter {
  46. private:
  47. DiskWriterEntries diskWriterEntries;
  48. bool isInRange(const DiskWriterEntry* entry, long long int offset) const;
  49. int calculateLength(const DiskWriterEntry* entry, long long int fileOffset, int rem) const;
  50. void clearEntries();
  51. #ifdef ENABLE_SHA1DIGEST
  52. MessageDigestContext ctx;
  53. void hashUpdate(const DiskWriterEntry* entry, long long int offset, long long int length) const;
  54. #endif // ENABLE_SHA1DIGEST
  55. public:
  56. MultiDiskWriter();
  57. virtual ~MultiDiskWriter();
  58. void setMultiFileEntries(const MultiFileEntries& multiFileEntries, int pieceLength);
  59. virtual void openFile(const string& filename);
  60. virtual void initAndOpenFile(string filename);
  61. virtual void closeFile();
  62. virtual void openExistingFile(string filename);
  63. virtual void writeData(const char* data, int len, long long int position = 0);
  64. virtual int readData(char* data, int len, long long int position);
  65. virtual string sha1Sum(long long int offset, long long int length);
  66. };
  67. #endif // _D_MULTI_DISK_WRITER_H_