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