| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_FILE_ADAPTER_FILEADAPTER_HPP |
| 4 |
#define MPT_IO_FILE_ADAPTER_FILEADAPTER_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/detect.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/saturate_cast.hpp" |
| 11 |
#include "mpt/io/base.hpp" |
| 12 |
#if !MPT_OS_WINDOWS |
| 13 |
#include "mpt/io/io.hpp" |
| 14 |
#include "mpt/io/io_stdstream.hpp" |
| 15 |
#include "mpt/io_file/fstream.hpp" |
| 16 |
#endif // !MPT_OS_WINDOWS |
| 17 |
#include "mpt/io_file_unique/unique_basename.hpp" |
| 18 |
#include "mpt/io_file_unique/unique_tempfilename.hpp" |
| 19 |
#if MPT_OS_WINDOWS |
| 20 |
#include "mpt/path/os_path.hpp" |
| 21 |
#endif // MPT_OS_WINDOWS |
| 22 |
#include "mpt/string_transcode/transcode.hpp" |
| 23 |
#if MPT_OS_WINDOWS |
| 24 |
#include "mpt/system_error/system_error.hpp" |
| 25 |
#endif // MPT_OS_WINDOWS |
| 26 |
|
| 27 |
#include <stdexcept> |
| 28 |
#if !MPT_OS_WINDOWS |
| 29 |
#include <system_error> |
| 30 |
#endif // MPT_OS_WINDOWS |
| 31 |
|
| 32 |
#include <cstddef> |
| 33 |
#if !MPT_OS_WINDOWS |
| 34 |
#include <cstdio> |
| 35 |
#endif // !MPT_OS_WINDOWS |
| 36 |
|
| 37 |
#if MPT_OS_WINDOWS |
| 38 |
#include <windows.h> |
| 39 |
#endif // MPT_OS_WINDOWS |
| 40 |
|
| 41 |
#if MPT_OS_WINDOWS |
| 42 |
#if MPT_OS_WINDOWS_WINRT |
| 43 |
#if MPT_WINRT_AT_LEAST(MPT_WIN_8) |
| 44 |
#define MPT_IO_FILE_ONDISKFILEWRAPPER_USE_CREATEFILE |
| 45 |
#endif |
| 46 |
#else // !MPT_OS_WINDOWS_WINRT |
| 47 |
#define MPT_IO_FILE_ONDISKFILEWRAPPER_USE_CREATEFILE |
| 48 |
#endif // MPT_OS_WINDOWS_WINRT |
| 49 |
#endif // MPT_OS_WINDOWS |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
namespace mpt { |
| 54 |
inline namespace MPT_INLINE_NS { |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
namespace IO { |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
template <typename TFileCursor> |
| 63 |
class FileAdapter { |
| 64 |
|
| 65 |
private: |
| 66 |
mpt::os_path m_Filename; |
| 67 |
bool m_IsTempFile; |
| 68 |
|
| 69 |
public: |
| 70 |
FileAdapter(TFileCursor & file, const mpt::os_path & tempName = unique_tempfilename{unique_basename{}, MPT_OS_PATH("tmp")}) |
| 71 |
: m_IsTempFile(false) { |
| 72 |
try { |
| 73 |
file.Rewind(); |
| 74 |
if (!file.GetOptionalFileName()) { |
| 75 |
|
| 76 |
#ifdef MPT_IO_FILE_ONDISKFILEWRAPPER_USE_CREATEFILE |
| 77 |
|
| 78 |
HANDLE hFile = NULL; |
| 79 |
#if MPT_OS_WINDOWS_WINRT |
| 80 |
hFile = mpt::windows::CheckFileHANDLE(CreateFile2(mpt::support_long_path(tempName).c_str(), GENERIC_WRITE, FILE_SHARE_READ, CREATE_ALWAYS, NULL)); |
| 81 |
#else |
| 82 |
hFile = mpt::windows::CheckFileHANDLE(CreateFile(mpt::support_long_path(tempName).c_str(), GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL)); |
| 83 |
#endif |
| 84 |
while (!file.EndOfFile()) { |
| 85 |
typename TFileCursor::PinnedView view = file.ReadPinnedView(mpt::IO::BUFFERSIZE_NORMAL); |
| 86 |
std::size_t towrite = view.size(); |
| 87 |
std::size_t written = 0; |
| 88 |
do { |
| 89 |
DWORD chunkSize = mpt::saturate_cast<DWORD>(towrite); |
| 90 |
DWORD chunkDone = 0; |
| 91 |
try { |
| 92 |
mpt::windows::CheckBOOL(WriteFile(hFile, view.data() + written, chunkSize, &chunkDone, NULL)); |
| 93 |
} catch (...) { |
| 94 |
CloseHandle(hFile); |
| 95 |
hFile = NULL; |
| 96 |
throw; |
| 97 |
} |
| 98 |
if (chunkDone != chunkSize) { |
| 99 |
CloseHandle(hFile); |
| 100 |
hFile = NULL; |
| 101 |
throw std::runtime_error("Incomplete WriteFile()."); |
| 102 |
} |
| 103 |
towrite -= chunkDone; |
| 104 |
written += chunkDone; |
| 105 |
} while (towrite > 0); |
| 106 |
} |
| 107 |
CloseHandle(hFile); |
| 108 |
hFile = NULL; |
| 109 |
|
| 110 |
#else // !MPT_IO_FILE_ONDISKFILEWRAPPER_USE_CREATEFILE |
| 111 |
|
| 112 |
mpt::IO::ofstream f(tempName, std::ios::binary); |
| 113 |
if (!f) { |
| 114 |
throw std::runtime_error("Error creating temporary file."); |
| 115 |
} |
| 116 |
while (!file.EndOfFile()) { |
| 117 |
typename TFileCursor::PinnedView view = file.ReadPinnedView(mpt::IO::BUFFERSIZE_NORMAL); |
| 118 |
std::size_t towrite = view.size(); |
| 119 |
std::size_t written = 0; |
| 120 |
do { |
| 121 |
std::size_t chunkSize = mpt::saturate_cast<std::size_t>(towrite); |
| 122 |
bool chunkOk = false; |
| 123 |
chunkOk = mpt::IO::WriteRaw(f, mpt::const_byte_span(view.data() + written, chunkSize)); |
| 124 |
if (!chunkOk) { |
| 125 |
throw std::runtime_error("Incomplete Write."); |
| 126 |
} |
| 127 |
towrite -= chunkSize; |
| 128 |
written += chunkSize; |
| 129 |
} while (towrite > 0); |
| 130 |
} |
| 131 |
f.close(); |
| 132 |
|
| 133 |
#endif // MPT_ONDISKFILEWRAPPER_NO_CREATEFILE |
| 134 |
|
| 135 |
m_Filename = tempName; |
| 136 |
m_IsTempFile = true; |
| 137 |
} else { |
| 138 |
#if defined(MPT_LIBCXX_QUIRK_NO_OPTIONAL_VALUE) |
| 139 |
m_Filename = *(file.GetOptionalFileName()); |
| 140 |
#else |
| 141 |
m_Filename = file.GetOptionalFileName().value(); |
| 142 |
#endif |
| 143 |
} |
| 144 |
} catch (const std::runtime_error &) { |
| 145 |
m_Filename = mpt::os_path{}; |
| 146 |
m_IsTempFile = false; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
~FileAdapter() { |
| 151 |
if (m_IsTempFile) { |
| 152 |
#if MPT_OS_WINDOWS |
| 153 |
DeleteFile(m_Filename.c_str()); |
| 154 |
#else // !MPT_OS_WINDOWS |
| 155 |
std::remove(m_Filename.c_str()); |
| 156 |
//std::error_code ec{}; |
| 157 |
//std::filesystem::remove(mpt::transcode<std::filesystem::path>(m_Filename.AsNative()), ec); |
| 158 |
#endif // MPT_OS_WINDOWS |
| 159 |
m_IsTempFile = false; |
| 160 |
} |
| 161 |
m_Filename = mpt::os_path{}; |
| 162 |
} |
| 163 |
|
| 164 |
public: |
| 165 |
bool IsValid() const { |
| 166 |
return !m_Filename.empty(); |
| 167 |
} |
| 168 |
|
| 169 |
mpt::os_path GetFilename() const { |
| 170 |
return m_Filename; |
| 171 |
} |
| 172 |
|
| 173 |
}; // class FileAdapter |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
} // namespace IO |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
} // namespace MPT_INLINE_NS |
| 182 |
} // namespace mpt |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
#endif // MPT_IO_FILE_ADAPTER_FILEADAPTER_HPP |