Services/AM: Expose CIAFile class in AM header

This commit is contained in:
shinyquagsire23 2017-11-14 15:31:19 -07:00
parent 1edbbf7f8c
commit 750e7e06b1
2 changed files with 171 additions and 164 deletions

View File

@ -10,9 +10,7 @@
#include "common/file_util.h" #include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "core/file_sys/cia_container.h"
#include "core/file_sys/errors.h" #include "core/file_sys/errors.h"
#include "core/file_sys/file_backend.h"
#include "core/file_sys/ncch_container.h" #include "core/file_sys/ncch_container.h"
#include "core/file_sys/title_metadata.h" #include "core/file_sys/title_metadata.h"
#include "core/hle/ipc.h" #include "core/hle/ipc.h"
@ -23,7 +21,6 @@
#include "core/hle/kernel/handle_table.h" #include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/server_session.h" #include "core/hle/kernel/server_session.h"
#include "core/hle/kernel/session.h" #include "core/hle/kernel/session.h"
#include "core/hle/result.h"
#include "core/hle/service/am/am.h" #include "core/hle/service/am/am.h"
#include "core/hle/service/am/am_app.h" #include "core/hle/service/am/am_app.h"
#include "core/hle/service/am/am_net.h" #include "core/hle/service/am/am_net.h"
@ -82,17 +79,12 @@ struct TicketInfo {
static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong"); static_assert(sizeof(TicketInfo) == 0x18, "Ticket info structure size is wrong");
// A file handled returned for CIAs to be written into and subsequently installed. ResultVal<size_t> CIAFile::Read(u64 offset, size_t length, u8* buffer) const {
class CIAFile final : public FileSys::FileBackend {
public:
explicit CIAFile(Service::FS::MediaType media_type) : media_type(media_type) {}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override {
UNIMPLEMENTED(); UNIMPLEMENTED();
return MakeResult<size_t>(length); return MakeResult<size_t>(length);
} }
ResultVal<size_t> WriteTitleMetadata(u64 offset, size_t length, const u8* buffer) { ResultVal<size_t> CIAFile::WriteTitleMetadata(u64 offset, size_t length, const u8* buffer) {
container.LoadTitleMetadata(data, container.GetTitleMetadataOffset()); container.LoadTitleMetadata(data, container.GetTitleMetadataOffset());
FileSys::TitleMetadata tmd = container.GetTitleMetadata(); FileSys::TitleMetadata tmd = container.GetTitleMetadata();
cia_installing_tid = tmd.GetTitleID(); cia_installing_tid = tmd.GetTitleID();
@ -128,7 +120,7 @@ public:
return MakeResult<size_t>(length); return MakeResult<size_t>(length);
} }
ResultVal<size_t> WriteContentData(u64 offset, size_t length, const u8* buffer) { ResultVal<size_t> CIAFile::WriteContentData(u64 offset, size_t length, const u8* buffer) {
// Data is not being buffered, so we have to keep track of how much of each <ID>.app // Data is not being buffered, so we have to keep track of how much of each <ID>.app
// has been written since we might get a written buffer which contains multiple .app // has been written since we might get a written buffer which contains multiple .app
// contents or only part of a larger .app's contents. // contents or only part of a larger .app's contents.
@ -151,8 +143,7 @@ public:
// Since the incoming TMD has already been written, we can use GetTitleContentPath // Since the incoming TMD has already been written, we can use GetTitleContentPath
// to get the content paths to write to. // to get the content paths to write to.
FileSys::TitleMetadata tmd = container.GetTitleMetadata(); FileSys::TitleMetadata tmd = container.GetTitleMetadata();
FileUtil::IOFile file( FileUtil::IOFile file(GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update),
GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update),
content_written[i] ? "ab" : "wb"); content_written[i] ? "ab" : "wb");
if (!file.IsOpen()) if (!file.IsOpen())
@ -171,7 +162,7 @@ public:
return MakeResult<size_t>(length); return MakeResult<size_t>(length);
} }
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override { ResultVal<size_t> CIAFile::Write(u64 offset, size_t length, bool flush, const u8* buffer) {
written += length; written += length;
// TODO(shinyquagsire23): Can we assume that things will only be written in sequence? // TODO(shinyquagsire23): Can we assume that things will only be written in sequence?
@ -219,8 +210,7 @@ public:
// The end of our TMD is at the beginning of Content data, so ensure we have that much // The end of our TMD is at the beginning of Content data, so ensure we have that much
// buffered before trying to parse. // buffered before trying to parse.
if (written >= container.GetContentOffset() && if (written >= container.GetContentOffset() && install_state != CIAInstallState::TMDLoaded) {
install_state != CIAInstallState::TMDLoaded) {
auto result = WriteTitleMetadata(offset, length, buffer); auto result = WriteTitleMetadata(offset, length, buffer);
if (result.Failed()) if (result.Failed())
return result; return result;
@ -238,34 +228,19 @@ public:
return MakeResult<size_t>(length); return MakeResult<size_t>(length);
} }
u64 GetSize() const override { u64 CIAFile::GetSize() const {
return written; return written;
} }
bool SetSize(u64 size) const override { bool CIAFile::SetSize(u64 size) const {
return false; return false;
} }
bool Close() const override { bool CIAFile::Close() const {
return true; return true;
} }
void Flush() const override {} void CIAFile::Flush() const {}
private:
// Whether it's installing an update, and what step of installation it is at
bool is_update = false;
CIAInstallState install_state = CIAInstallState::InstallStarted;
// How much has been written total, CIAContainer for the installing CIA, buffer of all data
// prior to content data, how much of each content index has been written, and where the CIA
// is being installed to
u64 written = 0;
FileSys::CIAContainer container;
std::vector<u8> data;
std::vector<u64> content_written;
Service::FS::MediaType media_type;
};
Service::FS::MediaType GetTitleMediaType(u64 titleId) { Service::FS::MediaType GetTitleMediaType(u64 titleId) {
u16 platform = static_cast<u16>(titleId >> 48); u16 platform = static_cast<u16>(titleId >> 48);

View File

@ -6,6 +6,9 @@
#include <string> #include <string>
#include "common/common_types.h" #include "common/common_types.h"
#include "core/file_sys/cia_container.h"
#include "core/file_sys/file_backend.h"
#include "core/hle/result.h"
namespace Service { namespace Service {
namespace FS { namespace FS {
@ -38,6 +41,35 @@ enum class CIAInstallState : u32 {
ContentWritten, ContentWritten,
}; };
// A file handled returned for CIAs to be written into and subsequently installed.
class CIAFile final : public FileSys::FileBackend {
public:
explicit CIAFile(Service::FS::MediaType media_type) : media_type(media_type) {}
ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
ResultVal<size_t> WriteTitleMetadata(u64 offset, size_t length, const u8* buffer);
ResultVal<size_t> WriteContentData(u64 offset, size_t length, const u8* buffer);
ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
u64 GetSize() const override;
bool SetSize(u64 size) const override;
bool Close() const override;
void Flush() const override;
private:
// Whether it's installing an update, and what step of installation it is at
bool is_update = false;
CIAInstallState install_state = CIAInstallState::InstallStarted;
// How much has been written total, CIAContainer for the installing CIA, buffer of all data
// prior to content data, how much of each content index has been written, and where the CIA
// is being installed to
u64 written = 0;
FileSys::CIAContainer container;
std::vector<u8> data;
std::vector<u64> content_written;
Service::FS::MediaType media_type;
};
/** /**
* Get the mediatype for an installed title * Get the mediatype for an installed title
* @param titleId the installed title ID * @param titleId the installed title ID