Debian Codegraph @

Source/Core/Common/DirectIOFile.h

raw symbols SCIP
1 // Copyright 2025 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <span>
7 #include <string>
8
9 #include "Common/CommonTypes.h"
10 #include "Common/IOFile.h"
11
12 namespace File
13 {
14 enum class AccessMode
15 {
16 Read,
17 Write,
18 ReadAndWrite,
19 };
20
21 enum class OpenMode
22 {
23 // Based on the provided `AccessMode`.
24 // Read: Existing.
25 // Write: Truncate.
26 // ReadAndWrite: Existing.
27 Default,
28
29 // Either create a new file or open an existing file.
30 Always,
31
32 // Like `Always`, but also erase the contents of an existing file.
33 Truncate,
34
35 // Require a file to already exist. Fail otherwise.
36 Existing,
37
38 // Require a new file be created. Fail if one already exists.
39 Create,
40 };
41
42 // This file wrapper avoids use of the underlying system file position.
43 // It keeps track of its own file position and read/write calls directly use it.
44 // This makes copied handles entirely thread safe.
45 class DirectIOFile final
46 {
47 public:
48 DirectIOFile();
49 ~DirectIOFile();
50
51 // Copies and moves are allowed.
52 DirectIOFile(const DirectIOFile&);
53 DirectIOFile& operator=(const DirectIOFile&);
54 DirectIOFile(DirectIOFile&&);
55 DirectIOFile& operator=(DirectIOFile&&);
56
57 explicit DirectIOFile(const std::string& path, AccessMode access_mode,
58 OpenMode open_mode = OpenMode::Default);
59
60 bool Open(const std::string& path, AccessMode access_mode,
61 OpenMode open_mode = OpenMode::Default);
62
63 bool Close();
64
65 bool IsOpen() const;
66
67 // An offset from the start of the file may be specified directly.
68 // These explicit offset versions entirely ignore the current file position.
69 // They are thread safe, even when used on the same object.
70
71 bool OffsetRead(u64 offset, u8* out_ptr, u64 size);
72 bool OffsetRead(u64 offset, std::span<u8> out_data)
73 {
74 return OffsetRead(offset, out_data.data(), out_data.size());
75 }
76 bool OffsetWrite(u64 offset, const u8* in_ptr, u64 size);
77 bool OffsetWrite(auto offset, std::span<const u8> in_data)
78 {
79 return OffsetWrite(offset, in_data.data(), in_data.size());
80 }
81
82 // These Read/Write functions advance the current position on success.
83
84 bool Read(u8* out_ptr, u64 size)
85 {
86 if (!OffsetRead(m_current_offset, out_ptr, size))
87 return false;
88 m_current_offset += size;
89 return true;
90 }
91 bool Read(std::span<u8> out_data) { return Read(out_data.data(), out_data.size()); }
92
93 bool Write(const u8* in_ptr, u64 size)
94 {
95 if (!OffsetWrite(m_current_offset, in_ptr, size))
96 return false;
97 m_current_offset += size;
98 return true;
99 }
100 bool Write(std::span<const u8> in_data) { return Write(in_data.data(), in_data.size()); }
101
102 // Returns 0 on error.
103 u64 GetSize() const;
104
105 bool Seek(s64 offset, SeekOrigin origin);
106
107 // Returns 0 when not open.
108 u64 Tell() const { return m_current_offset; }
109
110 bool Flush();
111
112 auto GetHandle() const
113 {
114 #if defined(_WIN32)
115 return m_handle;
116 #else
117 return m_fd;
118 #endif
119 }
120
121 private:
122 void Swap(DirectIOFile& other);
123 DirectIOFile Duplicate() const;
124
125 #if defined(_WIN32)
126 // A workaround to avoid including <windows.h> in this header.
127 // HANDLE is just void* and INVALID_HANDLE_VALUE is -1.
128 using HandleType = void*;
129 HandleType m_handle{HandleType(-1)};
130 #else
131 using HandleType = int;
132 HandleType m_fd{-1};
133 #endif
134
135 u64 m_current_offset{};
136 };
137
138 // These take an open file handle to avoid failures from other processes trying to open our files.
139 // This is mainly an issue on Windows.
140
141 bool Resize(DirectIOFile& file, u64 size);
142
143 // Attempts to replace a destination file if one already exists.
144 // Note: Windows uses `file`. Elsewhere uses `source_path`, but `file` is checked for openness.
145 bool Rename(DirectIOFile& file, const std::string& source_path,
146 const std::string& destination_path);
147 // Note: Ditto, only Windows actually uses the file handle. Provide both.
148 bool Delete(DirectIOFile& file, const std::string& filename);
149
150 } // namespace File