| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/FileBlob.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <memory> |
| 8 | #include <string> |
| 9 | #include <utility> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "Common/Assert.h" |
| 13 | #include "Common/FileUtil.h" |
| 14 | #include "Common/MsgHandler.h" |
| 15 | |
| 16 | namespace DiscIO |
| 17 | { |
| 18 | PlainFileReader::PlainFileReader(File::DirectIOFile file) : m_file(std::move(file)) |
| 19 | { |
| 20 | m_size = m_file.GetSize(); |
| 21 | } |
| 22 | |
| 23 | std::unique_ptr<PlainFileReader> PlainFileReader::Create(File::DirectIOFile file) |
| 24 | { |
| 25 | if (file.IsOpen()) |
| 26 | return std::unique_ptr<PlainFileReader>(new PlainFileReader(std::move(file))); |
| 27 | |
| 28 | return nullptr; |
| 29 | } |
| 30 | |
| 31 | std::unique_ptr<BlobReader> PlainFileReader::CopyReader() const |
| 32 | { |
| 33 | return Create(m_file); |
| 34 | } |
| 35 | |
| 36 | bool PlainFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) |
| 37 | { |
| 38 | return m_file.OffsetRead(offset, out_ptr, nbytes); |
| 39 | } |
| 40 | |
| 41 | bool ConvertToPlain(BlobReader* infile, const std::string& infile_path, |
| 42 | const std::string& outfile_path, const CompressCB& callback) |
| 43 | { |
| 44 | ASSERT(infile->GetDataSizeType() == DataSizeType::Accurate); |
| 45 | |
| 46 | File::DirectIOFile outfile(outfile_path, File::AccessMode::Write); |
| 47 | if (!outfile.IsOpen()) |
| 48 | { |
| 49 | PanicAlertFmtT( |
| 50 | "Failed to open the output file \"{0}\".\n" |
| 51 | "Check that you have permissions to write the target folder and that the media can " |
| 52 | "be written.", |
| 53 | outfile_path); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | constexpr size_t DESIRED_BUFFER_SIZE = 0x80000; |
| 58 | u64 buffer_size = infile->GetBlockSize(); |
| 59 | if (buffer_size == 0) |
| 60 | { |
| 61 | buffer_size = DESIRED_BUFFER_SIZE; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | while (buffer_size < DESIRED_BUFFER_SIZE) |
| 66 | buffer_size *= 2; |
| 67 | } |
| 68 | |
| 69 | std::vector<u8> buffer(buffer_size); |
| 70 | const u64 num_buffers = (infile->GetDataSize() + buffer_size - 1) / buffer_size; |
| 71 | int progress_monitor = std::max<int>(1, num_buffers / 100); |
| 72 | bool success = true; |
| 73 | |
| 74 | for (u64 i = 0; i < num_buffers; i++) |
| 75 | { |
| 76 | if (i % progress_monitor == 0) |
| 77 | { |
| 78 | const bool was_cancelled = |
| 79 | !callback(Common::GetStringT("Unpacking"), (float)i / (float)num_buffers); |
| 80 | if (was_cancelled) |
| 81 | { |
| 82 | success = false; |
| 83 | break; |
| 84 | } |
| 85 | } |
| 86 | const u64 inpos = i * buffer_size; |
| 87 | const u64 sz = std::min(buffer_size, infile->GetDataSize() - inpos); |
| 88 | if (!infile->Read(inpos, sz, buffer.data())) |
| 89 | { |
| 90 | PanicAlertFmtT("Failed to read from the input file \"{0}\".", infile_path); |
| 91 | success = false; |
| 92 | break; |
| 93 | } |
| 94 | if (!outfile.Write(buffer.data(), sz)) |
| 95 | { |
| 96 | PanicAlertFmtT("Failed to write the output file \"{0}\".\n" |
| 97 | "Check that you have enough space available on the target drive.", |
| 98 | outfile_path); |
| 99 | success = false; |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (!success) |
| 105 | { |
| 106 | // Remove the incomplete output file. |
| 107 | outfile.Close(); |
| 108 | File::Delete(outfile_path); |
| 109 | } |
| 110 | |
| 111 | return success; |
| 112 | } |
| 113 | |
| 114 | } // namespace DiscIO |