| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/IOFile.h" |
| 5 | |
| 6 | #include <cstdio> |
| 7 | #include <string> |
| 8 | |
| 9 | #ifdef _WIN32 |
| 10 | #include <io.h> |
| 11 | |
| 12 | #include "Common/CommonFuncs.h" |
| 13 | #include "Common/StringUtil.h" |
| 14 | #else |
| 15 | #include <unistd.h> |
| 16 | #endif |
| 17 | |
| 18 | #ifdef ANDROID |
| 19 | #include <algorithm> |
| 20 | |
| 21 | #include "jni/AndroidCommon/AndroidCommon.h" |
| 22 | #endif |
| 23 | |
| 24 | #include "Common/CommonTypes.h" |
| 25 | #include "Common/FileUtil.h" |
| 26 | |
| 27 | namespace File |
| 28 | { |
| 29 | IOFile::IOFile() : m_file(nullptr), m_good(true) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | IOFile::IOFile(std::FILE* file) : m_file(file), m_good(true) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | IOFile::IOFile(const std::string& filename, const char openmode[], SharedAccess sh) |
| 38 | : m_file(nullptr), m_good(true) |
| 39 | { |
| 40 | Open(filename, openmode, sh); |
| 41 | } |
| 42 | |
| 43 | IOFile::~IOFile() |
| 44 | { |
| 45 | Close(); |
| 46 | } |
| 47 | |
| 48 | IOFile::IOFile(IOFile&& other) noexcept : m_file(nullptr), m_good(true) |
| 49 | { |
| 50 | Swap(other); |
| 51 | } |
| 52 | |
| 53 | IOFile& IOFile::operator=(IOFile&& other) noexcept |
| 54 | { |
| 55 | Swap(other); |
| 56 | return *this; |
| 57 | } |
| 58 | |
| 59 | void IOFile::Swap(IOFile& other) noexcept |
| 60 | { |
| 61 | std::swap(m_file, other.m_file); |
| 62 | std::swap(m_good, other.m_good); |
| 63 | } |
| 64 | |
| 65 | bool IOFile::Open(const std::string& filename, const char openmode[], |
| 66 | [[maybe_unused]] SharedAccess sh) |
| 67 | { |
| 68 | Close(); |
| 69 | |
| 70 | #ifdef _WIN32 |
| 71 | if (sh == SharedAccess::Default) |
| 72 | { |
| 73 | m_good = _tfopen_s(&m_file, UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str()) == 0; |
| 74 | } |
| 75 | else if (sh == SharedAccess::Read) |
| 76 | { |
| 77 | m_file = _tfsopen(UTF8ToTStr(filename).c_str(), UTF8ToTStr(openmode).c_str(), SH_DENYWR); |
| 78 | m_good = m_file != nullptr; |
| 79 | } |
| 80 | #else |
| 81 | #ifdef ANDROID |
| 82 | if (IsPathAndroidContent(filename)) |
| 83 | m_file = fdopen(OpenAndroidContent(filename, OpenModeToAndroid(openmode)), openmode); |
| 84 | else |
| 85 | #endif |
| 86 | m_file = std::fopen(filename.c_str(), openmode); |
| 87 | |
| 88 | m_good = m_file != nullptr; |
| 89 | #endif |
| 90 | |
| 91 | return m_good; |
| 92 | } |
| 93 | |
| 94 | bool IOFile::Close() |
| 95 | { |
| 96 | if (!IsOpen() || 0 != std::fclose(m_file)) |
| 97 | m_good = false; |
| 98 | |
| 99 | m_file = nullptr; |
| 100 | return m_good; |
| 101 | } |
| 102 | |
| 103 | void IOFile::SetHandle(std::FILE* file) |
| 104 | { |
| 105 | Close(); |
| 106 | ClearError(); |
| 107 | m_file = file; |
| 108 | } |
| 109 | |
| 110 | u64 IOFile::GetSize() const |
| 111 | { |
| 112 | if (IsOpen()) |
| 113 | return File::GetSize(m_file); |
| 114 | else |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | bool IOFile::Seek(s64 offset, SeekOrigin origin) |
| 119 | { |
| 120 | int fseek_origin; |
| 121 | switch (origin) |
| 122 | { |
| 123 | case SeekOrigin::Begin: |
| 124 | fseek_origin = SEEK_SET; |
| 125 | break; |
| 126 | case SeekOrigin::Current: |
| 127 | fseek_origin = SEEK_CUR; |
| 128 | break; |
| 129 | case SeekOrigin::End: |
| 130 | fseek_origin = SEEK_END; |
| 131 | break; |
| 132 | default: |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (!IsOpen() || 0 != fseeko(m_file, offset, fseek_origin)) |
| 137 | m_good = false; |
| 138 | |
| 139 | return m_good; |
| 140 | } |
| 141 | |
| 142 | u64 IOFile::Tell() const |
| 143 | { |
| 144 | if (IsOpen()) |
| 145 | return ftello(m_file); |
| 146 | else |
| 147 | return UINT64_MAX; |
| 148 | } |
| 149 | |
| 150 | bool IOFile::Flush() |
| 151 | { |
| 152 | if (!IsOpen() || 0 != std::fflush(m_file)) |
| 153 | m_good = false; |
| 154 | |
| 155 | return m_good; |
| 156 | } |
| 157 | |
| 158 | bool IOFile::Resize(u64 size) |
| 159 | { |
| 160 | #ifdef _WIN32 |
| 161 | if (!IsOpen() || 0 != _chsize_s(_fileno(m_file), size)) |
| 162 | #else |
| 163 | // TODO: handle 64bit and growing |
| 164 | if (!IsOpen() || 0 != ftruncate(fileno(m_file), size)) |
| 165 | #endif |
| 166 | m_good = false; |
| 167 | |
| 168 | return m_good; |
| 169 | } |
| 170 | |
| 171 | } // namespace File |