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 <algorithm>
6 #include <cinttypes>
7 #include <cstddef>
8 #include <memory>
9 #include <system_error>
10 #include <type_traits>
11 #include <utility>
12 #include "common/assert.h"
13 #include "common/common_types.h"
14 #include "common/file_util.h"
15 #include "common/logging/log.h"
16 #include "core/core.h"
17 #include "core/file_sys/archive_backend.h"
18 #include "core/file_sys/archive_extsavedata.h"
19 #include "core/file_sys/archive_ncch.h"
20 #include "core/file_sys/archive_other_savedata.h"
21 #include "core/file_sys/archive_savedata.h"
22 #include "core/file_sys/archive_sdmc.h"
23 #include "core/file_sys/archive_sdmcwriteonly.h"
24 #include "core/file_sys/archive_selfncch.h"
25 #include "core/file_sys/archive_systemsavedata.h"
26 #include "core/file_sys/directory_backend.h"
27 #include "core/file_sys/errors.h"
28 #include "core/file_sys/file_backend.h"
29 #include "core/hle/result.h"
30 #include "core/hle/service/fs/archive.h"
31 
32 namespace Service::FS {
33 
GetMediaTypeFromPath(std::string_view path)34 MediaType GetMediaTypeFromPath(std::string_view path) {
35     if (path.rfind(FileUtil::GetUserPath(FileUtil::UserPath::NANDDir), 0) == 0) {
36         return MediaType::NAND;
37     }
38     if (path.rfind(FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir), 0) == 0) {
39         return MediaType::SDMC;
40     }
41     return MediaType::GameCard;
42 }
43 
GetArchive(ArchiveHandle handle)44 ArchiveBackend* ArchiveManager::GetArchive(ArchiveHandle handle) {
45     auto itr = handle_map.find(handle);
46     return (itr == handle_map.end()) ? nullptr : itr->second.get();
47 }
48 
OpenArchive(ArchiveIdCode id_code,const FileSys::Path & archive_path,u64 program_id)49 ResultVal<ArchiveHandle> ArchiveManager::OpenArchive(ArchiveIdCode id_code,
50                                                      const FileSys::Path& archive_path,
51                                                      u64 program_id) {
52     LOG_TRACE(Service_FS, "Opening archive with id code 0x{:08X}", id_code);
53 
54     auto itr = id_code_map.find(id_code);
55     if (itr == id_code_map.end()) {
56         return FileSys::ERROR_NOT_FOUND;
57     }
58 
59     CASCADE_RESULT(std::unique_ptr<ArchiveBackend> res,
60                    itr->second->Open(archive_path, program_id));
61 
62     // This should never even happen in the first place with 64-bit handles,
63     while (handle_map.count(next_handle) != 0) {
64         ++next_handle;
65     }
66     handle_map.emplace(next_handle, std::move(res));
67     return MakeResult(next_handle++);
68 }
69 
CloseArchive(ArchiveHandle handle)70 ResultCode ArchiveManager::CloseArchive(ArchiveHandle handle) {
71     if (handle_map.erase(handle) == 0)
72         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
73     else
74         return RESULT_SUCCESS;
75 }
76 
77 // TODO(yuriks): This might be what the fs:REG service is for. See the Register/Unregister calls in
78 // http://3dbrew.org/wiki/Filesystem_services#ProgramRegistry_service_.22fs:REG.22
RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory> && factory,ArchiveIdCode id_code)79 ResultCode ArchiveManager::RegisterArchiveType(std::unique_ptr<FileSys::ArchiveFactory>&& factory,
80                                                ArchiveIdCode id_code) {
81     auto result = id_code_map.emplace(id_code, std::move(factory));
82 
83     bool inserted = result.second;
84     ASSERT_MSG(inserted, "Tried to register more than one archive with same id code");
85 
86     auto& archive = result.first->second;
87     LOG_DEBUG(Service_FS, "Registered archive {} with id code 0x{:08X}", archive->GetName(),
88               id_code);
89     return RESULT_SUCCESS;
90 }
91 
92 std::pair<ResultVal<std::shared_ptr<File>>, std::chrono::nanoseconds>
OpenFileFromArchive(ArchiveHandle archive_handle,const FileSys::Path & path,const FileSys::Mode mode)93 ArchiveManager::OpenFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path,
94                                     const FileSys::Mode mode) {
95     ArchiveBackend* archive = GetArchive(archive_handle);
96     if (archive == nullptr) {
97         return std::make_pair(FileSys::ERR_INVALID_ARCHIVE_HANDLE, std::chrono::nanoseconds{0});
98     }
99 
100     const std::chrono::nanoseconds open_timeout_ns{archive->GetOpenDelayNs()};
101     auto backend = archive->OpenFile(path, mode);
102     if (backend.Failed()) {
103         return std::make_pair(backend.Code(), open_timeout_ns);
104     }
105 
106     auto file = std::make_shared<File>(system.Kernel(), std::move(backend).Unwrap(), path);
107     return std::make_pair(MakeResult(std::move(file)), open_timeout_ns);
108 }
109 
DeleteFileFromArchive(ArchiveHandle archive_handle,const FileSys::Path & path)110 ResultCode ArchiveManager::DeleteFileFromArchive(ArchiveHandle archive_handle,
111                                                  const FileSys::Path& path) {
112     ArchiveBackend* archive = GetArchive(archive_handle);
113     if (archive == nullptr)
114         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
115 
116     return archive->DeleteFile(path);
117 }
118 
RenameFileBetweenArchives(ArchiveHandle src_archive_handle,const FileSys::Path & src_path,ArchiveHandle dest_archive_handle,const FileSys::Path & dest_path)119 ResultCode ArchiveManager::RenameFileBetweenArchives(ArchiveHandle src_archive_handle,
120                                                      const FileSys::Path& src_path,
121                                                      ArchiveHandle dest_archive_handle,
122                                                      const FileSys::Path& dest_path) {
123     ArchiveBackend* src_archive = GetArchive(src_archive_handle);
124     ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
125     if (src_archive == nullptr || dest_archive == nullptr)
126         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
127 
128     if (src_archive == dest_archive) {
129         return src_archive->RenameFile(src_path, dest_path);
130     } else {
131         // TODO: Implement renaming across archives
132         return UnimplementedFunction(ErrorModule::FS);
133     }
134 }
135 
DeleteDirectoryFromArchive(ArchiveHandle archive_handle,const FileSys::Path & path)136 ResultCode ArchiveManager::DeleteDirectoryFromArchive(ArchiveHandle archive_handle,
137                                                       const FileSys::Path& path) {
138     ArchiveBackend* archive = GetArchive(archive_handle);
139     if (archive == nullptr)
140         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
141 
142     return archive->DeleteDirectory(path);
143 }
144 
DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,const FileSys::Path & path)145 ResultCode ArchiveManager::DeleteDirectoryRecursivelyFromArchive(ArchiveHandle archive_handle,
146                                                                  const FileSys::Path& path) {
147     ArchiveBackend* archive = GetArchive(archive_handle);
148     if (archive == nullptr)
149         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
150 
151     return archive->DeleteDirectoryRecursively(path);
152 }
153 
CreateFileInArchive(ArchiveHandle archive_handle,const FileSys::Path & path,u64 file_size)154 ResultCode ArchiveManager::CreateFileInArchive(ArchiveHandle archive_handle,
155                                                const FileSys::Path& path, u64 file_size) {
156     ArchiveBackend* archive = GetArchive(archive_handle);
157     if (archive == nullptr)
158         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
159 
160     return archive->CreateFile(path, file_size);
161 }
162 
CreateDirectoryFromArchive(ArchiveHandle archive_handle,const FileSys::Path & path)163 ResultCode ArchiveManager::CreateDirectoryFromArchive(ArchiveHandle archive_handle,
164                                                       const FileSys::Path& path) {
165     ArchiveBackend* archive = GetArchive(archive_handle);
166     if (archive == nullptr)
167         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
168 
169     return archive->CreateDirectory(path);
170 }
171 
RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,const FileSys::Path & src_path,ArchiveHandle dest_archive_handle,const FileSys::Path & dest_path)172 ResultCode ArchiveManager::RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle,
173                                                           const FileSys::Path& src_path,
174                                                           ArchiveHandle dest_archive_handle,
175                                                           const FileSys::Path& dest_path) {
176     ArchiveBackend* src_archive = GetArchive(src_archive_handle);
177     ArchiveBackend* dest_archive = GetArchive(dest_archive_handle);
178     if (src_archive == nullptr || dest_archive == nullptr)
179         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
180 
181     if (src_archive == dest_archive) {
182         return src_archive->RenameDirectory(src_path, dest_path);
183     } else {
184         // TODO: Implement renaming across archives
185         return UnimplementedFunction(ErrorModule::FS);
186     }
187 }
188 
OpenDirectoryFromArchive(ArchiveHandle archive_handle,const FileSys::Path & path)189 ResultVal<std::shared_ptr<Directory>> ArchiveManager::OpenDirectoryFromArchive(
190     ArchiveHandle archive_handle, const FileSys::Path& path) {
191     ArchiveBackend* archive = GetArchive(archive_handle);
192     if (archive == nullptr) {
193         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
194     }
195 
196     auto backend = archive->OpenDirectory(path);
197     if (backend.Failed()) {
198         return backend.Code();
199     }
200 
201     auto directory = std::make_shared<Directory>(std::move(backend).Unwrap(), path);
202     return MakeResult(std::move(directory));
203 }
204 
GetFreeBytesInArchive(ArchiveHandle archive_handle)205 ResultVal<u64> ArchiveManager::GetFreeBytesInArchive(ArchiveHandle archive_handle) {
206     const ArchiveBackend* archive = GetArchive(archive_handle);
207     if (archive == nullptr) {
208         return FileSys::ERR_INVALID_ARCHIVE_HANDLE;
209     }
210     return MakeResult(archive->GetFreeBytes());
211 }
212 
FormatArchive(ArchiveIdCode id_code,const FileSys::ArchiveFormatInfo & format_info,const FileSys::Path & path,u64 program_id)213 ResultCode ArchiveManager::FormatArchive(ArchiveIdCode id_code,
214                                          const FileSys::ArchiveFormatInfo& format_info,
215                                          const FileSys::Path& path, u64 program_id) {
216     auto archive_itr = id_code_map.find(id_code);
217     if (archive_itr == id_code_map.end()) {
218         return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
219     }
220 
221     return archive_itr->second->Format(path, format_info, program_id);
222 }
223 
GetArchiveFormatInfo(ArchiveIdCode id_code,const FileSys::Path & archive_path,u64 program_id)224 ResultVal<FileSys::ArchiveFormatInfo> ArchiveManager::GetArchiveFormatInfo(
225     ArchiveIdCode id_code, const FileSys::Path& archive_path, u64 program_id) {
226     auto archive = id_code_map.find(id_code);
227     if (archive == id_code_map.end()) {
228         return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
229     }
230 
231     return archive->second->GetFormatInfo(archive_path, program_id);
232 }
233 
CreateExtSaveData(MediaType media_type,u32 high,u32 low,const std::vector<u8> & smdh_icon,const FileSys::ArchiveFormatInfo & format_info,u64 program_id)234 ResultCode ArchiveManager::CreateExtSaveData(MediaType media_type, u32 high, u32 low,
235                                              const std::vector<u8>& smdh_icon,
236                                              const FileSys::ArchiveFormatInfo& format_info,
237                                              u64 program_id) {
238     // Construct the binary path to the archive first
239     FileSys::Path path =
240         FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
241 
242     auto archive = id_code_map.find(media_type == MediaType::NAND ? ArchiveIdCode::SharedExtSaveData
243                                                                   : ArchiveIdCode::ExtSaveData);
244 
245     if (archive == id_code_map.end()) {
246         return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
247     }
248 
249     auto ext_savedata = static_cast<FileSys::ArchiveFactory_ExtSaveData*>(archive->second.get());
250 
251     ResultCode result = ext_savedata->Format(path, format_info, program_id);
252     if (result.IsError())
253         return result;
254 
255     ext_savedata->WriteIcon(path, smdh_icon.data(), smdh_icon.size());
256     return RESULT_SUCCESS;
257 }
258 
DeleteExtSaveData(MediaType media_type,u32 high,u32 low)259 ResultCode ArchiveManager::DeleteExtSaveData(MediaType media_type, u32 high, u32 low) {
260     // Construct the binary path to the archive first
261     FileSys::Path path =
262         FileSys::ConstructExtDataBinaryPath(static_cast<u32>(media_type), high, low);
263 
264     std::string media_type_directory;
265     if (media_type == MediaType::NAND) {
266         media_type_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
267     } else if (media_type == MediaType::SDMC) {
268         media_type_directory = FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir);
269     } else {
270         LOG_ERROR(Service_FS, "Unsupported media type {}", media_type);
271         return ResultCode(-1); // TODO(Subv): Find the right error code
272     }
273 
274     // Delete all directories (/user, /boss) and the icon file.
275     std::string base_path =
276         FileSys::GetExtDataContainerPath(media_type_directory, media_type == MediaType::NAND);
277     std::string extsavedata_path = FileSys::GetExtSaveDataPath(base_path, path);
278     if (FileUtil::Exists(extsavedata_path) && !FileUtil::DeleteDirRecursively(extsavedata_path))
279         return ResultCode(-1); // TODO(Subv): Find the right error code
280     return RESULT_SUCCESS;
281 }
282 
DeleteSystemSaveData(u32 high,u32 low)283 ResultCode ArchiveManager::DeleteSystemSaveData(u32 high, u32 low) {
284     // Construct the binary path to the archive first
285     const FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
286 
287     const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
288     const std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory);
289     const std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path);
290     if (!FileUtil::DeleteDirRecursively(systemsavedata_path)) {
291         return ResultCode(-1); // TODO(Subv): Find the right error code
292     }
293 
294     return RESULT_SUCCESS;
295 }
296 
CreateSystemSaveData(u32 high,u32 low)297 ResultCode ArchiveManager::CreateSystemSaveData(u32 high, u32 low) {
298     // Construct the binary path to the archive first
299     const FileSys::Path path = FileSys::ConstructSystemSaveDataBinaryPath(high, low);
300 
301     const std::string& nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
302     const std::string base_path = FileSys::GetSystemSaveDataContainerPath(nand_directory);
303     const std::string systemsavedata_path = FileSys::GetSystemSaveDataPath(base_path, path);
304     if (!FileUtil::CreateFullPath(systemsavedata_path)) {
305         return ResultCode(-1); // TODO(Subv): Find the right error code
306     }
307 
308     return RESULT_SUCCESS;
309 }
310 
GetArchiveResource(MediaType media_type) const311 ResultVal<ArchiveResource> ArchiveManager::GetArchiveResource(MediaType media_type) const {
312     // TODO(Subv): Implement querying the actual size information for these storages.
313     ArchiveResource resource{};
314     resource.sector_size_in_bytes = 512;
315     resource.cluster_size_in_bytes = 16384;
316     resource.partition_capacity_in_clusters = 0x80000; // 8GiB capacity
317     resource.free_space_in_clusters = 0x80000;         // 8GiB free
318     return MakeResult(resource);
319 }
320 
RegisterArchiveTypes()321 void ArchiveManager::RegisterArchiveTypes() {
322     // TODO(Subv): Add the other archive types (see here for the known types:
323     // http://3dbrew.org/wiki/FS:OpenArchive#Archive_idcodes).
324 
325     std::string sdmc_directory = FileUtil::GetUserPath(FileUtil::UserPath::SDMCDir);
326     std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
327     auto sdmc_factory = std::make_unique<FileSys::ArchiveFactory_SDMC>(sdmc_directory);
328     if (sdmc_factory->Initialize())
329         RegisterArchiveType(std::move(sdmc_factory), ArchiveIdCode::SDMC);
330     else
331         LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path {}", sdmc_directory);
332 
333     auto sdmcwo_factory = std::make_unique<FileSys::ArchiveFactory_SDMCWriteOnly>(sdmc_directory);
334     if (sdmcwo_factory->Initialize())
335         RegisterArchiveType(std::move(sdmcwo_factory), ArchiveIdCode::SDMCWriteOnly);
336     else
337         LOG_ERROR(Service_FS, "Can't instantiate SDMCWriteOnly archive with path {}",
338                   sdmc_directory);
339 
340     // Create the SaveData archive
341     auto sd_savedata_source = std::make_shared<FileSys::ArchiveSource_SDSaveData>(sdmc_directory);
342     auto savedata_factory = std::make_unique<FileSys::ArchiveFactory_SaveData>(sd_savedata_source);
343     RegisterArchiveType(std::move(savedata_factory), ArchiveIdCode::SaveData);
344     auto other_savedata_permitted_factory =
345         std::make_unique<FileSys::ArchiveFactory_OtherSaveDataPermitted>(sd_savedata_source);
346     RegisterArchiveType(std::move(other_savedata_permitted_factory),
347                         ArchiveIdCode::OtherSaveDataPermitted);
348     auto other_savedata_general_factory =
349         std::make_unique<FileSys::ArchiveFactory_OtherSaveDataGeneral>(sd_savedata_source);
350     RegisterArchiveType(std::move(other_savedata_general_factory),
351                         ArchiveIdCode::OtherSaveDataGeneral);
352 
353     auto extsavedata_factory =
354         std::make_unique<FileSys::ArchiveFactory_ExtSaveData>(sdmc_directory, false);
355     RegisterArchiveType(std::move(extsavedata_factory), ArchiveIdCode::ExtSaveData);
356 
357     auto sharedextsavedata_factory =
358         std::make_unique<FileSys::ArchiveFactory_ExtSaveData>(nand_directory, true);
359     RegisterArchiveType(std::move(sharedextsavedata_factory), ArchiveIdCode::SharedExtSaveData);
360 
361     // Create the NCCH archive, basically a small variation of the RomFS archive
362     auto savedatacheck_factory = std::make_unique<FileSys::ArchiveFactory_NCCH>();
363     RegisterArchiveType(std::move(savedatacheck_factory), ArchiveIdCode::NCCH);
364 
365     auto systemsavedata_factory =
366         std::make_unique<FileSys::ArchiveFactory_SystemSaveData>(nand_directory);
367     RegisterArchiveType(std::move(systemsavedata_factory), ArchiveIdCode::SystemSaveData);
368 
369     auto selfncch_factory = std::make_unique<FileSys::ArchiveFactory_SelfNCCH>();
370     RegisterArchiveType(std::move(selfncch_factory), ArchiveIdCode::SelfNCCH);
371 }
372 
RegisterSelfNCCH(Loader::AppLoader & app_loader)373 void ArchiveManager::RegisterSelfNCCH(Loader::AppLoader& app_loader) {
374     auto itr = id_code_map.find(ArchiveIdCode::SelfNCCH);
375     if (itr == id_code_map.end()) {
376         LOG_ERROR(Service_FS,
377                   "Could not register a new NCCH because the SelfNCCH archive hasn't been created");
378         return;
379     }
380 
381     auto* factory = static_cast<FileSys::ArchiveFactory_SelfNCCH*>(itr->second.get());
382     factory->Register(app_loader);
383 }
384 
ArchiveManager(Core::System & system)385 ArchiveManager::ArchiveManager(Core::System& system) : system(system) {
386     RegisterArchiveTypes();
387 }
388 
389 } // namespace Service::FS
390