summaryrefslogtreecommitdiff
path: root/src/core/hle
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle')
-rw-r--r--src/core/hle/hle.cpp3
-rw-r--r--src/core/hle/kernel/kernel.cpp5
-rw-r--r--src/core/hle/service/fs/archive.cpp (renamed from src/core/hle/kernel/archive.cpp)284
-rw-r--r--src/core/hle/service/fs/archive.h (renamed from src/core/hle/kernel/archive.h)48
-rw-r--r--src/core/hle/service/fs/fs_user.cpp (renamed from src/core/hle/service/fs_user.cpp)130
-rw-r--r--src/core/hle/service/fs/fs_user.h (renamed from src/core/hle/service/fs_user.h)12
-rw-r--r--src/core/hle/service/service.cpp4
7 files changed, 245 insertions, 241 deletions
diff --git a/src/core/hle/hle.cpp b/src/core/hle/hle.cpp
index 3f73b5538..cc3d5255a 100644
--- a/src/core/hle/hle.cpp
+++ b/src/core/hle/hle.cpp
@@ -8,6 +8,7 @@
8#include "core/hle/hle.h" 8#include "core/hle/hle.h"
9#include "core/hle/kernel/thread.h" 9#include "core/hle/kernel/thread.h"
10#include "core/hle/service/service.h" 10#include "core/hle/service/service.h"
11#include "core/hle/service/fs/archive.h"
11 12
12//////////////////////////////////////////////////////////////////////////////////////////////////// 13////////////////////////////////////////////////////////////////////////////////////////////////////
13 14
@@ -56,6 +57,7 @@ void RegisterAllModules() {
56 57
57void Init() { 58void Init() {
58 Service::Init(); 59 Service::Init();
60 Service::FS::ArchiveInit();
59 61
60 RegisterAllModules(); 62 RegisterAllModules();
61 63
@@ -63,6 +65,7 @@ void Init() {
63} 65}
64 66
65void Shutdown() { 67void Shutdown() {
68 Service::FS::ArchiveShutdown();
66 Service::Shutdown(); 69 Service::Shutdown();
67 70
68 g_module_db.clear(); 71 g_module_db.clear();
diff --git a/src/core/hle/kernel/kernel.cpp b/src/core/hle/kernel/kernel.cpp
index b38be0a49..929422b36 100644
--- a/src/core/hle/kernel/kernel.cpp
+++ b/src/core/hle/kernel/kernel.cpp
@@ -9,7 +9,6 @@
9#include "core/core.h" 9#include "core/core.h"
10#include "core/hle/kernel/kernel.h" 10#include "core/hle/kernel/kernel.h"
11#include "core/hle/kernel/thread.h" 11#include "core/hle/kernel/thread.h"
12#include "core/hle/kernel/archive.h"
13 12
14namespace Kernel { 13namespace Kernel {
15 14
@@ -89,13 +88,11 @@ Object* ObjectPool::CreateByIDType(int type) {
89/// Initialize the kernel 88/// Initialize the kernel
90void Init() { 89void Init() {
91 Kernel::ThreadingInit(); 90 Kernel::ThreadingInit();
92 Kernel::ArchiveInit();
93} 91}
94 92
95/// Shutdown the kernel 93/// Shutdown the kernel
96void Shutdown() { 94void Shutdown() {
97 Kernel::ThreadingShutdown(); 95 Kernel::ThreadingShutdown();
98 Kernel::ArchiveShutdown();
99 96
100 g_object_pool.Clear(); // Free all kernel objects 97 g_object_pool.Clear(); // Free all kernel objects
101} 98}
@@ -106,8 +103,6 @@ void Shutdown() {
106 * @return True on success, otherwise false 103 * @return True on success, otherwise false
107 */ 104 */
108bool LoadExec(u32 entry_point) { 105bool LoadExec(u32 entry_point) {
109 Init();
110
111 Core::g_app_core->SetPC(entry_point); 106 Core::g_app_core->SetPC(entry_point);
112 107
113 // 0x30 is the typical main thread priority I've seen used so far 108 // 0x30 is the typical main thread priority I've seen used so far
diff --git a/src/core/hle/kernel/archive.cpp b/src/core/hle/service/fs/archive.cpp
index 0e3eb4564..caf82d556 100644
--- a/src/core/hle/kernel/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -2,23 +2,37 @@
2// Licensed under GPLv2 2// Licensed under GPLv2
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <map> 5#include <memory>
6#include <unordered_map>
6 7
7#include "common/common_types.h" 8#include "common/common_types.h"
8#include "common/file_util.h" 9#include "common/file_util.h"
9#include "common/math_util.h" 10#include "common/math_util.h"
10 11
11#include "core/file_sys/archive.h" 12#include "core/file_sys/archive_backend.h"
12#include "core/file_sys/archive_sdmc.h" 13#include "core/file_sys/archive_sdmc.h"
13#include "core/file_sys/directory.h" 14#include "core/file_sys/directory_backend.h"
14#include "core/hle/kernel/archive.h" 15#include "core/hle/service/fs/archive.h"
15#include "core/hle/kernel/session.h" 16#include "core/hle/kernel/session.h"
16#include "core/hle/result.h" 17#include "core/hle/result.h"
17 18
18//////////////////////////////////////////////////////////////////////////////////////////////////// 19// Specializes std::hash for ArchiveIdCode, so that we can use it in std::unordered_map.
19// Kernel namespace 20// Workaroung for libstdc++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60970
21namespace std {
22 template <>
23 struct hash<Service::FS::ArchiveIdCode> {
24 typedef Service::FS::ArchiveIdCode argument_type;
25 typedef std::size_t result_type;
26
27 result_type operator()(const argument_type& id_code) const {
28 typedef std::underlying_type<argument_type>::type Type;
29 return std::hash<Type>()(static_cast<Type>(id_code));
30 }
31 };
32}
20 33
21namespace Kernel { 34namespace Service {
35namespace FS {
22 36
23// Command to access archive file 37// Command to access archive file
24enum class FileCommand : u32 { 38enum class FileCommand : u32 {
@@ -43,78 +57,28 @@ enum class DirectoryCommand : u32 {
43 Close = 0x08020000, 57 Close = 0x08020000,
44}; 58};
45 59
46class Archive : public Kernel::Session { 60class Archive {
47public: 61public:
48 std::string GetName() const override { return "Archive: " + name; } 62 Archive(std::unique_ptr<FileSys::ArchiveBackend>&& backend, ArchiveIdCode id_code)
49 63 : backend(std::move(backend)), id_code(id_code) {
50 std::string name; ///< Name of archive (optional) 64 }
51 FileSys::Archive* backend; ///< Archive backend interface
52
53 ResultVal<bool> SyncRequest() override {
54 u32* cmd_buff = Kernel::GetCommandBuffer();
55 FileCommand cmd = static_cast<FileCommand>(cmd_buff[0]);
56 65
57 switch (cmd) { 66 std::string GetName() const { return "Archive: " + backend->GetName(); }
58 // Read from archive...
59 case FileCommand::Read:
60 {
61 u64 offset = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
62 u32 length = cmd_buff[3];
63 u32 address = cmd_buff[5];
64 67
65 // Number of bytes read 68 ArchiveIdCode id_code; ///< Id code of the archive
66 cmd_buff[2] = backend->Read(offset, length, Memory::GetPointer(address)); 69 std::unique_ptr<FileSys::ArchiveBackend> backend; ///< Archive backend interface
67 break;
68 }
69 // Write to archive...
70 case FileCommand::Write:
71 {
72 u64 offset = cmd_buff[1] | ((u64)cmd_buff[2] << 32);
73 u32 length = cmd_buff[3];
74 u32 flush = cmd_buff[4];
75 u32 address = cmd_buff[6];
76
77 // Number of bytes written
78 cmd_buff[2] = backend->Write(offset, length, flush, Memory::GetPointer(address));
79 break;
80 }
81 case FileCommand::GetSize:
82 {
83 u64 filesize = (u64) backend->GetSize();
84 cmd_buff[2] = (u32) filesize; // Lower word
85 cmd_buff[3] = (u32) (filesize >> 32); // Upper word
86 break;
87 }
88 case FileCommand::SetSize:
89 {
90 backend->SetSize(cmd_buff[1] | ((u64)cmd_buff[2] << 32));
91 break;
92 }
93 case FileCommand::Close:
94 {
95 LOG_TRACE(Service_FS, "Close %s %s", GetTypeName().c_str(), GetName().c_str());
96 CloseArchive(backend->GetIdCode());
97 break;
98 }
99 // Unknown command...
100 default:
101 {
102 LOG_ERROR(Service_FS, "Unknown command=0x%08X", cmd);
103 cmd_buff[0] = UnimplementedFunction(ErrorModule::FS).raw;
104 return MakeResult<bool>(false);
105 }
106 }
107 cmd_buff[1] = 0; // No error
108 return MakeResult<bool>(false);
109 }
110}; 70};
111 71
112class File : public Kernel::Session { 72class File : public Kernel::Session {
113public: 73public:
74 File(std::unique_ptr<FileSys::FileBackend>&& backend, const FileSys::Path& path)
75 : backend(std::move(backend)), path(path) {
76 }
77
114 std::string GetName() const override { return "Path: " + path.DebugStr(); } 78 std::string GetName() const override { return "Path: " + path.DebugStr(); }
115 79
116 FileSys::Path path; ///< Path of the file 80 FileSys::Path path; ///< Path of the file
117 std::unique_ptr<FileSys::File> backend; ///< File backend interface 81 std::unique_ptr<FileSys::FileBackend> backend; ///< File backend interface
118 82
119 ResultVal<bool> SyncRequest() override { 83 ResultVal<bool> SyncRequest() override {
120 u32* cmd_buff = Kernel::GetCommandBuffer(); 84 u32* cmd_buff = Kernel::GetCommandBuffer();
@@ -185,10 +149,14 @@ public:
185 149
186class Directory : public Kernel::Session { 150class Directory : public Kernel::Session {
187public: 151public:
152 Directory(std::unique_ptr<FileSys::DirectoryBackend>&& backend, const FileSys::Path& path)
153 : backend(std::move(backend)), path(path) {
154 }
155
188 std::string GetName() const override { return "Directory: " + path.DebugStr(); } 156 std::string GetName() const override { return "Directory: " + path.DebugStr(); }
189 157
190 FileSys::Path path; ///< Path of the directory 158 FileSys::Path path; ///< Path of the directory
191 std::unique_ptr<FileSys::Directory> backend; ///< File backend interface 159 std::unique_ptr<FileSys::DirectoryBackend> backend; ///< File backend interface
192 160
193 ResultVal<bool> SyncRequest() override { 161 ResultVal<bool> SyncRequest() override {
194 u32* cmd_buff = Kernel::GetCommandBuffer(); 162 u32* cmd_buff = Kernel::GetCommandBuffer();
@@ -230,105 +198,95 @@ public:
230 198
231//////////////////////////////////////////////////////////////////////////////////////////////////// 199////////////////////////////////////////////////////////////////////////////////////////////////////
232 200
233std::map<FileSys::Archive::IdCode, Handle> g_archive_map; ///< Map of file archives by IdCode 201/**
202 * Map of registered archives, identified by id code. Once an archive is registered here, it is
203 * never removed until the FS service is shut down.
204 */
205static std::unordered_map<ArchiveIdCode, std::unique_ptr<Archive>> id_code_map;
206
207/**
208 * Map of active archive handles. Values are pointers to the archives in `idcode_map`.
209 */
210static std::unordered_map<ArchiveHandle, Archive*> handle_map;
211static ArchiveHandle next_handle;
212
213static Archive* GetArchive(ArchiveHandle handle) {
214 auto itr = handle_map.find(handle);
215 return (itr == handle_map.end()) ? nullptr : itr->second;
216}
217
218ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code) {
219 LOG_TRACE(Service_FS, "Opening archive with id code 0x%08X", id_code);
234 220
235ResultVal<Handle> OpenArchive(FileSys::Archive::IdCode id_code) { 221 auto itr = id_code_map.find(id_code);
236 auto itr = g_archive_map.find(id_code); 222 if (itr == id_code_map.end()) {
237 if (itr == g_archive_map.end()) { 223 // TODO: Verify error against hardware
238 return ResultCode(ErrorDescription::NotFound, ErrorModule::FS, 224 return ResultCode(ErrorDescription::NotFound, ErrorModule::FS,
239 ErrorSummary::NotFound, ErrorLevel::Permanent); 225 ErrorSummary::NotFound, ErrorLevel::Permanent);
240 } 226 }
241 227
242 return MakeResult<Handle>(itr->second); 228 // This should never even happen in the first place with 64-bit handles,
229 while (handle_map.count(next_handle) != 0) {
230 ++next_handle;
231 }
232 handle_map.emplace(next_handle, itr->second.get());
233 return MakeResult<ArchiveHandle>(next_handle++);
243} 234}
244 235
245ResultCode CloseArchive(FileSys::Archive::IdCode id_code) { 236ResultCode CloseArchive(ArchiveHandle handle) {
246 auto itr = g_archive_map.find(id_code); 237 if (handle_map.erase(handle) == 0)
247 if (itr == g_archive_map.end()) {
248 LOG_ERROR(Service_FS, "Cannot close archive %d, does not exist!", (int)id_code);
249 return InvalidHandle(ErrorModule::FS); 238 return InvalidHandle(ErrorModule::FS);
250 } 239 else
251 240 return RESULT_SUCCESS;
252 LOG_TRACE(Service_FS, "Closed archive %d", (int) id_code);
253 return RESULT_SUCCESS;
254} 241}
255 242
256/** 243// TODO(yuriks): This might be what the fs:REG service is for. See the Register/Unregister calls in
257 * Mounts an archive 244// http://3dbrew.org/wiki/Filesystem_services#ProgramRegistry_service_.22fs:REG.22
258 * @param archive Pointer to the archive to mount 245ResultCode CreateArchive(std::unique_ptr<FileSys::ArchiveBackend>&& backend, ArchiveIdCode id_code) {
259 */ 246 auto result = id_code_map.emplace(id_code, std::make_unique<Archive>(std::move(backend), id_code));
260ResultCode MountArchive(Archive* archive) {
261 FileSys::Archive::IdCode id_code = archive->backend->GetIdCode();
262 ResultVal<Handle> archive_handle = OpenArchive(id_code);
263 if (archive_handle.Succeeded()) {
264 LOG_ERROR(Service_FS, "Cannot mount two archives with the same ID code! (%d)", (int) id_code);
265 return archive_handle.Code();
266 }
267 g_archive_map[id_code] = archive->GetHandle();
268 LOG_TRACE(Service_FS, "Mounted archive %s", archive->GetName().c_str());
269 return RESULT_SUCCESS;
270}
271 247
272ResultCode CreateArchive(FileSys::Archive* backend, const std::string& name) { 248 bool inserted = result.second;
273 Archive* archive = new Archive; 249 _dbg_assert_msg_(Service_FS, inserted, "Tried to register more than one archive with same id code");
274 Handle handle = Kernel::g_object_pool.Create(archive);
275 archive->name = name;
276 archive->backend = backend;
277 250
278 ResultCode result = MountArchive(archive); 251 auto& archive = result.first->second;
279 if (result.IsError()) { 252 LOG_DEBUG(Service_FS, "Registered archive %s with id code 0x%08X", archive->GetName().c_str(), id_code);
280 return result;
281 }
282
283 return RESULT_SUCCESS; 253 return RESULT_SUCCESS;
284} 254}
285 255
286ResultVal<Handle> OpenFileFromArchive(Handle archive_handle, const FileSys::Path& path, const FileSys::Mode mode) { 256ResultVal<Handle> OpenFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path, const FileSys::Mode mode) {
287 // TODO(bunnei): Binary type files get a raw file pointer to the archive. Currently, we create 257 Archive* archive = GetArchive(archive_handle);
288 // the archive file handles at app loading, and then keep them persistent throughout execution. 258 if (archive == nullptr)
289 // Archives file handles are just reused and not actually freed until emulation shut down.
290 // Verify if real hardware works this way, or if new handles are created each time
291 if (path.GetType() == FileSys::Binary)
292 // TODO(bunnei): FixMe - this is a hack to compensate for an incorrect FileSys backend
293 // design. While the functionally of this is OK, our implementation decision to separate
294 // normal files from archive file pointers is very likely wrong.
295 // See https://github.com/citra-emu/citra/issues/205
296 return MakeResult<Handle>(archive_handle);
297
298 File* file = new File;
299 Handle handle = Kernel::g_object_pool.Create(file);
300
301 Archive* archive = Kernel::g_object_pool.Get<Archive>(archive_handle);
302 if (archive == nullptr) {
303 return InvalidHandle(ErrorModule::FS); 259 return InvalidHandle(ErrorModule::FS);
304 }
305 file->path = path;
306 file->backend = archive->backend->OpenFile(path, mode);
307 260
308 if (!file->backend) { 261 std::unique_ptr<FileSys::FileBackend> backend = archive->backend->OpenFile(path, mode);
262 if (backend == nullptr) {
309 return ResultCode(ErrorDescription::NotFound, ErrorModule::FS, 263 return ResultCode(ErrorDescription::NotFound, ErrorModule::FS,
310 ErrorSummary::NotFound, ErrorLevel::Permanent); 264 ErrorSummary::NotFound, ErrorLevel::Permanent);
311 } 265 }
312 266
267 auto file = std::make_unique<File>(std::move(backend), path);
268 Handle handle = Kernel::g_object_pool.Create(file.release());
313 return MakeResult<Handle>(handle); 269 return MakeResult<Handle>(handle);
314} 270}
315 271
316ResultCode DeleteFileFromArchive(Handle archive_handle, const FileSys::Path& path) { 272ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
317 Archive* archive = Kernel::g_object_pool.GetFast<Archive>(archive_handle); 273 Archive* archive = GetArchive(archive_handle);
318 if (archive == nullptr) 274 if (archive == nullptr)
319 return InvalidHandle(ErrorModule::FS); 275 return InvalidHandle(ErrorModule::FS);
276
320 if (archive->backend->DeleteFile(path)) 277 if (archive->backend->DeleteFile(path))
321 return RESULT_SUCCESS; 278 return RESULT_SUCCESS;
322 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description 279 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
323 ErrorSummary::Canceled, ErrorLevel::Status); 280 ErrorSummary::Canceled, ErrorLevel::Status);
324} 281}
325 282
326ResultCode RenameFileBetweenArchives(Handle src_archive_handle, const FileSys::Path& src_path, 283ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle, const FileSys::Path& src_path,
327 Handle dest_archive_handle, const FileSys::Path& dest_path) { 284 ArchiveHandle dest_archive_handle, const FileSys::Path& dest_path) {
328 Archive* src_archive = Kernel::g_object_pool.GetFast<Archive>(src_archive_handle); 285 Archive* src_archive = GetArchive(src_archive_handle);
329 Archive* dest_archive = Kernel::g_object_pool.GetFast<Archive>(dest_archive_handle); 286 Archive* dest_archive = GetArchive(dest_archive_handle);
330 if (src_archive == nullptr || dest_archive == nullptr) 287 if (src_archive == nullptr || dest_archive == nullptr)
331 return InvalidHandle(ErrorModule::FS); 288 return InvalidHandle(ErrorModule::FS);
289
332 if (src_archive == dest_archive) { 290 if (src_archive == dest_archive) {
333 if (src_archive->backend->RenameFile(src_path, dest_path)) 291 if (src_archive->backend->RenameFile(src_path, dest_path))
334 return RESULT_SUCCESS; 292 return RESULT_SUCCESS;
@@ -336,36 +294,42 @@ ResultCode RenameFileBetweenArchives(Handle src_archive_handle, const FileSys::P
336 // TODO: Implement renaming across archives 294 // TODO: Implement renaming across archives
337 return UnimplementedFunction(ErrorModule::FS); 295 return UnimplementedFunction(ErrorModule::FS);
338 } 296 }
297
298 // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
299 // exist or similar. Verify.
339 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description 300 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
340 ErrorSummary::NothingHappened, ErrorLevel::Status); 301 ErrorSummary::NothingHappened, ErrorLevel::Status);
341} 302}
342 303
343ResultCode DeleteDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path) { 304ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
344 Archive* archive = Kernel::g_object_pool.GetFast<Archive>(archive_handle); 305 Archive* archive = GetArchive(archive_handle);
345 if (archive == nullptr) 306 if (archive == nullptr)
346 return InvalidHandle(ErrorModule::FS); 307 return InvalidHandle(ErrorModule::FS);
308
347 if (archive->backend->DeleteDirectory(path)) 309 if (archive->backend->DeleteDirectory(path))
348 return RESULT_SUCCESS; 310 return RESULT_SUCCESS;
349 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description 311 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
350 ErrorSummary::Canceled, ErrorLevel::Status); 312 ErrorSummary::Canceled, ErrorLevel::Status);
351} 313}
352 314
353ResultCode CreateDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path) { 315ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
354 Archive* archive = Kernel::g_object_pool.GetFast<Archive>(archive_handle); 316 Archive* archive = GetArchive(archive_handle);
355 if (archive == nullptr) 317 if (archive == nullptr)
356 return InvalidHandle(ErrorModule::FS); 318 return InvalidHandle(ErrorModule::FS);
319
357 if (archive->backend->CreateDirectory(path)) 320 if (archive->backend->CreateDirectory(path))
358 return RESULT_SUCCESS; 321 return RESULT_SUCCESS;
359 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description 322 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
360 ErrorSummary::Canceled, ErrorLevel::Status); 323 ErrorSummary::Canceled, ErrorLevel::Status);
361} 324}
362 325
363ResultCode RenameDirectoryBetweenArchives(Handle src_archive_handle, const FileSys::Path& src_path, 326ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle, const FileSys::Path& src_path,
364 Handle dest_archive_handle, const FileSys::Path& dest_path) { 327 ArchiveHandle dest_archive_handle, const FileSys::Path& dest_path) {
365 Archive* src_archive = Kernel::g_object_pool.GetFast<Archive>(src_archive_handle); 328 Archive* src_archive = GetArchive(src_archive_handle);
366 Archive* dest_archive = Kernel::g_object_pool.GetFast<Archive>(dest_archive_handle); 329 Archive* dest_archive = GetArchive(dest_archive_handle);
367 if (src_archive == nullptr || dest_archive == nullptr) 330 if (src_archive == nullptr || dest_archive == nullptr)
368 return InvalidHandle(ErrorModule::FS); 331 return InvalidHandle(ErrorModule::FS);
332
369 if (src_archive == dest_archive) { 333 if (src_archive == dest_archive) {
370 if (src_archive->backend->RenameDirectory(src_path, dest_path)) 334 if (src_archive->backend->RenameDirectory(src_path, dest_path))
371 return RESULT_SUCCESS; 335 return RESULT_SUCCESS;
@@ -373,6 +337,9 @@ ResultCode RenameDirectoryBetweenArchives(Handle src_archive_handle, const FileS
373 // TODO: Implement renaming across archives 337 // TODO: Implement renaming across archives
374 return UnimplementedFunction(ErrorModule::FS); 338 return UnimplementedFunction(ErrorModule::FS);
375 } 339 }
340
341 // TODO(yuriks): This code probably isn't right, it'll return a Status even if the file didn't
342 // exist or similar. Verify.
376 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description 343 return ResultCode(ErrorDescription::NoData, ErrorModule::FS, // TODO: verify description
377 ErrorSummary::NothingHappened, ErrorLevel::Status); 344 ErrorSummary::NothingHappened, ErrorLevel::Status);
378} 345}
@@ -383,44 +350,43 @@ ResultCode RenameDirectoryBetweenArchives(Handle src_archive_handle, const FileS
383 * @param path Path to the Directory inside of the Archive 350 * @param path Path to the Directory inside of the Archive
384 * @return Opened Directory object 351 * @return Opened Directory object
385 */ 352 */
386ResultVal<Handle> OpenDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path) { 353ResultVal<Handle> OpenDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path) {
387 Directory* directory = new Directory; 354 Archive* archive = GetArchive(archive_handle);
388 Handle handle = Kernel::g_object_pool.Create(directory); 355 if (archive == nullptr)
389
390 Archive* archive = Kernel::g_object_pool.Get<Archive>(archive_handle);
391 if (archive == nullptr) {
392 return InvalidHandle(ErrorModule::FS); 356 return InvalidHandle(ErrorModule::FS);
393 }
394 directory->path = path;
395 directory->backend = archive->backend->OpenDirectory(path);
396 357
397 if (!directory->backend) { 358 std::unique_ptr<FileSys::DirectoryBackend> backend = archive->backend->OpenDirectory(path);
359 if (backend == nullptr) {
398 return ResultCode(ErrorDescription::NotFound, ErrorModule::FS, 360 return ResultCode(ErrorDescription::NotFound, ErrorModule::FS,
399 ErrorSummary::NotFound, ErrorLevel::Permanent); 361 ErrorSummary::NotFound, ErrorLevel::Permanent);
400 } 362 }
401 363
364 auto directory = std::make_unique<Directory>(std::move(backend), path);
365 Handle handle = Kernel::g_object_pool.Create(directory.release());
402 return MakeResult<Handle>(handle); 366 return MakeResult<Handle>(handle);
403} 367}
404 368
405/// Initialize archives 369/// Initialize archives
406void ArchiveInit() { 370void ArchiveInit() {
407 g_archive_map.clear(); 371 next_handle = 1;
408 372
409 // TODO(Link Mauve): Add the other archive types (see here for the known types: 373 // TODO(Link Mauve): Add the other archive types (see here for the known types:
410 // http://3dbrew.org/wiki/FS:OpenArchive#Archive_idcodes). Currently the only half-finished 374 // http://3dbrew.org/wiki/FS:OpenArchive#Archive_idcodes). Currently the only half-finished
411 // archive type is SDMC, so it is the only one getting exposed. 375 // archive type is SDMC, so it is the only one getting exposed.
412 376
413 std::string sdmc_directory = FileUtil::GetUserPath(D_SDMC_IDX); 377 std::string sdmc_directory = FileUtil::GetUserPath(D_SDMC_IDX);
414 auto archive = new FileSys::Archive_SDMC(sdmc_directory); 378 auto archive = std::make_unique<FileSys::Archive_SDMC>(sdmc_directory);
415 if (archive->Initialize()) 379 if (archive->Initialize())
416 CreateArchive(archive, "SDMC"); 380 CreateArchive(std::move(archive), ArchiveIdCode::SDMC);
417 else 381 else
418 LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path %s", sdmc_directory.c_str()); 382 LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path %s", sdmc_directory.c_str());
419} 383}
420 384
421/// Shutdown archives 385/// Shutdown archives
422void ArchiveShutdown() { 386void ArchiveShutdown() {
423 g_archive_map.clear(); 387 handle_map.clear();
388 id_code_map.clear();
424} 389}
425 390
426} // namespace Kernel 391} // namespace FS
392} // namespace Service
diff --git a/src/core/hle/kernel/archive.h b/src/core/hle/service/fs/archive.h
index b50833a2b..a38de92e3 100644
--- a/src/core/hle/kernel/archive.h
+++ b/src/core/hle/service/fs/archive.h
@@ -6,34 +6,45 @@
6 6
7#include "common/common_types.h" 7#include "common/common_types.h"
8 8
9#include "core/file_sys/archive.h" 9#include "core/file_sys/archive_backend.h"
10#include "core/hle/kernel/kernel.h" 10#include "core/hle/kernel/kernel.h"
11#include "core/hle/result.h" 11#include "core/hle/result.h"
12 12
13//////////////////////////////////////////////////////////////////////////////////////////////////// 13namespace Service {
14// Kernel namespace 14namespace FS {
15 15
16namespace Kernel { 16/// Supported archive types
17enum class ArchiveIdCode : u32 {
18 RomFS = 0x00000003,
19 SaveData = 0x00000004,
20 ExtSaveData = 0x00000006,
21 SharedExtSaveData = 0x00000007,
22 SystemSaveData = 0x00000008,
23 SDMC = 0x00000009,
24 SDMCWriteOnly = 0x0000000A,
25};
26
27typedef u64 ArchiveHandle;
17 28
18/** 29/**
19 * Opens an archive 30 * Opens an archive
20 * @param id_code IdCode of the archive to open 31 * @param id_code IdCode of the archive to open
21 * @return Handle to the opened archive 32 * @return Handle to the opened archive
22 */ 33 */
23ResultVal<Handle> OpenArchive(FileSys::Archive::IdCode id_code); 34ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code);
24 35
25/** 36/**
26 * Closes an archive 37 * Closes an archive
27 * @param id_code IdCode of the archive to open 38 * @param id_code IdCode of the archive to open
28 */ 39 */
29ResultCode CloseArchive(FileSys::Archive::IdCode id_code); 40ResultCode CloseArchive(ArchiveHandle handle);
30 41
31/** 42/**
32 * Creates an Archive 43 * Creates an Archive
33 * @param backend File system backend interface to the archive 44 * @param backend File system backend interface to the archive
34 * @param name Name of Archive 45 * @param id_code Id code used to access this type of archive
35 */ 46 */
36ResultCode CreateArchive(FileSys::Archive* backend, const std::string& name); 47ResultCode CreateArchive(std::unique_ptr<FileSys::ArchiveBackend>&& backend, ArchiveIdCode id_code);
37 48
38/** 49/**
39 * Open a File from an Archive 50 * Open a File from an Archive
@@ -42,7 +53,7 @@ ResultCode CreateArchive(FileSys::Archive* backend, const std::string& name);
42 * @param mode Mode under which to open the File 53 * @param mode Mode under which to open the File
43 * @return Handle to the opened File object 54 * @return Handle to the opened File object
44 */ 55 */
45ResultVal<Handle> OpenFileFromArchive(Handle archive_handle, const FileSys::Path& path, const FileSys::Mode mode); 56ResultVal<Handle> OpenFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path, const FileSys::Mode mode);
46 57
47/** 58/**
48 * Delete a File from an Archive 59 * Delete a File from an Archive
@@ -50,7 +61,7 @@ ResultVal<Handle> OpenFileFromArchive(Handle archive_handle, const FileSys::Path
50 * @param path Path to the File inside of the Archive 61 * @param path Path to the File inside of the Archive
51 * @return Whether deletion succeeded 62 * @return Whether deletion succeeded
52 */ 63 */
53ResultCode DeleteFileFromArchive(Handle archive_handle, const FileSys::Path& path); 64ResultCode DeleteFileFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
54 65
55/** 66/**
56 * Rename a File between two Archives 67 * Rename a File between two Archives
@@ -60,8 +71,8 @@ ResultCode DeleteFileFromArchive(Handle archive_handle, const FileSys::Path& pat
60 * @param dest_path Path to the File inside of the destination Archive 71 * @param dest_path Path to the File inside of the destination Archive
61 * @return Whether rename succeeded 72 * @return Whether rename succeeded
62 */ 73 */
63ResultCode RenameFileBetweenArchives(Handle src_archive_handle, const FileSys::Path& src_path, 74ResultCode RenameFileBetweenArchives(ArchiveHandle src_archive_handle, const FileSys::Path& src_path,
64 Handle dest_archive_handle, const FileSys::Path& dest_path); 75 ArchiveHandle dest_archive_handle, const FileSys::Path& dest_path);
65 76
66/** 77/**
67 * Delete a Directory from an Archive 78 * Delete a Directory from an Archive
@@ -69,7 +80,7 @@ ResultCode RenameFileBetweenArchives(Handle src_archive_handle, const FileSys::P
69 * @param path Path to the Directory inside of the Archive 80 * @param path Path to the Directory inside of the Archive
70 * @return Whether deletion succeeded 81 * @return Whether deletion succeeded
71 */ 82 */
72ResultCode DeleteDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path); 83ResultCode DeleteDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
73 84
74/** 85/**
75 * Create a Directory from an Archive 86 * Create a Directory from an Archive
@@ -77,7 +88,7 @@ ResultCode DeleteDirectoryFromArchive(Handle archive_handle, const FileSys::Path
77 * @param path Path to the Directory inside of the Archive 88 * @param path Path to the Directory inside of the Archive
78 * @return Whether creation of directory succeeded 89 * @return Whether creation of directory succeeded
79 */ 90 */
80ResultCode CreateDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path); 91ResultCode CreateDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
81 92
82/** 93/**
83 * Rename a Directory between two Archives 94 * Rename a Directory between two Archives
@@ -87,8 +98,8 @@ ResultCode CreateDirectoryFromArchive(Handle archive_handle, const FileSys::Path
87 * @param dest_path Path to the Directory inside of the destination Archive 98 * @param dest_path Path to the Directory inside of the destination Archive
88 * @return Whether rename succeeded 99 * @return Whether rename succeeded
89 */ 100 */
90ResultCode RenameDirectoryBetweenArchives(Handle src_archive_handle, const FileSys::Path& src_path, 101ResultCode RenameDirectoryBetweenArchives(ArchiveHandle src_archive_handle, const FileSys::Path& src_path,
91 Handle dest_archive_handle, const FileSys::Path& dest_path); 102 ArchiveHandle dest_archive_handle, const FileSys::Path& dest_path);
92 103
93/** 104/**
94 * Open a Directory from an Archive 105 * Open a Directory from an Archive
@@ -96,7 +107,7 @@ ResultCode RenameDirectoryBetweenArchives(Handle src_archive_handle, const FileS
96 * @param path Path to the Directory inside of the Archive 107 * @param path Path to the Directory inside of the Archive
97 * @return Handle to the opened File object 108 * @return Handle to the opened File object
98 */ 109 */
99ResultVal<Handle> OpenDirectoryFromArchive(Handle archive_handle, const FileSys::Path& path); 110ResultVal<Handle> OpenDirectoryFromArchive(ArchiveHandle archive_handle, const FileSys::Path& path);
100 111
101/// Initialize archives 112/// Initialize archives
102void ArchiveInit(); 113void ArchiveInit();
@@ -104,4 +115,5 @@ void ArchiveInit();
104/// Shutdown archives 115/// Shutdown archives
105void ArchiveShutdown(); 116void ArchiveShutdown();
106 117
107} // namespace FileSys 118} // namespace FS
119} // namespace Service
diff --git a/src/core/hle/service/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp
index 672ba2475..0f75d5e3a 100644
--- a/src/core/hle/service/fs_user.cpp
+++ b/src/core/hle/service/fs/fs_user.cpp
@@ -3,18 +3,23 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include "common/common.h" 5#include "common/common.h"
6#include "common/scope_exit.h"
6 7
7#include "common/string_util.h" 8#include "common/string_util.h"
8#include "core/hle/kernel/archive.h" 9#include "core/hle/service/fs/archive.h"
9#include "core/hle/kernel/archive.h"
10#include "core/hle/result.h" 10#include "core/hle/result.h"
11#include "core/hle/service/fs_user.h" 11#include "core/hle/service/fs/fs_user.h"
12#include "core/settings.h" 12#include "core/settings.h"
13 13
14//////////////////////////////////////////////////////////////////////////////////////////////////// 14////////////////////////////////////////////////////////////////////////////////////////////////////
15// Namespace FS_User 15// Namespace FS_User
16 16
17namespace FS_User { 17namespace Service {
18namespace FS {
19
20static ArchiveHandle MakeArchiveHandle(u32 low_word, u32 high_word) {
21 return (u64)low_word | ((u64)high_word << 32);
22}
18 23
19static void Initialize(Service::Interface* self) { 24static void Initialize(Service::Interface* self) {
20 u32* cmd_buff = Kernel::GetCommandBuffer(); 25 u32* cmd_buff = Kernel::GetCommandBuffer();
@@ -57,11 +62,12 @@ static void OpenFile(Service::Interface* self) {
57 62
58 LOG_DEBUG(Service_FS, "path=%s, mode=%d attrs=%u", file_path.DebugStr().c_str(), mode.hex, attributes); 63 LOG_DEBUG(Service_FS, "path=%s, mode=%d attrs=%u", file_path.DebugStr().c_str(), mode.hex, attributes);
59 64
60 ResultVal<Handle> handle = Kernel::OpenFileFromArchive(archive_handle, file_path, mode); 65 ResultVal<Handle> handle = OpenFileFromArchive(archive_handle, file_path, mode);
61 cmd_buff[1] = handle.Code().raw; 66 cmd_buff[1] = handle.Code().raw;
62 if (handle.Succeeded()) { 67 if (handle.Succeeded()) {
63 cmd_buff[3] = *handle; 68 cmd_buff[3] = *handle;
64 } else { 69 } else {
70 cmd_buff[3] = 0;
65 LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str()); 71 LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str());
66 } 72 }
67} 73}
@@ -88,7 +94,7 @@ static void OpenFile(Service::Interface* self) {
88static void OpenFileDirectly(Service::Interface* self) { 94static void OpenFileDirectly(Service::Interface* self) {
89 u32* cmd_buff = Kernel::GetCommandBuffer(); 95 u32* cmd_buff = Kernel::GetCommandBuffer();
90 96
91 auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[2]); 97 auto archive_id = static_cast<FS::ArchiveIdCode>(cmd_buff[2]);
92 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[3]); 98 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
93 u32 archivename_size = cmd_buff[4]; 99 u32 archivename_size = cmd_buff[4];
94 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[5]); 100 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[5]);
@@ -106,25 +112,25 @@ static void OpenFileDirectly(Service::Interface* self) {
106 if (archive_path.GetType() != FileSys::Empty) { 112 if (archive_path.GetType() != FileSys::Empty) {
107 LOG_ERROR(Service_FS, "archive LowPath type other than empty is currently unsupported"); 113 LOG_ERROR(Service_FS, "archive LowPath type other than empty is currently unsupported");
108 cmd_buff[1] = UnimplementedFunction(ErrorModule::FS).raw; 114 cmd_buff[1] = UnimplementedFunction(ErrorModule::FS).raw;
115 cmd_buff[3] = 0;
109 return; 116 return;
110 } 117 }
111 118
112 // TODO(Link Mauve): Check if we should even get a handle for the archive, and don't leak it 119 ResultVal<ArchiveHandle> archive_handle = OpenArchive(archive_id);
113 // TODO(yuriks): Why is there all this duplicate (and seemingly useless) code up here?
114 ResultVal<Handle> archive_handle = Kernel::OpenArchive(archive_id);
115 cmd_buff[1] = archive_handle.Code().raw;
116 if (archive_handle.Failed()) { 120 if (archive_handle.Failed()) {
117 LOG_ERROR(Service_FS, "failed to get a handle for archive"); 121 LOG_ERROR(Service_FS, "failed to get a handle for archive");
122 cmd_buff[1] = archive_handle.Code().raw;
123 cmd_buff[3] = 0;
118 return; 124 return;
119 } 125 }
120 // cmd_buff[2] isn't used according to 3dmoo's implementation. 126 SCOPE_EXIT({ CloseArchive(*archive_handle); });
121 cmd_buff[3] = *archive_handle;
122 127
123 ResultVal<Handle> handle = Kernel::OpenFileFromArchive(*archive_handle, file_path, mode); 128 ResultVal<Handle> handle = OpenFileFromArchive(*archive_handle, file_path, mode);
124 cmd_buff[1] = handle.Code().raw; 129 cmd_buff[1] = handle.Code().raw;
125 if (handle.Succeeded()) { 130 if (handle.Succeeded()) {
126 cmd_buff[3] = *handle; 131 cmd_buff[3] = *handle;
127 } else { 132 } else {
133 cmd_buff[3] = 0;
128 LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str()); 134 LOG_ERROR(Service_FS, "failed to get a handle for file %s", file_path.DebugStr().c_str());
129 } 135 }
130} 136}
@@ -140,12 +146,10 @@ static void OpenFileDirectly(Service::Interface* self) {
140 * Outputs: 146 * Outputs:
141 * 1 : Result of function, 0 on success, otherwise error code 147 * 1 : Result of function, 0 on success, otherwise error code
142 */ 148 */
143void DeleteFile(Service::Interface* self) { 149static void DeleteFile(Service::Interface* self) {
144 u32* cmd_buff = Kernel::GetCommandBuffer(); 150 u32* cmd_buff = Kernel::GetCommandBuffer();
145 151
146 // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to 152 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
147 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
148 Handle archive_handle = static_cast<Handle>(cmd_buff[3]);
149 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]); 153 auto filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
150 u32 filename_size = cmd_buff[5]; 154 u32 filename_size = cmd_buff[5];
151 u32 filename_ptr = cmd_buff[7]; 155 u32 filename_ptr = cmd_buff[7];
@@ -155,7 +159,7 @@ void DeleteFile(Service::Interface* self) {
155 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s", 159 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s",
156 filename_type, filename_size, file_path.DebugStr().c_str()); 160 filename_type, filename_size, file_path.DebugStr().c_str());
157 161
158 cmd_buff[1] = Kernel::DeleteFileFromArchive(archive_handle, file_path).raw; 162 cmd_buff[1] = DeleteFileFromArchive(archive_handle, file_path).raw;
159} 163}
160 164
161/* 165/*
@@ -174,15 +178,13 @@ void DeleteFile(Service::Interface* self) {
174 * Outputs: 178 * Outputs:
175 * 1 : Result of function, 0 on success, otherwise error code 179 * 1 : Result of function, 0 on success, otherwise error code
176 */ 180 */
177void RenameFile(Service::Interface* self) { 181static void RenameFile(Service::Interface* self) {
178 u32* cmd_buff = Kernel::GetCommandBuffer(); 182 u32* cmd_buff = Kernel::GetCommandBuffer();
179 183
180 // TODO(Link Mauve): cmd_buff[2] and cmd_buff[6], aka archive handle lower word, aren't used according to 184 ArchiveHandle src_archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
181 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
182 Handle src_archive_handle = static_cast<Handle>(cmd_buff[3]);
183 auto src_filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]); 185 auto src_filename_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
184 u32 src_filename_size = cmd_buff[5]; 186 u32 src_filename_size = cmd_buff[5];
185 Handle dest_archive_handle = static_cast<Handle>(cmd_buff[7]); 187 ArchiveHandle dest_archive_handle = MakeArchiveHandle(cmd_buff[6], cmd_buff[7]);;
186 auto dest_filename_type = static_cast<FileSys::LowPathType>(cmd_buff[8]); 188 auto dest_filename_type = static_cast<FileSys::LowPathType>(cmd_buff[8]);
187 u32 dest_filename_size = cmd_buff[9]; 189 u32 dest_filename_size = cmd_buff[9];
188 u32 src_filename_ptr = cmd_buff[11]; 190 u32 src_filename_ptr = cmd_buff[11];
@@ -195,7 +197,7 @@ void RenameFile(Service::Interface* self) {
195 src_filename_type, src_filename_size, src_file_path.DebugStr().c_str(), 197 src_filename_type, src_filename_size, src_file_path.DebugStr().c_str(),
196 dest_filename_type, dest_filename_size, dest_file_path.DebugStr().c_str()); 198 dest_filename_type, dest_filename_size, dest_file_path.DebugStr().c_str());
197 199
198 cmd_buff[1] = Kernel::RenameFileBetweenArchives(src_archive_handle, src_file_path, dest_archive_handle, dest_file_path).raw; 200 cmd_buff[1] = RenameFileBetweenArchives(src_archive_handle, src_file_path, dest_archive_handle, dest_file_path).raw;
199} 201}
200 202
201/* 203/*
@@ -209,12 +211,10 @@ void RenameFile(Service::Interface* self) {
209 * Outputs: 211 * Outputs:
210 * 1 : Result of function, 0 on success, otherwise error code 212 * 1 : Result of function, 0 on success, otherwise error code
211 */ 213 */
212void DeleteDirectory(Service::Interface* self) { 214static void DeleteDirectory(Service::Interface* self) {
213 u32* cmd_buff = Kernel::GetCommandBuffer(); 215 u32* cmd_buff = Kernel::GetCommandBuffer();
214 216
215 // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to 217 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
216 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
217 Handle archive_handle = static_cast<Handle>(cmd_buff[3]);
218 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]); 218 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
219 u32 dirname_size = cmd_buff[5]; 219 u32 dirname_size = cmd_buff[5];
220 u32 dirname_ptr = cmd_buff[7]; 220 u32 dirname_ptr = cmd_buff[7];
@@ -224,7 +224,7 @@ void DeleteDirectory(Service::Interface* self) {
224 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s", 224 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s",
225 dirname_type, dirname_size, dir_path.DebugStr().c_str()); 225 dirname_type, dirname_size, dir_path.DebugStr().c_str());
226 226
227 cmd_buff[1] = Kernel::DeleteDirectoryFromArchive(archive_handle, dir_path).raw; 227 cmd_buff[1] = DeleteDirectoryFromArchive(archive_handle, dir_path).raw;
228} 228}
229 229
230/* 230/*
@@ -241,9 +241,7 @@ void DeleteDirectory(Service::Interface* self) {
241static void CreateDirectory(Service::Interface* self) { 241static void CreateDirectory(Service::Interface* self) {
242 u32* cmd_buff = Kernel::GetCommandBuffer(); 242 u32* cmd_buff = Kernel::GetCommandBuffer();
243 243
244 // TODO: cmd_buff[2], aka archive handle lower word, isn't used according to 244 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
245 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
246 Handle archive_handle = static_cast<Handle>(cmd_buff[3]);
247 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]); 245 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
248 u32 dirname_size = cmd_buff[5]; 246 u32 dirname_size = cmd_buff[5];
249 u32 dirname_ptr = cmd_buff[8]; 247 u32 dirname_ptr = cmd_buff[8];
@@ -252,7 +250,7 @@ static void CreateDirectory(Service::Interface* self) {
252 250
253 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_path.DebugStr().c_str()); 251 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_path.DebugStr().c_str());
254 252
255 cmd_buff[1] = Kernel::CreateDirectoryFromArchive(archive_handle, dir_path).raw; 253 cmd_buff[1] = CreateDirectoryFromArchive(archive_handle, dir_path).raw;
256} 254}
257 255
258/* 256/*
@@ -271,15 +269,13 @@ static void CreateDirectory(Service::Interface* self) {
271 * Outputs: 269 * Outputs:
272 * 1 : Result of function, 0 on success, otherwise error code 270 * 1 : Result of function, 0 on success, otherwise error code
273 */ 271 */
274void RenameDirectory(Service::Interface* self) { 272static void RenameDirectory(Service::Interface* self) {
275 u32* cmd_buff = Kernel::GetCommandBuffer(); 273 u32* cmd_buff = Kernel::GetCommandBuffer();
276 274
277 // TODO(Link Mauve): cmd_buff[2] and cmd_buff[6], aka archive handle lower word, aren't used according to 275 ArchiveHandle src_archive_handle = MakeArchiveHandle(cmd_buff[2], cmd_buff[3]);
278 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
279 Handle src_archive_handle = static_cast<Handle>(cmd_buff[3]);
280 auto src_dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]); 276 auto src_dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[4]);
281 u32 src_dirname_size = cmd_buff[5]; 277 u32 src_dirname_size = cmd_buff[5];
282 Handle dest_archive_handle = static_cast<Handle>(cmd_buff[7]); 278 ArchiveHandle dest_archive_handle = MakeArchiveHandle(cmd_buff[6], cmd_buff[7]);
283 auto dest_dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[8]); 279 auto dest_dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[8]);
284 u32 dest_dirname_size = cmd_buff[9]; 280 u32 dest_dirname_size = cmd_buff[9];
285 u32 src_dirname_ptr = cmd_buff[11]; 281 u32 src_dirname_ptr = cmd_buff[11];
@@ -292,15 +288,26 @@ void RenameDirectory(Service::Interface* self) {
292 src_dirname_type, src_dirname_size, src_dir_path.DebugStr().c_str(), 288 src_dirname_type, src_dirname_size, src_dir_path.DebugStr().c_str(),
293 dest_dirname_type, dest_dirname_size, dest_dir_path.DebugStr().c_str()); 289 dest_dirname_type, dest_dirname_size, dest_dir_path.DebugStr().c_str());
294 290
295 cmd_buff[1] = Kernel::RenameDirectoryBetweenArchives(src_archive_handle, src_dir_path, dest_archive_handle, dest_dir_path).raw; 291 cmd_buff[1] = RenameDirectoryBetweenArchives(src_archive_handle, src_dir_path, dest_archive_handle, dest_dir_path).raw;
296} 292}
297 293
294/**
295 * FS_User::OpenDirectory service function
296 * Inputs:
297 * 1 : Archive handle low word
298 * 2 : Archive handle high word
299 * 3 : Low path type
300 * 4 : Low path size
301 * 7 : (LowPathSize << 14) | 2
302 * 8 : Low path data pointer
303 * Outputs:
304 * 1 : Result of function, 0 on success, otherwise error code
305 * 3 : Directory handle
306 */
298static void OpenDirectory(Service::Interface* self) { 307static void OpenDirectory(Service::Interface* self) {
299 u32* cmd_buff = Kernel::GetCommandBuffer(); 308 u32* cmd_buff = Kernel::GetCommandBuffer();
300 309
301 // TODO(Link Mauve): cmd_buff[2], aka archive handle lower word, isn't used according to 310 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[1], cmd_buff[2]);
302 // 3dmoo's or ctrulib's implementations. Triple check if it's really the case.
303 Handle archive_handle = static_cast<Handle>(cmd_buff[2]);
304 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[3]); 311 auto dirname_type = static_cast<FileSys::LowPathType>(cmd_buff[3]);
305 u32 dirname_size = cmd_buff[4]; 312 u32 dirname_size = cmd_buff[4];
306 u32 dirname_ptr = cmd_buff[6]; 313 u32 dirname_ptr = cmd_buff[6];
@@ -309,7 +316,7 @@ static void OpenDirectory(Service::Interface* self) {
309 316
310 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_path.DebugStr().c_str()); 317 LOG_DEBUG(Service_FS, "type=%d size=%d data=%s", dirname_type, dirname_size, dir_path.DebugStr().c_str());
311 318
312 ResultVal<Handle> handle = Kernel::OpenDirectoryFromArchive(archive_handle, dir_path); 319 ResultVal<Handle> handle = OpenDirectoryFromArchive(archive_handle, dir_path);
313 cmd_buff[1] = handle.Code().raw; 320 cmd_buff[1] = handle.Code().raw;
314 if (handle.Succeeded()) { 321 if (handle.Succeeded()) {
315 cmd_buff[3] = *handle; 322 cmd_buff[3] = *handle;
@@ -334,7 +341,7 @@ static void OpenDirectory(Service::Interface* self) {
334static void OpenArchive(Service::Interface* self) { 341static void OpenArchive(Service::Interface* self) {
335 u32* cmd_buff = Kernel::GetCommandBuffer(); 342 u32* cmd_buff = Kernel::GetCommandBuffer();
336 343
337 auto archive_id = static_cast<FileSys::Archive::IdCode>(cmd_buff[1]); 344 auto archive_id = static_cast<FS::ArchiveIdCode>(cmd_buff[1]);
338 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]); 345 auto archivename_type = static_cast<FileSys::LowPathType>(cmd_buff[2]);
339 u32 archivename_size = cmd_buff[3]; 346 u32 archivename_size = cmd_buff[3];
340 u32 archivename_ptr = cmd_buff[5]; 347 u32 archivename_ptr = cmd_buff[5];
@@ -348,16 +355,34 @@ static void OpenArchive(Service::Interface* self) {
348 return; 355 return;
349 } 356 }
350 357
351 ResultVal<Handle> handle = Kernel::OpenArchive(archive_id); 358 ResultVal<ArchiveHandle> handle = OpenArchive(archive_id);
352 cmd_buff[1] = handle.Code().raw; 359 cmd_buff[1] = handle.Code().raw;
353 if (handle.Succeeded()) { 360 if (handle.Succeeded()) {
354 // cmd_buff[2] isn't used according to 3dmoo's implementation. 361 cmd_buff[2] = *handle & 0xFFFFFFFF;
355 cmd_buff[3] = *handle; 362 cmd_buff[3] = (*handle >> 32) & 0xFFFFFFFF;
356 } else { 363 } else {
364 cmd_buff[2] = cmd_buff[3] = 0;
357 LOG_ERROR(Service_FS, "failed to get a handle for archive"); 365 LOG_ERROR(Service_FS, "failed to get a handle for archive");
358 } 366 }
359} 367}
360 368
369/**
370 * FS_User::CloseArchive service function
371 * Inputs:
372 * 0 : 0x080E0080
373 * 1 : Archive handle low word
374 * 2 : Archive handle high word
375 * Outputs:
376 * 0 : ??? TODO(yuriks): Verify return header
377 * 1 : Result of function, 0 on success, otherwise error code
378 */
379static void CloseArchive(Service::Interface* self) {
380 u32* cmd_buff = Kernel::GetCommandBuffer();
381
382 ArchiveHandle archive_handle = MakeArchiveHandle(cmd_buff[1], cmd_buff[2]);
383 cmd_buff[1] = CloseArchive(archive_handle).raw;
384}
385
361/* 386/*
362* FS_User::IsSdmcDetected service function 387* FS_User::IsSdmcDetected service function
363* Outputs: 388* Outputs:
@@ -373,7 +398,7 @@ static void IsSdmcDetected(Service::Interface* self) {
373 LOG_DEBUG(Service_FS, "called"); 398 LOG_DEBUG(Service_FS, "called");
374} 399}
375 400
376const Interface::FunctionInfo FunctionTable[] = { 401const FSUserInterface::FunctionInfo FunctionTable[] = {
377 {0x000100C6, nullptr, "Dummy1"}, 402 {0x000100C6, nullptr, "Dummy1"},
378 {0x040100C4, nullptr, "Control"}, 403 {0x040100C4, nullptr, "Control"},
379 {0x08010002, Initialize, "Initialize"}, 404 {0x08010002, Initialize, "Initialize"},
@@ -389,7 +414,7 @@ const Interface::FunctionInfo FunctionTable[] = {
389 {0x080B0102, OpenDirectory, "OpenDirectory"}, 414 {0x080B0102, OpenDirectory, "OpenDirectory"},
390 {0x080C00C2, OpenArchive, "OpenArchive"}, 415 {0x080C00C2, OpenArchive, "OpenArchive"},
391 {0x080D0144, nullptr, "ControlArchive"}, 416 {0x080D0144, nullptr, "ControlArchive"},
392 {0x080E0080, nullptr, "CloseArchive"}, 417 {0x080E0080, CloseArchive, "CloseArchive"},
393 {0x080F0180, nullptr, "FormatThisUserSaveData"}, 418 {0x080F0180, nullptr, "FormatThisUserSaveData"},
394 {0x08100200, nullptr, "CreateSystemSaveData"}, 419 {0x08100200, nullptr, "CreateSystemSaveData"},
395 {0x08110040, nullptr, "DeleteSystemSaveData"}, 420 {0x08110040, nullptr, "DeleteSystemSaveData"},
@@ -465,11 +490,12 @@ const Interface::FunctionInfo FunctionTable[] = {
465//////////////////////////////////////////////////////////////////////////////////////////////////// 490////////////////////////////////////////////////////////////////////////////////////////////////////
466// Interface class 491// Interface class
467 492
468Interface::Interface() { 493FSUserInterface::FSUserInterface() {
469 Register(FunctionTable, ARRAY_SIZE(FunctionTable)); 494 Register(FunctionTable, ARRAY_SIZE(FunctionTable));
470} 495}
471 496
472Interface::~Interface() { 497FSUserInterface::~FSUserInterface() {
473} 498}
474 499
475} // namespace 500} // namespace FS
501} // namespace Service
diff --git a/src/core/hle/service/fs_user.h b/src/core/hle/service/fs/fs_user.h
index 005382540..80e3804e0 100644
--- a/src/core/hle/service/fs_user.h
+++ b/src/core/hle/service/fs/fs_user.h
@@ -9,15 +9,16 @@
9//////////////////////////////////////////////////////////////////////////////////////////////////// 9////////////////////////////////////////////////////////////////////////////////////////////////////
10// Namespace FS_User 10// Namespace FS_User
11 11
12namespace FS_User { 12namespace Service {
13namespace FS {
13 14
14/// Interface to "fs:USER" service 15/// Interface to "fs:USER" service
15class Interface : public Service::Interface { 16class FSUserInterface : public Service::Interface {
16public: 17public:
17 18
18 Interface(); 19 FSUserInterface();
19 20
20 ~Interface(); 21 ~FSUserInterface();
21 22
22 /** 23 /**
23 * Gets the string port name used by CTROS for the service 24 * Gets the string port name used by CTROS for the service
@@ -28,4 +29,5 @@ public:
28 } 29 }
29}; 30};
30 31
31} // namespace 32} // namespace FS
33} // namespace Service
diff --git a/src/core/hle/service/service.cpp b/src/core/hle/service/service.cpp
index b33172932..2230045e3 100644
--- a/src/core/hle/service/service.cpp
+++ b/src/core/hle/service/service.cpp
@@ -17,7 +17,7 @@
17#include "core/hle/service/csnd_snd.h" 17#include "core/hle/service/csnd_snd.h"
18#include "core/hle/service/dsp_dsp.h" 18#include "core/hle/service/dsp_dsp.h"
19#include "core/hle/service/err_f.h" 19#include "core/hle/service/err_f.h"
20#include "core/hle/service/fs_user.h" 20#include "core/hle/service/fs/fs_user.h"
21#include "core/hle/service/frd_u.h" 21#include "core/hle/service/frd_u.h"
22#include "core/hle/service/gsp_gpu.h" 22#include "core/hle/service/gsp_gpu.h"
23#include "core/hle/service/hid_user.h" 23#include "core/hle/service/hid_user.h"
@@ -99,7 +99,7 @@ void Init() {
99 g_manager->AddService(new DSP_DSP::Interface); 99 g_manager->AddService(new DSP_DSP::Interface);
100 g_manager->AddService(new ERR_F::Interface); 100 g_manager->AddService(new ERR_F::Interface);
101 g_manager->AddService(new FRD_U::Interface); 101 g_manager->AddService(new FRD_U::Interface);
102 g_manager->AddService(new FS_User::Interface); 102 g_manager->AddService(new FS::FSUserInterface);
103 g_manager->AddService(new GSP_GPU::Interface); 103 g_manager->AddService(new GSP_GPU::Interface);
104 g_manager->AddService(new HID_User::Interface); 104 g_manager->AddService(new HID_User::Interface);
105 g_manager->AddService(new IR_RST::Interface); 105 g_manager->AddService(new IR_RST::Interface);