MultiFileAllocationIterator.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 "MultiFileAllocationIterator.h"
  36. #include "MultiDiskAdaptor.h"
  37. #include "FileEntry.h"
  38. #include "AdaptiveFileAllocationIterator.h"
  39. #include "TruncFileAllocationIterator.h"
  40. #ifdef HAVE_SOME_FALLOCATE
  41. #include "FallocFileAllocationIterator.h"
  42. #endif // HAVE_SOME_FALLOCATE
  43. #include "DiskWriter.h"
  44. #include "DefaultDiskWriterFactory.h"
  45. #include "LogFactory.h"
  46. namespace aria2 {
  47. MultiFileAllocationIterator::MultiFileAllocationIterator(
  48. MultiDiskAdaptor* diskAdaptor)
  49. : diskAdaptor_{diskAdaptor},
  50. entryItr_{std::begin(diskAdaptor_->getDiskWriterEntries())}
  51. {
  52. }
  53. MultiFileAllocationIterator::~MultiFileAllocationIterator()
  54. {
  55. if (diskWriter_) {
  56. diskWriter_->closeFile();
  57. }
  58. }
  59. void MultiFileAllocationIterator::allocateChunk()
  60. {
  61. if (fileAllocationIterator_) {
  62. if (!fileAllocationIterator_->finished()) {
  63. fileAllocationIterator_->allocateChunk();
  64. return;
  65. }
  66. if (diskWriter_) {
  67. diskWriter_->closeFile();
  68. diskWriter_.reset();
  69. }
  70. fileAllocationIterator_.reset();
  71. ++entryItr_;
  72. }
  73. while (entryItr_ != std::end(diskAdaptor_->getDiskWriterEntries())) {
  74. if (!(*entryItr_)->getDiskWriter()) {
  75. ++entryItr_;
  76. continue;
  77. }
  78. auto& fileEntry = (*entryItr_)->getFileEntry();
  79. // we use dedicated DiskWriter instead of
  80. // (*entryItr_)->getDiskWriter(). This is because
  81. // SingleFileAllocationIterator cannot reopen file if file is
  82. // closed by OpenedFileCounter.
  83. diskWriter_ =
  84. DefaultDiskWriterFactory().newDiskWriter((*entryItr_)->getFilePath());
  85. // Open file before calling DiskWriterEntry::size(). Calling
  86. // private function of MultiDiskAdaptor.
  87. diskWriter_->openFile(fileEntry->getLength());
  88. if ((*entryItr_)->needsFileAllocation() &&
  89. (*entryItr_)->size() < fileEntry->getLength()) {
  90. A2_LOG_INFO(fmt("Allocating file %s: target size=%" PRId64
  91. ", current size=%" PRId64,
  92. (*entryItr_)->getFilePath().c_str(),
  93. fileEntry->getLength(), (*entryItr_)->size()));
  94. switch (diskAdaptor_->getFileAllocationMethod()) {
  95. #ifdef HAVE_SOME_FALLOCATE
  96. case (DiskAdaptor::FILE_ALLOC_FALLOC):
  97. fileAllocationIterator_ = make_unique<FallocFileAllocationIterator>(
  98. diskWriter_.get(), (*entryItr_)->size(), fileEntry->getLength());
  99. break;
  100. #endif // HAVE_SOME_FALLOCATE
  101. case (DiskAdaptor::FILE_ALLOC_TRUNC):
  102. fileAllocationIterator_ = make_unique<TruncFileAllocationIterator>(
  103. diskWriter_.get(), (*entryItr_)->size(), fileEntry->getLength());
  104. break;
  105. default:
  106. fileAllocationIterator_ = make_unique<AdaptiveFileAllocationIterator>(
  107. diskWriter_.get(), (*entryItr_)->size(), fileEntry->getLength());
  108. break;
  109. }
  110. fileAllocationIterator_->allocateChunk();
  111. return;
  112. }
  113. diskWriter_->closeFile();
  114. diskWriter_.reset();
  115. ++entryItr_;
  116. }
  117. }
  118. bool MultiFileAllocationIterator::finished()
  119. {
  120. return entryItr_ == std::end(diskAdaptor_->getDiskWriterEntries()) &&
  121. (!fileAllocationIterator_ || fileAllocationIterator_->finished());
  122. }
  123. int64_t MultiFileAllocationIterator::getCurrentLength()
  124. {
  125. if (!fileAllocationIterator_) {
  126. return 0;
  127. }
  128. else {
  129. return fileAllocationIterator_->getCurrentLength();
  130. }
  131. }
  132. int64_t MultiFileAllocationIterator::getTotalLength()
  133. {
  134. if (!fileAllocationIterator_) {
  135. return 0;
  136. }
  137. else {
  138. return fileAllocationIterator_->getTotalLength();
  139. }
  140. }
  141. const DiskWriterEntries&
  142. MultiFileAllocationIterator::getDiskWriterEntries() const
  143. {
  144. return diskAdaptor_->getDiskWriterEntries();
  145. }
  146. } // namespace aria2