AbstractSingleDiskAdaptor.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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() = default;
  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. for (auto& d : entry->getDataSet()) {
  87. A2_LOG_DEBUG(fmt("Cache flush goff=%" PRId64 ", len=%lu", d->goff,
  88. static_cast<unsigned long>(d->len)));
  89. writeData(d->data + d->offset, d->len, d->goff);
  90. }
  91. }
  92. bool AbstractSingleDiskAdaptor::fileExists()
  93. {
  94. return File(getFilePath()).exists();
  95. }
  96. int64_t AbstractSingleDiskAdaptor::size() { return File(getFilePath()).size(); }
  97. void AbstractSingleDiskAdaptor::truncate(int64_t length)
  98. {
  99. diskWriter_->truncate(length);
  100. }
  101. std::unique_ptr<FileAllocationIterator>
  102. AbstractSingleDiskAdaptor::fileAllocationIterator()
  103. {
  104. switch (getFileAllocationMethod()) {
  105. #ifdef HAVE_SOME_FALLOCATE
  106. case (DiskAdaptor::FILE_ALLOC_FALLOC):
  107. return make_unique<FallocFileAllocationIterator>(diskWriter_.get(), size(),
  108. totalLength_);
  109. #endif // HAVE_SOME_FALLOCATE
  110. case (DiskAdaptor::FILE_ALLOC_TRUNC):
  111. return make_unique<TruncFileAllocationIterator>(diskWriter_.get(), size(),
  112. totalLength_);
  113. default:
  114. return make_unique<AdaptiveFileAllocationIterator>(diskWriter_.get(),
  115. size(), totalLength_);
  116. }
  117. }
  118. void AbstractSingleDiskAdaptor::enableReadOnly()
  119. {
  120. diskWriter_->enableReadOnly();
  121. readOnly_ = true;
  122. }
  123. void AbstractSingleDiskAdaptor::disableReadOnly()
  124. {
  125. diskWriter_->disableReadOnly();
  126. readOnly_ = false;
  127. }
  128. void AbstractSingleDiskAdaptor::enableMmap() { diskWriter_->enableMmap(); }
  129. void AbstractSingleDiskAdaptor::cutTrailingGarbage()
  130. {
  131. if (File(getFilePath()).size() > totalLength_) {
  132. diskWriter_->truncate(totalLength_);
  133. }
  134. }
  135. void AbstractSingleDiskAdaptor::setDiskWriter(
  136. std::unique_ptr<DiskWriter> diskWriter)
  137. {
  138. diskWriter_ = std::move(diskWriter);
  139. }
  140. void AbstractSingleDiskAdaptor::setTotalLength(int64_t totalLength)
  141. {
  142. totalLength_ = totalLength;
  143. }
  144. } // namespace aria2