ContentRedirectionModule/src/export.cpp

96 lines
3.4 KiB
C++
Raw Normal View History

2022-04-14 22:41:41 +02:00
#include "FSWrapper.h"
#include "FSWrapperMergeDirsWithParent.h"
#include "FileUtils.h"
#include "IFSWrapper.h"
#include "utils/logger.h"
#include <content_redirection/redirection.h>
#include <coreinit/debug.h>
#include <mutex>
#include <wums/exports.h>
ContentRedirectionApiErrorType CRAddFSLayer(CRLayerHandle *handle, const char *layerName, const char *replacementDir, FSLayerType layerType) {
if (!handle || layerName == nullptr || replacementDir == nullptr) {
DEBUG_FUNCTION_LINE_ERR("CONTENT_REDIRECTION_API_ERROR_INVALID_ARG");
return CONTENT_REDIRECTION_API_ERROR_INVALID_ARG;
}
IFSWrapper *ptr;
if (layerType == FS_LAYER_TYPE_CONTENT_REPLACE) {
ptr = new (std::nothrow) FSWrapper(layerName, "/vol/content", replacementDir, false, false);
} else if (layerType == FS_LAYER_TYPE_CONTENT_MERGE) {
ptr = new (std::nothrow) FSWrapperMergeDirsWithParent(layerName, "/vol/content", replacementDir, true);
} else if (layerType == FS_LAYER_TYPE_SAVE_REPLACE) {
ptr = new (std::nothrow) FSWrapper(layerName, "/vol/save", replacementDir, false, true);
} else {
DEBUG_FUNCTION_LINE_ERR("CONTENT_REDIRECTION_API_ERROR_UNKNOWN_LAYER_DIR_TYPE: %s %s %d", layerName, replacementDir, layerType);
return CONTENT_REDIRECTION_API_ERROR_UNKNOWN_FS_LAYER_TYPE;
}
if (ptr) {
DEBUG_FUNCTION_LINE_VERBOSE("Added new layer (%s). Replacement dir: %s Type:%d", layerName, replacementDir, layerType);
std::lock_guard<std::mutex> lock(fsLayerMutex);
fsLayers.push_back(ptr);
*handle = (CRLayerHandle) ptr;
return CONTENT_REDIRECTION_API_ERROR_NONE;
}
DEBUG_FUNCTION_LINE_ERR("Failed to allocate memory");
return CONTENT_REDIRECTION_API_ERROR_NO_MEMORY;
}
ContentRedirectionApiErrorType CRRemoveFSLayer(CRLayerHandle handle) {
std::lock_guard<std::mutex> lock(fsLayerMutex);
auto count = 0;
auto found = false;
IFSWrapper *layer;
for (auto &cur : fsLayers) {
if ((CRLayerHandle) cur == handle) {
found = true;
layer = cur;
break;
}
count++;
}
if (!found) {
DEBUG_FUNCTION_LINE_ERR("CONTENT_REDIRECTION_API_ERROR_LAYER_NOT_FOUND for handle %08X", handle);
return CONTENT_REDIRECTION_API_ERROR_LAYER_NOT_FOUND;
}
fsLayers.erase(fsLayers.begin() + count);
delete layer;
return CONTENT_REDIRECTION_API_ERROR_NONE;
}
ContentRedirectionApiErrorType CRSetActive(CRLayerHandle handle, bool active) {
std::lock_guard<std::mutex> lock(fsLayerMutex);
auto found = false;
IFSWrapper *layer;
for (auto &cur : fsLayers) {
if ((CRLayerHandle) cur == handle) {
found = true;
layer = cur;
break;
}
}
if (!found) {
DEBUG_FUNCTION_LINE_ERR("CONTENT_REDIRECTION_API_ERROR_LAYER_NOT_FOUND for handle %08X", handle);
return CONTENT_REDIRECTION_API_ERROR_LAYER_NOT_FOUND;
}
layer->setActive(active);
return CONTENT_REDIRECTION_API_ERROR_NONE;
}
uint32_t CRGetVersion() {
return CONTENT_REDIRECT_MODULE_VERSION;
}
int CRAddDevice(const devoptab_t *device) {
return AddDevice(device);
}
int CRRemoveDevice(const char *name) {
return RemoveDevice(name);
}
WUMS_EXPORT_FUNCTION(CRGetVersion);
WUMS_EXPORT_FUNCTION(CRAddFSLayer);
WUMS_EXPORT_FUNCTION(CRRemoveFSLayer);
WUMS_EXPORT_FUNCTION(CRSetActive);
WUMS_EXPORT_FUNCTION(CRAddDevice);
WUMS_EXPORT_FUNCTION(CRRemoveDevice);