Move FixPathCase to Common/File/Path

This commit is contained in:
Henrik Rydgård 2021-09-11 21:17:23 +02:00
parent d876834561
commit de3d711616
6 changed files with 145 additions and 147 deletions

View File

@ -2,6 +2,7 @@
#include <cstring> #include <cstring>
#include "Common/File/Path.h" #include "Common/File/Path.h"
#include "Common/File/FileUtil.h"
#include "Common/StringUtils.h" #include "Common/StringUtils.h"
#include "Common/Log.h" #include "Common/Log.h"
#include "Common/Data/Encoding/Utf8.h" #include "Common/Data/Encoding/Utf8.h"
@ -9,6 +10,12 @@
#include "android/jni/app-android.h" #include "android/jni/app-android.h"
#include "android/jni/AndroidContentURI.h" #include "android/jni/AndroidContentURI.h"
#if HOST_IS_CASE_SENSITIVE
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#endif
Path::Path(const std::string &str) { Path::Path(const std::string &str) {
Init(str); Init(str);
} }
@ -346,3 +353,106 @@ bool Path::ComputePathTo(const Path &other, std::string &path) const {
return true; return true;
} }
} }
#if HOST_IS_CASE_SENSITIVE
static bool FixFilenameCase(const std::string &path, std::string &filename) {
// Are we lucky?
if (File::Exists(Path(path + filename)))
return true;
size_t filenameSize = filename.size(); // size in bytes, not characters
for (size_t i = 0; i < filenameSize; i++)
{
filename[i] = tolower(filename[i]);
}
//TODO: lookup filename in cache for "path"
struct dirent *result = NULL;
DIR *dirp = opendir(path.c_str());
if (!dirp)
return false;
bool retValue = false;
while ((result = readdir(dirp)))
{
if (strlen(result->d_name) != filenameSize)
continue;
size_t i;
for (i = 0; i < filenameSize; i++)
{
if (filename[i] != tolower(result->d_name[i]))
break;
}
if (i < filenameSize)
continue;
filename = result->d_name;
retValue = true;
}
closedir(dirp);
return retValue;
}
bool FixPathCase(const Path &realBasePath, std::string &path, FixPathCaseBehavior behavior) {
if (realBasePath.Type() == PathType::CONTENT_URI) {
// Nothing to do. These are already case insensitive, I think.
return true;
}
std::string basePath = realBasePath.ToString();
size_t len = path.size();
if (len == 0)
return true;
if (path[len - 1] == '/')
{
len--;
if (len == 0)
return true;
}
std::string fullPath;
fullPath.reserve(basePath.size() + len + 1);
fullPath.append(basePath);
size_t start = 0;
while (start < len)
{
size_t i = path.find('/', start);
if (i == std::string::npos)
i = len;
if (i > start)
{
std::string component = path.substr(start, i - start);
// Fix case and stop on nonexistant path component
if (FixFilenameCase(fullPath, component) == false) {
// Still counts as success if partial matches allowed or if this
// is the last component and only the ones before it are required
return (behavior == FPC_PARTIAL_ALLOWED || (behavior == FPC_PATH_MUST_EXIST && i >= len));
}
path.replace(start, i - start, component);
fullPath.append(1, '/');
fullPath.append(component);
}
start = i + 1;
}
return true;
}
#endif

View File

@ -4,6 +4,26 @@
#include <string> #include <string>
#if defined(__APPLE__)
#if TARGET_OS_IPHONE
#define HOST_IS_CASE_SENSITIVE 1
#elif TARGET_IPHONE_SIMULATOR
#define HOST_IS_CASE_SENSITIVE 0
#else
// Mac OSX case sensitivity defaults off, but is user configurable (when
// creating a filesytem), so assume the worst:
#define HOST_IS_CASE_SENSITIVE 1
#endif
#elif defined(_WIN32)
#define HOST_IS_CASE_SENSITIVE 0
#else // Android, Linux, BSD (and the rest?)
#define HOST_IS_CASE_SENSITIVE 1
#endif
enum class PathType { enum class PathType {
UNDEFINED = 0, UNDEFINED = 0,
NATIVE = 1, // Can be relative. NATIVE = 1, // Can be relative.
@ -108,3 +128,18 @@ private:
PathType type_; PathType type_;
}; };
// Utility function for fixing the case of paths. Only present on Unix-like systems.
#if HOST_IS_CASE_SENSITIVE
enum FixPathCaseBehavior {
FPC_FILE_MUST_EXIST, // all path components must exist (rmdir, move from)
FPC_PATH_MUST_EXIST, // all except the last one must exist - still tries to fix last one (fopen, move to)
FPC_PARTIAL_ALLOWED, // don't care how many exist (mkdir recursive)
};
bool FixPathCase(const Path &basePath, std::string &path, FixPathCaseBehavior behavior);
#endif

View File

@ -1919,16 +1919,3 @@ bool SavedataParam::IsInSaveDataList(std::string saveName, int count) {
} }
return false; return false;
} }
bool SavedataParam::IsSaveDirectoryExist(SceUtilitySavedataParam* param) {
std::string dirPath = savePath + GetGameName(param) + GetSaveName(param);
PSPFileInfo directoryInfo = pspFileSystem.GetFileInfo(dirPath);
return directoryInfo.exists;
}
bool SavedataParam::IsSfoFileExist(SceUtilitySavedataParam* param) {
std::string dirPath = savePath + GetGameName(param) + GetSaveName(param);
std::string sfoPath = dirPath + "/" + SFO_FILENAME;
PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfoPath);
return sfoInfo.exists;
}

