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>
|
2013-07-04 08:37:16 +00:00
|
|
|
#ifndef strcasecmp
|
|
|
|
#define strcasecmp _stricmp
|
|
|
|
#endif
|
2014-12-30 18:19:09 +00:00
|
|
|
#define fseeko _fseeki64
|
|
|
|
#define ftello _ftelli64
|
2012-03-24 22:39:19 +00:00
|
|
|
#else
|
|
|
|
#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
|
|
|
|
2012-03-24 22:39:19 +00:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "file/file_util.h"
|
2013-08-26 16:59:08 +00:00
|
|
|
#include "util/text/utf8.h"
|
2012-03-24 22:39:19 +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
|
|
|
|
|
2013-08-27 07:27:06 +00:00
|
|
|
FILE *openCFile(const std::string &filename, const char *mode)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32) && defined(UNICODE)
|
|
|
|
return _wfopen(ConvertUTF8ToWString(filename).c_str(), ConvertUTF8ToWString(mode).c_str());
|
|
|
|
#else
|
|
|
|
return fopen(filename.c_str(), mode);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-04-12 13:18:43 +00:00
|
|
|
bool writeStringToFile(bool text_file, const std::string &str, const char *filename)
|
2012-03-24 22:39:19 +00:00
|
|
|
{
|
2013-08-27 07:27:06 +00:00
|
|
|
FILE *f = openCFile(filename, text_file ? "w" : "wb");
|
2012-10-31 12:23:16 +00:00
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
size_t len = str.size();
|
|
|
|
if (len != fwrite(str.data(), 1, str.size(), f))
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
2012-12-08 22:26:14 +00:00
|
|
|
bool writeDataToFile(bool text_file, const void* data, const unsigned int size, const char *filename)
|
|
|
|
{
|
2013-08-27 07:27:06 +00:00
|
|
|
FILE *f = openCFile(filename, text_file ? "w" : "wb");
|
2012-12-08 22:26:14 +00:00
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
size_t len = size;
|
|
|
|
if (len != fwrite(data, 1, len, f))
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-24 22:39:19 +00:00
|
|
|
uint64_t GetSize(FILE *f)
|
|
|
|
{
|
2014-12-30 18:19:09 +00:00
|
|
|
// This will only support 64-bit when large file support is available.
|
|
|
|
// That won't be the case on some versions of Android, at least.
|
2016-10-12 09:13:16 +00:00
|
|
|
#if defined(__ANDROID__) || (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS < 64)
|
2014-12-30 18:19:09 +00:00
|
|
|
int fd = fileno(f);
|
|
|
|
|
|
|
|
off64_t pos = lseek64(fd, 0, SEEK_CUR);
|
|
|
|
off64_t size = lseek64(fd, 0, SEEK_END);
|
|
|
|
if (size != pos && lseek64(fd, pos, SEEK_SET) != pos) {
|
|
|
|
// Should error here.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
#else
|
|
|
|
uint64_t pos = ftello(f);
|
2012-10-31 12:23:16 +00:00
|
|
|
if (fseek(f, 0, SEEK_END) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-30 18:19:09 +00:00
|
|
|
uint64_t size = ftello(f);
|
2012-10-31 12:23:16 +00:00
|
|
|
// Reset the seek position to where it was when we started.
|
2014-12-30 18:19:09 +00:00
|
|
|
if (size != pos && fseeko(f, pos, SEEK_SET) != 0) {
|
|
|
|
// Should error here.
|
2012-10-31 12:23:16 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return size;
|
2014-12-30 18:19:09 +00:00
|
|
|
#endif
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 14:30:55 +00:00
|
|
|
bool readFileToString(bool text_file, const char *filename, std::string &str)
|
2012-03-24 22:39:19 +00:00
|
|
|
{
|
2013-08-27 07:27:06 +00:00
|
|
|
FILE *f = openCFile(filename, text_file ? "r" : "rb");
|
2012-10-31 12:23:16 +00:00
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
size_t len = (size_t)GetSize(f);
|
|
|
|
char *buf = new char[len + 1];
|
|
|
|
buf[fread(buf, 1, len, f)] = 0;
|
|
|
|
str = std::string(buf, len);
|
|
|
|
fclose(f);
|
|
|
|
delete [] buf;
|
|
|
|
return true;
|
2012-03-24 22:39:19 +00:00
|
|
|
}
|
|
|
|
|
2012-12-08 22:26:14 +00:00
|
|
|
|
|
|
|
bool readDataFromFile(bool text_file, unsigned char* &data, const unsigned int size, const char *filename)
|
|
|
|
{
|
2013-08-27 07:27:06 +00:00
|
|
|
FILE *f = openCFile(filename, text_file ? "r" : "rb");
|
2012-12-08 22:26:14 +00:00
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
size_t len = (size_t)GetSize(f);
|
2013-08-26 16:59:08 +00:00
|
|
|
if(len < size) {
|
2012-12-08 22:26:14 +00:00
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
data[fread(data, 1, size, f)] = 0;
|
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-28 10:37:10 +00:00
|
|
|
// Returns true if filename is a directory
|
2013-08-26 16:59:08 +00:00
|
|
|
bool isDirectory(const std::string &filename) {
|
2012-12-01 09:35:55 +00:00
|
|
|
FileInfo info;
|
|
|
|
getFileInfo(filename.c_str(), &info);
|
|
|
|
return info.isDirectory;
|
|
|
|
}
|
|
|
|
|
2013-08-26 16:59:08 +00:00
|
|
|
bool getFileInfo(const char *path, FileInfo *fileInfo) {
|
2012-12-01 09:35:55 +00:00
|
|
|
// TODO: Expand relative paths?
|
|
|
|
fileInfo->fullName = path;
|
|
|
|
|
2012-10-30 15:23:08 +00:00
|
|
|
#ifdef _WIN32
|
2013-04-13 19:22:03 +00:00
|
|
|
WIN32_FILE_ATTRIBUTE_DATA attrs;
|
2013-08-27 07:27:06 +00:00
|
|
|
if (!GetFileAttributesExW(ConvertUTF8ToWString(path).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;
|
2012-10-30 15:23:08 +00:00
|
|
|
#else
|
2012-10-28 10:37:10 +00:00
|
|
|
|
2012-12-01 09:35:55 +00:00
|
|
|
std::string copy(path);
|
2012-10-28 10:37:10 +00:00
|
|
|
|
2017-07-03 14:16:25 +00:00
|
|
|
#ifdef __ANDROID__ && __ANDROID_API__ < 21
|
|
|
|
struct stat file_info;
|
|
|
|
int result = stat(copy.c_str(), &file_info);
|
|
|
|
#else
|
|
|
|
struct stat64 file_info;
|
2012-10-28 10:37:10 +00:00
|
|
|
int result = stat64(copy.c_str(), &file_info);
|
2017-07-03 14:16:25 +00:00
|
|
|
#endif
|
2012-10-28 10:37:10 +00:00
|
|
|
if (result < 0) {
|
2012-12-01 09:35:55 +00:00
|
|
|
WLOG("IsDirectory: stat failed on %s", path);
|
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;
|
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
|
|
|
}
|
|
|
|
|
2012-10-31 12:34:27 +00:00
|
|
|
std::string getFileExtension(const std::string &fn) {
|
2013-08-26 16:59:08 +00:00
|
|
|
int pos = (int)fn.rfind(".");
|
2012-10-31 12:34:27 +00:00
|
|
|
if (pos < 0) return "";
|
|
|
|
std::string ext = fn.substr(pos+1);
|
|
|
|
for (size_t i = 0; i < ext.size(); i++) {
|
|
|
|
ext[i] = tolower(ext[i]);
|
|
|
|
}
|
|
|
|
return ext;
|
|
|
|
}
|
|
|
|
|
2013-07-04 08:37:16 +00:00
|
|
|
bool FileInfo::operator <(const FileInfo &other) const {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-03-12 18:49:03 +00:00
|
|
|
size_t getFilesInDir(const char *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;
|
|
|
|
std::string tmp;
|
|
|
|
if (filter) {
|
|
|
|
while (*filter) {
|
|
|
|
if (*filter == ':') {
|
|
|
|
filters.insert(tmp);
|
|
|
|
tmp = "";
|
|
|
|
} else {
|
|
|
|
tmp.push_back(*filter);
|
|
|
|
}
|
|
|
|
filter++;
|
|
|
|
}
|
|
|
|
}
|
2013-04-18 21:39:22 +00:00
|
|
|
if (tmp.size())
|
|
|
|
filters.insert(tmp);
|
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;
|
2017-02-24 19:26:38 +00:00
|
|
|
HANDLE hFind = FindFirstFileEx((ConvertUTF8ToWString(directory) + L"\\*").c_str(), FindExInfoStandard, &ffd, FindExSearchNameMatch, NULL, 0);
|
2012-10-31 12:23:16 +00:00
|
|
|
if (hFind == INVALID_HANDLE_VALUE) {
|
|
|
|
FindClose(hFind);
|
|
|
|
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_large { struct dirent entry; char padding[FILENAME_MAX+1]; };
|
|
|
|
struct dirent_large diren;
|
|
|
|
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 += "/";
|
|
|
|
|
2012-10-31 12:23:16 +00:00
|
|
|
DIR *dirp = opendir(directory);
|
|
|
|
if (!dirp)
|
|
|
|
return 0;
|
|
|
|
// non windows loop
|
2012-11-14 15:43:33 +00:00
|
|
|
while (!readdir_r(dirp, (dirent*) &diren, &result) && result)
|
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 ".."
|
|
|
|
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
|
|
|
|
((virtualName[0] == '.') && (virtualName[1] == '.') &&
|
|
|
|
(virtualName[2] == '\0')))
|
|
|
|
continue;
|
2012-10-28 10:37:10 +00:00
|
|
|
|
2015-11-16 06:36:13 +00:00
|
|
|
// Remove dotfiles (optional with flag.)
|
2014-03-12 18:49:03 +00:00
|
|
|
if (!(flags & GETFILES_GETHIDDEN) && virtualName[0] == '.')
|
2012-10-28 10:37:10 +00:00
|
|
|
continue;
|
2012-10-31 12:34:27 +00:00
|
|
|
|
2012-10-31 12:23:16 +00:00
|
|
|
FileInfo info;
|
|
|
|
info.name = virtualName;
|
2013-10-14 17:15:32 +00:00
|
|
|
std::string dir = directory;
|
|
|
|
|
|
|
|
// 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;
|
2012-10-31 12:23:16 +00:00
|
|
|
info.isDirectory = isDirectory(info.fullName);
|
2012-11-27 15:38:24 +00:00
|
|
|
info.exists = true;
|
2013-06-09 10:40:53 +00:00
|
|
|
info.size = 0;
|
2012-10-31 12:34:27 +00:00
|
|
|
if (!info.isDirectory) {
|
|
|
|
std::string ext = getFileExtension(info.fullName);
|
|
|
|
if (filter) {
|
|
|
|
if (filters.find(ext) == filters.end())
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:09:09 +00:00
|
|
|
if (files)
|
|
|
|
files->push_back(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
|
|
|
}
|
|
|
|
|
2013-06-27 14:20:18 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Returns a vector with the device names
|
|
|
|
std::vector<std::string> getWindowsDrives()
|
|
|
|
{
|
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);
|
|
|
|
std::vector<TCHAR> buff(buffsize);
|
|
|
|
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);
|
|
|
|
|
|
|
|
// 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
|