1 // Copyright 2014 Citra Emulator Project
2 // Licensed under GPLv2 or any later version
3 // Refer to the license.txt file included.
4 
5 #include <cstring>
6 #include <memory>
7 #include <utility>
8 #include "common/archives.h"
9 #include "common/common_types.h"
10 #include "common/logging/log.h"
11 #include "core/file_sys/ivfc_archive.h"
12 
13 ////////////////////////////////////////////////////////////////////////////////////////////////////
14 // FileSys namespace
15 
16 SERIALIZE_EXPORT_IMPL(FileSys::IVFCFile)
17 SERIALIZE_EXPORT_IMPL(FileSys::IVFCFileInMemory)
18 SERIALIZE_EXPORT_IMPL(FileSys::IVFCDelayGenerator)
19 SERIALIZE_EXPORT_IMPL(FileSys::RomFSDelayGenerator)
20 SERIALIZE_EXPORT_IMPL(FileSys::ExeFSDelayGenerator)
21 
22 namespace FileSys {
23 
IVFCArchive(std::shared_ptr<RomFSReader> file,std::unique_ptr<DelayGenerator> delay_generator_)24 IVFCArchive::IVFCArchive(std::shared_ptr<RomFSReader> file,
25                          std::unique_ptr<DelayGenerator> delay_generator_)
26     : romfs_file(std::move(file)) {
27     delay_generator = std::move(delay_generator_);
28 }
29 
GetName() const30 std::string IVFCArchive::GetName() const {
31     return "IVFC";
32 }
33 
OpenFile(const Path & path,const Mode & mode) const34 ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path,
35                                                               const Mode& mode) const {
36     std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<IVFCDelayGenerator>();
37     return MakeResult<std::unique_ptr<FileBackend>>(
38         std::make_unique<IVFCFile>(romfs_file, std::move(delay_generator)));
39 }
40 
DeleteFile(const Path & path) const41 ResultCode IVFCArchive::DeleteFile(const Path& path) const {
42     LOG_CRITICAL(Service_FS, "Attempted to delete a file from an IVFC archive ({}).", GetName());
43     // TODO(Subv): Verify error code
44     return ResultCode(ErrorDescription::NoData, ErrorModule::FS, ErrorSummary::Canceled,
45                       ErrorLevel::Status);
46 }
47 
RenameFile(const Path & src_path,const Path & dest_path) const48 ResultCode IVFCArchive::RenameFile(const Path& src_path, const Path& dest_path) const {
49     LOG_CRITICAL(Service_FS, "Attempted to rename a file within an IVFC archive ({}).", GetName());
50     // TODO(wwylele): Use correct error code
51     return ResultCode(-1);
52 }
53 
DeleteDirectory(const Path & path) const54 ResultCode IVFCArchive::DeleteDirectory(const Path& path) const {
55     LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an IVFC archive ({}).",
56                  GetName());
57     // TODO(wwylele): Use correct error code
58     return ResultCode(-1);
59 }
60 
DeleteDirectoryRecursively(const Path & path) const61 ResultCode IVFCArchive::DeleteDirectoryRecursively(const Path& path) const {
62     LOG_CRITICAL(Service_FS, "Attempted to delete a directory from an IVFC archive ({}).",
63                  GetName());
64     // TODO(wwylele): Use correct error code
65     return ResultCode(-1);
66 }
67 
CreateFile(const Path & path,u64 size) const68 ResultCode IVFCArchive::CreateFile(const Path& path, u64 size) const {
69     LOG_CRITICAL(Service_FS, "Attempted to create a file in an IVFC archive ({}).", GetName());
70     // TODO: Verify error code
71     return ResultCode(ErrorDescription::NotAuthorized, ErrorModule::FS, ErrorSummary::NotSupported,
72                       ErrorLevel::Permanent);
73 }
74 
CreateDirectory(const Path & path) const75 ResultCode IVFCArchive::CreateDirectory(const Path& path) const {
76     LOG_CRITICAL(Service_FS, "Attempted to create a directory in an IVFC archive ({}).", GetName());
77     // TODO(wwylele): Use correct error code
78     return ResultCode(-1);
79 }
80 
RenameDirectory(const Path & src_path,const Path & dest_path) const81 ResultCode IVFCArchive::RenameDirectory(const Path& src_path, const Path& dest_path) const {
82     LOG_CRITICAL(Service_FS, "Attempted to rename a file within an IVFC archive ({}).", GetName());
83     // TODO(wwylele): Use correct error code
84     return ResultCode(-1);
85 }
86 
OpenDirectory(const Path & path) const87 ResultVal<std::unique_ptr<DirectoryBackend>> IVFCArchive::OpenDirectory(const Path& path) const {
88     return MakeResult<std::unique_ptr<DirectoryBackend>>(std::make_unique<IVFCDirectory>());
89 }
90 
GetFreeBytes() const91 u64 IVFCArchive::GetFreeBytes() const {
92     LOG_WARNING(Service_FS, "Attempted to get the free space in an IVFC archive");
93     return 0;
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////////////////////////
97 
IVFCFile(std::shared_ptr<RomFSReader> file,std::unique_ptr<DelayGenerator> delay_generator_)98 IVFCFile::IVFCFile(std::shared_ptr<RomFSReader> file,
99                    std::unique_ptr<DelayGenerator> delay_generator_)
100     : romfs_file(std::move(file)) {
101     delay_generator = std::move(delay_generator_);
102 }
103 
Read(const u64 offset,const std::size_t length,u8 * buffer) const104 ResultVal<std::size_t> IVFCFile::Read(const u64 offset, const std::size_t length,
105                                       u8* buffer) const {
106     LOG_TRACE(Service_FS, "called offset={}, length={}", offset, length);
107     return MakeResult<std::size_t>(romfs_file->ReadFile(offset, length, buffer));
108 }
109 
Write(const u64 offset,const std::size_t length,const bool flush,const u8 * buffer)110 ResultVal<std::size_t> IVFCFile::Write(const u64 offset, const std::size_t length, const bool flush,
111                                        const u8* buffer) {
112     LOG_ERROR(Service_FS, "Attempted to write to IVFC file");
113     // TODO(Subv): Find error code
114     return MakeResult<std::size_t>(0);
115 }
116 
GetSize() const117 u64 IVFCFile::GetSize() const {
118     return romfs_file->GetSize();
119 }
120 
SetSize(const u64 size) const121 bool IVFCFile::SetSize(const u64 size) const {
122     LOG_ERROR(Service_FS, "Attempted to set the size of an IVFC file");
123     return false;
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////////////////////////
127 
IVFCFileInMemory(std::vector<u8> bytes,u64 offset,u64 size,std::unique_ptr<DelayGenerator> delay_generator_)128 IVFCFileInMemory::IVFCFileInMemory(std::vector<u8> bytes, u64 offset, u64 size,
129                                    std::unique_ptr<DelayGenerator> delay_generator_)
130     : romfs_file(std::move(bytes)), data_offset(offset), data_size(size) {
131     delay_generator = std::move(delay_generator_);
132 }
133 
Read(const u64 offset,const std::size_t length,u8 * buffer) const134 ResultVal<std::size_t> IVFCFileInMemory::Read(const u64 offset, const std::size_t length,
135                                               u8* buffer) const {
136     LOG_TRACE(Service_FS, "called offset={}, length={}", offset, length);
137     std::size_t read_length = (std::size_t)std::min((u64)length, data_size - offset);
138 
139     std::memcpy(buffer, romfs_file.data() + data_offset + offset, read_length);
140     return MakeResult<std::size_t>(read_length);
141 }
142 
Write(const u64 offset,const std::size_t length,const bool flush,const u8 * buffer)143 ResultVal<std::size_t> IVFCFileInMemory::Write(const u64 offset, const std::size_t length,
144                                                const bool flush, const u8* buffer) {
145     LOG_ERROR(Service_FS, "Attempted to write to IVFC file");
146     // TODO(Subv): Find error code
147     return MakeResult<std::size_t>(0);
148 }
149 
GetSize() const150 u64 IVFCFileInMemory::GetSize() const {
151     return data_size;
152 }
153 
SetSize(const u64 size) const154 bool IVFCFileInMemory::SetSize(const u64 size) const {
155     LOG_ERROR(Service_FS, "Attempted to set the size of an IVFC file");
156     return false;
157 }
158 
159 } // namespace FileSys
160