ES: Move title dir creation logic into separate function

Since we're going to need it in the boot code, move it out to a
separate, easily reusable function. This also makes the InitImport
logic clearer.
This commit is contained in:
Léo Lam 2018-05-10 22:27:21 +02:00
parent 722d31124c
commit e6c489f1d4
2 changed files with 32 additions and 13 deletions

View File

@ -139,6 +139,8 @@ public:
ReturnCode SetUpStreamKey(u32 uid, const u8* ticket_view, const IOS::ES::TMDReader& tmd, ReturnCode SetUpStreamKey(u32 uid, const u8* ticket_view, const IOS::ES::TMDReader& tmd,
u32* handle); u32* handle);
bool CreateTitleDirectories(u64 title_id, u16 group_id) const;
private: private:
enum enum
{ {

View File

@ -222,40 +222,56 @@ static bool DeleteDirectoriesIfEmpty(FS::FileSystem* fs, const std::string& path
return true; return true;
} }
bool ES::InitImport(const IOS::ES::TMDReader& tmd) bool ES::CreateTitleDirectories(u64 title_id, u16 group_id) const
{ {
const auto fs = m_ios.GetFS(); const auto fs = m_ios.GetFS();
const std::string content_dir = Common::GetTitleContentPath(tmd.GetTitleId());
const std::string import_content_dir = Common::GetImportTitlePath(tmd.GetTitleId()) + "/content";
const std::string content_dir = Common::GetTitleContentPath(title_id);
const auto result1 = fs->CreateFullPath(PID_KERNEL, PID_KERNEL, content_dir + '/', 0, const auto result1 = fs->CreateFullPath(PID_KERNEL, PID_KERNEL, content_dir + '/', 0,
FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::Read); FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::Read);
const auto result2 = fs->SetMetadata(PID_KERNEL, content_dir, PID_KERNEL, PID_KERNEL, 0, const auto result2 = fs->SetMetadata(PID_KERNEL, content_dir, PID_KERNEL, PID_KERNEL, 0,
FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::None); FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::None);
const auto result3 = fs->CreateFullPath(PID_KERNEL, PID_KERNEL, import_content_dir + '/', 0, if (result1 != FS::ResultCode::Success || result2 != FS::ResultCode::Success)
FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::None);
if (result1 != FS::ResultCode::Success || result2 != FS::ResultCode::Success ||
result3 != FS::ResultCode::Success)
{ {
ERROR_LOG(IOS_ES, "InitImport: Failed to create content dir for %016" PRIx64, tmd.GetTitleId()); ERROR_LOG(IOS_ES, "Failed to create or set metadata on content dir for %016" PRIx64, title_id);
return false; return false;
} }
const std::string data_dir = Common::GetTitleDataPath(tmd.GetTitleId()); const std::string data_dir = Common::GetTitleDataPath(title_id);
const auto data_dir_contents = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, data_dir); const auto data_dir_contents = fs->ReadDirectory(PID_KERNEL, PID_KERNEL, data_dir);
if (!data_dir_contents && if (!data_dir_contents &&
(data_dir_contents.Error() != FS::ResultCode::NotFound || (data_dir_contents.Error() != FS::ResultCode::NotFound ||
fs->CreateDirectory(PID_KERNEL, PID_KERNEL, data_dir, 0, FS::Mode::ReadWrite, FS::Mode::None, fs->CreateDirectory(PID_KERNEL, PID_KERNEL, data_dir, 0, FS::Mode::ReadWrite, FS::Mode::None,
FS::Mode::None) != FS::ResultCode::Success)) FS::Mode::None) != FS::ResultCode::Success))
{ {
ERROR_LOG(IOS_ES, "Failed to create data dir for %016" PRIx64, title_id);
return false; return false;
} }
IOS::ES::UIDSys uid_sys{fs}; IOS::ES::UIDSys uid_sys{fs};
const u32 uid = uid_sys.GetOrInsertUIDForTitle(tmd.GetTitleId()); const u32 uid = uid_sys.GetOrInsertUIDForTitle(title_id);
if (fs->SetMetadata(0, data_dir, uid, tmd.GetGroupId(), 0, FS::Mode::ReadWrite, FS::Mode::None, if (fs->SetMetadata(0, data_dir, uid, group_id, 0, FS::Mode::ReadWrite, FS::Mode::None,
FS::Mode::None) != FS::ResultCode::Success) FS::Mode::None) != FS::ResultCode::Success)
{ {
ERROR_LOG(IOS_ES, "Failed to set metadata on data dir for %016" PRIx64, title_id);
return false;
}
return true;
}
bool ES::InitImport(const IOS::ES::TMDReader& tmd)
{
if (!CreateTitleDirectories(tmd.GetTitleId(), tmd.GetGroupId()))
return false;
const auto fs = m_ios.GetFS();
const std::string import_content_dir = Common::GetImportTitlePath(tmd.GetTitleId()) + "/content";
const auto result = fs->CreateFullPath(PID_KERNEL, PID_KERNEL, import_content_dir + '/', 0,
FS::Mode::ReadWrite, FS::Mode::ReadWrite, FS::Mode::None);
if (result != FS::ResultCode::Success)
{
ERROR_LOG(IOS_ES, "InitImport: Failed to create content dir for %016" PRIx64, tmd.GetTitleId());
return false; return false;
} }
@ -265,8 +281,9 @@ bool ES::InitImport(const IOS::ES::TMDReader& tmd)
if (!file_info || !file_info->is_file) if (!file_info || !file_info->is_file)
return true; return true;
const auto result = fs->Rename(PID_KERNEL, PID_KERNEL, content_dir, import_content_dir); const std::string content_dir = Common::GetTitleContentPath(tmd.GetTitleId());
if (result != FS::ResultCode::Success) const auto rename_result = fs->Rename(PID_KERNEL, PID_KERNEL, content_dir, import_content_dir);
if (rename_result != FS::ResultCode::Success)
{ {
ERROR_LOG(IOS_ES, "InitImport: Failed to move content dir for %016" PRIx64, tmd.GetTitleId()); ERROR_LOG(IOS_ES, "InitImport: Failed to move content dir for %016" PRIx64, tmd.GetTitleId());
return false; return false;