2017-02-24 19:26:38 +00:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
2012-03-24 22:39:19 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
2012-12-01 09:35:55 +00:00
|
|
|
#include <direct.h>
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2020-03-04 06:53:03 +00:00
|
|
|
#include <strings.h>
|
2012-03-24 22:39:19 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
2012-12-13 04:39:44 +00:00
|
|
|
#include <errno.h>
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
2012-11-07 17:29:35 +00:00
|
|
|
#include <cstring>
|
2012-03-24 22:39:19 +00:00
|
|
|
#include <string>
|
2012-10-31 12:34:27 +00:00
|
|
|
#include <set>
|
2012-11-01 08:45:27 +00:00
|
|
|
#include <algorithm>
|
2013-07-04 08:37:16 +00:00
|
|
|
#include <cstdio>
|
2012-10-31 12:34:27 +00:00
|
|
|
#include <sys/stat.h>
|
2012-10-31 19:42:43 +00:00
|
|
|
#include <ctype.h>
|
2012-10-31 12:34:27 +00:00
|
|
|
|
2020-10-01 11:05:04 +00:00
|
|
|
#include "Common/Data/Encoding/Utf8.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
#include "Common/StringUtils.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/File/DirListing.h"
|
2021-04-25 18:38:22 +00:00
|
|
|
#include "Common/File/FileUtil.h"
|
2020-09-29 10:19:22 +00:00
|
|
|
|
2015-05-25 17:11:33 +00:00
|
|
|
#if !defined(__linux__) && !defined(_WIN32) && !defined(__QNX__)
|
2012-11-19 00:36:07 +00:00
|
|
|
#define stat64 stat
|
|
|
|
#endif
|
|
|
|
|
2020-03-04 06:53:03 +00:00
|
|
|
#ifdef HAVE_LIBNX
|
|
|
|
// Far from optimal, but I guess it works...
|
|
|
|
#define fseeko fseek
|
|
|
|
#define ftello ftell
|
|
|
|
#define fileno
|
|
|
|
#endif // HAVE_LIBNX
|
|
|
|
|
2021-04-25 18:38:22 +00:00
|
|
|
namespace File {
|
2012-12-01 09:35:55 +00:00
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
bool GetFileInfo(const Path &path, FileInfo * fileInfo) {
|
|
|
|
switch (path.Type()) {
|
|
|
|
case PathType::NATIVE:
|
|
|
|
break; // OK
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-01 09:35:55 +00:00
|
|
|
// TODO: Expand relative paths?
|
2021-05-05 23:31:38 +00:00
|
|
|
fileInfo->fullName = path.ToString();
|
2012-12-01 09:35:55 +00:00
|
|
|
|
2012-10-30 15:23:08 +00:00
|
|
|
#ifdef _WIN32
|
2021-04-25 18:52:29 +00:00
|
|
|
auto FiletimeToStatTime = [](FILETIME ft) {
|
|
|
|
const int windowsTickResolution = 10000000;
|
|
|
|
const int64_t secToUnixEpoch = 11644473600LL;
|
|
|
|
int64_t ticks = ((uint64_t)ft.dwHighDateTime << 32) | ft.dwLowDateTime;
|
|
|
|
return (int64_t)(ticks / windowsTickResolution - secToUnixEpoch);
|
|
|
|
};
|
|
|
|
|
2013-04-13 19:22:03 +00:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA attrs;
|
2021-05-05 23:31:38 +00:00
|
|
|
if (!GetFileAttributesExW(path.ToWString().c_str(), GetFileExInfoStandard, &attrs)) {
|
2013-04-21 15:13:07 +00:00
|
|
|
fileInfo->size = 0;
|
|
|
|
fileInfo->isDirectory = false;
|
2013-06-08 15:43:27 +00:00
|
|
|
fileInfo->exists = false;
|
2013-04-21 15:13:07 +00:00
|
|
|
return false;
|
2013-02-26 22:07:30 +00:00
|
|
|
}
|
2013-04-21 15:13:07 +00:00
|
|
|
fileInfo->size = (uint64_t)attrs.nFileSizeLow | ((uint64_t)attrs.nFileSizeHigh << 32);
|
2013-04-13 19:22:03 +00:00
|
|
|
fileInfo->isDirectory = (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
fileInfo->isWritable = (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == 0;
|
2013-06-08 15:43:27 +00:00
|
|
|
fileInfo->exists = true;
|
2021-04-25 18:52:29 +00:00
|
|
|
fileInfo->atime = FiletimeToStatTime(attrs.ftLastAccessTime);
|
|
|
|
fileInfo->mtime = FiletimeToStatTime(attrs.ftLastWriteTime);
|
|
|
|
fileInfo->ctime = FiletimeToStatTime(attrs.ftCreationTime);
|
|
|
|
if (attrs.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
|
|
|
|
fileInfo->access = 0444; // Read
|
|
|
|
} else {
|
|
|
|
fileInfo->access = 0666; // Read/Write
|
|
|
|
}
|
|
|
|
if (attrs.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
|
|
|
fileInfo->access |= 0111; // Execute
|
|
|
|
}
|
2012-10-30 15:23:08 +00:00
|
|
|
#else
|
2012-10-28 10:37:10 +00:00
|
|
|
|
2017-08-18 08:53:47 +00:00
|
|
|
#if (defined __ANDROID__) && (__ANDROID_API__ < 21)
|
2017-07-03 14:16:25 +00:00
|
|
|
struct stat file_info;
|
2021-05-05 23:31:38 +00:00
|
|
|
int result = stat(path.c_str(), &file_info);
|
2017-07-03 14:16:25 +00:00
|
|
|
#else
|
|
|
|
struct stat64 file_info;
|
2021-05-05 23:31:38 +00:00
|
|
|
int result = stat64(path.c_str(), &file_info);
|
2017-07-03 14:16:25 +00:00
|
|
|
#endif
|
2012-10-28 10:37:10 +00:00
|
|
|
if (result < 0) {
|
2013-06-08 15:43:27 +00:00
|
|
|
fileInfo->exists = false;
|
2012-10-28 10:37:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-01 09:35:55 +00:00
|
|
|
fileInfo->isDirectory = S_ISDIR(file_info.st_mode);
|
|
|
|
fileInfo->isWritable = false;
|
2013-02-26 22:07:30 +00:00
|
|
|
fileInfo->size = file_info.st_size;
|
2013-06-08 15:43:27 +00:00
|
|
|
fileInfo->exists = true;
|
2021-04-25 18:52:29 +00:00
|
|
|
fileInfo->atime = file_info.st_atime;
|
|
|
|
fileInfo->mtime = file_info.st_mtime;
|
|
|
|
fileInfo->ctime = file_info.st_ctime;
|
|
|
|
fileInfo->access = file_info.st_mode & 0x1ff;
|
2012-12-01 09:35:55 +00:00
|
|
|
// HACK: approximation
|
|
|
|
if (file_info.st_mode & 0200)
|
|
|
|
fileInfo->isWritable = true;
|
2012-10-30 15:23:08 +00:00
|
|
|
#endif
|
2012-12-01 09:35:55 +00:00
|
|
|
return true;
|
2012-10-28 10:37:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
bool GetModifTime(const Path &filename, tm & return_time) {
|
2021-04-25 18:52:29 +00:00
|
|
|
memset(&return_time, 0, sizeof(return_time));
|
|
|
|
FileInfo info;
|
2021-05-05 23:31:38 +00:00
|
|
|
if (GetFileInfo(filename, &info)) {
|
2021-04-25 18:52:29 +00:00
|
|
|
time_t t = info.mtime;
|
|
|
|
localtime_r((time_t*)&t, &return_time);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2012-10-31 12:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 18:48:47 +00:00
|
|
|
bool FileInfo::operator <(const FileInfo & other) const {
|
2013-07-04 08:37:16 +00:00
|
|
|
if (isDirectory && !other.isDirectory)
|
|
|
|
return true;
|
|
|
|
else if (!isDirectory && other.isDirectory)
|
|
|
|
return false;
|
|
|
|
if (strcasecmp(name.c_str(), other.name.c_str()) < 0)
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
size_t GetFilesInDir(const Path &directory, std::vector<FileInfo> * files, const char *filter, int flags) {
|
2012-10-31 12:23:16 +00:00
|
|
|
size_t foundEntries = 0;
|
2012-10-31 12:34:27 +00:00
|
|
|
std::set<std::string> filters;
|
|
|
|
if (filter) {
|
2019-06-27 08:37:49 +00:00
|
|
|
std::string tmp;
|
2012-10-31 12:34:27 +00:00
|
|
|
while (*filter) {
|
|
|
|
if (*filter == ':') {
|
2019-06-27 08:37:49 +00:00
|
|
|
filters.insert(std::move(tmp));
|
2021-05-11 04:28:51 +00:00
|
|
|
tmp.clear();
|
2012-10-31 12:34:27 +00:00
|
|
|
} else {
|
|
|
|
tmp.push_back(*filter);
|
|
|
|
}
|
|
|
|
filter++;
|
|
|
|
}
|
2019-06-27 08:37:49 +00:00
|
|
|
if (!tmp.empty())
|
|
|
|
filters.insert(std::move(tmp));
|
2012-10-31 12:34:27 +00:00
|
|
|
}
|
2012-03-24 22:39:19 +00:00
|
|
|
#ifdef _WIN32
|
2012-10-31 12:23:16 +00:00
|
|
|
// Find the first file in the directory.
|
|
|
|
WIN32_FIND_DATA ffd;
|
2021-05-05 23:31:38 +00:00
|
|
|
HANDLE hFind = FindFirstFileEx((directory.ToWString() + L"\\*").c_str(), FindExInfoStandard, &ffd, FindExSearchNameMatch, NULL, 0);
|
2012-10-31 12:23:16 +00:00
|
|
|
if (hFind == INVALID_HANDLE_VALUE) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// windows loop
|
|
|
|
do
|
|
|
|
{
|
2013-08-26 16:59:08 +00:00
|
|
|
const std::string virtualName = ConvertWStringToUTF8(ffd.cFileName);
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2012-11-14 15:43:33 +00:00
|
|
|
struct dirent *result = NULL;
|
2012-03-24 22:39:19 +00:00
|
|
|
|
2013-06-27 14:20:18 +00:00
|
|
|
//std::string directoryWithSlash = directory;
|
|
|
|
//if (directoryWithSlash.back() != '/')
|
|
|
|
// directoryWithSlash += "/";
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
DIR *dirp = opendir(directory.c_str());
|
2012-10-31 12:23:16 +00:00
|
|
|
if (!dirp)
|
|
|
|
return 0;
|
|
|
|
// non windows loop
|
2019-01-08 11:29:59 +00:00
|
|
|
while ((result = readdir(dirp)))
|
2012-10-31 12:23:16 +00:00
|
|
|
{
|
|
|
|
const std::string virtualName(result->d_name);
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
2012-10-31 12:23:16 +00:00
|
|
|
// check for "." and ".."
|
2019-06-27 08:37:49 +00:00
|
|
|
if (virtualName == "." || virtualName == "..")
|
2012-10-31 12:23:16 +00:00
|
|
|
continue;
|
2012-10-28 10:37:10 +00:00
|
|
|
|
2015-11-16 06:36:13 +00:00
|
|
|
// Remove dotfiles (optional with flag.)
|
2019-06-27 08:37:49 +00:00
|
|
|
if (!(flags & GETFILES_GETHIDDEN))
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != 0)
|
|
|
|
continue;
|
|
|
|
#else
|
|
|
|
if (virtualName[0] == '.')
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
}
|
2012-10-31 12:34:27 +00:00
|
|
|
|
2012-10-31 12:23:16 +00:00
|
|
|
FileInfo info;
|
|
|
|
info.name = virtualName;
|
2021-05-05 23:31:38 +00:00
|
|
|
|
|
|
|
// It's OK not to use Path /-concat here.
|
|
|
|
std::string dir = directory.ToString();
|
2013-10-14 17:15:32 +00:00
|
|
|
|
|
|
|
// Only append a slash if there isn't one on the end.
|
|
|
|
size_t lastSlash = dir.find_last_of("/");
|
|
|
|
if (lastSlash != (dir.length() - 1))
|
|
|
|
dir.append("/");
|
|
|
|
|
|
|
|
info.fullName = dir + virtualName;
|
2021-05-05 23:31:38 +00:00
|
|
|
info.isDirectory = IsDirectory(Path(info.fullName));
|
2012-11-27 15:38:24 +00:00
|
|
|
info.exists = true;
|
2013-06-09 10:40:53 +00:00
|
|
|
info.size = 0;
|
2020-05-17 13:12:38 +00:00
|
|
|
info.isWritable = false; // TODO - implement some kind of check
|
2012-10-31 12:34:27 +00:00
|
|
|
if (!info.isDirectory) {
|
2021-05-05 23:31:38 +00:00
|
|
|
std::string ext = Path(info.fullName).GetFileExtension();
|
2021-05-11 04:28:51 +00:00
|
|
|
if (!ext.empty()) {
|
|
|
|
ext = ext.substr(1); // Remove the dot.
|
|
|
|
if (filter && filters.find(ext) == filters.end()) {
|
2012-10-31 12:34:27 +00:00
|
|
|
continue;
|
2021-05-11 04:28:51 +00:00
|
|
|
}
|
2012-10-31 12:34:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:09:09 +00:00
|
|
|
if (files)
|
2019-06-27 08:37:49 +00:00
|
|
|
files->push_back(std::move(info));
|
2013-12-13 15:13:18 +00:00
|
|
|
foundEntries++;
|
2012-10-31 12:34:27 +00:00
|
|
|
#ifdef _WIN32
|
2012-10-31 12:23:16 +00:00
|
|
|
} while (FindNextFile(hFind, &ffd) != 0);
|
|
|
|
FindClose(hFind);
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
2012-10-31 12:23:16 +00:00
|
|
|
}
|
|
|
|
closedir(dirp);
|
2012-03-24 22:39:19 +00:00
|
|
|
#endif
|
2013-12-13 15:09:09 +00:00
|
|
|
if (files)
|
|
|
|
std::sort(files->begin(), files->end());
|
2012-10-31 12:23:16 +00:00
|
|
|
return foundEntries;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
int64_t GetDirectoryRecursiveSize(const Path &path, const char *filter, int flags) {
|
2019-03-17 13:32:55 +00:00
|
|
|
std::vector<FileInfo> fileInfo;
|
2021-05-05 23:31:38 +00:00
|
|
|
GetFilesInDir(path, &fileInfo, filter, flags);
|
2019-03-17 13:32:55 +00:00
|
|
|
int64_t sizeSum = 0;
|
2021-05-05 23:31:38 +00:00
|
|
|
// Note: GetFilesInDir does not fill in fileSize.
|
2019-03-17 13:32:55 +00:00
|
|
|
for (size_t i = 0; i < fileInfo.size(); i++) {
|
|
|
|
FileInfo finfo;
|
2021-05-05 23:31:38 +00:00
|
|
|
GetFileInfo(Path(fileInfo[i].fullName), &finfo);
|
2019-03-17 13:32:55 +00:00
|
|
|
if (!finfo.isDirectory)
|
|
|
|
sizeSum += finfo.size;
|
|
|
|
else
|
2021-05-05 23:31:38 +00:00
|
|
|
sizeSum += GetDirectoryRecursiveSize(Path(finfo.fullName), filter, flags);
|
2019-03-17 13:32:55 +00:00
|
|
|
}
|
|
|
|
return sizeSum;
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:20:18 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Returns a vector with the device names
|
2021-04-25 18:57:37 +00:00
|
|
|
std::vector<std::string> GetWindowsDrives()
|
2013-06-27 14:20:18 +00:00
|
|
|
{
|
2017-02-24 19:26:38 +00:00
|
|
|
#if PPSSPP_PLATFORM(UWP)
|
|
|
|
return std::vector<std::string>(); // TODO UWP http://stackoverflow.com/questions/37404405/how-to-get-logical-drives-names-in-windows-10
|
|
|
|
#else
|
2013-06-27 14:20:18 +00:00
|
|
|
std::vector<std::string> drives;
|
|
|
|
|
|
|
|
const DWORD buffsize = GetLogicalDriveStrings(0, NULL);
|
2020-09-29 16:36:59 +00:00
|
|
|
std::vector<wchar_t> buff(buffsize);
|
2013-06-27 14:20:18 +00:00
|
|
|
if (GetLogicalDriveStrings(buffsize, buff.data()) == buffsize - 1)
|
|
|
|
{
|
|
|
|
auto drive = buff.data();
|
|
|
|
while (*drive)
|
|
|
|
{
|
2013-08-26 16:59:08 +00:00
|
|
|
std::string str(ConvertWStringToUTF8(drive));
|
2013-06-27 14:20:18 +00:00
|
|
|
str.pop_back(); // we don't want the final backslash
|
|
|
|
str += "/";
|
|
|
|
drives.push_back(str);
|
2020-10-04 18:48:47 +00:00
|
|
|
|
2013-06-27 14:20:18 +00:00
|
|
|
// advance to next drive
|
|
|
|
while (*drive++) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return drives;
|
2017-02-24 19:26:38 +00:00
|
|
|
#endif
|
2013-06-27 14:20:18 +00:00
|
|
|
}
|
2013-07-04 08:37:16 +00:00
|
|
|
#endif
|
2021-04-25 18:38:22 +00:00
|
|
|
|
2021-04-25 18:52:29 +00:00
|
|
|
} // namespace File
|