File: DirectIOFile.cpp

package info (click to toggle)
dolphin-emu 2512%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 76,328 kB
  • sloc: cpp: 499,023; ansic: 119,674; python: 6,547; sh: 2,338; makefile: 1,093; asm: 726; pascal: 257; javascript: 183; perl: 97; objc: 75; xml: 30
file content (366 lines) | stat: -rw-r--r-- 9,424 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "Common/DirectIOFile.h"

#include <utility>

#if defined(_WIN32)
#include <windows.h>

#include <cstring>

#include "Common/Buffer.h"
#include "Common/CommonFuncs.h"
#include "Common/MathUtil.h"
#include "Common/StringUtil.h"
#else
#include <string>

#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

#ifdef ANDROID
#include "jni/AndroidCommon/AndroidCommon.h"

#include "Common/Lazy.h"
#endif

#include "Common/FileUtil.h"
#endif

#include "Common/Assert.h"

namespace File
{
DirectIOFile::DirectIOFile() = default;

DirectIOFile::~DirectIOFile()
{
  Close();
}

DirectIOFile::DirectIOFile(const DirectIOFile& other)
{
  *this = other.Duplicate();
}

DirectIOFile& DirectIOFile::operator=(const DirectIOFile& other)
{
  return *this = other.Duplicate();
}

DirectIOFile::DirectIOFile(DirectIOFile&& other)
{
  Swap(other);
}

DirectIOFile& DirectIOFile::operator=(DirectIOFile&& other)
{
  Close();
  Swap(other);
  return *this;
}

DirectIOFile::DirectIOFile(const std::string& path, AccessMode access_mode, OpenMode open_mode)
{
  Open(path, access_mode, open_mode);
}

bool DirectIOFile::Open(const std::string& path, AccessMode access_mode, OpenMode open_mode)
{
  ASSERT(!IsOpen());

  if (open_mode == OpenMode::Default)
    open_mode = (access_mode == AccessMode::Write) ? OpenMode::Truncate : OpenMode::Existing;

  // This is not a sensible combination. Fail here to not rely on OS-specific behaviors.
  if (access_mode == AccessMode::Read && open_mode == OpenMode::Truncate)
    return false;

#if defined(_WIN32)
  DWORD desired_access = GENERIC_READ | GENERIC_WRITE;
  if (access_mode == AccessMode::Read)
    desired_access = GENERIC_READ;
  else if (access_mode == AccessMode::Write)
    desired_access = GENERIC_WRITE;

  if (access_mode != AccessMode::Read)
  {
    // Allow deleting and renaming through our handle.
    desired_access |= DELETE;
  }

  // All sharing is allowed to more closely match default behavior on other OSes.
  constexpr DWORD share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;

  DWORD creation_disposition = OPEN_ALWAYS;
  if (open_mode == OpenMode::Truncate)
    creation_disposition = CREATE_ALWAYS;
  else if (open_mode == OpenMode::Create)
    creation_disposition = CREATE_NEW;
  else if (open_mode == OpenMode::Existing)
    creation_disposition = OPEN_EXISTING;

  m_handle = CreateFile(UTF8ToTStr(path).c_str(), desired_access, share_mode, nullptr,
                        creation_disposition, FILE_ATTRIBUTE_NORMAL, nullptr);
  if (!IsOpen())
    WARN_LOG_FMT(COMMON, "CreateFile: {}", Common::GetLastErrorString());

#else
#if defined(ANDROID)
  if (IsPathAndroidContent(path))
  {
    // Android documentation says that "w" may or may not truncate.
    // In case it does, we'll use "rw" when we don't want truncation.
    // This wrongly enables read access when we don't need it.
    if (access_mode == AccessMode::Write && open_mode != OpenMode::Truncate)
      access_mode = AccessMode::ReadAndWrite;

    std::string open_mode_str = "rw";
    if (access_mode == AccessMode::Read)
      open_mode_str = "r";
    else if (access_mode == AccessMode::Write)
      open_mode_str = "w";

    if (open_mode == OpenMode::Truncate)
      open_mode_str += 't';

    if (open_mode == OpenMode::Create)
    {
      ASSERT_MSG(COMMON, false, "DirectIOFile doesn't support creating SAF files");
      return false;
    }

    m_fd = OpenAndroidContent(path, open_mode_str);

    if (!IsOpen() && (open_mode == OpenMode::Always || open_mode == OpenMode::Truncate))
      ASSERT_MSG(COMMON, Exists(path), "DirectIOFile doesn't support creating SAF files");

    return IsOpen();
  }
#endif
  int flags = O_RDWR;
  if (access_mode == AccessMode::Read)
    flags = O_RDONLY;
  else if (access_mode == AccessMode::Write)
    flags = O_WRONLY;

  if (open_mode == OpenMode::Truncate)
    flags |= O_CREAT | O_TRUNC;
  else if (open_mode == OpenMode::Create)
    flags |= O_CREAT | O_EXCL;
  else if (open_mode != OpenMode::Existing)
    flags |= O_CREAT;

  m_fd = open(path.c_str(), flags, 0666);

#endif

  return IsOpen();
}

bool DirectIOFile::Close()
{
  if (!IsOpen())
    return false;

  m_current_offset = 0;

#if defined(_WIN32)
  return CloseHandle(std::exchange(m_handle, INVALID_HANDLE_VALUE)) != 0;
#else
  return close(std::exchange(m_fd, -1)) == 0;
#endif
}

bool DirectIOFile::IsOpen() const
{
#if defined(_WIN32)
  return m_handle != INVALID_HANDLE_VALUE;
#else
  return m_fd != -1;
#endif
}

#if defined(_WIN32)
template <auto* TransferFunc>
static bool OverlappedTransfer(HANDLE handle, u64 offset, auto* data_ptr, u64 size)
{
  // ReadFile/WriteFile take a 32bit size so we must loop to handle our 64bit size.
  while (true)
  {
    OVERLAPPED overlapped{};
    overlapped.Offset = DWORD(offset);
    overlapped.OffsetHigh = DWORD(offset >> 32);

    DWORD bytes_transferred{};
    if (TransferFunc(handle, data_ptr, MathUtil::SaturatingCast<DWORD>(size), &bytes_transferred,
                     &overlapped) == 0)
    {
      ERROR_LOG_FMT(COMMON, "OverlappedTransfer: {}", Common::GetLastErrorString());
      return false;
    }

    size -= bytes_transferred;

    if (size == 0)
      return true;

    offset += bytes_transferred;
    data_ptr += bytes_transferred;
  }
}
#endif

bool DirectIOFile::OffsetRead(u64 offset, u8* out_ptr, u64 size)
{
#if defined(_WIN32)
  return OverlappedTransfer<ReadFile>(m_handle, offset, out_ptr, size);
#else
  return pread(m_fd, out_ptr, size, off_t(offset)) == ssize_t(size);
#endif
}

bool DirectIOFile::OffsetWrite(u64 offset, const u8* in_ptr, u64 size)
{
#if defined(_WIN32)
  return OverlappedTransfer<WriteFile>(m_handle, offset, in_ptr, size);
#else
  return pwrite(m_fd, in_ptr, size, off_t(offset)) == ssize_t(size);
#endif
}

u64 DirectIOFile::GetSize() const
{
#if defined(_WIN32)
  LARGE_INTEGER result{};
  if (GetFileSizeEx(m_handle, &result) != 0)
    return result.QuadPart;
#else
  struct stat st{};
  if (fstat(m_fd, &st) == 0)
    return st.st_size;
#endif

  return 0;
}

bool DirectIOFile::Seek(s64 offset, SeekOrigin origin)
{
  if (!IsOpen())
    return false;

  u64 reference_pos = 0;
  switch (origin)
  {
  case SeekOrigin::Current:
    reference_pos = m_current_offset;
    break;
  case SeekOrigin::End:
    reference_pos = GetSize();
    break;
  default:
    break;
  }

  // Don't let our current offset underflow.
  if (offset < 0 && u64(-offset) > reference_pos)
    return false;

  m_current_offset = reference_pos + offset;
  return true;
}

bool DirectIOFile::Flush()
{
#if defined(_WIN32)
  return FlushFileBuffers(m_handle) != 0;
#else
  return fsync(m_fd) == 0;
#endif
}

void DirectIOFile::Swap(DirectIOFile& other)
{
#if defined(_WIN32)
  std::swap(m_handle, other.m_handle);
#else
  std::swap(m_fd, other.m_fd);
#endif
  std::swap(m_current_offset, other.m_current_offset);
}

DirectIOFile DirectIOFile::Duplicate() const
{
  DirectIOFile result;

  if (!IsOpen())
    return result;

#if defined(_WIN32)
  const auto current_process = GetCurrentProcess();
  if (DuplicateHandle(current_process, m_handle, current_process, &result.m_handle, 0, FALSE,
                      DUPLICATE_SAME_ACCESS) == 0)
  {
    ERROR_LOG_FMT(COMMON, "DuplicateHandle: {}", Common::GetLastErrorString());
  }
#else
  result.m_fd = dup(m_fd);
#endif

  ASSERT(result.IsOpen());

  result.m_current_offset = m_current_offset;

  return result;
}

bool Resize(DirectIOFile& file, u64 size)
{
#if defined(_WIN32)
  // This operation is not "atomic", but it's the only thing we're using the file pointer for.
  // Concurrent `Resize` would need some external synchronization to prevent race regardless.
  const LARGE_INTEGER distance{.QuadPart = LONGLONG(size)};
  return (SetFilePointerEx(file.GetHandle(), distance, nullptr, FILE_BEGIN) != 0) &&
         (SetEndOfFile(file.GetHandle()) != 0);
#else
  return ftruncate(file.GetHandle(), off_t(size)) == 0;
#endif
}

bool Rename(DirectIOFile& file, const std::string& source_path [[maybe_unused]],
            const std::string& destination_path)
{
#if defined(_WIN32)
  const auto dest_name = UTF8ToWString(destination_path);
  const auto dest_name_byte_size = DWORD(dest_name.size() * sizeof(WCHAR));
  FILE_RENAME_INFO info{
      .ReplaceIfExists = TRUE,
      .FileNameLength = dest_name_byte_size,  // The size in bytes, not including null termination.
  };
  constexpr auto filename_struct_offset = offsetof(FILE_RENAME_INFO, FileName);
  Common::UniqueBuffer<u8> buffer(filename_struct_offset + dest_name_byte_size + sizeof(WCHAR));
  std::memcpy(buffer.data(), &info, filename_struct_offset);
  std::memcpy(buffer.data() + filename_struct_offset, dest_name.c_str(),
              dest_name_byte_size + sizeof(WCHAR));
  return SetFileInformationByHandle(file.GetHandle(), FileRenameInfo, buffer.data(),
                                    DWORD(buffer.size())) != 0;
#else
  return file.IsOpen() && Rename(source_path, destination_path);
#endif
}

bool Delete(DirectIOFile& file, const std::string& filename)
{
#if defined(_WIN32)
  FILE_DISPOSITION_INFO info{.DeleteFile = TRUE};
  return SetFileInformationByHandle(file.GetHandle(), FileDispositionInfo, &info, sizeof(info)) !=
         0;
#else
  return file.IsOpen() && Delete(filename, IfAbsentBehavior::NoConsoleWarning);
#endif
}

}  // namespace File