40 static_assert(std::is_base_of_v<ExternalFsFileFormat, T>);
42 std::shared_ptr<T> ext_fs_file;
43 bool rw_from_ext_fs_file;
44 std::string ext_fs_path;
49 fs::CreateStdioDirectory(ext_fs_file->ext_fs_root_path);
52 virtual bool ExistsImpl(
const std::string &path,
size_t &out_size) = 0;
53 virtual Result OpenImpl(
const std::string &path) = 0;
54 virtual Result GetSizeImpl(
size_t &out_offset) = 0;
55 virtual Result SetOffsetImpl(
const size_t offset,
const fs::Position pos) = 0;
56 virtual Result GetOffsetImpl(
size_t &out_offset) = 0;
57 virtual Result ReadImpl(
void *read_buf,
const size_t read_size,
size_t &out_read_size) = 0;
58 virtual Result CloseImpl() = 0;
60 bool Exists(
const std::string &path,
size_t &out_size)
override {
61 return this->ExistsImpl(path, out_size);
64 Result Open(
const std::string &path,
const fs::OpenMode mode)
override {
65 this->ext_fs_path = this->ext_fs_file->GetExternalFsPath(path);
67 this->rw_from_ext_fs_file = fs::IsStdioFile(this->ext_fs_path) || fs::CanWriteWithMode(mode);
68 if(this->rw_from_ext_fs_file) {
69 EnsureBaseStdioDirectoryExists(this->ext_fs_path);
70 return this->ext_fs_bin_file.Open(std::make_shared<fs::StdioFileHandle>(), this->ext_fs_path, mode);
73 return this->OpenImpl(path);
77 Result GetSize(
size_t &out_size)
override {
78 if(this->rw_from_ext_fs_file) {
80 return this->ext_fs_bin_file.GetSize(out_size);
83 return this->GetSizeImpl(out_size);
87 Result SetOffset(
const size_t offset,
const fs::Position pos)
override {
88 if(this->rw_from_ext_fs_file) {
90 case fs::Position::Begin: {
91 return this->ext_fs_bin_file.SetAbsoluteOffset(offset);
93 case fs::Position::Current: {
94 return this->ext_fs_bin_file.MoveOffset(offset);
97 NTR_R_FAIL(ResultInvalidSeekPosition);
102 return this->SetOffsetImpl(offset, pos);
106 Result GetOffset(
size_t &out_offset)
override {
107 if(this->rw_from_ext_fs_file) {
108 return this->ext_fs_bin_file.GetAbsoluteOffset(out_offset);
111 return this->GetOffsetImpl(out_offset);
115 Result Read(
void *read_buf,
const size_t read_size,
size_t &out_read_size)
override {
116 if(this->rw_from_ext_fs_file) {
117 return this->ext_fs_bin_file.ReadData(read_buf, read_size, out_read_size);
120 return this->ReadImpl(read_buf, read_size, out_read_size);
124 Result Write(
const void *write_buf,
const size_t write_size)
override {
125 if(this->rw_from_ext_fs_file) {
126 return this->ext_fs_bin_file.WriteData(write_buf, write_size);
129 NTR_R_FAIL(ResultWriteNotSupported);
134 this->ext_fs_path.clear();
135 if(this->rw_from_ext_fs_file) {
136 return this->ext_fs_bin_file.Close();
139 return this->CloseImpl();