MultiDiskWriter.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "MultiDiskWriter.h"
  36. #include "DlAbortEx.h"
  37. #include "Util.h"
  38. #include "message.h"
  39. #include <errno.h>
  40. MultiDiskWriter::MultiDiskWriter(int pieceLength):
  41. pieceLength(pieceLength),
  42. ctx(DIGEST_ALGO_SHA1),
  43. fileAllocator(0) {
  44. ctx.digestInit();
  45. }
  46. MultiDiskWriter::~MultiDiskWriter() {
  47. clearEntries();
  48. }
  49. void MultiDiskWriter::clearEntries() {
  50. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  51. itr != diskWriterEntries.end(); itr++) {
  52. delete *itr;
  53. }
  54. diskWriterEntries.clear();
  55. }
  56. void MultiDiskWriter::setFileEntries(const FileEntries& fileEntries) {
  57. clearEntries();
  58. for(FileEntries::const_iterator itr = fileEntries.begin();
  59. itr != fileEntries.end(); itr++) {
  60. diskWriterEntries.push_back(new DiskWriterEntry(*itr));
  61. }
  62. }
  63. void MultiDiskWriter::configureFileAllocator(DiskWriterEntry* entry) {
  64. if(entry->fileEntry->isRequested()) {
  65. entry->diskWriter->setFileAllocator(fileAllocator);
  66. } else {
  67. entry->diskWriter->setFileAllocator(0);
  68. }
  69. }
  70. void MultiDiskWriter::openFile(const string& filename) {
  71. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  72. itr != diskWriterEntries.end(); itr++) {
  73. configureFileAllocator(*itr);
  74. (*itr)->diskWriter->openFile(filename+"/"+(*itr)->fileEntry->getPath());
  75. }
  76. }
  77. // filename is a directory which is specified by the user in the option.
  78. void MultiDiskWriter::initAndOpenFile(const string& filename) {
  79. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  80. itr != diskWriterEntries.end(); itr++) {
  81. configureFileAllocator(*itr);
  82. (*itr)->diskWriter->initAndOpenFile(filename+"/"+(*itr)->fileEntry->getPath());
  83. }
  84. }
  85. void MultiDiskWriter::closeFile() {
  86. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  87. itr != diskWriterEntries.end(); itr++) {
  88. (*itr)->diskWriter->closeFile();
  89. }
  90. }
  91. void MultiDiskWriter::openExistingFile(const string& filename) {
  92. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  93. itr != diskWriterEntries.end(); itr++) {
  94. (*itr)->diskWriter->setFileAllocator(0);
  95. (*itr)->diskWriter->openExistingFile(filename+"/"+(*itr)->fileEntry->getPath());
  96. }
  97. }
  98. void MultiDiskWriter::writeData(const char* data, int len, long long int offset) {
  99. long long int fileOffset = offset;
  100. bool writing = false;
  101. int rem = len;
  102. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  103. itr != diskWriterEntries.end() && rem != 0; itr++) {
  104. if(isInRange(*itr, offset) || writing) {
  105. int writeLength = calculateLength(*itr, fileOffset, rem);
  106. (*itr)->diskWriter->writeData(data+(len-rem), writeLength, fileOffset);
  107. rem -= writeLength;
  108. writing = true;
  109. fileOffset = 0;
  110. } else {
  111. fileOffset -= (*itr)->fileEntry->getLength();
  112. }
  113. }
  114. if(!writing) {
  115. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
  116. }
  117. }
  118. bool MultiDiskWriter::isInRange(const DiskWriterEntry* entry, long long int offset) const {
  119. return entry->fileEntry->getOffset() <= offset &&
  120. offset < entry->fileEntry->getOffset()+entry->fileEntry->getLength();
  121. }
  122. int MultiDiskWriter::calculateLength(const DiskWriterEntry* entry, long long int fileOffset, int rem) const {
  123. int length;
  124. if(entry->fileEntry->getLength() < fileOffset+rem) {
  125. length = entry->fileEntry->getLength()-fileOffset;
  126. } else {
  127. length = rem;
  128. }
  129. return length;
  130. }
  131. int MultiDiskWriter::readData(char* data, int len, long long int offset) {
  132. long long int fileOffset = offset;
  133. bool reading = false;
  134. int rem = len;
  135. int totalReadLength = 0;
  136. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  137. itr != diskWriterEntries.end() && rem != 0; itr++) {
  138. if(isInRange(*itr, offset) || reading) {
  139. int readLength = calculateLength((*itr), fileOffset, rem);
  140. totalReadLength += (*itr)->diskWriter->readData(data+(len-rem), readLength, fileOffset);
  141. rem -= readLength;
  142. reading = true;
  143. fileOffset = 0;
  144. } else {
  145. fileOffset -= (*itr)->fileEntry->getLength();
  146. }
  147. }
  148. if(!reading) {
  149. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
  150. }
  151. return totalReadLength;
  152. }
  153. void MultiDiskWriter::hashUpdate(DiskWriterEntry* entry, long long int offset, long long int length) {
  154. int BUFSIZE = 16*1024;
  155. char buf[BUFSIZE];
  156. for(int i = 0; i < length/BUFSIZE; i++) {
  157. if(BUFSIZE != entry->diskWriter->readData(buf, BUFSIZE, offset)) {
  158. throw string("error");
  159. }
  160. ctx.digestUpdate(buf, BUFSIZE);
  161. offset += BUFSIZE;
  162. }
  163. int r = length%BUFSIZE;
  164. if(r > 0) {
  165. if(r != entry->diskWriter->readData(buf, r, offset)) {
  166. throw string("error");
  167. }
  168. ctx.digestUpdate(buf, r);
  169. }
  170. }
  171. string MultiDiskWriter::sha1Sum(long long int offset, long long int length) {
  172. long long int fileOffset = offset;
  173. bool reading = false;
  174. int rem = length;
  175. ctx.digestReset();
  176. try {
  177. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  178. itr != diskWriterEntries.end() && rem != 0; itr++) {
  179. if(isInRange(*itr, offset) || reading) {
  180. int readLength = calculateLength((*itr), fileOffset, rem);
  181. hashUpdate(*itr, fileOffset, readLength);
  182. rem -= readLength;
  183. reading = true;
  184. fileOffset = 0;
  185. } else {
  186. fileOffset -= (*itr)->fileEntry->getLength();
  187. }
  188. }
  189. if(!reading) {
  190. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, offset);
  191. }
  192. unsigned char hashValue[20];
  193. ctx.digestFinal(hashValue);
  194. return Util::toHex(hashValue, 20);
  195. } catch(string ex) {
  196. throw new DlAbortEx(EX_FILE_SHA1SUM, "", strerror(errno));
  197. }
  198. }