forked from RetroAchievements/RAIntegration
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWindowsFileSystem.cpp
More file actions
255 lines (210 loc) · 8.69 KB
/
Copy pathWindowsFileSystem.cpp
File metadata and controls
255 lines (210 loc) · 8.69 KB
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
#include "WindowsFileSystem.hh"
#include "util\Log.hh"
#include "util\Strings.hh"
#include "services\impl\FileTextReader.hh"
#include "services\impl\FileTextWriter.hh"
#undef DeleteFile
#undef MoveFile
namespace ra {
namespace services {
namespace impl {
WindowsFileSystem::WindowsFileSystem() noexcept
{
// determine the home directory from the executable's path
wchar_t sBuffer[MAX_PATH]{};
GetModuleFileNameW(nullptr, sBuffer, MAX_PATH);
PathRemoveFileSpecW(sBuffer);
m_sBaseDirectory = sBuffer;
if (!m_sBaseDirectory.empty())
{
if (m_sBaseDirectory.back() != L'\\')
m_sBaseDirectory.push_back(L'\\');
}
}
GSL_SUPPRESS_F6
const std::wstring& WindowsFileSystem::MakeAbsolute(std::wstring& sBuffer, const std::wstring& sPath) const noexcept
{
if (!PathIsRelativeW(sPath.c_str()))
return sPath;
sBuffer.reserve(m_sBaseDirectory.length() + sPath.length());
sBuffer.assign(m_sBaseDirectory);
sBuffer.append(sPath);
return sBuffer;
}
std::wstring WindowsFileSystem::GetDirectory(const std::wstring& sPath) const
{
const auto nIndex = sPath.find_last_of(L"/\\");
if (nIndex != std::string::npos)
return std::wstring(sPath, 0, nIndex);
return sPath;
}
std::wstring WindowsFileSystem::GetFileName(const std::wstring& sPath) const
{
const auto nIndex = sPath.find_last_of(L"/\\");
if (nIndex != std::string::npos)
return std::wstring(sPath, nIndex + 1);
return sPath;
}
std::wstring WindowsFileSystem::GetExtension(const std::wstring& sPath) const
{
const auto nIndex = sPath.find_last_of(L"/\\.");
if (nIndex != std::string::npos && sPath.at(nIndex) == '.')
return std::wstring(sPath, nIndex + 1);
return L"";
}
std::wstring WindowsFileSystem::RemoveExtension(const std::wstring& sPath) const
{
const auto nIndex = sPath.find_last_of(L"/\\.");
if (nIndex != std::string::npos && sPath.at(nIndex) == '.')
return std::wstring(sPath, 0, nIndex);
return sPath;
}
bool WindowsFileSystem::DirectoryExists(const std::wstring& sDirectory) const noexcept
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sDirectory);
const DWORD nAttrib = GetFileAttributesW(sAbsolutePath.c_str());
return (nAttrib != INVALID_FILE_ATTRIBUTES) && (nAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
bool WindowsFileSystem::CreateDirectory(const std::wstring& sDirectory) const noexcept
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sDirectory);
return (CreateDirectoryW(sAbsolutePath.c_str(), nullptr) != 0);
}
size_t WindowsFileSystem::GetFilesInDirectory(const std::wstring& sDirectory, _Inout_ std::vector<std::wstring>& vResults) const
{
std::wstring sBuffer;
std::wstring sSearchString = MakeAbsolute(sBuffer, sDirectory);
sSearchString += L"\\*";
WIN32_FIND_DATAW ffdFile;
HANDLE hFind = FindFirstFileW(sSearchString.c_str(), &ffdFile);
if (hFind == INVALID_HANDLE_VALUE)
return 0U;
const size_t nInitialSize = vResults.size();
do
{
if (!(ffdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
vResults.emplace_back(ffdFile.cFileName);
} while (FindNextFileW(hFind, &ffdFile) != 0);
FindClose(hFind);
return vResults.size() - nInitialSize;
}
bool WindowsFileSystem::DeleteFile(const std::wstring& sPath) const noexcept
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sPath);
return (DeleteFileW(sAbsolutePath.c_str()) != 0);
}
bool WindowsFileSystem::MoveFile(const std::wstring& sOldPath, const std::wstring& sNewPath) const noexcept
{
std::wstring sBufferNew, sBufferOld;
const auto& sAbsolutePathNew = MakeAbsolute(sBufferNew, sNewPath);
const auto& sAbsolutePathOld = MakeAbsolute(sBufferOld, sOldPath);
return (MoveFileW(sAbsolutePathOld.c_str(), sAbsolutePathNew.c_str()) != 0);
}
bool WindowsFileSystem::CopyFile(const std::wstring& sOldPath, const std::wstring& sNewPath) const noexcept
{
std::wstring sBufferNew, sBufferOld;
const auto& sAbsolutePathNew = MakeAbsolute(sBufferNew, sNewPath);
const auto& sAbsolutePathOld = MakeAbsolute(sBufferOld, sOldPath);
return (CopyFileW(sAbsolutePathOld.c_str(), sAbsolutePathNew.c_str(), FALSE) != 0);
}
int64_t WindowsFileSystem::GetFileSize(const std::wstring& sPath) const
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sPath);
WIN32_FILE_ATTRIBUTE_DATA fadFile;
if (!GetFileAttributesExW(sAbsolutePath.c_str(), GET_FILEEX_INFO_LEVELS::GetFileExInfoStandard, &fadFile))
{
if (GetLastError() != ERROR_FILE_NOT_FOUND && ra::services::ServiceLocator::Exists<ra::services::ILogger>())
{
RA_LOG_ERR("Error %d getting file size: %s", GetLastError(), ra::util::String::Narrow(sPath).c_str());
}
return -1;
}
if (fadFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (ra::services::ServiceLocator::Exists<ra::services::ILogger>())
{
RA_LOG_ERR("File size requested for directory: %s", ra::util::String::Narrow(sPath).c_str());
}
return -1;
}
const LARGE_INTEGER nSize{fadFile.nFileSizeLow, ra::to_signed(fadFile.nFileSizeHigh)};
return nSize.QuadPart;
}
std::chrono::system_clock::time_point WindowsFileSystem::GetLastModified(const std::wstring& sPath) const
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sPath);
WIN32_FILE_ATTRIBUTE_DATA fadFile;
if (!GetFileAttributesExW(sAbsolutePath.c_str(), GET_FILEEX_INFO_LEVELS::GetFileExInfoStandard, &fadFile))
{
RA_LOG_ERR("Error %d getting last modified for file: %s", GetLastError(), ra::util::String::Narrow(sPath).c_str());
return std::chrono::system_clock::time_point();
}
// FILETIME (ftLastWriteTime) is 100-nanosecond intervals since Jan 1 1601. time_t is 1-second intervals since
// Jan 1 1970. Convert from 100-nanosecond intervals to 1-second intervals, then subtract the number of seconds
// between the two dates. See https://www.gamedev.net/forums/topic/565693-converting-filetime-to-time_t-on-windows/
const ULARGE_INTEGER nFileTime{ fadFile.ftLastWriteTime.dwLowDateTime, fadFile.ftLastWriteTime.dwHighDateTime };
const time_t tFileTime{ra::to_signed((nFileTime.QuadPart / 10000000ULL) - 11644473600ULL)};
return std::chrono::system_clock::from_time_t(tFileTime);
}
std::unique_ptr<TextReader> WindowsFileSystem::OpenTextFile(const std::wstring& sPath) const
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sPath);
auto pReader = std::make_unique<FileTextReader>(sAbsolutePath);
if (!pReader->GetFStream().is_open())
{
RA_LOG_INFO("Failed to open \"%s\": %d", ra::util::String::Narrow(sPath).c_str(), errno);
return std::unique_ptr<TextReader>();
}
return std::unique_ptr<TextReader>(pReader.release());
}
std::unique_ptr<TextWriter> WindowsFileSystem::CreateTextFile(const std::wstring& sPath) const
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sPath);
auto pWriter = std::make_unique<FileTextWriter>(sAbsolutePath);
if (!pWriter->GetFStream().is_open())
{
RA_LOG_WARN("Failed to create \"%s\": %d", ra::util::String::Narrow(sPath).c_str(), errno);
return std::unique_ptr<TextWriter>();
}
return std::unique_ptr<TextWriter>(pWriter.release());
}
std::unique_ptr<TextWriter> WindowsFileSystem::AppendTextFile(const std::wstring& sPath) const
{
std::wstring sBuffer;
const auto& sAbsolutePath = MakeAbsolute(sBuffer, sPath);
// cannot use std::ios::app, or the SetPosition method doesn't work
// have to specify std::ios::in or the previous contents are lost
auto pWriter = std::make_unique<FileTextWriter>(sAbsolutePath, std::ios::ate | std::ios::in | std::ios::out);
const gsl::not_null<const std::ofstream*> oStream{gsl::make_not_null(&pWriter->GetFStream())};
if (!oStream->is_open())
{
// failed to open the file - try creating it
std::ofstream file(sPath, std::ios::out);
if (file.is_open())
{
// create succeeded, reopen it
file.close();
pWriter = std::make_unique<FileTextWriter>(sPath, std::ios::in | std::ios::out);
}
else
{
// create failed
if (ra::services::ServiceLocator::Exists<ra::services::ILogger>())
{
RA_LOG_WARN("Failed to open \"%s\" for append: %d", ra::util::String::Narrow(sPath).c_str(), errno);
}
return std::unique_ptr<TextWriter>();
}
}
return std::unique_ptr<TextWriter>(pWriter.release());
}
} // namespace impl
} // namespace services
} // namespace ra