Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix save user redirection #44

Merged
merged 4 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ FROM ghcr.io/wiiu-env/devkitppc:20240423

COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20230621 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/wiiumodulesystem:20240424 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libcontentredirection:20240424 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libcontentredirection:20240428 /artifacts $DEVKITPRO

WORKDIR project
18 changes: 7 additions & 11 deletions src/FSWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ FSError FSWrapper::FSReadDirWrapper(FSDirectoryHandle handle, FSDirectoryEntry *
if (strcmp(entry_->d_name, ".") == 0 || strcmp(entry_->d_name, "..") == 0) {
entry->info.size = 0;
} else {
#ifdef _DIRENT_HAVE_D_STAT
translate_stat(&entry_->d_stat, &entry->info);
#else
struct stat sb {};
auto path = string_format("%s/%s", dirHandle->path, entry_->d_name);
std::replace(path.begin(), path.end(), '\\', '/');
Expand All @@ -120,13 +123,14 @@ FSError FSWrapper::FSReadDirWrapper(FSDirectoryHandle handle, FSDirectoryEntry *
result = FS_ERROR_MEDIA_ERROR;
break;
}
#endif
}
}
result = FS_ERROR_OK;
} else {
auto err = errno;
if (err != 0) {
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to read dir %08X (handle %08X)", getName().c_str(), dir, handle);
if (err != 0 && err != 2) { // newlib currently has a bug and doesn't clear errno properly when the end of a dir is reached
DEBUG_FUNCTION_LINE_ERR("[%s] Failed to read dir %08X (handle %08X). errno %d (%s)", getName().c_str(), dir, handle, err, strerror(err));
result = FS_ERROR_MEDIA_ERROR;
}
}
Expand Down Expand Up @@ -670,15 +674,7 @@ bool FSWrapper::IsFileModeAllowed(const char *mode) {
}

bool FSWrapper::IsPathToReplace(const std::string_view &path) {
if (!path.starts_with(pPathToReplace)) {
return false;
}

if (std::ranges::any_of(pIgnorePaths.cbegin(), pIgnorePaths.cend(), [&path](auto &ignorePath) { return path.starts_with(ignorePath); })) {
return false;
}

return true;
return path.starts_with(pPathToReplace);
}

std::string FSWrapper::GetNewPath(const std::string_view &path) {
Expand Down
7 changes: 1 addition & 6 deletions src/FSWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@

class FSWrapper : public IFSWrapper {
public:
FSWrapper(const std::string &name, const std::string &pathToReplace, const std::string &replacePathWith, bool fallbackOnError, bool isWriteable, std::vector<std::string> ignorePaths = {}) {
FSWrapper(const std::string &name, const std::string &pathToReplace, const std::string &replacePathWith, bool fallbackOnError, bool isWriteable) {
this->pName = name;
this->pPathToReplace = pathToReplace;
this->pReplacePathWith = replacePathWith;
this->pFallbackOnError = fallbackOnError;
this->pIsWriteable = isWriteable;
this->pCheckIfDeleted = fallbackOnError;
this->pIgnorePaths = std::move(ignorePaths);

std::replace(pPathToReplace.begin(), pPathToReplace.end(), '\\', '/');
std::replace(pReplacePathWith.begin(), pReplacePathWith.end(), '\\', '/');
for (auto &ignorePath : pIgnorePaths) {
std::replace(ignorePath.begin(), ignorePath.end(), '\\', '/');
}
}
~FSWrapper() override {
{
Expand Down Expand Up @@ -133,7 +129,6 @@ class FSWrapper : public IFSWrapper {
private:
std::string pPathToReplace;
std::string pReplacePathWith;
std::vector<std::string> pIgnorePaths;
bool pIsWriteable = false;
std::mutex openFilesMutex;
std::mutex openDirsMutex;
Expand Down
15 changes: 11 additions & 4 deletions src/export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include "FileUtils.h"
#include "IFSWrapper.h"
#include "malloc.h"
#include "utils/StringTools.h"
#include "utils/logger.h"
#include "utils/utils.h"
#include <content_redirection/redirection.h>
#include <coreinit/dynload.h>
#include <mutex>
#include <nn/act.h>
#include <wums/exports.h>

struct AOCTitle {
Expand Down Expand Up @@ -115,10 +117,15 @@ ContentRedirectionApiErrorType CRAddFSLayer(CRLayerHandle *handle, const char *l
} else if (layerType == FS_LAYER_TYPE_SAVE_REPLACE) {
DEBUG_FUNCTION_LINE_INFO("Redirecting \"/vol/save\" to \"%s\", mode: \"replace\"", replacementDir);
ptr = make_unique_nothrow<FSWrapper>(layerName, "/vol/save", replacementDir, false, true);
} else if (layerType == FS_LAYER_TYPE_SAVE_REPLACE_IGNORE_VOL_SAVE_COMMON) {
DEBUG_FUNCTION_LINE_INFO("Redirecting \"/vol/save\" to \"%s\", mode: \"replace\", ignore: (\"/vol/save/common\")", replacementDir);
std::vector<std::string> ignorePaths({"/vol/save/common"});
ptr = make_unique_nothrow<FSWrapper>(layerName, "/vol/save", replacementDir, false, true, ignorePaths);
} else if (layerType == FS_LAYER_TYPE_SAVE_REPLACE_FOR_CURRENT_USER) {
nn::act::Initialize();
nn::act::PersistentId persistentId = nn::act::GetPersistentId();
nn::act::Finalize();

std::string user = string_format("/vol/save/%08X", 0x80000000 | persistentId);

DEBUG_FUNCTION_LINE_INFO("Redirecting \"%s\" to \"%s\", mode: \"replace\"", user.c_str(), replacementDir);
ptr = make_unique_nothrow<FSWrapper>(layerName, user, 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;
Expand Down