AbstractSingleDiskAdaptor.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "AbstractSingleDiskAdaptor.h"
  36. #include "File.h"
  37. #include "AdaptiveFileAllocationIterator.h"
  38. #include "DiskWriter.h"
  39. #include "FileEntry.h"
  40. #include "TruncFileAllocationIterator.h"
  41. #include "WrDiskCacheEntry.h"
  42. #include "LogFactory.h"
  43. #ifdef HAVE_SOME_FALLOCATE
  44. #include "FallocFileAllocationIterator.h"
  45. #endif // HAVE_SOME_FALLOCATE
  46. namespace aria2 {
  47. AbstractSingleDiskAdaptor::AbstractSingleDiskAdaptor()
  48. : totalLength_(0), readOnly_(false)
  49. {
  50. }
  51. AbstractSingleDiskAdaptor::~AbstractSingleDiskAdaptor() {}
  52. void AbstractSingleDiskAdaptor::initAndOpenFile()
  53. {
  54. diskWriter_->initAndOpenFile(totalLength_);
  55. }
  56. void AbstractSingleDiskAdaptor::openFile()
  57. {
  58. diskWriter_->openFile(totalLength_);
  59. }
  60. void AbstractSingleDiskAdaptor::closeFile() { diskWriter_->closeFile(); }
  61. void AbstractSingleDiskAdaptor::openExistingFile()
  62. {
  63. diskWriter_->openExistingFile(totalLength_);
  64. }
  65. void AbstractSingleDiskAdaptor::writeData(const unsigned char* data, size_t len,
  66. int64_t offset)
  67. {
  68. diskWriter_->writeData(data, len, offset);
  69. }
  70. ssize_t AbstractSingleDiskAdaptor::readData(unsigned char* data, size_t len,
  71. int64_t offset)
  72. {
  73. return diskWriter_->readData(data, len, offset);
  74. }
  75. ssize_t AbstractSingleDiskAdaptor::readDataDropCache(unsigned char* data,
  76. size_t len, int64_t offset)
  77. {
  78. auto rv = readData(data, len, offset);
  79. if (rv > 0) {
  80. diskWriter_->dropCache(len, offset);
  81. }
  82. return rv;
  83. }
  84. void AbstractSingleDiskAdaptor::writeCache(const WrDiskCacheEntry* entry)
  85. {
  86. // Write cached data in 4KiB aligned offset. This reduces disk
  87. // activity especially on Windows 7 NTFS. In this code, we assume
  88. // that maximum length of DataCell data is 16KiB to simplify the
  89. // code.
  90. unsigned char buf[16_k];
  91. int64_t start = 0;
  92. size_t buflen = 0;
  93. size_t buffoffset = 0;
  94. const WrDiskCacheEntry::DataCellSet& dataSet = entry->getDataSet();
  95. for (auto& d : dataSet) {
  96. if (start + static_cast<ssize_t>(buflen) < d->goff) {
  97. A2_LOG_DEBUG(fmt("Cache flush goff=%" PRId64 ", len=%lu", start,
  98. static_cast<unsigned long>(buflen)));
  99. writeData(buf + buffoffset, buflen - buffoffset, start);
  100. start = d->goff;
  101. buflen = buffoffset = 0;
  102. }
  103. if (buflen == 0 && (d->goff & 0xfff) == 0 && (d->len & 0xfff) == 0) {
  104. // Already aligned. Write it without copy.
  105. A2_LOG_DEBUG(fmt("Cache flush goff=%" PRId64 ", len=%lu", start,
  106. static_cast<unsigned long>(d->len)));
  107. writeData(d->data + d->offset, d->len, start);
  108. start += d->len;
  109. }
  110. else {
  111. if (buflen == 0) {
  112. buflen = buffoffset = d->goff & 0xfff;
  113. }
  114. size_t wlen = std::min(sizeof(buf) - buflen, d->len);
  115. memcpy(buf + buflen, d->data + d->offset, wlen);
  116. buflen += wlen;
  117. if (buflen == sizeof(buf)) {
  118. A2_LOG_DEBUG(fmt("Cache flush goff=%" PRId64 ", len=%lu", start,
  119. static_cast<unsigned long>(buflen)));
  120. writeData(buf + buffoffset, buflen - buffoffset, start);
  121. memcpy(buf, d->data + d->offset + wlen, d->len - wlen);
  122. start += sizeof(buf) - buffoffset;
  123. buflen = d->len - wlen;
  124. buffoffset = 0;
  125. }
  126. }
  127. }
  128. writeData(buf + buffoffset, buflen - buffoffset, start);
  129. }
  130. bool AbstractSingleDiskAdaptor::fileExists()
  131. {
  132. return File(getFilePath()).exists();
  133. }
  134. int64_t AbstractSingleDiskAdaptor::size() { return File(getFilePath()).size(); }
  135. void AbstractSingleDiskAdaptor::truncate(int64_t length)
  136. {
  137. diskWriter_->truncate(length);
  138. }
  139. std::unique_ptr<FileAllocationIterator>
  140. AbstractSingleDiskAdaptor::fileAllocationIterator()
  141. {
  142. switch (getFileAllocationMethod()) {
  143. #ifdef HAVE_SOME_FALLOCATE
  144. case (DiskAdaptor::FILE_ALLOC_FALLOC):
  145. return make_unique<FallocFileAllocationIterator>(diskWriter_.get(), size(),
  146. totalLength_);
  147. #endif // HAVE_SOME_FALLOCATE
  148. case (DiskAdaptor::FILE_ALLOC_TRUNC):
  149. return make_unique<TruncFileAllocationIterator>(diskWriter_.get(), size(),
  150. totalLength_);
  151. default:
  152. return make_unique<AdaptiveFileAllocationIterator>(diskWriter_.get(),
  153. size(), totalLength_);
  154. }
  155. }
  156. void AbstractSingleDiskAdaptor::enableReadOnly()
  157. {
  158. diskWriter_->enableReadOnly();
  159. readOnly_ = true;
  160. }
  161. void AbstractSingleDiskAdaptor::disableReadOnly()
  162. {
  163. diskWriter_->disableReadOnly();
  164. readOnly_ = false;
  165. }
  166. void AbstractSingleDiskAdaptor::enableMmap() { diskWriter_->enableMmap(); }
  167. void AbstractSingleDiskAdaptor::cutTrailingGarbage()
  168. {
  169. if (File(getFilePath()).size() > totalLength_) {
  170. diskWriter_->truncate(totalLength_);
  171. }
  172. }
  173. void AbstractSingleDiskAdaptor::setDiskWriter(
  174. std::unique_ptr<DiskWriter> diskWriter)
  175. {
  176. diskWriter_ = std::move(diskWriter);
  177. }
  178. void AbstractSingleDiskAdaptor::setTotalLength(int64_t totalLength)
  179. {
  180. totalLength_ = totalLength;
  181. }
  182. } // namespace aria2