MultiDiskAdaptor.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "FileEntry.h"
  40. #include "MultiFileAllocationIterator.h"
  41. #include "DefaultDiskWriterFactory.h"
  42. #include "DlAbortEx.h"
  43. #include "File.h"
  44. namespace aria2 {
  45. DiskWriterEntry::DiskWriterEntry(const SharedHandle<FileEntry>& fileEntry):
  46. fileEntry(fileEntry),
  47. diskWriter(0) {}
  48. DiskWriterEntry::~DiskWriterEntry() {}
  49. std::string DiskWriterEntry::getFilePath(const std::string& topDir) const
  50. {
  51. return topDir+"/"+fileEntry->getPath();
  52. }
  53. void DiskWriterEntry::initAndOpenFile(const std::string& topDir)
  54. {
  55. diskWriter->initAndOpenFile(getFilePath(topDir), fileEntry->getLength());
  56. }
  57. void DiskWriterEntry::openFile(const std::string& topDir)
  58. {
  59. diskWriter->openFile(getFilePath(topDir), fileEntry->getLength());
  60. }
  61. void DiskWriterEntry::openExistingFile(const std::string& topDir)
  62. {
  63. diskWriter->openExistingFile(getFilePath(topDir), fileEntry->getLength());
  64. }
  65. void DiskWriterEntry::closeFile()
  66. {
  67. diskWriter->closeFile();
  68. }
  69. bool DiskWriterEntry::fileExists(const std::string& topDir)
  70. {
  71. return File(getFilePath(topDir)).exists();
  72. }
  73. int64_t DiskWriterEntry::size() const
  74. {
  75. return diskWriter->size();
  76. }
  77. SharedHandle<FileEntry> DiskWriterEntry::getFileEntry() const
  78. {
  79. return fileEntry;
  80. }
  81. void DiskWriterEntry::setDiskWriter(const SharedHandle<DiskWriter>& diskWriter)
  82. {
  83. this->diskWriter = diskWriter;
  84. }
  85. SharedHandle<DiskWriter> DiskWriterEntry::getDiskWriter() const
  86. {
  87. return diskWriter;
  88. }
  89. bool DiskWriterEntry::operator<(const DiskWriterEntry& entry) const
  90. {
  91. return fileEntry < entry.fileEntry;
  92. }
  93. MultiDiskAdaptor::MultiDiskAdaptor():
  94. pieceLength(0) {}
  95. MultiDiskAdaptor::~MultiDiskAdaptor() {}
  96. void MultiDiskAdaptor::resetDiskWriterEntries()
  97. {
  98. diskWriterEntries.clear();
  99. for(FileEntries::const_iterator itr = fileEntries.begin();
  100. itr != fileEntries.end(); itr++) {
  101. DiskWriterEntryHandle entry = new DiskWriterEntry(*itr);
  102. if((*itr)->isRequested()) {
  103. entry->setDiskWriter(DefaultDiskWriterFactory().newDiskWriter());
  104. } else {
  105. entry->setDiskWriter(new DefaultDiskWriter());
  106. }
  107. entry->getDiskWriter()->setDirectIOAllowed(_directIOAllowed);
  108. diskWriterEntries.push_back(entry);
  109. }
  110. }
  111. std::string MultiDiskAdaptor::getTopDirPath() const
  112. {
  113. return storeDir+"/"+topDir;
  114. }
  115. void MultiDiskAdaptor::mkdir() const
  116. {
  117. for(FileEntries::const_iterator itr = fileEntries.begin();
  118. itr != fileEntries.end(); itr++) {
  119. (*itr)->setupDir(getTopDirPath());
  120. }
  121. }
  122. void MultiDiskAdaptor::openFile()
  123. {
  124. mkdir();
  125. resetDiskWriterEntries();
  126. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  127. itr != diskWriterEntries.end(); itr++) {
  128. (*itr)->openFile(getTopDirPath());
  129. }
  130. }
  131. void MultiDiskAdaptor::initAndOpenFile()
  132. {
  133. mkdir();
  134. resetDiskWriterEntries();
  135. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  136. itr != diskWriterEntries.end(); itr++) {
  137. (*itr)->initAndOpenFile(getTopDirPath());
  138. }
  139. }
  140. void MultiDiskAdaptor::openExistingFile()
  141. {
  142. resetDiskWriterEntries();
  143. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  144. itr != diskWriterEntries.end(); itr++) {
  145. (*itr)->openExistingFile(getTopDirPath());
  146. }
  147. }
  148. void MultiDiskAdaptor::closeFile()
  149. {
  150. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  151. itr != diskWriterEntries.end(); itr++) {
  152. (*itr)->closeFile();
  153. }
  154. }
  155. void MultiDiskAdaptor::onDownloadComplete()
  156. {
  157. closeFile();
  158. openFile();
  159. }
  160. void MultiDiskAdaptor::writeData(const unsigned char* data, int32_t len,
  161. int64_t offset)
  162. {
  163. int64_t fileOffset = offset;
  164. bool writing = false;
  165. int32_t rem = len;
  166. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  167. itr != diskWriterEntries.end() && rem != 0; itr++) {
  168. if(isInRange(*itr, offset) || writing) {
  169. int32_t writeLength = calculateLength(*itr, fileOffset, rem);
  170. (*itr)->getDiskWriter()->writeData(data+(len-rem), writeLength, fileOffset);
  171. rem -= writeLength;
  172. writing = true;
  173. fileOffset = 0;
  174. } else {
  175. fileOffset -= (*itr)->getFileEntry()->getLength();
  176. }
  177. }
  178. if(!writing) {
  179. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::itos(offset, true).c_str());
  180. }
  181. }
  182. bool MultiDiskAdaptor::isInRange(const DiskWriterEntryHandle entry,
  183. int64_t offset) const
  184. {
  185. return entry->getFileEntry()->getOffset() <= offset &&
  186. offset < entry->getFileEntry()->getOffset()+entry->getFileEntry()->getLength();
  187. }
  188. int32_t MultiDiskAdaptor::calculateLength(const DiskWriterEntryHandle entry,
  189. int64_t fileOffset,
  190. int32_t rem) const
  191. {
  192. int32_t length;
  193. if(entry->getFileEntry()->getLength() < fileOffset+rem) {
  194. length = entry->getFileEntry()->getLength()-fileOffset;
  195. } else {
  196. length = rem;
  197. }
  198. return length;
  199. }
  200. int32_t MultiDiskAdaptor::readData(unsigned char* data, int32_t len, int64_t offset)
  201. {
  202. int64_t fileOffset = offset;
  203. bool reading = false;
  204. int32_t rem = len;
  205. int32_t totalReadLength = 0;
  206. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  207. itr != diskWriterEntries.end() && rem != 0; itr++) {
  208. if(isInRange(*itr, offset) || reading) {
  209. int32_t readLength = calculateLength((*itr), fileOffset, rem);
  210. totalReadLength += (*itr)->getDiskWriter()->readData(data+(len-rem), readLength, fileOffset);
  211. rem -= readLength;
  212. reading = true;
  213. fileOffset = 0;
  214. } else {
  215. fileOffset -= (*itr)->getFileEntry()->getLength();
  216. }
  217. }
  218. if(!reading) {
  219. throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::itos(offset, true).c_str());
  220. }
  221. return totalReadLength;
  222. }
  223. bool MultiDiskAdaptor::fileExists()
  224. {
  225. if(diskWriterEntries.empty()) {
  226. resetDiskWriterEntries();
  227. }
  228. for(DiskWriterEntries::iterator itr = diskWriterEntries.begin();
  229. itr != diskWriterEntries.end(); itr++) {
  230. if((*itr)->fileExists(getTopDirPath())) {
  231. return true;
  232. }
  233. }
  234. return false;
  235. }
  236. // TODO call DiskWriter::openFile() before calling this function.
  237. int64_t MultiDiskAdaptor::size() const
  238. {
  239. int64_t size = 0;
  240. for(DiskWriterEntries::const_iterator itr = diskWriterEntries.begin();
  241. itr != diskWriterEntries.end(); itr++) {
  242. size += (*itr)->size();
  243. }
  244. return size;
  245. }
  246. FileAllocationIteratorHandle MultiDiskAdaptor::fileAllocationIterator()
  247. {
  248. return new MultiFileAllocationIterator(this);
  249. }
  250. void MultiDiskAdaptor::enableDirectIO()
  251. {
  252. for(DiskWriterEntries::const_iterator itr = diskWriterEntries.begin();
  253. itr != diskWriterEntries.end(); ++itr) {
  254. (*itr)->getDiskWriter()->enableDirectIO();
  255. }
  256. }
  257. void MultiDiskAdaptor::disableDirectIO()
  258. {
  259. for(DiskWriterEntries::const_iterator itr = diskWriterEntries.begin();
  260. itr != diskWriterEntries.end(); ++itr) {
  261. (*itr)->getDiskWriter()->disableDirectIO();
  262. }
  263. }
  264. } // namespace aria2