| 109 | } |
| 110 | |
| 111 | int64_t ChunksDownloadStrategy::LoadOrInitChunks(std::string const & fName, int64_t fileSize, int64_t chunkSize) |
| 112 | { |
| 113 | ASSERT(fileSize > 0, ()); |
| 114 | |
| 115 | if (Platform::IsFileExistsByFullPath(fName)) |
| 116 | { |
| 117 | try |
| 118 | { |
| 119 | FileReader r(fName); |
| 120 | ReaderSource<FileReader> src(r); |
| 121 | |
| 122 | int64_t const readSize = ReadVarInt<int64_t>(src); |
| 123 | if (readSize == fileSize) |
| 124 | { |
| 125 | // Load chunks. |
| 126 | uint64_t const size = src.Size(); |
| 127 | int const stSize = sizeof(ChunkT); |
| 128 | auto const count = static_cast<size_t>(size / stSize); |
| 129 | ASSERT_EQUAL(size, stSize * count, ()); |
| 130 | |
| 131 | m_chunks.resize(count); |
| 132 | src.Read(&m_chunks[0], stSize * count); |
| 133 | |
| 134 | // Reset status "downloading" to "free". |
| 135 | int64_t downloadedSize = 0; |
| 136 | size_t completed = 0; |
| 137 | for (size_t i = 0; i < count - 1; ++i) |
| 138 | { |
| 139 | if (m_chunks[i].m_status != CHUNK_COMPLETE) |
| 140 | m_chunks[i].m_status = CHUNK_FREE; |
| 141 | else |
| 142 | { |
| 143 | downloadedSize += (m_chunks[i + 1].m_pos - m_chunks[i].m_pos); |
| 144 | ++completed; |
| 145 | } |
| 146 | } |
| 147 | LOG(LINFO, ("Resumed file: downloaded", downloadedSize / 1024 / 1024, "out of", fileSize / 1024 / 1024, |
| 148 | "MB; completed chunks", completed, "out of", count - 1)); |
| 149 | |
| 150 | return downloadedSize; |
| 151 | } |
| 152 | } |
| 153 | catch (FileReader::Exception const & e) |
| 154 | { |
| 155 | LOG(LDEBUG, (e.Msg())); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | InitChunks(fileSize, chunkSize); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | std::string ChunksDownloadStrategy::ChunkFinished(bool success, RangeT const & range) |
| 164 | { |
no test coverage detected