mirror of
https://github.com/wiiu-env/homebrew_on_menu_plugin.git
synced 2024-11-22 10:39:16 +01:00
Guard access to global variables with mutex
This commit is contained in:
parent
8c107d78a3
commit
b845992d44
@ -2,6 +2,7 @@
|
|||||||
#include "fileinfos.h"
|
#include "fileinfos.h"
|
||||||
#include "utils/logger.h"
|
#include "utils/logger.h"
|
||||||
#include <coreinit/cache.h>
|
#include <coreinit/cache.h>
|
||||||
|
#include <coreinit/mutex.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -9,15 +10,20 @@
|
|||||||
|
|
||||||
FileHandleWrapper gFileHandleWrapper[FILE_WRAPPER_SIZE] __attribute__((section(".data")));
|
FileHandleWrapper gFileHandleWrapper[FILE_WRAPPER_SIZE] __attribute__((section(".data")));
|
||||||
|
|
||||||
|
extern OSMutex fileWrapperMutex;
|
||||||
|
|
||||||
int FileHandleWrapper_GetSlot() {
|
int FileHandleWrapper_GetSlot() {
|
||||||
|
OSLockMutex(&fileWrapperMutex);
|
||||||
|
int res = -1;
|
||||||
for (int i = 0; i < FILE_WRAPPER_SIZE; i++) {
|
for (int i = 0; i < FILE_WRAPPER_SIZE; i++) {
|
||||||
if (!gFileHandleWrapper[i].inUse) {
|
if (!gFileHandleWrapper[i].inUse) {
|
||||||
gFileHandleWrapper[i].inUse = true;
|
gFileHandleWrapper[i].inUse = true;
|
||||||
DCFlushRange(&gFileHandleWrapper[i], sizeof(FileHandleWrapper));
|
res = i;
|
||||||
return i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
OSMemoryBarrier();
|
||||||
|
OSUnlockMutex(&fileWrapperMutex);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int OpenFileForID(int id, const char *filepath, int *handle) {
|
int OpenFileForID(int id, const char *filepath, int *handle) {
|
||||||
|
@ -1,24 +1,33 @@
|
|||||||
#include "fileinfos.h"
|
#include "fileinfos.h"
|
||||||
#include "utils/logger.h"
|
#include "utils/logger.h"
|
||||||
|
#include <coreinit/cache.h>
|
||||||
|
#include <coreinit/mutex.h>
|
||||||
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <rpxloader.h>
|
#include <rpxloader.h>
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
FileInfos gFileInfos[FILE_INFO_SIZE] __attribute__((section(".data")));
|
FileInfos gFileInfos[FILE_INFO_SIZE] __attribute__((section(".data")));
|
||||||
|
extern OSMutex fileinfoMutex;
|
||||||
|
|
||||||
int32_t getIDByLowerTitleID(uint32_t lowerTitleID) {
|
int32_t getIDByLowerTitleID(uint32_t lowerTitleID) {
|
||||||
|
OSLockMutex(&fileinfoMutex);
|
||||||
|
int res = -1;
|
||||||
for (int i = 0; i < FILE_INFO_SIZE; i++) {
|
for (int i = 0; i < FILE_INFO_SIZE; i++) {
|
||||||
if (strlen(gFileInfos[i].path) > 0 && gFileInfos[i].lowerTitleID == lowerTitleID) {
|
if (strlen(gFileInfos[i].path) > 0 && gFileInfos[i].lowerTitleID == lowerTitleID) {
|
||||||
return i;
|
res = i;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
OSMemoryBarrier();
|
||||||
|
OSUnlockMutex(&fileinfoMutex);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unmountRomfs(uint32_t id) {
|
void unmountRomfs(uint32_t id) {
|
||||||
if (id >= FILE_INFO_SIZE) {
|
if (id >= FILE_INFO_SIZE) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
OSLockMutex(&fileinfoMutex);
|
||||||
if (gFileInfos[id].romfsMounted) {
|
if (gFileInfos[id].romfsMounted) {
|
||||||
char romName[10];
|
char romName[10];
|
||||||
snprintf(romName, 10, "%08X", id);
|
snprintf(romName, 10, "%08X", id);
|
||||||
@ -27,6 +36,8 @@ void unmountRomfs(uint32_t id) {
|
|||||||
DEBUG_FUNCTION_LINE("res: %d", res);
|
DEBUG_FUNCTION_LINE("res: %d", res);
|
||||||
gFileInfos[id].romfsMounted = false;
|
gFileInfos[id].romfsMounted = false;
|
||||||
}
|
}
|
||||||
|
OSMemoryBarrier();
|
||||||
|
OSUnlockMutex(&fileinfoMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void unmountAllRomfs() {
|
void unmountAllRomfs() {
|
||||||
@ -40,6 +51,8 @@ bool mountRomfs(uint32_t id) {
|
|||||||
DEBUG_FUNCTION_LINE("HANDLE WAS TOO BIG %d", id);
|
DEBUG_FUNCTION_LINE("HANDLE WAS TOO BIG %d", id);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
OSLockMutex(&fileinfoMutex);
|
||||||
|
bool result = false;
|
||||||
if (!gFileInfos[id].romfsMounted) {
|
if (!gFileInfos[id].romfsMounted) {
|
||||||
char buffer[256];
|
char buffer[256];
|
||||||
snprintf(buffer, 256, "/vol/external01/%s", gFileInfos[id].path);
|
snprintf(buffer, 256, "/vol/external01/%s", gFileInfos[id].path);
|
||||||
@ -50,11 +63,14 @@ bool mountRomfs(uint32_t id) {
|
|||||||
if ((res = RL_MountBundle(romName, buffer, BundleSource_FileDescriptor_CafeOS)) == 0) {
|
if ((res = RL_MountBundle(romName, buffer, BundleSource_FileDescriptor_CafeOS)) == 0) {
|
||||||
DEBUG_FUNCTION_LINE("Mounted successfully ");
|
DEBUG_FUNCTION_LINE("Mounted successfully ");
|
||||||
gFileInfos[id].romfsMounted = true;
|
gFileInfos[id].romfsMounted = true;
|
||||||
return true;
|
result = true;
|
||||||
} else {
|
} else {
|
||||||
DEBUG_FUNCTION_LINE("Mounting failed %d", res);
|
DEBUG_FUNCTION_LINE("Mounting failed %d", res);
|
||||||
return false;
|
result = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
OSMemoryBarrier();
|
||||||
|
OSUnlockMutex(&fileinfoMutex);
|
||||||
|
return result;
|
||||||
}
|
}
|
10
src/main.cpp
10
src/main.cpp
@ -8,6 +8,7 @@
|
|||||||
#include <coreinit/debug.h>
|
#include <coreinit/debug.h>
|
||||||
#include <coreinit/filesystem.h>
|
#include <coreinit/filesystem.h>
|
||||||
#include <coreinit/mcp.h>
|
#include <coreinit/mcp.h>
|
||||||
|
#include <coreinit/mutex.h>
|
||||||
#include <coreinit/systeminfo.h>
|
#include <coreinit/systeminfo.h>
|
||||||
#include <coreinit/title.h>
|
#include <coreinit/title.h>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
@ -49,11 +50,16 @@ extern "C" void _SYSLaunchTitleWithStdArgsInNoSplash(uint64_t, uint32_t);
|
|||||||
|
|
||||||
WUPS_USE_WUT_DEVOPTAB();
|
WUPS_USE_WUT_DEVOPTAB();
|
||||||
|
|
||||||
|
OSMutex fileWrapperMutex;
|
||||||
|
OSMutex fileinfoMutex;
|
||||||
|
|
||||||
INITIALIZE_PLUGIN() {
|
INITIALIZE_PLUGIN() {
|
||||||
memset((void *) ¤t_launched_title_info, 0, sizeof(current_launched_title_info));
|
memset((void *) ¤t_launched_title_info, 0, sizeof(current_launched_title_info));
|
||||||
memset((void *) &gLaunchXML, 0, sizeof(gLaunchXML));
|
memset((void *) &gLaunchXML, 0, sizeof(gLaunchXML));
|
||||||
memset((void *) &gFileInfos, 0, sizeof(gFileInfos));
|
memset((void *) &gFileInfos, 0, sizeof(gFileInfos));
|
||||||
gHomebrewLaunched = FALSE;
|
gHomebrewLaunched = FALSE;
|
||||||
|
OSInitMutex(&fileWrapperMutex);
|
||||||
|
OSInitMutex(&fileinfoMutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
ON_APPLICATION_START() {
|
ON_APPLICATION_START() {
|
||||||
@ -266,6 +272,7 @@ DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t *outTitleCount,
|
|||||||
int32_t result = real_MCP_TitleList(handle, outTitleCount, titleList, size);
|
int32_t result = real_MCP_TitleList(handle, outTitleCount, titleList, size);
|
||||||
uint32_t titlecount = *outTitleCount;
|
uint32_t titlecount = *outTitleCount;
|
||||||
|
|
||||||
|
OSLockMutex(&fileinfoMutex);
|
||||||
for (auto &gFileInfo : gFileInfos) {
|
for (auto &gFileInfo : gFileInfos) {
|
||||||
if (gFileInfo.lowerTitleID == 0) {
|
if (gFileInfo.lowerTitleID == 0) {
|
||||||
break;
|
break;
|
||||||
@ -273,6 +280,7 @@ DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t *outTitleCount,
|
|||||||
memcpy(&(titleList[titlecount]), &(gFileInfo.titleInfo), sizeof(gFileInfo.titleInfo));
|
memcpy(&(titleList[titlecount]), &(gFileInfo.titleInfo), sizeof(gFileInfo.titleInfo));
|
||||||
titlecount++;
|
titlecount++;
|
||||||
}
|
}
|
||||||
|
OSUnlockMutex(&fileinfoMutex);
|
||||||
|
|
||||||
*outTitleCount = titlecount;
|
*outTitleCount = titlecount;
|
||||||
|
|
||||||
@ -345,6 +353,7 @@ DECL_FUNCTION(FSStatus, FSCloseFile, FSClient *client, FSCmdBlock *block, FSFile
|
|||||||
} else if ((handle & 0xFF000000) == 0xFF000000) {
|
} else if ((handle & 0xFF000000) == 0xFF000000) {
|
||||||
int32_t fd = (handle & 0x00000FFF);
|
int32_t fd = (handle & 0x00000FFF);
|
||||||
int32_t romid = (handle & 0x00FFF000) >> 12;
|
int32_t romid = (handle & 0x00FFF000) >> 12;
|
||||||
|
OSLockMutex(&fileinfoMutex);
|
||||||
uint32_t rl_handle = gFileHandleWrapper[fd].handle;
|
uint32_t rl_handle = gFileHandleWrapper[fd].handle;
|
||||||
RL_FileClose(rl_handle);
|
RL_FileClose(rl_handle);
|
||||||
if (gFileInfos[romid].openedFiles--) {
|
if (gFileInfos[romid].openedFiles--) {
|
||||||
@ -354,6 +363,7 @@ DECL_FUNCTION(FSStatus, FSCloseFile, FSClient *client, FSCmdBlock *block, FSFile
|
|||||||
unmountRomfs(romid);
|
unmountRomfs(romid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
OSUnlockMutex(&fileinfoMutex);
|
||||||
return FS_STATUS_OK;
|
return FS_STATUS_OK;
|
||||||
}
|
}
|
||||||
return real_FSCloseFile(client, block, handle, flags);
|
return real_FSCloseFile(client, block, handle, flags);
|
||||||
|
Loading…
Reference in New Issue
Block a user