forked from Rezonality/zep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.cpp
287 lines (247 loc) · 6.97 KB
/
filesystem.cpp
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
#include "zep/filesystem.h"
#include "zep/editor.h" // FOR ZEP_UNUSED
#include <fstream>
#include "zep/mcommon/logger.h"
#include "zep/mcommon/string/stringutils.h"
#undef ERROR
#if defined(ZEP_FEATURE_CPP_FILE_SYSTEM)
#include <filesystem>
#ifdef __APPLE__
namespace cpp_fs = std::__fs::filesystem;
#else
namespace cpp_fs = std::filesystem;
#endif
namespace Zep
{
ZepFileSystemCPP::ZepFileSystemCPP(const fs::path& configPath)
{
// Use the config path
m_configPath = configPath;
m_workingDirectory = fs::path(cpp_fs::current_path().string());
// Didn't find the config path, try the working directory
if (!Exists(m_configPath))
{
m_configPath = m_workingDirectory;
}
ZLOG(INFO, "Config Dir: " << m_configPath.string());
ZLOG(INFO, "Working Dir: " << m_workingDirectory.string());
}
ZepFileSystemCPP::~ZepFileSystemCPP()
{
}
void ZepFileSystemCPP::SetWorkingDirectory(const fs::path& path)
{
if (!IsDirectory(path))
{
return;
}
// Set the file system's current working directory too.
cpp_fs::current_path(path.string());
m_workingDirectory = path;
}
const fs::path& ZepFileSystemCPP::GetWorkingDirectory() const
{
return m_workingDirectory;
}
fs::path ZepFileSystemCPP::GetConfigPath() const
{
return m_configPath;
}
bool ZepFileSystemCPP::MakeDirectories(const fs::path& path)
{
return cpp_fs::create_directories(path.c_str());
}
bool ZepFileSystemCPP::IsDirectory(const fs::path& path) const
{
if (!Exists(path))
{
return false;
}
return cpp_fs::is_directory(path.string());
}
bool ZepFileSystemCPP::IsReadOnly(const fs::path& path) const
{
auto perms = cpp_fs::status(path.string()).permissions();
if ((perms & cpp_fs::perms::owner_write) == cpp_fs::perms::owner_write)
{
return false;
}
return true;
}
std::string ZepFileSystemCPP::Read(const fs::path& fileName)
{
std::ifstream in(fileName, std::ios::in | std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(size_t(in.tellg()));
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return (contents);
}
else
{
ZLOG(ERROR, "File Not Found: " << fileName.string());
}
return std::string();
}
bool ZepFileSystemCPP::Write(const fs::path& fileName, const void* pData, size_t size)
{
FILE* pFile;
pFile = fopen(fileName.string().c_str(), "wb");
if (!pFile)
{
return false;
}
// Valid to open/close with size == 0, which will truncate
if (size != 0)
{
fwrite(pData, sizeof(uint8_t), size, pFile);
}
fclose(pFile);
return true;
}
void ZepFileSystemCPP::ScanDirectory(const fs::path& path, std::function<bool(const fs::path& path, bool& dont_recurse)> fnScan) const
{
for (auto itr = cpp_fs::recursive_directory_iterator(path.string());
itr != cpp_fs::recursive_directory_iterator();
itr++)
{
auto p = fs::path(itr->path().string());
bool recurse = true;
if (!fnScan(p, recurse))
return;
if (!recurse && itr.recursion_pending())
{
itr.disable_recursion_pending();
}
}
}
bool ZepFileSystemCPP::Exists(const fs::path& path) const
{
try
{
return cpp_fs::exists(path.string());
}
catch (cpp_fs::filesystem_error& err)
{
ZEP_UNUSED(err);
ZLOG(ERROR, "Exception: " << err.what());
return false;
}
}
bool ZepFileSystemCPP::Equivalent(const fs::path& path1, const fs::path& path2) const
{
try
{
// The below API expects existing files! Best we can do is direct compare of paths
if (!cpp_fs::exists(path1.string()) || !cpp_fs::exists(path2.string()))
{
return Canonical(path1).string() == Canonical(path2).string();
}
return cpp_fs::equivalent(path1.string(), path2.string());
}
catch (cpp_fs::filesystem_error& err)
{
ZEP_UNUSED(err);
ZLOG(ERROR, "Exception: " << err.what());
return path1 == path2;
}
}
fs::path ZepFileSystemCPP::Canonical(const fs::path& path) const
{
try
{
#ifdef __unix__
// TODO: Remove when unix doesn't need <experimental/filesystem>
// I can't remember why weakly_connical is used....
return fs::path(cpp_fs::canonical(path.string()).string());
#else
return fs::path(cpp_fs::weakly_canonical(path.string()).string());
#endif
}
catch (cpp_fs::filesystem_error& err)
{
ZEP_UNUSED(err);
ZLOG(ERROR, "Exception: " << err.what());
return path;
}
}
fs::path ZepFileSystemCPP::GetSearchRoot(const fs::path& start, bool& foundGit) const
{
foundGit = false;
auto findStartPath = [&](const fs::path& startPath) {
if (!startPath.empty())
{
auto testPath = startPath;
if (!IsDirectory(testPath))
{
testPath = testPath.parent_path();
}
if (!(m_flags & ZepFileSystemFlags::SearchGitRoot))
{
return testPath;
}
while (!testPath.empty() && IsDirectory(testPath))
{
foundGit = false;
// Look in this dir
ScanDirectory(testPath, [&](const fs::path& p, bool& recurse) -> bool {
// Not looking at sub folders
recurse = false;
// Found the .git repo
if (p.extension() == ".git" && IsDirectory(p))
{
foundGit = true;
// Quit search
return false;
}
return true;
});
// If found, return it as the path we need
if (foundGit)
{
return testPath;
}
if (!testPath.has_relative_path())
{
break;
}
testPath = testPath.parent_path();
}
// Didn't find a sensible search root, so start at the parent folder of the start path if it is a file
if (!IsDirectory(startPath))
{
if (IsDirectory(startPath.parent_path()))
{
return startPath.parent_path();
}
}
}
return startPath;
};
fs::path workingDir = GetWorkingDirectory();
auto startPath = findStartPath(start);
if (startPath.empty())
{
startPath = findStartPath(workingDir);
if (startPath.empty())
{
startPath = GetWorkingDirectory();
}
}
// Failure case, just use current path
if (startPath.empty())
{
startPath = start;
}
return startPath;
}
void ZepFileSystemCPP::SetFlags(uint32_t flags)
{
m_flags = flags;
}
} // namespace Zep
#endif // CPP_FILESYSTEM