finish refactor, add docs and CI
This commit is contained in:
+34
-47
@@ -24,20 +24,19 @@
|
||||
* reasonable ways as different from the original version.
|
||||
*/
|
||||
|
||||
#include <nxst/infra/fs/io.hpp>
|
||||
#include <nxst/infra/fs/handles.hpp>
|
||||
#include <nxst/app/main.hpp>
|
||||
#include <nxst/infra/sys/logger.hpp>
|
||||
#include <vector>
|
||||
|
||||
bool io::fileExists(const std::string& path)
|
||||
{
|
||||
#include <nxst/app/main.hpp>
|
||||
#include <nxst/infra/fs/handles.hpp>
|
||||
#include <nxst/infra/fs/io.hpp>
|
||||
#include <nxst/infra/sys/logger.hpp>
|
||||
|
||||
bool io::fileExists(const std::string& path) {
|
||||
struct stat buffer;
|
||||
return (stat(path.c_str(), &buffer) == 0);
|
||||
}
|
||||
|
||||
void io::copyFile(const std::string& srcPath, const std::string& dstPath)
|
||||
{
|
||||
void io::copyFile(const std::string& srcPath, const std::string& dstPath) {
|
||||
g_isTransferringFile = true;
|
||||
|
||||
nxst::FileHandle src(fopen(srcPath.c_str(), "rb"));
|
||||
@@ -63,14 +62,13 @@ void io::copyFile(const std::string& srcPath, const std::string& dstPath)
|
||||
u64 offset = 0;
|
||||
|
||||
size_t slashpos = srcPath.rfind('/');
|
||||
g_currentFile = srcPath.substr(slashpos + 1, srcPath.length() - slashpos - 1);
|
||||
g_currentFile = srcPath.substr(slashpos + 1, srcPath.length() - slashpos - 1);
|
||||
|
||||
while (offset < sz) {
|
||||
u32 count = (u32)fread(buf.data(), 1, BUFFER_SIZE, src.get());
|
||||
if (count == 0) {
|
||||
nxst::log::error("fread returned 0 for %s at offset %llu/%llu (errno %d). Aborting.",
|
||||
srcPath.c_str(), (unsigned long long)offset,
|
||||
(unsigned long long)sz, errno);
|
||||
srcPath.c_str(), (unsigned long long)offset, (unsigned long long)sz, errno);
|
||||
break;
|
||||
}
|
||||
fwrite(buf.data(), 1, count, dst.get());
|
||||
@@ -84,10 +82,9 @@ void io::copyFile(const std::string& srcPath, const std::string& dstPath)
|
||||
g_isTransferringFile = false;
|
||||
}
|
||||
|
||||
Result io::copyDirectory(const std::string& srcPath, const std::string& dstPath)
|
||||
{
|
||||
Result io::copyDirectory(const std::string& srcPath, const std::string& dstPath) {
|
||||
Result res = 0;
|
||||
bool quit = false;
|
||||
bool quit = false;
|
||||
Directory items(srcPath);
|
||||
|
||||
if (!items.good()) {
|
||||
@@ -104,12 +101,10 @@ Result io::copyDirectory(const std::string& srcPath, const std::string& dstPath)
|
||||
newsrc += "/";
|
||||
newdst += "/";
|
||||
res = io::copyDirectory(newsrc, newdst);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
quit = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
io::copyFile(newsrc, newdst);
|
||||
}
|
||||
}
|
||||
@@ -117,20 +112,17 @@ Result io::copyDirectory(const std::string& srcPath, const std::string& dstPath)
|
||||
return 0;
|
||||
}
|
||||
|
||||
Result io::createDirectory(const std::string& path)
|
||||
{
|
||||
Result io::createDirectory(const std::string& path) {
|
||||
mkdir(path.c_str(), 0777);
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool io::directoryExists(const std::string& path)
|
||||
{
|
||||
bool io::directoryExists(const std::string& path) {
|
||||
struct stat sb;
|
||||
return (stat(path.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode));
|
||||
}
|
||||
|
||||
Result io::deleteFolderRecursively(const std::string& path)
|
||||
{
|
||||
Result io::deleteFolderRecursively(const std::string& path) {
|
||||
Directory dir(path);
|
||||
if (!dir.good()) {
|
||||
return dir.error();
|
||||
@@ -142,8 +134,7 @@ Result io::deleteFolderRecursively(const std::string& path)
|
||||
deleteFolderRecursively(newpath);
|
||||
newpath = path + dir.entry(i);
|
||||
rmdir(newpath.c_str());
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
std::string newpath = path + dir.entry(i);
|
||||
std::remove(newpath.c_str());
|
||||
}
|
||||
@@ -153,14 +144,12 @@ Result io::deleteFolderRecursively(const std::string& path)
|
||||
return 0;
|
||||
}
|
||||
|
||||
nxst::Result<std::string> io::backup(size_t index, AccountUid uid)
|
||||
{
|
||||
nxst::Result<std::string> io::backup(size_t index, AccountUid uid) {
|
||||
Title title;
|
||||
getTitle(title, uid, index);
|
||||
|
||||
nxst::log::info("Started backup of %s. Title id: 0x%016lX; User id: 0x%lX%lX.",
|
||||
title.name().c_str(), title.id(),
|
||||
title.userId().uid[1], title.userId().uid[0]);
|
||||
nxst::log::info("Started backup of %s. Title id: 0x%016lX; User id: 0x%lX%lX.", title.name().c_str(),
|
||||
title.id(), title.userId().uid[1], title.userId().uid[0]);
|
||||
|
||||
nxst::FsFileSystemHandle fsHandle;
|
||||
Result res = FileSystem::mount(fsHandle.get(), title.id(), title.userId());
|
||||
@@ -180,7 +169,8 @@ nxst::Result<std::string> io::backup(size_t index, AccountUid uid)
|
||||
}
|
||||
fsHandle.release(); // devfs now owns the kernel handle
|
||||
|
||||
std::string suggestion = StringUtils::removeNotAscii(StringUtils::removeAccents(Account::username(title.userId())));
|
||||
std::string suggestion =
|
||||
StringUtils::removeNotAscii(StringUtils::removeAccents(Account::username(title.userId())));
|
||||
|
||||
io::createDirectory(title.path());
|
||||
std::string dst_path = title.path() + "/" + suggestion;
|
||||
@@ -215,15 +205,13 @@ nxst::Result<std::string> io::backup(size_t index, AccountUid uid)
|
||||
}
|
||||
|
||||
// Creates the save data filesystem for a title if it doesn't exist yet.
|
||||
static void createSaveIfNeeded(u64 title_id, AccountUid uid)
|
||||
{
|
||||
static void createSaveIfNeeded(u64 title_id, AccountUid uid) {
|
||||
std::vector<u8> nsacd_buf(sizeof(NsApplicationControlData), 0);
|
||||
auto* nsacd = reinterpret_cast<NsApplicationControlData*>(nsacd_buf.data());
|
||||
|
||||
size_t outsize = 0;
|
||||
if (!R_SUCCEEDED(nsGetApplicationControlData(NsApplicationControlSource_Storage,
|
||||
title_id, nsacd,
|
||||
sizeof(NsApplicationControlData), &outsize))) {
|
||||
if (!R_SUCCEEDED(nsGetApplicationControlData(NsApplicationControlSource_Storage, title_id, nsacd,
|
||||
sizeof(NsApplicationControlData), &outsize))) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -231,30 +219,29 @@ static void createSaveIfNeeded(u64 title_id, AccountUid uid)
|
||||
|
||||
FsSaveDataAttribute attr = {};
|
||||
attr.application_id = title_id;
|
||||
attr.uid = uid;
|
||||
attr.uid = uid;
|
||||
attr.save_data_type = FsSaveDataType_Account;
|
||||
attr.save_data_rank = FsSaveDataRank_Primary;
|
||||
|
||||
FsSaveDataCreationInfo create_info = {};
|
||||
create_info.save_data_size = (s64)nsacd->nacp.user_account_save_data_size;
|
||||
create_info.journal_size = (s64)nsacd->nacp.user_account_save_data_journal_size;
|
||||
create_info.available_size = 0x4000;
|
||||
create_info.owner_id = nsacd->nacp.save_data_owner_id;
|
||||
create_info.save_data_size = (s64)nsacd->nacp.user_account_save_data_size;
|
||||
create_info.journal_size = (s64)nsacd->nacp.user_account_save_data_journal_size;
|
||||
create_info.available_size = 0x4000;
|
||||
create_info.owner_id = nsacd->nacp.save_data_owner_id;
|
||||
create_info.save_data_space_id = FsSaveDataSpaceId_User;
|
||||
|
||||
fsCreateSaveDataFileSystem(&attr, &create_info, &meta);
|
||||
}
|
||||
|
||||
nxst::Result<std::string> io::restore(size_t index, AccountUid uid, size_t cellIndex, const std::string& nameFromCell)
|
||||
{
|
||||
nxst::Result<std::string> io::restore(size_t index, AccountUid uid, size_t cellIndex,
|
||||
const std::string& nameFromCell) {
|
||||
(void)cellIndex;
|
||||
|
||||
Title title;
|
||||
getTitle(title, uid, index);
|
||||
|
||||
nxst::log::info("Started restore of %s. Title id: 0x%016lX; User id: 0x%lX%lX.",
|
||||
title.name().c_str(), title.id(),
|
||||
title.userId().uid[1], title.userId().uid[0]);
|
||||
nxst::log::info("Started restore of %s. Title id: 0x%016lX; User id: 0x%lX%lX.", title.name().c_str(),
|
||||
title.id(), title.userId().uid[1], title.userId().uid[0]);
|
||||
|
||||
createSaveIfNeeded(title.id(), uid);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user