refactor: phase 6 cleanup — RAII handles and io::restore split

- AccountProfileHandle RAII wrapper in handles.hpp; applied in
  account.cpp (getUser, iconPath) replacing manual accountProfileClose
- FILE* in iconPath replaced with existing FileHandle RAII wrapper
- io::restore split into clearSaveRoot + extractAndCommit static helpers;
  public signature unchanged
- sendAll/recvAll kept as bool — callers don't propagate the error string,
  Result<void> would add noise without benefit

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-03 14:56:38 +03:00
parent 836956394a
commit 6f8ede035f
4 changed files with 187 additions and 43 deletions
+12 -15
View File
@@ -28,6 +28,7 @@
#include <sys/stat.h>
#include <nxst/domain/account.hpp>
#include <nxst/infra/fs/handles.hpp>
static std::map<AccountUid, User> mUsers;
@@ -59,15 +60,15 @@ std::vector<AccountUid> Account::ids(void) {
static User getUser(AccountUid id) {
User user{id, ""};
AccountProfile profile;
nxst::AccountProfileHandle profile;
AccountProfileBase profilebase;
memset(&profilebase, 0, sizeof(profilebase));
if (R_SUCCEEDED(accountGetProfile(&profile, id))) {
if (R_SUCCEEDED(accountProfileGet(&profile, NULL, &profilebase))) {
if (R_SUCCEEDED(accountGetProfile(profile.get(), id))) {
profile.valid = true;
if (R_SUCCEEDED(accountProfileGet(profile.get(), NULL, &profilebase))) {
user.name = std::string(profilebase.nickname);
}
accountProfileClose(&profile);
}
return user;
}
@@ -95,28 +96,24 @@ std::string Account::iconPath(AccountUid id) {
mkdir("sdmc:/switch/NXST", 0755);
mkdir("sdmc:/switch/NXST/cache", 0755);
AccountProfile profile;
if (R_FAILED(accountGetProfile(&profile, id)))
nxst::AccountProfileHandle profile;
if (R_FAILED(accountGetProfile(profile.get(), id)))
return "";
profile.valid = true;
u32 imgSize = 0;
if (R_FAILED(accountProfileGetImageSize(&profile, &imgSize)) || imgSize == 0) {
accountProfileClose(&profile);
if (R_FAILED(accountProfileGetImageSize(profile.get(), &imgSize)) || imgSize == 0)
return "";
}
std::vector<u8> buf(imgSize);
u32 outSize = 0;
Result r = accountProfileLoadImage(&profile, buf.data(), imgSize, &outSize);
accountProfileClose(&profile);
if (R_FAILED(r) || outSize == 0)
if (R_FAILED(accountProfileLoadImage(profile.get(), buf.data(), imgSize, &outSize)) || outSize == 0)
return "";
FILE* f = fopen(path, "wb");
nxst::FileHandle f(fopen(path, "wb"));
if (!f)
return "";
fwrite(buf.data(), 1, outSize, f);
fclose(f);
fwrite(buf.data(), 1, outSize, f.get());
return std::string(path);
}
+41 -28
View File
@@ -233,6 +233,40 @@ static void createSaveIfNeeded(u64 title_id, AccountUid uid) {
fsCreateSaveDataFileSystem(&attr, &create_info, &meta);
}
static nxst::Result<void> clearSaveRoot(const std::string& dst_path) {
Directory save_root(dst_path);
for (size_t i = 0, sz = save_root.size(); i < sz; i++) {
if (save_root.folder(i)) {
io::deleteFolderRecursively(dst_path + save_root.entry(i) + "/");
rmdir((dst_path + save_root.entry(i)).c_str());
} else {
std::remove((dst_path + save_root.entry(i)).c_str());
}
}
Result res = fsdevCommitDevice("save");
if (R_FAILED(res)) {
nxst::log::error("Failed to commit save after clearing with result 0x%08lX.", res);
return nxst::Result<void>::failure("Failed to commit save after delete.");
}
return nxst::Result<void>::success();
}
static nxst::Result<void> extractAndCommit(const std::string& src_path, const std::string& dst_path) {
Result res = io::copyDirectory(src_path, dst_path);
if (R_FAILED(res)) {
nxst::log::error("Failed to copy %s to save:/ with result 0x%08lX.", src_path.c_str(), res);
return nxst::Result<void>::failure("Failed to restore save.");
}
res = fsdevCommitDevice("save");
if (R_FAILED(res)) {
nxst::log::error("Failed to commit save with result 0x%08lX.", res);
return nxst::Result<void>::failure("Failed to commit to save device.");
}
return nxst::Result<void>::success();
}
nxst::Result<std::string> io::restore(size_t index, AccountUid uid, size_t cellIndex,
const std::string& nameFromCell) {
(void)cellIndex;
@@ -276,37 +310,16 @@ nxst::Result<std::string> io::restore(size_t index, AccountUid uid, size_t cellI
}
}
{
Directory save_root(dst_path);
for (size_t i = 0, sz = save_root.size(); i < sz; i++) {
if (save_root.folder(i)) {
io::deleteFolderRecursively(dst_path + save_root.entry(i) + "/");
rmdir((dst_path + save_root.entry(i)).c_str());
} else {
std::remove((dst_path + save_root.entry(i)).c_str());
}
}
auto clear_res = clearSaveRoot(dst_path);
if (!clear_res.isOk()) {
FileSystem::unmount();
return nxst::Result<std::string>::failure(clear_res.error());
}
res = fsdevCommitDevice("save");
if (R_FAILED(res)) {
auto extract_res = extractAndCommit(src_path, dst_path);
if (!extract_res.isOk()) {
FileSystem::unmount();
nxst::log::error("Failed to commit save after clearing with result 0x%08lX.", res);
return nxst::Result<std::string>::failure("Failed to commit save after delete.");
}
res = io::copyDirectory(src_path, dst_path);
if (R_FAILED(res)) {
FileSystem::unmount();
nxst::log::error("Failed to copy %s to save:/ with result 0x%08lX.", src_path.c_str(), res);
return nxst::Result<std::string>::failure("Failed to restore save.");
}
res = fsdevCommitDevice("save");
if (R_FAILED(res)) {
FileSystem::unmount();
nxst::log::error("Failed to commit save with result 0x%08lX.", res);
return nxst::Result<std::string>::failure("Failed to commit to save device.");
return nxst::Result<std::string>::failure(extract_res.error());
}
blinkLed(4);