From 0a6cca34028dd471f152809b3a8e2f49d2b1c19d Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 7 Feb 2025 15:53:14 +0100 Subject: [PATCH] Clean up --- src/FSWrapper.cpp | 60 +++++++++++++++------------- src/FSWrapper.h | 18 +++++---- src/FSWrapperMergeDirsWithParent.cpp | 37 +++++++++-------- src/FSWrapperMergeDirsWithParent.h | 10 ++--- src/FileUtils.cpp | 50 ++++++++++++----------- src/FileUtils.h | 4 +- src/IFSWrapper.h | 14 +++++-- src/export.cpp | 13 +++--- src/utils/StringTools.h | 4 +- src/utils/utils.cpp | 2 +- src/utils/utils.h | 2 +- 11 files changed, 117 insertions(+), 97 deletions(-) diff --git a/src/FSWrapper.cpp b/src/FSWrapper.cpp index 5a151c1..e9ba5c9 100644 --- a/src/FSWrapper.cpp +++ b/src/FSWrapper.cpp @@ -3,12 +3,15 @@ #include "utils/StringTools.h" #include "utils/logger.h" #include "utils/utils.h" -#include + + #include #include -#include -#include + +#include #include + +#include #include #include #include @@ -68,7 +71,7 @@ FSError FSWrapper::FSOpenDirWrapper(const char *path, FSDirectoryHandle *handle) return result; } -FSError FSWrapper::FSReadDirWrapper(FSDirectoryHandle handle, FSDirectoryEntry *entry) { +FSError FSWrapper::FSReadDirWrapper(const FSDirectoryHandle handle, FSDirectoryEntry *entry) { if (!isValidDirHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -139,7 +142,7 @@ FSError FSWrapper::FSReadDirWrapper(FSDirectoryHandle handle, FSDirectoryEntry * return result; } -FSError FSWrapper::FSCloseDirWrapper(FSDirectoryHandle handle) { +FSError FSWrapper::FSCloseDirWrapper(const FSDirectoryHandle handle) { if (!isValidDirHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -158,7 +161,7 @@ FSError FSWrapper::FSCloseDirWrapper(FSDirectoryHandle handle) { return result; } -FSError FSWrapper::FSRewindDirWrapper(FSDirectoryHandle handle) { +FSError FSWrapper::FSRewindDirWrapper(const FSDirectoryHandle handle) { if (!isValidDirHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -295,7 +298,7 @@ FSError FSWrapper::FSOpenFileWrapper(const char *path, const char *mode, FSFileH return result; } -FSError FSWrapper::FSCloseFileWrapper(FSFileHandle handle) { +FSError FSWrapper::FSCloseFileWrapper(const FSFileHandle handle) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -360,7 +363,7 @@ FSError FSWrapper::FSGetStatWrapper(const char *path, FSStat *stats) { return result; } -FSError FSWrapper::FSGetStatFileWrapper(FSFileHandle handle, FSStat *stats) { +FSError FSWrapper::FSGetStatFileWrapper(const FSFileHandle handle, FSStat *stats) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -382,7 +385,7 @@ FSError FSWrapper::FSGetStatFileWrapper(FSFileHandle handle, FSStat *stats) { return result; } -FSError FSWrapper::FSReadFileWrapper(void *buffer, uint32_t size, uint32_t count, FSFileHandle handle, [[maybe_unused]] uint32_t unk1) { +FSError FSWrapper::FSReadFileWrapper(void *buffer, const uint32_t size, const uint32_t count, const FSFileHandle handle, [[maybe_unused]] uint32_t unk1) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -417,7 +420,7 @@ FSError FSWrapper::FSReadFileWrapper(void *buffer, uint32_t size, uint32_t count return result; } -FSError FSWrapper::FSReadFileWithPosWrapper(void *buffer, uint32_t size, uint32_t count, uint32_t pos, FSFileHandle handle, int32_t unk1) { +FSError FSWrapper::FSReadFileWithPosWrapper(void *buffer, const uint32_t size, const uint32_t count, const uint32_t pos, const FSFileHandle handle, const int32_t unk1) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -432,7 +435,7 @@ FSError FSWrapper::FSReadFileWithPosWrapper(void *buffer, uint32_t size, uint32_ return result; } -FSError FSWrapper::FSSetPosFileWrapper(FSFileHandle handle, uint32_t pos) { +FSError FSWrapper::FSSetPosFileWrapper(const FSFileHandle handle, const uint32_t pos) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -457,7 +460,7 @@ FSError FSWrapper::FSSetPosFileWrapper(FSFileHandle handle, uint32_t pos) { return result; } -FSError FSWrapper::FSGetPosFileWrapper(FSFileHandle handle, uint32_t *pos) { +FSError FSWrapper::FSGetPosFileWrapper(const FSFileHandle handle, uint32_t *pos) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -478,7 +481,7 @@ FSError FSWrapper::FSGetPosFileWrapper(FSFileHandle handle, uint32_t *pos) { return result; } -FSError FSWrapper::FSIsEofWrapper(FSFileHandle handle) { +FSError FSWrapper::FSIsEofWrapper(const FSFileHandle handle) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -509,7 +512,7 @@ FSError FSWrapper::FSIsEofWrapper(FSFileHandle handle) { return result; } -FSError FSWrapper::FSTruncateFileWrapper(FSFileHandle handle) { +FSError FSWrapper::FSTruncateFileWrapper(const FSFileHandle handle) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -542,7 +545,7 @@ FSError FSWrapper::FSTruncateFileWrapper(FSFileHandle handle) { return result; } -FSError FSWrapper::FSWriteFileWrapper(const uint8_t *buffer, uint32_t size, uint32_t count, FSFileHandle handle, [[maybe_unused]] uint32_t unk1) { +FSError FSWrapper::FSWriteFileWrapper(const uint8_t *buffer, const uint32_t size, const uint32_t count, const FSFileHandle handle, [[maybe_unused]] uint32_t unk1) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -569,7 +572,7 @@ FSError FSWrapper::FSWriteFileWrapper(const uint8_t *buffer, uint32_t size, uint result = FS_ERROR_MEDIA_ERROR; } } else { - result = static_cast(((uint32_t) writeRes) / size); + result = static_cast(static_cast(writeRes) / size); } return result; @@ -630,7 +633,7 @@ FSError FSWrapper::FSRenameWrapper(const char *oldPath, const char *newPath) { return FS_ERROR_OK; } -FSError FSWrapper::FSFlushFileWrapper(FSFileHandle handle) { +FSError FSWrapper::FSFlushFileWrapper(const FSFileHandle handle) { if (!isValidFileHandle(handle)) { return FS_ERROR_FORCE_PARENT_LAYER; } @@ -639,8 +642,8 @@ FSError FSWrapper::FSFlushFileWrapper(FSFileHandle handle) { return FS_ERROR_ACCESS_ERROR; } - auto fileHandle = getFileFromHandle(handle); - int real_fd = fileHandle->fd; + const auto fileHandle = getFileFromHandle(handle); + const int real_fd = fileHandle->fd; DEBUG_FUNCTION_LINE_VERBOSE("[%s] fsync fd %08X (FSFileHandle %08X)", real_fd, handle); FSError result = FS_ERROR_OK; @@ -682,7 +685,7 @@ std::string FSWrapper::GetNewPath(const std::string_view &path) { auto subStr = path.substr(this->pPathToReplace.length()); auto res = string_format("%s%.*s", this->pReplacePathWith.c_str(), int(subStr.length()), subStr.data()); - std::replace(res.begin(), res.end(), '\\', '/'); + std::ranges::replace(res, '\\', '/'); uint32_t length = res.size(); @@ -700,15 +703,16 @@ std::string FSWrapper::GetNewPath(const std::string_view &path) { } bool FSWrapper::isValidFileHandle(FSFileHandle handle) { - std::lock_guard lock(openFilesMutex); + std::lock_guard lock(openFilesMutex); return std::ranges::any_of(openFiles, [handle](auto &cur) { return cur->handle == handle; }); } bool FSWrapper::isValidDirHandle(FSDirectoryHandle handle) { - std::lock_guard lock(openDirsMutex); + std::lock_guard lock(openDirsMutex); return std::ranges::any_of(openDirs, [handle](auto &cur) { return cur->handle == handle; }); } + std::shared_ptr FSWrapper::getNewFileHandle() { return make_shared_nothrow(); } @@ -717,8 +721,8 @@ std::shared_ptr FSWrapper::getNewDirHandle() { return make_shared_nothrow(); } -std::shared_ptr FSWrapper::getFileFromHandle(FSFileHandle handle) { - std::lock_guard lock(openFilesMutex); +std::shared_ptr FSWrapper::getFileFromHandle(const FSFileHandle handle) { + std::lock_guard lock(openFilesMutex); for (auto &file : openFiles) { if (file->handle == handle) { return file; @@ -729,8 +733,8 @@ std::shared_ptr FSWrapper::getFileFromHandle(FSFileHandle handle) { return nullptr; } -std::shared_ptr FSWrapper::getDirFromHandle(FSDirectoryHandle handle) { - std::lock_guard lock(openDirsMutex); +std::shared_ptr FSWrapper::getDirFromHandle(const FSDirectoryHandle handle) { + std::lock_guard lock(openDirsMutex); for (auto &dir : openDirs) { if (dir->handle == handle) { return dir; @@ -742,13 +746,13 @@ std::shared_ptr FSWrapper::getDirFromHandle(FSDirectoryHandle handle) { } void FSWrapper::deleteDirHandle(FSDirectoryHandle handle) { - if (!remove_locked_first_if(openDirsMutex, openDirs, [handle](auto &cur) { return (FSFileHandle) cur->handle == handle; })) { + if (!remove_locked_first_if(openDirsMutex, openDirs, [handle](auto &cur) { return static_cast(cur->handle) == handle; })) { DEBUG_FUNCTION_LINE_ERR("[%s] Delete failed because the handle %08X was not found", getName().c_str(), handle); } } void FSWrapper::deleteFileHandle(FSFileHandle handle) { - if (!remove_locked_first_if(openFilesMutex, openFiles, [handle](auto &cur) { return (FSFileHandle) cur->handle == handle; })) { + if (!remove_locked_first_if(openFilesMutex, openFiles, [handle](auto &cur) { return static_cast(cur->handle) == handle; })) { DEBUG_FUNCTION_LINE_ERR("[%s] Delete failed because the handle %08X was not found", getName().c_str(), handle); } } diff --git a/src/FSWrapper.h b/src/FSWrapper.h index b9a2eb6..b3a2005 100644 --- a/src/FSWrapper.h +++ b/src/FSWrapper.h @@ -1,16 +1,19 @@ #pragma once + #include "DirInfo.h" #include "FileInfo.h" #include "IFSWrapper.h" #include "utils/logger.h" + #include -#include + +#include #include #include class FSWrapper : public IFSWrapper { public: - FSWrapper(const std::string &name, const std::string &pathToReplace, const std::string &replacePathWith, bool fallbackOnError, bool isWriteable) { + FSWrapper(const std::string &name, const std::string &pathToReplace, const std::string &replacePathWith, const bool fallbackOnError, const bool isWriteable) { this->pName = name; this->pPathToReplace = pathToReplace; this->pReplacePathWith = replacePathWith; @@ -18,16 +21,16 @@ public: this->pIsWriteable = isWriteable; this->pCheckIfDeleted = fallbackOnError; - std::replace(pPathToReplace.begin(), pPathToReplace.end(), '\\', '/'); - std::replace(pReplacePathWith.begin(), pReplacePathWith.end(), '\\', '/'); + std::ranges::replace(pPathToReplace, '\\', '/'); + std::ranges::replace(pReplacePathWith, '\\', '/'); } ~FSWrapper() override { { - std::lock_guard lockFiles(openFilesMutex); + std::lock_guard lockFiles(openFilesMutex); openFiles.clear(); } { - std::lock_guard lockDirs(openDirsMutex); + std::lock_guard lockDirs(openDirsMutex); openDirs.clear(); } } @@ -35,7 +38,6 @@ public: FSError FSOpenDirWrapper(const char *path, FSDirectoryHandle *handle) override; - FSError FSReadDirWrapper(FSDirectoryHandle handle, FSDirectoryEntry *entry) override; @@ -96,7 +98,7 @@ public: FSError FSFlushFileWrapper(FSFileHandle handle) override; uint32_t getLayerId() override { - return (uint32_t) this; + return getHandle(); } protected: diff --git a/src/FSWrapperMergeDirsWithParent.cpp b/src/FSWrapperMergeDirsWithParent.cpp index df82c5b..a641bd0 100644 --- a/src/FSWrapperMergeDirsWithParent.cpp +++ b/src/FSWrapperMergeDirsWithParent.cpp @@ -2,9 +2,11 @@ #include "utils/StringTools.h" #include "utils/logger.h" #include "utils/utils.h" + #include #include #include + #include FSError FSWrapperMergeDirsWithParent::FSOpenDirWrapper(const char *path, @@ -27,11 +29,11 @@ FSError FSWrapperMergeDirsWithParent::FSOpenDirWrapper(const char *path, dirHandle->readResultNumberOfEntries = 0; dirHandle->realDirHandle = 0; - if (clientHandle) { + if (mClientHandle) { FSADirectoryHandle realHandle = 0; DEBUG_FUNCTION_LINE_VERBOSE("[%s] Call FSAOpenDir with %s for parent layer", getName().c_str(), path); FSError err; - if ((err = FSAOpenDir(clientHandle, path, &realHandle)) == FS_ERROR_OK) { + if ((err = FSAOpenDir(mClientHandle, path, &realHandle)) == FS_ERROR_OK) { dirHandle->realDirHandle = realHandle; } else { DEBUG_FUNCTION_LINE_ERR("[%s] Failed to open real dir %s. %s (%d)", getName().c_str(), path, FSAGetStatusStr(err), err); @@ -58,6 +60,10 @@ FSError FSWrapperMergeDirsWithParent::FSReadDirWrapper(FSADirectoryHandle handle return FS_ERROR_INVALID_DIRHANDLE; } auto dirHandle = getDirExFromHandle(handle); + if (!dirHandle) { + DEBUG_FUNCTION_LINE_ERR("[%s] No valid dir handle %08X", getName().c_str(), handle); + return FS_ERROR_INVALID_DIRHANDLE; + } if (res == FS_ERROR_OK) { if (dirHandle->readResultCapacity == 0) { dirHandle->readResult = (FSDirectoryEntryEx *) malloc(sizeof(FSDirectoryEntryEx)); @@ -92,16 +98,15 @@ FSError FSWrapperMergeDirsWithParent::FSReadDirWrapper(FSADirectoryHandle handle } OSMemoryBarrier(); - } else if (res == FS_ERROR_END_OF_DIR) { // Read the real directory. if (dirHandle->realDirHandle != 0) { - if (clientHandle) { + if (mClientHandle) { FSADirectoryEntry realDirEntry; FSError readDirResult; while (true) { DEBUG_FUNCTION_LINE_VERBOSE("[%s] Call FSReadDir with %08X for parent layer", getName().c_str(), dirHandle->realDirHandle); - readDirResult = FSAReadDir(clientHandle, dirHandle->realDirHandle, &realDirEntry); + readDirResult = FSAReadDir(mClientHandle, dirHandle->realDirHandle, &realDirEntry); if (readDirResult == FS_ERROR_OK) { bool found = false; auto nameDeleted = deletePrefix + realDirEntry.name; @@ -156,9 +161,9 @@ FSError FSWrapperMergeDirsWithParent::FSCloseDirWrapper(FSADirectoryHandle handl } auto dirHandle = getDirExFromHandle(handle); if (dirHandle->realDirHandle != 0) { - if (clientHandle) { + if (mClientHandle) { DEBUG_FUNCTION_LINE_VERBOSE("[%s] Call FSCloseDir with %08X for parent layer", getName().c_str(), dirHandle->realDirHandle); - auto realResult = FSACloseDir(clientHandle, dirHandle->realDirHandle); + auto realResult = FSACloseDir(mClientHandle, dirHandle->realDirHandle); if (realResult == FS_ERROR_OK) { dirHandle->realDirHandle = 0; } else { @@ -201,10 +206,10 @@ FSError FSWrapperMergeDirsWithParent::FSRewindDirWrapper(FSADirectoryHandle hand } if (dirHandle->realDirHandle != 0) { - if (clientHandle) { + if (mClientHandle) { DEBUG_FUNCTION_LINE_VERBOSE("[%s] Call FSARewindDir with %08X for parent layer", getName().c_str(), dirHandle->realDirHandle); FSError err; - if ((err = FSARewindDir(clientHandle, dirHandle->realDirHandle)) == FS_ERROR_OK) { + if ((err = FSARewindDir(mClientHandle, dirHandle->realDirHandle)) == FS_ERROR_OK) { dirHandle->realDirHandle = 0; } else { DEBUG_FUNCTION_LINE_ERR("[%s] Failed to rewind dir for realDirHandle %08X. %s (%d)", getName().c_str(), dirHandle->realDirHandle, FSAGetStatusStr(err), err); @@ -229,20 +234,20 @@ FSWrapperMergeDirsWithParent::FSWrapperMergeDirsWithParent(const std::string &na fallbackOnError, false) { FSAInit(); - this->clientHandle = FSAAddClient(nullptr); - if (clientHandle < 0) { - DEBUG_FUNCTION_LINE_ERR("[%s] FSAClientHandle failed: %s (%d)", name.c_str(), FSAGetStatusStr(static_cast(clientHandle)), clientHandle); - clientHandle = 0; + this->mClientHandle = FSAAddClient(nullptr); + if (mClientHandle < 0) { + DEBUG_FUNCTION_LINE_ERR("[%s] FSAClientHandle failed: %s (%d)", name.c_str(), FSAGetStatusStr(static_cast(mClientHandle)), mClientHandle); + mClientHandle = 0; } } FSWrapperMergeDirsWithParent::~FSWrapperMergeDirsWithParent() { - if (clientHandle) { + if (mClientHandle) { FSError res; - if ((res = FSADelClient(clientHandle)) != FS_ERROR_OK) { + if ((res = FSADelClient(mClientHandle)) != FS_ERROR_OK) { DEBUG_FUNCTION_LINE_ERR("[%s] FSADelClient failed: %s (%d)", FSAGetStatusStr(res), res); } - clientHandle = 0; + mClientHandle = 0; } } diff --git a/src/FSWrapperMergeDirsWithParent.h b/src/FSWrapperMergeDirsWithParent.h index 24a56f1..0458d71 100644 --- a/src/FSWrapperMergeDirsWithParent.h +++ b/src/FSWrapperMergeDirsWithParent.h @@ -1,10 +1,10 @@ #pragma once #include "DirInfoEx.h" #include "FSWrapper.h" -#include -#include -class FSWrapperMergeDirsWithParent : public FSWrapper { +#include + +class FSWrapperMergeDirsWithParent final : public FSWrapper { public: FSWrapperMergeDirsWithParent(const std::string &name, const std::string &pathToReplace, @@ -28,11 +28,11 @@ public: bool SkipDeletedFilesInReadDir() override; uint32_t getLayerId() override { - return (uint32_t) clientHandle; + return static_cast(mClientHandle); } private: - FSAClientHandle clientHandle; + FSAClientHandle mClientHandle; std::shared_ptr getDirExFromHandle(FSDirectoryHandle handle); }; diff --git a/src/FileUtils.cpp b/src/FileUtils.cpp index 1165536..ef23d62 100644 --- a/src/FileUtils.cpp +++ b/src/FileUtils.cpp @@ -11,39 +11,41 @@ #include #include -std::mutex workingDirMutex; -std::map workingDirs; +namespace { + std::mutex sWorkingDirMutex; + std::map sWorkingDirs; +} // namespace -std::mutex fsLayerMutex; -std::vector> fsLayers; +std::mutex gFSLayerMutex; +std::vector> gFSLayers; -std::string getFullPathGeneric(FSAClientHandle client, const char *path, std::mutex &mutex, std::map &map) { - std::lock_guard workingDirLock(mutex); +std::string getFullPathGeneric(const FSAClientHandle client, const char *path, std::mutex &mutex, const std::map &map) { + std::lock_guard workingDirLock(mutex); std::string res; if (path[0] != '/' && path[0] != '\\') { if (map.count(client) == 0) { DEBUG_FUNCTION_LINE_WARN("No working dir found for client %08X, fallback to \"/\"", client); - workingDirs[client] = "/"; + sWorkingDirs[client] = "/"; } res = string_format("%s%s", map.at(client).c_str(), path); } else { res = path; } - std::replace(res.begin(), res.end(), '\\', '/'); + std::ranges::replace(res, '\\', '/'); return res; } -void setWorkingDirGeneric(FSAClientHandle client, const char *path, std::mutex &mutex, std::map &map) { +void setWorkingDirGeneric(const FSAClientHandle client, const char *path, std::mutex &mutex, std::map &map) { if (!path) { DEBUG_FUNCTION_LINE_WARN("Path was NULL"); return; } - std::lock_guard workingDirLock(mutex); + std::lock_guard workingDirLock(mutex); std::string cwd(path); if (cwd.empty() || cwd.back() != '/') { @@ -54,22 +56,22 @@ void setWorkingDirGeneric(FSAClientHandle client, const char *path, std::mutex & } -std::string getFullPath(FSAClientHandle pClient, const char *path) { - return getFullPathGeneric(pClient, path, workingDirMutex, workingDirs); +std::string getFullPath(const FSAClientHandle pClient, const char *path) { + return getFullPathGeneric(pClient, path, sWorkingDirMutex, sWorkingDirs); } -void setWorkingDir(FSAClientHandle client, const char *path) { - setWorkingDirGeneric(client, path, workingDirMutex, workingDirs); +void setWorkingDir(const FSAClientHandle client, const char *path) { + setWorkingDirGeneric(client, path, sWorkingDirMutex, sWorkingDirs); } void clearFSLayer() { { - std::lock_guard workingDirLock(workingDirMutex); - workingDirs.clear(); + std::lock_guard workingDirLock(sWorkingDirMutex); + sWorkingDirs.clear(); } { - std::lock_guard layerLock(fsLayerMutex); - fsLayers.clear(); + std::lock_guard layerLock(gFSLayerMutex); + gFSLayers.clear(); } } @@ -93,11 +95,11 @@ bool sendMessageToThread(FSShimWrapperMessage *param) { } FSError doForLayer(FSShimWrapper *param) { - std::lock_guard lock(fsLayerMutex); - if (!fsLayers.empty()) { - uint32_t startIndex = fsLayers.size(); - for (uint32_t i = fsLayers.size(); i > 0; i--) { - if ((uint32_t) fsLayers[i - 1]->getLayerId() == param->shim->clientHandle) { + std::lock_guard lock(gFSLayerMutex); + if (!gFSLayers.empty()) { + uint32_t startIndex = gFSLayers.size(); + for (uint32_t i = gFSLayers.size(); i > 0; i--) { + if (gFSLayers[i - 1]->getLayerId() == param->shim->clientHandle) { startIndex = i - 1; break; } @@ -105,7 +107,7 @@ FSError doForLayer(FSShimWrapper *param) { if (startIndex > 0) { for (uint32_t i = startIndex; i > 0; i--) { - auto &layer = fsLayers[i - 1]; + auto &layer = gFSLayers[i - 1]; if (!layer->isActive()) { continue; } diff --git a/src/FileUtils.h b/src/FileUtils.h index f31f5df..5095d4a 100644 --- a/src/FileUtils.h +++ b/src/FileUtils.h @@ -55,8 +55,8 @@ struct FSShimWrapperMessage { extern bool gThreadsRunning; extern FSIOThreadData gThreadData[3]; -extern std::mutex fsLayerMutex; -extern std::vector> fsLayers; +extern std::mutex gFSLayerMutex; +extern std::vector> gFSLayers; #define fsaShimPrepareRequestReadFile ((FSError(*)(FSAShimBuffer * shim, IOSHandle clientHandle, uint8_t * buffer, uint32_t size, uint32_t count, uint32_t pos, FSFileHandle handle, FSAReadFlag readFlags))(0x101C400 + 0x436cc)) #define fsaShimPrepareRequestWriteFile ((FSError(*)(FSAShimBuffer * shim, IOSHandle clientHandle, const uint8_t *buffer, uint32_t size, uint32_t count, uint32_t pos, FSFileHandle handle, FSAWriteFlag writeFlags))(0x101C400 + 0x437f4)) diff --git a/src/IFSWrapper.h b/src/IFSWrapper.h index 528709d..6d2faa7 100644 --- a/src/IFSWrapper.h +++ b/src/IFSWrapper.h @@ -1,6 +1,7 @@ #pragma once #include -#include +#include + #include #define FS_ERROR_EXTRA_MASK 0xFFF00000 @@ -53,7 +54,6 @@ public: return FS_ERROR_FORCE_PARENT_LAYER; } - virtual FSError FSGetStatFileWrapper(FSAFileHandle handle, FSAStat *stats) { return FS_ERROR_FORCE_PARENT_LAYER; @@ -151,11 +151,17 @@ public: virtual uint32_t getLayerId() = 0; virtual uint32_t getHandle() { - return (uint32_t) this; + return reinterpret_cast(this); + } + + IFSWrapper() { + // Abuse this as a stable handle that references itself and survives std::move + *mHandle = reinterpret_cast(mHandle.get()); } private: - bool pIsActive = true; + bool pIsActive = true; + std::unique_ptr mHandle = std::make_unique(); protected: bool pFallbackOnError = false; diff --git a/src/export.cpp b/src/export.cpp index a2d6c24..363ca78 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -15,6 +15,7 @@ struct AOCTitle { WUT_UNKNOWN_BYTES(0x68); }; + WUT_CHECK_SIZE(AOCTitle, 0x68); bool getAOCPath(std::string &outStr) { @@ -90,7 +91,7 @@ end: return result; } -ContentRedirectionApiErrorType CRAddFSLayer(CRLayerHandle *handle, const char *layerName, const char *replacementDir, FSLayerType layerType) { +ContentRedirectionApiErrorType CRAddFSLayer(CRLayerHandle *handle, const char *layerName, const char *replacementDir, const FSLayerType layerType) { if (!handle || layerName == nullptr || replacementDir == nullptr) { DEBUG_FUNCTION_LINE_WARN("CONTENT_REDIRECTION_API_ERROR_INVALID_ARG"); return CONTENT_REDIRECTION_API_ERROR_INVALID_ARG; @@ -132,9 +133,9 @@ ContentRedirectionApiErrorType CRAddFSLayer(CRLayerHandle *handle, const char *l } if (ptr) { DEBUG_FUNCTION_LINE_VERBOSE("Added new layer (%s). Replacement dir: %s Type:%d", layerName, replacementDir, layerType); - std::lock_guard lock(fsLayerMutex); + std::lock_guard lock(gFSLayerMutex); *handle = (CRLayerHandle) ptr->getHandle(); - fsLayers.push_back(std::move(ptr)); + gFSLayers.push_back(std::move(ptr)); return CONTENT_REDIRECTION_API_ERROR_NONE; } DEBUG_FUNCTION_LINE_ERR("Failed to allocate memory"); @@ -142,7 +143,7 @@ ContentRedirectionApiErrorType CRAddFSLayer(CRLayerHandle *handle, const char *l } ContentRedirectionApiErrorType CRRemoveFSLayer(CRLayerHandle handle) { - if (!remove_locked_first_if(fsLayerMutex, fsLayers, [handle](auto &cur) { return (CRLayerHandle) cur->getHandle() == handle; })) { + if (!remove_locked_first_if(gFSLayerMutex, gFSLayers, [handle](auto &cur) { return (CRLayerHandle) cur->getHandle() == handle; })) { DEBUG_FUNCTION_LINE_WARN("CONTENT_REDIRECTION_API_ERROR_LAYER_NOT_FOUND for handle %08X", handle); return CONTENT_REDIRECTION_API_ERROR_LAYER_NOT_FOUND; } @@ -150,8 +151,8 @@ ContentRedirectionApiErrorType CRRemoveFSLayer(CRLayerHandle handle) { } ContentRedirectionApiErrorType CRSetActive(CRLayerHandle handle, bool active) { - std::lock_guard lock(fsLayerMutex); - for (auto &cur : fsLayers) { + std::lock_guard lock(gFSLayerMutex); + for (auto &cur : gFSLayers) { if ((CRLayerHandle) cur->getHandle() == handle) { cur->setActive(active); return CONTENT_REDIRECTION_API_ERROR_NONE; diff --git a/src/utils/StringTools.h b/src/utils/StringTools.h index 78ec91a..996072a 100644 --- a/src/utils/StringTools.h +++ b/src/utils/StringTools.h @@ -13,12 +13,12 @@ std::string string_format(const std::string &format, Args... args) { return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside } -static inline bool starts_with_case_insensitive(std::string_view str, std::string_view prefix) { +static inline bool starts_with_case_insensitive(const std::string_view str, const std::string_view prefix) { if (str.size() < prefix.size()) return false; return std::equal(prefix.begin(), prefix.end(), str.begin(), - [](char a, char b) { + [](const char a, const char b) { return std::tolower(a) == std::tolower(b); }); } diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index 5d338bf..b141d9f 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -54,7 +54,7 @@ FSTime translate_time(time_t timeValue) { return adjustedTimeValue * 1000000; } -void translate_stat(struct stat *posStat, FSStat *fsStat) { +void translate_stat(const struct stat *posStat, FSStat *fsStat) { memset(fsStat, 0, sizeof(FSStat)); fsStat->size = posStat->st_size; diff --git a/src/utils/utils.h b/src/utils/utils.h index a9d7a6f..ef974ce 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -32,4 +32,4 @@ bool remove_locked_first_if(std::mutex &mutex, std::vector &list, #define ROUNDDOWN(val, align) ((val) & ~(align - 1)) #define ROUNDUP(val, align) ROUNDDOWN(((val) + (align - 1)), align) -void translate_stat(struct stat *posStat, FSStat *fsStat); \ No newline at end of file +void translate_stat(const struct stat *posStat, FSStat *fsStat); \ No newline at end of file