2019-11-21 09:48:47 +00:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-11-21 09:48:47 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include "amsmitm_initialization.hpp"
|
|
|
|
#include "amsmitm_fs_utils.hpp"
|
|
|
|
|
|
|
|
namespace ams::mitm::fs {
|
|
|
|
|
2019-12-04 14:53:52 +00:00
|
|
|
using namespace ams::fs;
|
|
|
|
|
2019-11-21 09:48:47 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Globals. */
|
|
|
|
FsFileSystem g_sd_filesystem;
|
|
|
|
|
|
|
|
/* Helpers. */
|
|
|
|
Result EnsureSdInitialized() {
|
2021-10-09 22:49:53 +01:00
|
|
|
R_UNLESS(serviceIsActive(std::addressof(g_sd_filesystem.s)), ams::fs::ResultSdCardNotPresent());
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-11-21 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 14:53:52 +00:00
|
|
|
void FormatAtmosphereRomfsPath(char *dst_path, size_t dst_path_size, ncm::ProgramId program_id, const char *src_path) {
|
|
|
|
return FormatAtmosphereSdPath(dst_path, dst_path_size, program_id, "romfs", src_path);
|
|
|
|
}
|
|
|
|
|
2019-11-21 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGlobalSdCardFileSystem() {
|
2021-10-09 22:49:53 +01:00
|
|
|
R_ABORT_UNLESS(fsOpenSdCardFileSystem(std::addressof(g_sd_filesystem)));
|
2019-11-21 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 11:00:38 +00:00
|
|
|
Result CreateSdFile(const char *path, s64 size, s32 option) {
|
|
|
|
R_TRY(EnsureSdInitialized());
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fsFsCreateFile(std::addressof(g_sd_filesystem), path, size, option));
|
2019-12-06 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 03:50:49 +00:00
|
|
|
Result DeleteSdFile(const char *path) {
|
|
|
|
R_TRY(EnsureSdInitialized());
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fsFsDeleteFile(std::addressof(g_sd_filesystem), path));
|
2021-01-31 03:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasSdFile(const char *path) {
|
|
|
|
if (R_FAILED(EnsureSdInitialized())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FsDirEntryType type;
|
2021-10-09 22:49:53 +01:00
|
|
|
if (R_FAILED(fsFsGetEntryType(std::addressof(g_sd_filesystem), path, std::addressof(type)))) {
|
2021-01-31 03:50:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return type == FsDirEntryType_File;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasAtmosphereSdFile(const char *path) {
|
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), path);
|
|
|
|
return HasSdFile(fixed_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result DeleteAtmosphereSdFile(const char *path) {
|
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(DeleteSdFile(fixed_path));
|
2021-01-31 03:50:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 11:00:38 +00:00
|
|
|
Result CreateAtmosphereSdFile(const char *path, s64 size, s32 option) {
|
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(CreateSdFile(fixed_path, size, option));
|
2019-12-06 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 09:48:47 +00:00
|
|
|
Result OpenSdFile(FsFile *out, const char *path, u32 mode) {
|
|
|
|
R_TRY(EnsureSdInitialized());
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fsFsOpenFile(std::addressof(g_sd_filesystem), path, mode, out));
|
2019-11-21 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenAtmosphereSdFile(FsFile *out, const char *path, u32 mode) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-11-21 09:48:47 +00:00
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(OpenSdFile(out, fixed_path, mode));
|
2019-11-21 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenAtmosphereSdFile(FsFile *out, ncm::ProgramId program_id, const char *path, u32 mode) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-11-21 09:48:47 +00:00
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), program_id, path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(OpenSdFile(out, fixed_path, mode));
|
2019-11-21 09:48:47 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 14:53:52 +00:00
|
|
|
Result OpenAtmosphereSdRomfsFile(FsFile *out, ncm::ProgramId program_id, const char *path, u32 mode) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-12-04 14:53:52 +00:00
|
|
|
FormatAtmosphereRomfsPath(fixed_path, sizeof(fixed_path), program_id, path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(OpenSdFile(out, fixed_path, mode));
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenAtmosphereRomfsFile(FsFile *out, ncm::ProgramId program_id, const char *path, u32 mode, FsFileSystem *fs) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-12-04 14:53:52 +00:00
|
|
|
FormatAtmosphereRomfsPath(fixed_path, sizeof(fixed_path), program_id, path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fsFsOpenFile(fs, fixed_path, mode, out));
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 11:00:38 +00:00
|
|
|
Result CreateSdDirectory(const char *path) {
|
|
|
|
R_TRY(EnsureSdInitialized());
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fsFsCreateDirectory(std::addressof(g_sd_filesystem), path));
|
2019-12-06 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result CreateAtmosphereSdDirectory(const char *path) {
|
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(CreateSdDirectory(fixed_path));
|
2019-12-06 11:00:38 +00:00
|
|
|
}
|
|
|
|
|
2019-12-04 14:53:52 +00:00
|
|
|
Result OpenSdDirectory(FsDir *out, const char *path, u32 mode) {
|
|
|
|
R_TRY(EnsureSdInitialized());
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fsFsOpenDirectory(std::addressof(g_sd_filesystem), path, mode, out));
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenAtmosphereSdDirectory(FsDir *out, const char *path, u32 mode) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-12-04 14:53:52 +00:00
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(OpenSdDirectory(out, fixed_path, mode));
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenAtmosphereSdDirectory(FsDir *out, ncm::ProgramId program_id, const char *path, u32 mode) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-12-04 14:53:52 +00:00
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), program_id, path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(OpenSdDirectory(out, fixed_path, mode));
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenAtmosphereSdRomfsDirectory(FsDir *out, ncm::ProgramId program_id, const char *path, u32 mode) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-12-04 14:53:52 +00:00
|
|
|
FormatAtmosphereRomfsPath(fixed_path, sizeof(fixed_path), program_id, path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(OpenSdDirectory(out, fixed_path, mode));
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenAtmosphereRomfsDirectory(FsDir *out, ncm::ProgramId program_id, const char *path, u32 mode, FsFileSystem *fs) {
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-12-04 14:53:52 +00:00
|
|
|
FormatAtmosphereRomfsPath(fixed_path, sizeof(fixed_path), program_id, path);
|
2022-03-26 21:48:33 +00:00
|
|
|
R_RETURN(fsFsOpenDirectory(fs, fixed_path, mode, out));
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 14:41:47 +00:00
|
|
|
void FormatAtmosphereSdPath(char *dst_path, size_t dst_path_size, const char *src_path) {
|
|
|
|
if (src_path[0] == '/') {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere%s", src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
} else {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere/%s", src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormatAtmosphereSdPath(char *dst_path, size_t dst_path_size, const char *subdir, const char *src_path) {
|
|
|
|
if (src_path[0] == '/') {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere/%s%s", subdir, src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
} else {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere/%s/%s", subdir, src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormatAtmosphereSdPath(char *dst_path, size_t dst_path_size, ncm::ProgramId program_id, const char *src_path) {
|
|
|
|
if (src_path[0] == '/') {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere/contents/%016lx%s", static_cast<u64>(program_id), src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
} else {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere/contents/%016lx/%s", static_cast<u64>(program_id), src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FormatAtmosphereSdPath(char *dst_path, size_t dst_path_size, ncm::ProgramId program_id, const char *subdir, const char *src_path) {
|
|
|
|
if (src_path[0] == '/') {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere/contents/%016lx/%s%s", static_cast<u64>(program_id), subdir, src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
} else {
|
2021-01-12 10:59:41 +00:00
|
|
|
util::SNPrintf(dst_path, dst_path_size, "/atmosphere/contents/%016lx/%s/%s", static_cast<u64>(program_id), subdir, src_path);
|
2020-02-25 14:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:53:52 +00:00
|
|
|
bool HasSdRomfsContent(ncm::ProgramId program_id) {
|
|
|
|
/* Check if romfs.bin is present. */
|
|
|
|
{
|
|
|
|
FsFile romfs_file;
|
2021-10-09 22:49:53 +01:00
|
|
|
if (R_SUCCEEDED(OpenAtmosphereSdFile(std::addressof(romfs_file), program_id, "romfs.bin", OpenMode_Read))) {
|
|
|
|
fsFileClose(std::addressof(romfs_file));
|
2019-12-04 14:53:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for romfs folder with content. */
|
|
|
|
FsDir romfs_dir;
|
2022-07-26 00:24:31 +01:00
|
|
|
if (R_FAILED(OpenAtmosphereSdRomfsDirectory(std::addressof(romfs_dir), program_id, "", fs::OpenDirectoryMode_All))) {
|
2019-12-04 14:53:52 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-09 22:49:53 +01:00
|
|
|
ON_SCOPE_EXIT { fsDirClose(std::addressof(romfs_dir)); };
|
2019-12-04 14:53:52 +00:00
|
|
|
|
|
|
|
/* Verify the folder has at least one entry. */
|
|
|
|
s64 num_entries = 0;
|
2021-10-09 22:49:53 +01:00
|
|
|
return R_SUCCEEDED(fsDirGetEntryCount(std::addressof(romfs_dir), std::addressof(num_entries))) && num_entries > 0;
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result SaveAtmosphereSdFile(FsFile *out, ncm::ProgramId program_id, const char *path, void *data, size_t size) {
|
|
|
|
R_TRY(EnsureSdInitialized());
|
|
|
|
|
2019-12-06 11:00:38 +00:00
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
2019-12-04 14:53:52 +00:00
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), program_id, path);
|
|
|
|
|
|
|
|
/* Unconditionally create. */
|
|
|
|
/* Don't check error, as a failure here should be okay. */
|
|
|
|
FsFile f;
|
2021-10-09 22:49:53 +01:00
|
|
|
fsFsCreateFile(std::addressof(g_sd_filesystem), fixed_path, size, 0);
|
2019-12-04 14:53:52 +00:00
|
|
|
|
|
|
|
/* Try to open. */
|
2021-10-09 22:49:53 +01:00
|
|
|
R_TRY(fsFsOpenFile(std::addressof(g_sd_filesystem), fixed_path, OpenMode_ReadWrite, std::addressof(f)));
|
|
|
|
auto file_guard = SCOPE_GUARD { fsFileClose(std::addressof(f)); };
|
2019-12-04 14:53:52 +00:00
|
|
|
|
|
|
|
/* Try to set the size. */
|
2021-10-09 22:49:53 +01:00
|
|
|
R_TRY(fsFileSetSize(std::addressof(f), static_cast<s64>(size)));
|
2019-12-04 14:53:52 +00:00
|
|
|
|
|
|
|
/* Try to write data. */
|
2021-10-09 22:49:53 +01:00
|
|
|
R_TRY(fsFileWrite(std::addressof(f), 0, data, size, FsWriteOption_Flush));
|
2019-12-04 14:53:52 +00:00
|
|
|
|
|
|
|
/* Set output. */
|
|
|
|
file_guard.Cancel();
|
|
|
|
*out = f;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-12-04 14:53:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 13:02:07 +00:00
|
|
|
Result CreateAndOpenAtmosphereSdFile(FsFile *out, ncm::ProgramId program_id, const char *path, size_t size) {
|
|
|
|
R_TRY(EnsureSdInitialized());
|
|
|
|
|
|
|
|
char fixed_path[ams::fs::EntryNameLengthMax + 1];
|
|
|
|
FormatAtmosphereSdPath(fixed_path, sizeof(fixed_path), program_id, path);
|
|
|
|
|
|
|
|
/* Unconditionally create. */
|
|
|
|
/* Don't check error, as a failure here should be okay. */
|
|
|
|
FsFile f;
|
2021-10-09 22:49:53 +01:00
|
|
|
fsFsCreateFile(std::addressof(g_sd_filesystem), fixed_path, size, 0);
|
2019-12-30 13:02:07 +00:00
|
|
|
|
|
|
|
/* Try to open. */
|
2021-10-09 22:49:53 +01:00
|
|
|
R_TRY(fsFsOpenFile(std::addressof(g_sd_filesystem), fixed_path, OpenMode_ReadWrite, std::addressof(f)));
|
|
|
|
auto file_guard = SCOPE_GUARD { fsFileClose(std::addressof(f)); };
|
2019-12-30 13:02:07 +00:00
|
|
|
|
|
|
|
/* Try to set the size. */
|
2021-10-09 22:49:53 +01:00
|
|
|
R_TRY(fsFileSetSize(std::addressof(f), static_cast<s64>(size)));
|
2019-12-30 13:02:07 +00:00
|
|
|
|
|
|
|
/* Set output. */
|
|
|
|
file_guard.Cancel();
|
|
|
|
*out = f;
|
2022-03-26 07:14:36 +00:00
|
|
|
R_SUCCEED();
|
2019-12-30 13:02:07 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-21 09:48:47 +00:00
|
|
|
}
|