CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/File/DirListing.cpp
Views: 1401
1
#include "ppsspp_config.h"
2
3
#if PPSSPP_PLATFORM(WINDOWS)
4
#define WIN32_LEAN_AND_MEAN
5
#include <Windows.h>
6
#include <direct.h>
7
#if PPSSPP_PLATFORM(UWP)
8
#include <fileapifromapp.h>
9
#include <UWP/UWPHelpers/StorageManager.h>
10
#endif
11
#else
12
#include <strings.h>
13
#include <dirent.h>
14
#include <unistd.h>
15
#include <errno.h>
16
#endif
17
#include <cstring>
18
#include <string>
19
#include <set>
20
#include <algorithm>
21
#include <cstdio>
22
#include <sys/stat.h>
23
#include <ctype.h>
24
25
#include "Common/Data/Encoding/Utf8.h"
26
#include "Common/StringUtils.h"
27
#include "Common/Net/URL.h"
28
#include "Common/File/DirListing.h"
29
#include "Common/File/FileUtil.h"
30
#include "Common/File/AndroidStorage.h"
31
32
#if !defined(__linux__) && !defined(_WIN32) && !defined(__QNX__)
33
#define stat64 stat
34
#endif
35
36
#ifdef HAVE_LIBNX
37
// Far from optimal, but I guess it works...
38
#define fseeko fseek
39
#define ftello ftell
40
#define fileno
41
#endif // HAVE_LIBNX
42
43
namespace File {
44
45
#if PPSSPP_PLATFORM(WINDOWS)
46
static uint64_t FiletimeToStatTime(FILETIME ft) {
47
const int windowsTickResolution = 10000000;
48
const int64_t secToUnixEpoch = 11644473600LL;
49
int64_t ticks = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
50
return (int64_t)(ticks / windowsTickResolution - secToUnixEpoch);
51
};
52
#endif
53
54
bool GetFileInfo(const Path &path, FileInfo * fileInfo) {
55
switch (path.Type()) {
56
case PathType::NATIVE:
57
break; // OK
58
case PathType::CONTENT_URI:
59
return Android_GetFileInfo(path.ToString(), fileInfo);
60
default:
61
return false;
62
}
63
64
// TODO: Expand relative paths?
65
fileInfo->fullName = path;
66
67
#if PPSSPP_PLATFORM(WINDOWS)
68
WIN32_FILE_ATTRIBUTE_DATA attrs;
69
#if PPSSPP_PLATFORM(UWP)
70
if (!GetFileAttributesExFromAppW(path.ToWString().c_str(), GetFileExInfoStandard, &attrs)) {
71
#else
72
if (!GetFileAttributesExW(path.ToWString().c_str(), GetFileExInfoStandard, &attrs)) {
73
#endif
74
fileInfo->size = 0;
75
fileInfo->isDirectory = false;
76
fileInfo->exists = false;
77
return false;
78
}
79
fileInfo->size = (uint64_t)attrs.nFileSizeLow | ((uint64_t)attrs.nFileSizeHigh << 32);
80
fileInfo->isDirectory = (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
81
fileInfo->isWritable = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
82
fileInfo->exists = true;
83
fileInfo->atime = FiletimeToStatTime(attrs.ftLastAccessTime);
84
fileInfo->mtime = FiletimeToStatTime(attrs.ftLastWriteTime);
85
fileInfo->ctime = FiletimeToStatTime(attrs.ftCreationTime);
86
if (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
87
fileInfo->access = 0444; // Read
88
} else {
89
fileInfo->access = 0666; // Read/Write
90
}
91
if (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
92
fileInfo->access |= 0111; // Execute
93
}
94
#else
95
96
#if (defined __ANDROID__) && (__ANDROID_API__ < 21)
97
struct stat file_info;
98
int result = stat(path.c_str(), &file_info);
99
#else
100
struct stat64 file_info;
101
int result = stat64(path.c_str(), &file_info);
102
#endif
103
if (result < 0) {
104
fileInfo->exists = false;
105
return false;
106
}
107
108
fileInfo->isDirectory = S_ISDIR(file_info.st_mode);
109
fileInfo->isWritable = false;
110
fileInfo->size = file_info.st_size;
111
fileInfo->exists = true;
112
fileInfo->atime = file_info.st_atime;
113
fileInfo->mtime = file_info.st_mtime;
114
fileInfo->ctime = file_info.st_ctime;
115
fileInfo->access = file_info.st_mode & 0x1ff;
116
// HACK: approximation
117
if (file_info.st_mode & 0200)
118
fileInfo->isWritable = true;
119
#endif
120
return true;
121
}
122
123
bool GetModifTime(const Path &filename, tm & return_time) {
124
memset(&return_time, 0, sizeof(return_time));
125
FileInfo info;
126
if (GetFileInfo(filename, &info)) {
127
time_t t = info.mtime;
128
localtime_r((time_t*)&t, &return_time);
129
return true;
130
} else {
131
return false;
132
}
133
}
134
135
bool FileInfo::operator <(const FileInfo & other) const {
136
if (isDirectory && !other.isDirectory)
137
return true;
138
else if (!isDirectory && other.isDirectory)
139
return false;
140
if (strcasecmp(name.c_str(), other.name.c_str()) < 0)
141
return true;
142
else
143
return false;
144
}
145
146
std::vector<File::FileInfo> ApplyFilter(std::vector<File::FileInfo> files, const char *filter) {
147
std::set<std::string> filters;
148
if (filter) {
149
std::string tmp;
150
while (*filter) {
151
if (*filter == ':') {
152
filters.emplace("." + tmp);
153
tmp.clear();
154
} else {
155
tmp.push_back(*filter);
156
}
157
filter++;
158
}
159
if (!tmp.empty())
160
filters.emplace("." + tmp);
161
}
162
163
auto pred = [&](const File::FileInfo &info) {
164
if (info.isDirectory || !filter)
165
return false;
166
std::string ext = info.fullName.GetFileExtension();
167
return filters.find(ext) == filters.end();
168
};
169
files.erase(std::remove_if(files.begin(), files.end(), pred), files.end());
170
return files;
171
}
172
173
bool GetFilesInDir(const Path &directory, std::vector<FileInfo> *files, const char *filter, int flags) {
174
if (directory.Type() == PathType::CONTENT_URI) {
175
bool exists = false;
176
std::vector<File::FileInfo> fileList = Android_ListContentUri(directory.ToString(), &exists);
177
*files = ApplyFilter(fileList, filter);
178
std::sort(files->begin(), files->end());
179
return exists;
180
}
181
182
std::set<std::string> filters;
183
if (filter) {
184
std::string tmp;
185
while (*filter) {
186
if (*filter == ':') {
187
filters.insert(tmp);
188
tmp.clear();
189
} else {
190
tmp.push_back(*filter);
191
}
192
filter++;
193
}
194
if (!tmp.empty())
195
filters.insert(tmp);
196
}
197
198
#if PPSSPP_PLATFORM(WINDOWS)
199
if (directory.IsRoot()) {
200
// Special path that means root of file system.
201
std::vector<std::string> drives = File::GetWindowsDrives();
202
for (auto drive = drives.begin(); drive != drives.end(); ++drive) {
203
if (*drive == "A:/" || *drive == "B:/")
204
continue;
205
File::FileInfo fake;
206
fake.fullName = Path(*drive);
207
fake.name = *drive;
208
fake.isDirectory = true;
209
fake.exists = true;
210
fake.size = 0;
211
fake.isWritable = false;
212
files->push_back(fake);
213
}
214
return files->size();
215
}
216
// Find the first file in the directory.
217
WIN32_FIND_DATA ffd;
218
std::wstring wpath = directory.ToWString();
219
wpath += L"\\*";
220
#if PPSSPP_PLATFORM(UWP)
221
HANDLE hFind = FindFirstFileExFromAppW(wpath.c_str(), FindExInfoStandard, &ffd, FindExSearchNameMatch, NULL, 0);
222
#else
223
HANDLE hFind = FindFirstFileEx(wpath.c_str(), FindExInfoStandard, &ffd, FindExSearchNameMatch, NULL, 0);
224
#endif
225
if (hFind == INVALID_HANDLE_VALUE) {
226
#if PPSSPP_PLATFORM(UWP)
227
// This step just to avoid empty results by adding fake folders
228
// it will help also to navigate back between selected folder
229
// we must ignore this function for any request other than UI navigation
230
if (GetFakeFolders(directory, files, filter, filters))
231
return true;
232
#endif
233
return false;
234
}
235
do {
236
const std::string virtualName = ConvertWStringToUTF8(ffd.cFileName);
237
// check for "." and ".."
238
if (!(flags & GETFILES_GET_NAVIGATION_ENTRIES) && (virtualName == "." || virtualName == ".."))
239
continue;
240
// Remove dotfiles (optional with flag.)
241
if (!(flags & GETFILES_GETHIDDEN)) {
242
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)
243
continue;
244
}
245
246
FileInfo info;
247
info.name = virtualName;
248
info.fullName = directory / virtualName;
249
info.exists = true;
250
info.size = ((uint64_t)ffd.nFileSizeHigh << 32) | ffd.nFileSizeLow;
251
info.isDirectory = (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
252
info.isWritable = (ffd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
253
info.atime = FiletimeToStatTime(ffd.ftLastAccessTime);
254
info.mtime = FiletimeToStatTime(ffd.ftLastWriteTime);
255
info.ctime = FiletimeToStatTime(ffd.ftCreationTime);
256
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
257
info.access = 0444; // Read
258
} else {
259
info.access = 0666; // Read/Write
260
}
261
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
262
info.access |= 0111; // Execute
263
}
264
if (!info.isDirectory) {
265
std::string ext = info.fullName.GetFileExtension();
266
if (!ext.empty()) {
267
ext = ext.substr(1); // Remove the dot.
268
if (filter && filters.find(ext) == filters.end()) {
269
continue;
270
}
271
}
272
}
273
files->push_back(info);
274
} while (FindNextFile(hFind, &ffd) != 0);
275
FindClose(hFind);
276
#else
277
struct dirent *result = NULL;
278
DIR *dirp = opendir(directory.c_str());
279
if (!dirp)
280
return false;
281
while ((result = readdir(dirp))) {
282
const std::string virtualName(result->d_name);
283
// check for "." and ".."
284
if (!(flags & GETFILES_GET_NAVIGATION_ENTRIES) && (virtualName == "." || virtualName == ".."))
285
continue;
286
287
// Remove dotfiles (optional with flag.)
288
if (!(flags & GETFILES_GETHIDDEN)) {
289
if (virtualName[0] == '.')
290
continue;
291
}
292
293
// Let's just reuse GetFileInfo. We're calling stat anyway to get isDirectory information.
294
Path fullName = directory / virtualName;
295
296
FileInfo info;
297
info.name = virtualName;
298
if (!GetFileInfo(fullName, &info)) {
299
continue;
300
}
301
if (!info.isDirectory) {
302
std::string ext = info.fullName.GetFileExtension();
303
if (!ext.empty()) {
304
ext = ext.substr(1); // Remove the dot.
305
if (filter && filters.find(ext) == filters.end()) {
306
continue;
307
}
308
}
309
}
310
files->push_back(info);
311
}
312
closedir(dirp);
313
#endif
314
std::sort(files->begin(), files->end());
315
return true;
316
}
317
318
#if PPSSPP_PLATFORM(WINDOWS)
319
// Returns a vector with the device names
320
std::vector<std::string> GetWindowsDrives()
321
{
322
std::vector<std::string> drives;
323
324
#if PPSSPP_PLATFORM(UWP)
325
DWORD logicaldrives = GetLogicalDrives();
326
for (int i = 0; i < 26; i++)
327
{
328
if (logicaldrives & (1 << i))
329
{
330
CHAR driveName[] = { (CHAR)(TEXT('A') + i), TEXT(':'), TEXT('\\'), TEXT('\0') };
331
std::string str(driveName);
332
drives.push_back(driveName);
333
}
334
}
335
return drives;
336
#else
337
const DWORD buffsize = GetLogicalDriveStrings(0, NULL);
338
std::vector<wchar_t> buff(buffsize);
339
if (GetLogicalDriveStrings(buffsize, buff.data()) == buffsize - 1)
340
{
341
auto drive = buff.data();
342
while (*drive)
343
{
344
std::string str(ConvertWStringToUTF8(drive));
345
str.pop_back(); // we don't want the final backslash
346
str += "/";
347
drives.push_back(str);
348
349
// advance to next drive
350
while (*drive++) {}
351
}
352
}
353
return drives;
354
#endif // PPSSPP_PLATFORM(UWP)
355
}
356
#endif // PPSSPP_PLATFORM(WINDOWS)
357
358
} // namespace File
359
360