MultiDiskAdaptor.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "MultiDiskAdaptor.h"
  36. #include "DefaultDiskWriter.h"
  37. #include "message.h"
  38. #include "Util.h"
  39. #include "MultiFileAllocationIterator.h"
  40. #include "DefaultDiskWriterFactory.h"
  41. #include "DlAbortEx.h"
  42. void MultiDiskAdaptor::resetDiskWriterEntries()
  43. {
  44. diskWriterEntries.clear();
  45. for(FileEntries::const_iterator itr = fileEntries.begin();
  46. itr != fileEntries.end(); itr++) {
  47. DiskWriterEntryHandle entry = new DiskWriterEntry(*itr);
  48. if((*itr)->isRequested()) {
  49. entry->setDiskWriter(DefaultDiskWriterFactory().newDiskWriter());
  50. } else {
  51. entry->setDiskWriter(new DefaultDiskWriter());
  52. }
  53. entry->getDiskWriter()->setDirectIOAllowed(_directIOAllowed);
  54. diskWriterEntries.push_back(entry);
  55. }
  56. }
  57. string MultiDiskAdaptor::getTopDirPath() const
  58. {
  59. return storeDir+"/"+topDir;
  60. }
  61. void MultiDiskAdaptor::mkdir() const
  62. {
  63. for(FileEntries::const_iterator itr = fileEntries.begin();
  64. itr != fileEntries.end(); itr++) {
  65. (*itr)->setupDir(getTopDirPath());
  66. }
  67. }
  68. void MultiDiskAdaptor::openFile()
  69. {
  70. mkdir();
  71. resetDiskWriterEntries();
  72. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  73. itr != diskWriterEntries.end(); itr++) {
  74. (*itr)->openFile(getTopDirPath());
  75. }
  76. }
  77. void MultiDiskAdaptor::initAndOpenFile()
  78. {
  79. mkdir();
  80. resetDiskWriterEntries();
  81. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  82. itr != diskWriterEntries.end(); itr++) {
  83. (*itr)->initAndOpenFile(getTopDirPath());
  84. }
  85. }
  86. void MultiDiskAdaptor::openExistingFile()
  87. {
  88. resetDiskWriterEntries();
  89. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  90. itr != diskWriterEntries.end(); itr++) {
  91. (*itr)->openExistingFile(getTopDirPath());
  92. }
  93. }
  94. void MultiDiskAdaptor::closeFile()
  95. {
  96. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  97. itr != diskWriterEntries.end(); itr++) {
  98. (*itr)->closeFile();
  99. }
  100. }
  101. void MultiDiskAdaptor::onDownloadComplete()
  102. {
  103. closeFile();
  104. openFile();
  105. }
  106. void MultiDiskAdaptor::writeData(const unsigned char* data, int32_t len,
  107. int64_t offset)
  108. {
  109. int64_t fileOffset = offset;
  110. bool writing = false;
  111. int32_t rem = len;
  112. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  113. itr != diskWriterEntries.end() && rem != 0; itr++) {
  114. if(isInRange(*itr, offset) || writing) {
  115. int32_t writeLength = calculateLength(*itr, fileOffset, rem);
  116. (*itr)->getDiskWriter()->writeData(data+(len-rem), writeLength, fileOffset);
  117. rem -= writeLength;
  118. writing = true;
  119. fileOffset = 0;
  120. } else {
  121. fileOffset -= (*itr)->getFileEntry()->getLength();
  122. }
  123. }
  124. if(!writing) {
  125. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::llitos(offset, true).c_str());
  126. }
  127. }
  128. bool MultiDiskAdaptor::isInRange(const DiskWriterEntryHandle entry,
  129. int64_t offset) const
  130. {
  131. return entry->getFileEntry()->getOffset() <= offset &&
  132. offset < entry->getFileEntry()->getOffset()+entry->getFileEntry()->getLength();
  133. }
  134. int32_t MultiDiskAdaptor::calculateLength(const DiskWriterEntryHandle entry,
  135. int64_t fileOffset,
  136. int32_t rem) const
  137. {
  138. int32_t length;
  139. if(entry->getFileEntry()->getLength() < fileOffset+rem) {
  140. length = entry->getFileEntry()->getLength()-fileOffset;
  141. } else {
  142. length = rem;
  143. }
  144. return length;
  145. }
  146. int32_t MultiDiskAdaptor::readData(unsigned char* data, int32_t len, int64_t offset)
  147. {
  148. int64_t fileOffset = offset;
  149. bool reading = false;
  150. int32_t rem = len;
  151. int32_t totalReadLength = 0;
  152. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  153. itr != diskWriterEntries.end() && rem != 0; itr++) {
  154. if(isInRange(*itr, offset) || reading) {
  155. int32_t readLength = calculateLength((*itr), fileOffset, rem);
  156. totalReadLength += (*itr)->getDiskWriter()->readData(data+(len-rem), readLength, fileOffset);
  157. rem -= readLength;
  158. reading = true;
  159. fileOffset = 0;
  160. } else {
  161. fileOffset -= (*itr)->getFileEntry()->getLength();
  162. }
  163. }
  164. if(!reading) {
  165. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::llitos(offset, true).c_str());
  166. }
  167. return totalReadLength;
  168. }
  169. bool MultiDiskAdaptor::fileExists()
  170. {
  171. if(diskWriterEntries.empty()) {
  172. resetDiskWriterEntries();
  173. }
  174. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  175. itr != diskWriterEntries.end(); itr++) {
  176. if((*itr)->fileExists(getTopDirPath())) {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. // TODO call DiskWriter::openFile() before calling this function.
  183. int64_t MultiDiskAdaptor::size() const
  184. {
  185. int64_t size = 0;
  186. for(DiskWriterEntries::const_iterator itr = diskWriterEntries.begin();
  187. itr != diskWriterEntries.end(); itr++) {
  188. size += (*itr)->size();
  189. }
  190. return size;
  191. }
  192. FileAllocationIteratorHandle MultiDiskAdaptor::fileAllocationIterator()
  193. {
  194. return new MultiFileAllocationIterator(this);
  195. }
  196. void MultiDiskAdaptor::enableDirectIO()
  197. {
  198. for(DiskWriterEntries::const_iterator itr = diskWriterEntries.begin();
  199. itr != diskWriterEntries.end(); ++itr) {
  200. (*itr)->getDiskWriter()->enableDirectIO();
  201. }
  202. }
  203. void MultiDiskAdaptor::disableDirectIO()
  204. {
  205. for(DiskWriterEntries::const_iterator itr = diskWriterEntries.begin();
  206. itr != diskWriterEntries.end(); ++itr) {
  207. (*itr)->getDiskWriter()->disableDirectIO();
  208. }
  209. }