ppsspp/Common/File/DirListing.cpp

247 lines
6.0 KiB
C++
Raw Normal View History

#include "ppsspp_config.h"
2012-03-24 22:39:19 +00:00
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <direct.h>
2012-03-24 22:39:19 +00:00
#else
#include <strings.h>
2012-03-24 22:39:19 +00:00
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
2012-03-24 22:39:19 +00:00
#endif
#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>
#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
#include "Common/Data/Encoding/Utf8.h"
#include "Common/StringUtils.h"
#include "Common/File/DirListing.h"
2015-05-25 17:11:33 +00:00
#if !defined(__linux__) && !defined(_WIN32) && !defined(__QNX__)
#define stat64 stat
#endif
#ifdef HAVE_LIBNX
// Far from optimal, but I guess it works...
#define fseeko fseek
#define ftello ftell
#define fileno
#endif // HAVE_LIBNX
2012-10-28 10:37:10 +00:00
// Returns true if filename is a directory
bool isDirectory(const std::string & filename) {
FileInfo info;
getFileInfo(filename.c_str(), &info);
return info.isDirectory;
}
bool getFileInfo(const char *path, FileInfo * fileInfo) {
// TODO: Expand relative paths?
fileInfo->fullName = path;
2012-10-30 15:23:08 +00:00
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA attrs;
if (!GetFileAttributesExW(ConvertUTF8ToWString(path).c_str(), GetFileExInfoStandard, &attrs)) {
fileInfo->size = 0;
fileInfo->isDirectory = false;
2013-06-08 15:43:27 +00:00
fileInfo->exists = false;
return false;
}
fileInfo->size = (uint64_t)attrs.nFileSizeLow | ((uint64_t)attrs.nFileSizeHigh << 32);
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
std::string copy(path);
2012-10-28 10:37:10 +00:00
2017-08-18 08:53:47 +00:00
#if (defined __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);
#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;
}
fileInfo->isDirectory = S_ISDIR(file_info.st_mode);
fileInfo->isWritable = false;
fileInfo->size = file_info.st_size;
2013-06-08 15:43:27 +00:00
fileInfo->exists = true;
// HACK: approximation
if (file_info.st_mode & 0200)
fileInfo->isWritable = true;
2012-10-30 15:23:08 +00:00
#endif
return true;
2012-10-28 10:37:10 +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);
2012-10-31 12:34:27 +00:00
for (size_t i = 0; i < ext.size(); i++) {
ext[i] = tolower(ext[i]);
}
return ext;
}
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;
}
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;
if (filter) {
std::string tmp;
2012-10-31 12:34:27 +00:00
while (*filter) {
if (*filter == ':') {
filters.insert(std::move(tmp));
2012-10-31 12:34:27 +00:00
} else {
tmp.push_back(*filter);
}
filter++;
}
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;
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) {
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
//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
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 ".."
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.)
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;
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;
info.isWritable = false; // TODO - implement some kind of check
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;
}
}
if (files)
files->push_back(std::move(info));
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
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
}
int64_t getDirectoryRecursiveSize(const std::string & path, const char *filter, int flags) {
std::vector<FileInfo> fileInfo;
getFilesInDir(path.c_str(), &fileInfo, filter, flags);
int64_t sizeSum = 0;
// Note: getFileInDir does not fill in fileSize properly.
for (size_t i = 0; i < fileInfo.size(); i++) {
FileInfo finfo;
getFileInfo(fileInfo[i].fullName.c_str(), &finfo);
if (!finfo.isDirectory)
sizeSum += finfo.size;
else
sizeSum += getDirectoryRecursiveSize(finfo.fullName, filter, flags);
}
return sizeSum;
}
#ifdef _WIN32
// Returns a vector with the device names
std::vector<std::string> getWindowsDrives()
{
#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
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);
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));
str.pop_back(); // we don't want the final backslash
str += "/";
drives.push_back(str);
// advance to next drive
while (*drive++) {}
}
}
return drives;
#endif
}
#endif