MultiDiskWriter.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "MultiDiskWriter.h"
  23. #include "DlAbortEx.h"
  24. #include "Util.h"
  25. #include "message.h"
  26. #include <errno.h>
  27. MultiDiskWriter::MultiDiskWriter(int pieceLength):
  28. pieceLength(pieceLength),
  29. ctx(DIGEST_ALGO_SHA1) {
  30. ctx.digestInit();
  31. }
  32. MultiDiskWriter::~MultiDiskWriter() {
  33. clearEntries();
  34. ctx.digestFree();
  35. }
  36. void MultiDiskWriter::clearEntries() {
  37. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  38. itr != diskWriterEntries.end(); itr++) {
  39. delete *itr;
  40. }
  41. diskWriterEntries.clear();
  42. }
  43. void MultiDiskWriter::setFileEntries(const FileEntries& fileEntries) {
  44. clearEntries();
  45. for(FileEntries::const_iterator itr = fileEntries.begin();
  46. itr != fileEntries.end(); itr++) {
  47. diskWriterEntries.push_back(new DiskWriterEntry(*itr));
  48. }
  49. }
  50. void MultiDiskWriter::openFile(const string& filename) {
  51. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  52. itr != diskWriterEntries.end(); itr++) {
  53. (*itr)->diskWriter->openFile(filename+"/"+(*itr)->fileEntry.path);
  54. }
  55. }
  56. // filename is a directory which is specified by the user in the option.
  57. void MultiDiskWriter::initAndOpenFile(const string& filename) {
  58. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  59. itr != diskWriterEntries.end(); itr++) {
  60. (*itr)->diskWriter->initAndOpenFile(filename+"/"+(*itr)->fileEntry.path);
  61. }
  62. }
  63. void MultiDiskWriter::closeFile() {
  64. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  65. itr != diskWriterEntries.end(); itr++) {
  66. (*itr)->diskWriter->closeFile();
  67. }
  68. }
  69. void MultiDiskWriter::openExistingFile(const string& filename) {
  70. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  71. itr != diskWriterEntries.end(); itr++) {
  72. (*itr)->diskWriter->openExistingFile(filename+"/"+(*itr)->fileEntry.path);
  73. }
  74. }
  75. void MultiDiskWriter::writeData(const char* data, int len, long long int offset) {
  76. long long int fileOffset = offset;
  77. bool writing = false;
  78. int rem = len;
  79. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  80. itr != diskWriterEntries.end() && rem != 0; itr++) {
  81. if(isInRange(*itr, offset) || writing) {
  82. int writeLength = calculateLength(*itr, fileOffset, rem);
  83. (*itr)->diskWriter->writeData(data+(len-rem), writeLength, fileOffset);
  84. rem -= writeLength;
  85. writing = true;
  86. fileOffset = 0;
  87. } else {
  88. fileOffset -= (*itr)->fileEntry.length;
  89. }
  90. }
  91. if(!writing) {
  92. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
  93. }
  94. }
  95. bool MultiDiskWriter::isInRange(const DiskWriterEntry* entry, long long int offset) const {
  96. return entry->fileEntry.offset <= offset &&
  97. offset < entry->fileEntry.offset+entry->fileEntry.length;
  98. }
  99. int MultiDiskWriter::calculateLength(const DiskWriterEntry* entry, long long int fileOffset, int rem) const {
  100. int length;
  101. if(entry->fileEntry.length < fileOffset+rem) {
  102. length = entry->fileEntry.length-fileOffset;
  103. } else {
  104. length = rem;
  105. }
  106. return length;
  107. }
  108. int MultiDiskWriter::readData(char* data, int len, long long int offset) {
  109. long long int fileOffset = offset;
  110. bool reading = false;
  111. int rem = len;
  112. int totalReadLength = 0;
  113. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  114. itr != diskWriterEntries.end() && rem != 0; itr++) {
  115. if(isInRange(*itr, offset) || reading) {
  116. int readLength = calculateLength((*itr), fileOffset, rem);
  117. totalReadLength += (*itr)->diskWriter->readData(data+(len-rem), readLength, fileOffset);
  118. rem -= readLength;
  119. reading = true;
  120. fileOffset = 0;
  121. } else {
  122. fileOffset -= (*itr)->fileEntry.length;
  123. }
  124. }
  125. if(!reading) {
  126. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
  127. }
  128. return totalReadLength;
  129. }
  130. void MultiDiskWriter::hashUpdate(DiskWriterEntry* entry, long long int offset, long long int length) {
  131. int BUFSIZE = 16*1024;
  132. char buf[BUFSIZE];
  133. for(int i = 0; i < length/BUFSIZE; i++) {
  134. if(BUFSIZE != entry->diskWriter->readData(buf, BUFSIZE, offset)) {
  135. throw string("error");
  136. }
  137. ctx.digestUpdate(buf, BUFSIZE);
  138. offset += BUFSIZE;
  139. }
  140. int r = length%BUFSIZE;
  141. if(r > 0) {
  142. if(r != entry->diskWriter->readData(buf, r, offset)) {
  143. throw string("error");
  144. }
  145. ctx.digestUpdate(buf, r);
  146. }
  147. }
  148. string MultiDiskWriter::sha1Sum(long long int offset, long long int length) {
  149. long long int fileOffset = offset;
  150. bool reading = false;
  151. int rem = length;
  152. ctx.digestReset();
  153. try {
  154. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  155. itr != diskWriterEntries.end() && rem != 0; itr++) {
  156. if(isInRange(*itr, offset) || reading) {
  157. int readLength = calculateLength((*itr), fileOffset, rem);
  158. hashUpdate(*itr, fileOffset, readLength);
  159. rem -= readLength;
  160. reading = true;
  161. fileOffset = 0;
  162. } else {
  163. fileOffset -= (*itr)->fileEntry.length;
  164. }
  165. }
  166. if(!reading) {
  167. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
  168. }
  169. unsigned char hashValue[20];
  170. ctx.digestFinal(hashValue);
  171. return Util::toHex(hashValue, 20);
  172. } catch(string ex) {
  173. throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
  174. }
  175. }