-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathfilesystem.cpp
More file actions
177 lines (145 loc) · 5.33 KB
/
Copy pathfilesystem.cpp
File metadata and controls
177 lines (145 loc) · 5.33 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
#include <filesystem.hpp>
#include <os.hpp>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(_WIN32)
#include <direct.h>
#include <io.h>
#include <lmcons.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <windows.h>
#elif defined(__HAIKU__)
#include <FindDirectory.h>
#include <Path.h>
#else
#include <dirent.h>
#include <pwd.h>
#include <unistd.h>
#endif
nonstd::expected<void, std::string> FileSystem::createDirectory(const std::string &path) {
std::string p = path;
std::replace(p.begin(), p.end(), '\\', '/');
size_t pos = 0;
while ((pos = p.find('/', pos)) != std::string::npos) {
std::string dir = p.substr(0, pos++);
if (dir.empty()) continue;
if (dir == OS::getFilesystemRootPrefix()) continue; // Fixes DS but hopefully doesn't negatively affect other platforms????
struct stat st;
if (stat(dir.c_str(), &st) != 0) {
#ifdef _WIN32
if (_mkdir(dir.c_str()) != 0 && errno != EEXIST) {
#else
if (mkdir(dir.c_str(), 0777) != 0 && errno != EEXIST) {
#endif
return nonstd::make_unexpected("Failed to create directory, " + dir + ", " + std::to_string(errno));
}
}
}
return {};
}
void FileSystem::renameFile(const std::string &originalPath, const std::string &newPath) {
rename(originalPath.c_str(), newPath.c_str());
}
nonstd::expected<void, std::string> FileSystem::removeDirectory(const std::string &path) {
struct stat st;
if (stat(path.c_str(), &st) != 0) {
return nonstd::make_unexpected("Directory not found, " + path + ", " + std::to_string(errno));
}
if (!(st.st_mode & S_IFDIR)) {
return nonstd::make_unexpected("Not a directory, " + path + ", " + std::to_string(errno));
}
#ifdef _WIN32
std::wstring wpath(path.size(), L' ');
wpath.resize(std::mbstowcs(&wpath[0], path.c_str(), path.size()) + 1);
SHFILEOPSTRUCTW options = {0};
options.wFunc = FO_DELETE;
options.pFrom = wpath.c_str();
options.fFlags = FOF_NO_UI | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
if (SHFileOperationW(&options) != 0) {
return nonstd::make_unexpected("Directory removal failed, " + path + ", " + std::to_string(errno));
}
#else
DIR *dir = opendir(path.c_str());
if (dir == nullptr) {
return nonstd::make_unexpected("Directory open failed, " + path + ", " + std::to_string(errno));
}
struct dirent *entry;
while ((entry = readdir(dir)) != nullptr) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
std::string fullPath = path + "/" + entry->d_name;
struct stat entrySt;
if (stat(fullPath.c_str(), &entrySt) == 0) {
if (S_ISDIR(entrySt.st_mode)) {
auto potentialError = removeDirectory(fullPath);
if (!potentialError.has_value()) return nonstd::make_unexpected(potentialError.error());
} else {
if (remove(fullPath.c_str()) != 0) {
return nonstd::make_unexpected("File removal failed, " + fullPath + ", " + std::to_string(errno));
}
}
}
}
closedir(dir);
if (rmdir(path.c_str()) != 0) {
return nonstd::make_unexpected("Directory removal failed, " + path + ", " + std::to_string(errno));
}
#endif
return {};
}
bool FileSystem::fileExists(const std::string &path) {
struct stat buffer;
return (stat(path.c_str(), &buffer) == 0);
}
std::string FileSystem::parentPath(const std::string &path) {
size_t pos = path.find_last_of("/\\");
if (std::string::npos != pos)
return path.substr(0, pos);
return "";
}
nonstd::expected<std::vector<std::string>, std::string> FileSystem::listDirectory(const std::string &path) {
std::vector<std::string> files;
#if defined(_WIN32)
std::string searchPath = path;
if (searchPath.empty()) {
searchPath = ".";
}
if (searchPath.back() != '/' && searchPath.back() != '\\') {
searchPath += "/*";
} else {
searchPath += "*";
}
std::wstring wsearchPath(searchPath.size(), L' ');
wsearchPath.resize(std::mbstowcs(&wsearchPath[0], searchPath.c_str(), searchPath.size()));
WIN32_FIND_DATAW findData;
HANDLE hFind = FindFirstFileW(wsearchPath.c_str(), &findData);
if (hFind == INVALID_HANDLE_VALUE) {
return nonstd::make_unexpected("Failed to open directory, " + path + ", " + std::to_string(GetLastError()));
}
do {
int size_needed = WideCharToMultiByte(CP_UTF8, 0, findData.cFileName, -1, NULL, 0, NULL, NULL);
std::string fileName(size_needed - 1, 0);
WideCharToMultiByte(CP_UTF8, 0, findData.cFileName, -1, &fileName[0], size_needed, NULL, NULL);
if (fileName != "." && fileName != "..") {
files.push_back(fileName);
}
} while (FindNextFileW(hFind, &findData) != 0);
FindClose(hFind);
#else
DIR *dir = opendir(path.c_str());
if (dir == nullptr) {
return nonstd::make_unexpected("Failed to open directory, " + path + ", " + std::to_string(errno));
}
struct dirent *entry;
while ((entry = readdir(dir)) != nullptr) {
const std::string fileName = entry->d_name;
if (fileName != "." && fileName != "..") {
files.push_back(fileName);
}
}
closedir(dir);
#endif
return files;
}