View File

@ -319,8 +319,6 @@ public:
bool GetSize(SceUtilitySavedataParam* param); bool GetSize(SceUtilitySavedataParam* param);
int GetSaveCryptMode(SceUtilitySavedataParam* param, const std::string &saveDirName); int GetSaveCryptMode(SceUtilitySavedataParam* param, const std::string &saveDirName);
bool IsInSaveDataList(std::string saveName, int count); bool IsInSaveDataList(std::string saveName, int count);
bool IsSaveDirectoryExist(SceUtilitySavedataParam* param);
bool IsSfoFileExist(SceUtilitySavedataParam* param);
std::string GetGameName(const SceUtilitySavedataParam *param) const; std::string GetGameName(const SceUtilitySavedataParam *param) const;
std::string GetSaveName(const SceUtilitySavedataParam *param) const; std::string GetSaveName(const SceUtilitySavedataParam *param) const;

View File

@ -66,108 +66,6 @@
#include <fcntl.h> #include <fcntl.h>
#endif #endif
#if HOST_IS_CASE_SENSITIVE
static bool FixFilenameCase(const std::string &path, std::string &filename) {
// Are we lucky?
if (File::Exists(Path(path + filename)))
return true;
size_t filenameSize = filename.size(); // size in bytes, not characters
for (size_t i = 0; i < filenameSize; i++)
{
filename[i] = tolower(filename[i]);
}
//TODO: lookup filename in cache for "path"
struct dirent *result = NULL;
DIR *dirp = opendir(path.c_str());
if (!dirp)
return false;
bool retValue = false;
while ((result = readdir(dirp)))
{
if (strlen(result->d_name) != filenameSize)
continue;
size_t i;
for (i = 0; i < filenameSize; i++)
{
if (filename[i] != tolower(result->d_name[i]))
break;
}
if (i < filenameSize)
continue;
filename = result->d_name;
retValue = true;
}
closedir(dirp);
return retValue;
}
bool FixPathCase(const Path &realBasePath, std::string &path, FixPathCaseBehavior behavior) {
if (realBasePath.Type() == PathType::CONTENT_URI) {
// Nothing to do. These are already case insensitive, I think.
return true;
}
std::string basePath = realBasePath.ToString();
size_t len = path.size();
if (len == 0)
return true;
if (path[len - 1] == '/')
{
len--;
if (len == 0)
return true;
}
std::string fullPath;
fullPath.reserve(basePath.size() + len + 1);
fullPath.append(basePath);
size_t start = 0;
while (start < len)
{
size_t i = path.find('/', start);
if (i == std::string::npos)
i = len;
if (i > start)
{
std::string component = path.substr(start, i - start);
// Fix case and stop on nonexistant path component
if (FixFilenameCase(fullPath, component) == false) {
// Still counts as success if partial matches allowed or if this
// is the last component and only the ones before it are required
return (behavior == FPC_PARTIAL_ALLOWED || (behavior == FPC_PATH_MUST_EXIST && i >= len));
}
path.replace(start, i - start, component);
fullPath.append(1, '/');
fullPath.append(component);
}
start = i + 1;
}
return true;
}
#endif
DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, const Path & _basePath, FileSystemFlags _flags) : basePath(_basePath), flags(_flags) { DirectoryFileSystem::DirectoryFileSystem(IHandleAllocator *_hAlloc, const Path & _basePath, FileSystemFlags _flags) : basePath(_basePath), flags(_flags) {
File::CreateFullPath(basePath); File::CreateFullPath(basePath);
hAlloc = _hAlloc; hAlloc = _hAlloc;

View File

@ -28,36 +28,6 @@
typedef void * HANDLE; typedef void * HANDLE;
#endif #endif
#if defined(__APPLE__)
#if TARGET_OS_IPHONE
#define HOST_IS_CASE_SENSITIVE 1
#elif TARGET_IPHONE_SIMULATOR
#define HOST_IS_CASE_SENSITIVE 0
#else
// Mac OSX case sensitivity defaults off, but is user configurable (when
// creating a filesytem), so assume the worst:
#define HOST_IS_CASE_SENSITIVE 1
#endif
#elif defined(_WIN32)
#define HOST_IS_CASE_SENSITIVE 0
#else // Android, Linux, BSD (and the rest?)
#define HOST_IS_CASE_SENSITIVE 1
#endif
#if HOST_IS_CASE_SENSITIVE
enum FixPathCaseBehavior {
FPC_FILE_MUST_EXIST, // all path components must exist (rmdir, move from)
FPC_PATH_MUST_EXIST, // all except the last one must exist - still tries to fix last one (fopen, move to)
FPC_PARTIAL_ALLOWED, // don't care how many exist (mkdir recursive)
};
bool FixPathCase(const Path &basePath, std::string &path, FixPathCaseBehavior behavior);
#endif
struct DirectoryFileHandle { struct DirectoryFileHandle {
enum Flags { enum Flags {
NORMAL, NORMAL,