Files
OpenRCT2-Web/src/openrct2/core/FileScanner.cpp
T

436 lines
12 KiB
C++
Raw Normal View History

2016-06-28 00:16:35 +01:00
/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
2016-06-28 00:16:35 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-06-28 00:16:35 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-06-28 00:16:35 +01:00
*****************************************************************************/
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
#include <windows.h>
2016-10-22 22:33:46 +01:00
#endif
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
2017-06-12 18:00:15 +01:00
#elif defined(_WIN32)
// Windows needs this for widechar <-> utf8 conversion utils
#include "../localisation/Language.h"
#endif
2018-06-22 22:58:39 +02:00
#include "FileScanner.h"
#include "Memory.hpp"
#include "Numerics.hpp"
2018-06-22 22:58:39 +02:00
#include "Path.hpp"
#include "String.hpp"
#include <memory>
2016-10-22 19:10:16 +01:00
#include <stack>
2017-01-05 18:13:48 +00:00
#include <string>
#include <vector>
2016-06-28 00:16:35 +01:00
using namespace OpenRCT2;
2017-01-05 18:13:48 +00:00
enum class DIRECTORY_CHILD_TYPE
{
DC_DIRECTORY,
DC_FILE,
};
struct DirectoryChild
{
DIRECTORY_CHILD_TYPE Type;
std::string Name;
// Files only
2018-06-22 22:58:39 +02:00
uint64_t Size = 0;
uint64_t LastModified = 0;
};
2023-05-09 21:08:46 +02:00
static uint32_t GetPathChecksum(u8string_view path);
2018-06-22 22:58:39 +02:00
static bool MatchWildcard(const utf8* fileName, const utf8* pattern);
class FileScannerBase : public IFileScanner
2016-06-28 00:16:35 +01:00
{
2016-10-22 19:10:16 +01:00
private:
struct DirectoryState
2016-06-28 00:16:35 +01:00
{
2023-05-09 21:08:46 +02:00
u8string Path;
std::vector<DirectoryChild> Listing;
2018-06-22 22:58:39 +02:00
int32_t Index = 0;
2016-10-22 19:10:16 +01:00
};
// Options
2023-05-09 21:08:46 +02:00
const u8string _rootPath;
const std::vector<std::string> _patterns;
const bool _recurse;
2016-10-22 19:10:16 +01:00
// State
2018-06-22 22:58:39 +02:00
bool _started = false;
std::stack<DirectoryState> _directoryStack;
2016-10-22 19:10:16 +01:00
// Current
2023-05-09 21:08:46 +02:00
FileScanner::FileInfo _currentFileInfo;
u8string _currentPath;
2016-10-22 19:10:16 +01:00
public:
2023-05-09 21:08:46 +02:00
FileScannerBase(u8string_view pattern, bool recurse)
: _rootPath(Path::GetDirectory(pattern))
, _patterns(GetPatterns(Path::GetFileName(pattern)))
, _recurse(recurse)
2016-10-22 19:10:16 +01:00
{
}
2023-05-09 21:08:46 +02:00
~FileScannerBase() override = default;
2016-10-22 19:10:16 +01:00
2023-05-09 21:08:46 +02:00
const FileScanner::FileInfo& GetFileInfo() const override
2016-10-22 19:10:16 +01:00
{
return _currentFileInfo;
2016-10-22 19:10:16 +01:00
}
2023-05-09 21:08:46 +02:00
const u8string& GetPath() const override
2016-10-22 19:10:16 +01:00
{
return _currentPath;
2016-10-22 19:10:16 +01:00
}
2023-05-09 21:08:46 +02:00
u8string GetPathRelative() const override
{
2023-05-09 21:08:46 +02:00
return Path::GetRelative(_currentPath, _rootPath);
}
2016-10-22 19:10:16 +01:00
void Reset() override
{
_started = false;
2023-05-09 21:08:46 +02:00
_directoryStack = {};
_currentPath.clear();
2016-10-22 19:10:16 +01:00
}
bool Next() override
{
if (!_started)
2016-06-28 00:16:35 +01:00
{
_started = true;
PushState(_rootPath);
}
2017-12-03 23:45:43 +01:00
while (!_directoryStack.empty())
{
2018-06-22 22:58:39 +02:00
DirectoryState* state = &_directoryStack.top();
state->Index++;
2020-04-22 17:09:29 +02:00
if (state->Index >= static_cast<int32_t>(state->Listing.size()))
2016-06-28 00:16:35 +01:00
{
_directoryStack.pop();
2016-10-22 19:10:16 +01:00
}
else
2016-10-22 19:10:16 +01:00
{
2018-06-22 22:58:39 +02:00
const DirectoryChild* child = &state->Listing[state->Index];
if (child->Type == DIRECTORY_CHILD_TYPE::DC_DIRECTORY)
2016-10-22 19:10:16 +01:00
{
if (_recurse)
{
auto childPath = Path::Combine(state->Path, child->Name);
PushState(childPath);
}
}
2017-01-05 18:13:48 +00:00
else if (PatternMatch(child->Name))
{
2023-05-09 21:08:46 +02:00
_currentPath = Path::Combine(state->Path, child->Name);
2023-05-09 21:08:46 +02:00
_currentFileInfo.Name = child->Name;
_currentFileInfo.Size = child->Size;
_currentFileInfo.LastModified = child->LastModified;
2016-10-22 19:10:16 +01:00
return true;
}
}
}
return false;
}
2016-10-22 19:10:16 +01:00
2024-07-09 15:17:07 +02:00
virtual void GetDirectoryChildren(std::vector<DirectoryChild>& children, const std::string& path) = 0;
private:
2018-06-22 22:58:39 +02:00
void PushState(const std::string& directory)
{
DirectoryState newState;
2017-01-05 18:13:48 +00:00
newState.Path = directory;
newState.Index = -1;
GetDirectoryChildren(newState.Listing, directory);
_directoryStack.push(newState);
}
2018-06-22 22:58:39 +02:00
bool PatternMatch(const std::string& fileName)
{
2018-06-22 22:58:39 +02:00
for (const auto& pattern : _patterns)
{
2017-01-05 18:13:48 +00:00
if (MatchWildcard(fileName.c_str(), pattern.c_str()))
2016-10-22 19:10:16 +01:00
{
return true;
2016-06-28 00:16:35 +01:00
}
}
return false;
}
2018-06-22 22:58:39 +02:00
static std::vector<std::string> GetPatterns(const std::string& delimitedPatterns)
{
2017-01-05 18:13:48 +00:00
std::vector<std::string> patterns;
2018-06-22 22:58:39 +02:00
const utf8* start = delimitedPatterns.c_str();
const utf8* ch = start;
utf8 c;
do
{
c = *ch;
if (c == '\0' || c == ';')
{
2020-04-22 17:09:29 +02:00
size_t length = static_cast<size_t>(ch - start);
if (length > 0)
{
patterns.emplace_back(start, length);
}
start = ch + 1;
}
ch++;
2018-06-22 22:58:39 +02:00
} while (c != '\0');
2017-01-05 18:13:48 +00:00
patterns.shrink_to_fit();
return patterns;
}
};
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
class FileScannerWindows final : public FileScannerBase
{
public:
2023-05-09 21:08:46 +02:00
FileScannerWindows(u8string_view pattern, bool recurse)
: FileScannerBase(pattern, recurse)
{
}
2018-06-22 22:58:39 +02:00
void GetDirectoryChildren(std::vector<DirectoryChild>& children, const std::string& path) override
{
auto pattern = path + "\\*";
2019-07-22 23:11:12 +01:00
auto wPattern = String::ToWideChar(pattern.c_str());
WIN32_FIND_DATAW findData;
HANDLE hFile = FindFirstFileW(wPattern.c_str(), &findData);
if (hFile != INVALID_HANDLE_VALUE)
{
do
{
2018-06-22 22:58:39 +02:00
if (lstrcmpW(findData.cFileName, L".") != 0 && lstrcmpW(findData.cFileName, L"..") != 0)
{
children.push_back(CreateChild(&findData));
}
2018-06-22 22:58:39 +02:00
} while (FindNextFileW(hFile, &findData));
FindClose(hFile);
}
2016-10-22 19:10:16 +01:00
}
2016-06-28 00:16:35 +01:00
2016-10-22 19:10:16 +01:00
private:
2018-06-22 22:58:39 +02:00
static DirectoryChild CreateChild(const WIN32_FIND_DATAW* child)
2016-10-22 19:10:16 +01:00
{
DirectoryChild result;
result.Name = String::ToUtf8(child->cFileName);
if (child->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
2016-06-28 00:16:35 +01:00
{
result.Type = DIRECTORY_CHILD_TYPE::DC_DIRECTORY;
2016-06-28 00:16:35 +01:00
}
else
2016-06-28 00:16:35 +01:00
{
result.Type = DIRECTORY_CHILD_TYPE::DC_FILE;
2022-10-17 19:21:18 +02:00
result.Size = (static_cast<uint64_t>(child->nFileSizeHigh) << 32uLL) | static_cast<uint64_t>(child->nFileSizeLow);
result.LastModified = (static_cast<uint64_t>(child->ftLastWriteTime.dwHighDateTime) << 32uLL)
2020-07-14 12:41:13 -03:00
| static_cast<uint64_t>(child->ftLastWriteTime.dwLowDateTime);
2016-06-28 00:16:35 +01:00
}
return result;
2016-06-28 00:16:35 +01:00
}
2016-10-22 19:10:16 +01:00
};
2016-06-28 00:16:35 +01:00
2017-06-12 18:00:15 +01:00
#endif // _WIN32
2016-10-22 22:33:46 +01:00
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
class FileScannerUnix final : public FileScannerBase
{
public:
2023-05-09 21:08:46 +02:00
FileScannerUnix(u8string_view pattern, bool recurse)
2016-10-22 22:33:46 +01:00
: FileScannerBase(pattern, recurse)
{
}
2018-06-22 22:58:39 +02:00
void GetDirectoryChildren(std::vector<DirectoryChild>& children, const std::string& path) override
2016-10-22 22:33:46 +01:00
{
2018-06-22 22:58:39 +02:00
struct dirent** namelist;
int32_t count = scandir(path.c_str(), &namelist, FilterFunc, alphasort);
2016-10-22 22:33:46 +01:00
if (count > 0)
{
for (int32_t i = 0; i < count; i++)
2016-10-22 22:33:46 +01:00
{
2018-06-22 22:58:39 +02:00
const struct dirent* node = namelist[i];
if (!String::Equals(node->d_name, ".") && !String::Equals(node->d_name, ".."))
2016-10-22 23:09:45 +01:00
{
children.push_back(CreateChild(path.c_str(), node));
2016-10-22 23:09:45 +01:00
}
2016-10-23 22:27:15 +02:00
free(namelist[i]);
2016-10-22 22:33:46 +01:00
}
2016-10-23 22:27:15 +02:00
free(namelist);
2016-10-22 22:33:46 +01:00
}
}
private:
2018-06-22 22:58:39 +02:00
static int32_t FilterFunc(const struct dirent* d)
2016-10-22 22:33:46 +01:00
{
return 1;
}
2018-06-22 22:58:39 +02:00
static DirectoryChild CreateChild(const utf8* directory, const struct dirent* node)
2016-10-22 22:33:46 +01:00
{
DirectoryChild result;
result.Name = std::string(node->d_name);
if (node->d_type == DT_DIR)
2016-10-22 22:33:46 +01:00
{
result.Type = DIRECTORY_CHILD_TYPE::DC_DIRECTORY;
2016-10-22 22:33:46 +01:00
}
else
{
result.Type = DIRECTORY_CHILD_TYPE::DC_FILE;
2016-10-22 22:33:46 +01:00
// Get the full path of the file
auto path = Path::Combine(directory, node->d_name);
2016-10-22 22:33:46 +01:00
2018-06-22 22:58:39 +02:00
struct stat statInfo
{
};
int32_t statRes = stat(path.c_str(), &statInfo);
2016-10-22 22:33:46 +01:00
if (statRes != -1)
{
result.Size = statInfo.st_size;
result.LastModified = statInfo.st_mtime;
if (S_ISDIR(statInfo.st_mode))
{
result.Type = DIRECTORY_CHILD_TYPE::DC_DIRECTORY;
}
2016-10-22 22:33:46 +01:00
}
}
return result;
}
};
#endif // defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
std::unique_ptr<IFileScanner> Path::ScanDirectory(const std::string& pattern, bool recurse)
2016-06-28 00:16:35 +01:00
{
2017-06-12 18:00:15 +01:00
#ifdef _WIN32
return std::make_unique<FileScannerWindows>(pattern, recurse);
2016-10-22 22:33:46 +01:00
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
return std::make_unique<FileScannerUnix>(pattern, recurse);
#endif
}
2018-06-22 22:58:39 +02:00
void Path::QueryDirectory(QueryDirectoryResult* result, const std::string& pattern)
2016-10-23 01:12:17 +01:00
{
auto scanner = Path::ScanDirectory(pattern, true);
2016-10-23 01:12:17 +01:00
while (scanner->Next())
{
2023-05-09 21:08:46 +02:00
const FileScanner::FileInfo& fileInfo = scanner->GetFileInfo();
const u8string& path = scanner->GetPath();
2016-10-23 01:12:17 +01:00
result->TotalFiles++;
2023-05-09 21:08:46 +02:00
result->TotalFileSize += fileInfo.Size;
result->FileDateModifiedChecksum ^= static_cast<uint32_t>(fileInfo.LastModified >> 32)
^ static_cast<uint32_t>(fileInfo.LastModified & 0xFFFFFFFF);
result->FileDateModifiedChecksum = Numerics::ror32(result->FileDateModifiedChecksum, 5);
2016-10-23 01:12:17 +01:00
result->PathChecksum += GetPathChecksum(path);
}
}
2018-06-22 22:58:39 +02:00
std::vector<std::string> Path::GetDirectories(const std::string& path)
{
auto scanner = ScanDirectory(path, false);
2018-06-22 22:58:39 +02:00
auto baseScanner = static_cast<FileScannerBase*>(scanner.get());
std::vector<DirectoryChild> children;
baseScanner->GetDirectoryChildren(children, path);
std::vector<std::string> subDirectories;
2018-06-22 22:58:39 +02:00
for (const auto& c : children)
{
if (c.Type == DIRECTORY_CHILD_TYPE::DC_DIRECTORY)
{
subDirectories.push_back(c.Name);
}
}
return subDirectories;
}
2023-05-09 21:08:46 +02:00
static uint32_t GetPathChecksum(u8string_view path)
2016-10-23 01:12:17 +01:00
{
uint32_t hash = 0xD8430DED;
2023-05-09 21:08:46 +02:00
for (const utf8 ch : path)
2016-10-23 01:12:17 +01:00
{
2023-05-09 21:08:46 +02:00
hash += ch;
2016-10-23 01:12:17 +01:00
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
/**
* Due to FindFirstFile / FindNextFile searching for DOS names as well, *.doc also matches *.docx which isn't what the pattern
* specified. This will verify if a filename does indeed match the pattern we asked for.
* @remarks Based on algorithm (http://xoomer.virgilio.it/acantato/dev/wildcard/wildmatch.html)
*/
2018-06-22 22:58:39 +02:00
static bool MatchWildcard(const utf8* fileName, const utf8* pattern)
{
while (*fileName != '\0')
{
2018-06-22 22:58:39 +02:00
switch (*pattern)
{
case '?':
if (*fileName == '.')
{
2018-06-22 22:58:39 +02:00
return false;
}
break;
case '*':
do
{
pattern++;
} while (*pattern == '*');
if (*pattern == '\0')
{
return false;
}
while (*fileName != '\0')
{
if (MatchWildcard(fileName++, pattern))
{
return true;
}
}
return false;
2018-06-22 22:58:39 +02:00
default:
if (toupper(*fileName) != toupper(*pattern))
{
return false;
}
break;
}
pattern++;
fileName++;
}
while (*pattern == '*')
{
++fileName;
}
return *pattern == '\0';
2016-06-28 00:16:35 +01:00
}