-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
FileSystemProxy.cpp
785 lines (653 loc) · 25.4 KB
/
FileSystemProxy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "Core/IOS/FS/FileSystemProxy.h"
#include <algorithm>
#include <cstring>
#include <string_view>
#include <fmt/format.h>
#include "Common/ChunkFile.h"
#include "Common/EnumUtils.h"
#include "Common/StringUtil.h"
#include "Common/Swap.h"
#include "Core/HW/Memmap.h"
#include "Core/HW/SystemTimers.h"
#include "Core/IOS/FS/FileSystem.h"
#include "Core/IOS/Uids.h"
#include "Core/System.h"
namespace IOS::HLE
{
using namespace IOS::HLE::FS;
static IPCReply GetFSReply(s32 return_value, SystemTimers::TimeBaseTick extra_tb_ticks = {})
{
return IPCReply{return_value, IPC_OVERHEAD_TICKS + extra_tb_ticks};
}
/// Duration of a superblock write (in timebase ticks).
constexpr SystemTimers::TimeBaseTick GetSuperblockWriteTbTicks(int ios_version)
{
if (ios_version == 28 || ios_version == 80)
return 3350000_tbticks;
if (ios_version < 28)
return 4100000_tbticks;
return 3170000_tbticks;
}
/// Duration of a file cluster write (in timebase ticks).
constexpr SystemTimers::TimeBaseTick GetClusterWriteTbTicks(int ios_version)
{
// Based on the time it takes to write two clusters (0x8000 bytes), divided by two.
if (ios_version < 28)
return 370000_tbticks;
return 300000_tbticks;
}
/// Duration of a file cluster read (in timebase ticks).
constexpr SystemTimers::TimeBaseTick GetClusterReadTbTicks(int ios_version)
{
if (ios_version == 28 || ios_version == 80)
return 125000_tbticks;
if (ios_version < 28)
return 165000_tbticks;
return 115000_tbticks;
}
constexpr SystemTimers::TimeBaseTick GetFSModuleMemcpyTbTicks(int ios_version, u64 copy_size)
{
// FS handles cached read/writes by doing a simple memcpy on an internal buffer.
// The following equations were obtained by doing cached reads with various read sizes
// on real hardware and performing a linear regression. R^2 was close to 1 (0.9999),
// which is not entirely surprising.
// Older versions of IOS have a simplistic implementation of memcpy that does not optimize
// large copies by copying 16 bytes or 32 bytes per chunk.
if (ios_version < 28)
return SystemTimers::TimeBaseTick(1.0 * copy_size + 3.0);
return SystemTimers::TimeBaseTick(0.636 * copy_size + 150.0);
}
constexpr SystemTimers::TimeBaseTick GetFreeClusterCheckTbTicks()
{
return 1000_tbticks;
}
constexpr size_t CLUSTER_DATA_SIZE = 0x4000;
FSCore::FSCore(Kernel& ios) : m_ios(ios)
{
if (ios.GetFS()->Delete(PID_KERNEL, PID_KERNEL, "/tmp") == ResultCode::Success)
{
ios.GetFS()->CreateDirectory(PID_KERNEL, PID_KERNEL, "/tmp", 0,
{Mode::ReadWrite, Mode::ReadWrite, Mode::ReadWrite});
}
}
FSCore::~FSCore() = default;
FSDevice::FSDevice(EmulationKernel& ios, FSCore& core, const std::string& device_name)
: EmulationDevice(ios, device_name), m_core(core)
{
}
FSDevice::~FSDevice() = default;
void FSDevice::DoState(PointerWrap& p)
{
Device::DoState(p);
p.Do(m_core.m_dirty_cache);
p.Do(m_core.m_cache_chain_index);
p.Do(m_core.m_cache_fd);
p.Do(m_core.m_next_fd);
p.Do(m_core.m_fd_map);
}
template <typename... Args>
static void LogResult(ResultCode code, fmt::format_string<Args...> format, Args&&... args)
{
const std::string command = fmt::format(format, std::forward<Args>(args)...);
const auto type =
code == ResultCode::Success ? Common::Log::LogLevel::LINFO : Common::Log::LogLevel::LERROR;
GENERIC_LOG_FMT(Common::Log::LogType::IOS_FS, type, "Command: {}: Result {}", command,
Common::ToUnderlying(ConvertResult(code)));
}
template <typename T, typename... Args>
static void LogResult(const Result<T>& result, fmt::format_string<Args...> format, Args&&... args)
{
const auto result_code = result.Succeeded() ? ResultCode::Success : result.Error();
LogResult(result_code, format, std::forward<Args>(args)...);
}
enum class FileLookupMode
{
Normal,
/// Timing model to use when FS splits the path into a parent and a file name
/// and looks up each of them individually.
Split,
};
// Note: lookups normally stop at the first non existing path (as the FST cannot be traversed
// further when a directory doesn't exist). However for the sake of simplicity we assume that the
// entire lookup succeeds because otherwise we'd need to check whether every path component exists.
static SystemTimers::TimeBaseTick EstimateFileLookupTicks(const std::string& path,
FileLookupMode mode)
{
const size_t number_of_path_components = std::count(path.cbegin(), path.cend(), '/');
if (number_of_path_components == 0)
return 0_tbticks;
// Paths that end with a slash are invalid and rejected early in FS.
if (!path.empty() && *path.rbegin() == '/')
return 300_tbticks;
if (mode == FileLookupMode::Normal)
return SystemTimers::TimeBaseTick(680 * number_of_path_components);
return SystemTimers::TimeBaseTick(1000 + 340 * number_of_path_components);
}
/// Get a reply with the correct timing for operations that modify the superblock.
///
/// A superblock flush takes a very large amount of time, so other delays are ignored
/// to simplify the implementation as they are insignificant.
static IPCReply GetReplyForSuperblockOperation(int ios_version, ResultCode result)
{
const auto ticks =
result == ResultCode::Success ? GetSuperblockWriteTbTicks(ios_version) : 0_tbticks;
return GetFSReply(ConvertResult(result), ticks);
}
std::optional<IPCReply> FSDevice::Open(const OpenRequest& request)
{
return MakeIPCReply([&](Ticks t) {
return m_core
.Open(request.uid, request.gid, request.path, static_cast<Mode>(request.flags & 3),
request.fd, t)
.Release();
});
}
FSCore::ScopedFd FSCore::Open(FS::Uid uid, FS::Gid gid, const std::string& path, FS::Mode mode,
std::optional<u32> ipc_fd, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
if (m_fd_map.size() >= 16)
return {this, ConvertResult(ResultCode::NoFreeHandle), ticks};
if (path.size() >= 64)
return {this, ConvertResult(ResultCode::Invalid), ticks};
const u64 fd = ipc_fd.has_value() ? u64(*ipc_fd) : m_next_fd++;
if (path == "/dev/fs")
{
m_fd_map[fd] = {gid, uid, INVALID_FD};
return {this, static_cast<s64>(fd), ticks};
}
ticks.Add(EstimateFileLookupTicks(path, FileLookupMode::Normal));
auto backend_fd = m_ios.GetFS()->OpenFile(uid, gid, path, mode);
LogResult(backend_fd, "OpenFile({})", path);
if (!backend_fd)
return {this, ConvertResult(backend_fd.Error()), ticks};
auto& handle = m_fd_map[fd] = {gid, uid, backend_fd->Release()};
std::strncpy(handle.name.data(), path.c_str(), handle.name.size());
return {this, static_cast<s64>(fd), ticks};
}
std::optional<IPCReply> FSDevice::Close(u32 fd)
{
return MakeIPCReply([&](Ticks t) { return m_core.Close(static_cast<u64>(fd), t); });
}
s32 FSCore::Close(u64 fd, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const auto& handle = m_fd_map[fd];
if (handle.fs_fd != INVALID_FD)
{
if (fd == m_cache_fd)
{
ticks.Add(SimulateFlushFileCache());
m_cache_fd.reset();
}
if (handle.superblock_flush_needed)
ticks.Add(GetSuperblockWriteTbTicks(m_ios.GetVersion()));
const ResultCode result = m_ios.GetFS()->Close(handle.fs_fd);
LogResult(result, "Close({})", handle.name.data());
m_fd_map.erase(fd);
if (result != ResultCode::Success)
return ConvertResult(result);
}
else
{
m_fd_map.erase(fd);
}
return IPC_SUCCESS;
}
u64 FSCore::SimulatePopulateFileCache(u64 fd, u32 offset, u32 file_size)
{
if (HasCacheForFile(fd, offset))
return 0;
u64 ticks = SimulateFlushFileCache();
if ((offset % CLUSTER_DATA_SIZE != 0 || offset != file_size) && offset < file_size)
ticks += GetClusterReadTbTicks(m_ios.GetVersion());
m_cache_fd = fd;
m_cache_chain_index = static_cast<u16>(offset / CLUSTER_DATA_SIZE);
return ticks;
}
u64 FSCore::SimulateFlushFileCache()
{
if (!m_cache_fd.has_value() || !m_dirty_cache)
return 0;
m_dirty_cache = false;
m_fd_map[*m_cache_fd].superblock_flush_needed = true;
return GetClusterWriteTbTicks(m_ios.GetVersion());
}
// Simulate parts of the FS read/write logic to estimate ticks for file operations correctly.
u64 FSCore::EstimateTicksForReadWrite(const Handle& handle, u64 fd, IPCCommandType command,
u32 size)
{
u64 ticks = 0;
const bool is_write = command == IPC_CMD_WRITE;
const Result<FileStatus> status = m_ios.GetFS()->GetFileStatus(handle.fs_fd);
u32 offset = status->offset;
u32 count = size;
if (!is_write && count + offset > status->size)
count = status->size - offset;
while (count != 0)
{
u32 copy_length;
// Fast path (if not cached): FS copies an entire cluster directly from/to the request.
if (!HasCacheForFile(fd, offset) && count >= CLUSTER_DATA_SIZE &&
offset % CLUSTER_DATA_SIZE == 0)
{
ticks += (is_write ? GetClusterWriteTbTicks : GetClusterReadTbTicks)(m_ios.GetVersion());
copy_length = CLUSTER_DATA_SIZE;
if (is_write)
m_fd_map[fd].superblock_flush_needed = true;
}
else
{
ticks += SimulatePopulateFileCache(fd, offset, status->size);
const u32 start = offset - m_cache_chain_index * CLUSTER_DATA_SIZE;
copy_length = std::min<u32>(CLUSTER_DATA_SIZE - start, count);
// FS essentially just does a memcpy from/to an internal buffer.
ticks += GetFSModuleMemcpyTbTicks(m_ios.GetVersion(), copy_length);
if (is_write)
ticks += GetFreeClusterCheckTbTicks();
m_dirty_cache = is_write;
if (is_write && (offset + copy_length) % CLUSTER_DATA_SIZE == 0)
ticks += SimulateFlushFileCache();
}
offset += copy_length;
count -= copy_length;
}
return ticks;
}
bool FSCore::HasCacheForFile(u64 fd, u32 offset) const
{
const u16 chain_index = static_cast<u16>(offset / CLUSTER_DATA_SIZE);
return m_cache_fd == fd && m_cache_chain_index == chain_index;
}
std::optional<IPCReply> FSDevice::Read(const ReadWriteRequest& request)
{
return MakeIPCReply([&](Ticks t) {
auto& system = GetSystem();
auto& memory = system.GetMemory();
return m_core.Read(request.fd, memory.GetPointerForRange(request.buffer, request.size),
request.size, request.buffer, t);
});
}
s32 FSCore::Read(u64 fd, u8* data, u32 size, std::optional<u32> ipc_buffer_addr, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const Handle& handle = m_fd_map[fd];
if (handle.fs_fd == INVALID_FD)
return ConvertResult(ResultCode::Invalid);
// Simulate the FS read logic to estimate ticks. Note: this must be done before reading.
ticks.Add(EstimateTicksForReadWrite(handle, fd, IPC_CMD_READ, size));
const Result<u32> result = m_ios.GetFS()->ReadBytesFromFile(handle.fs_fd, data, size);
if (ipc_buffer_addr)
LogResult(result, "Read({}, 0x{:08x}, {})", handle.name.data(), *ipc_buffer_addr, size);
if (!result)
return ConvertResult(result.Error());
return *result;
}
std::optional<IPCReply> FSDevice::Write(const ReadWriteRequest& request)
{
return MakeIPCReply([&](Ticks t) {
auto& system = GetSystem();
auto& memory = system.GetMemory();
return m_core.Write(request.fd, memory.GetPointerForRange(request.buffer, request.size),
request.size, request.buffer, t);
});
}
s32 FSCore::Write(u64 fd, const u8* data, u32 size, std::optional<u32> ipc_buffer_addr, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const Handle& handle = m_fd_map[fd];
if (handle.fs_fd == INVALID_FD)
return ConvertResult(ResultCode::Invalid);
// Simulate the FS write logic to estimate ticks. Must be done before writing.
ticks.Add(EstimateTicksForReadWrite(handle, fd, IPC_CMD_WRITE, size));
const Result<u32> result = m_ios.GetFS()->WriteBytesToFile(handle.fs_fd, data, size);
if (ipc_buffer_addr)
LogResult(result, "Write({}, 0x{:08x}, {})", handle.name.data(), *ipc_buffer_addr, size);
if (!result)
return ConvertResult(result.Error());
return *result;
}
std::optional<IPCReply> FSDevice::Seek(const SeekRequest& request)
{
return MakeIPCReply([&](Ticks t) {
return m_core.Seek(request.fd, request.offset, HLE::FS::SeekMode(request.mode), t);
});
}
s32 FSCore::Seek(u64 fd, u32 offset, FS::SeekMode mode, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const Handle& handle = m_fd_map[fd];
if (handle.fs_fd == INVALID_FD)
return ConvertResult(ResultCode::Invalid);
const Result<u32> result = m_ios.GetFS()->SeekFile(handle.fs_fd, offset, mode);
LogResult(result, "Seek({}, 0x{:08x}, {})", handle.name.data(), offset, static_cast<int>(mode));
if (!result)
return ConvertResult(result.Error());
return *result;
}
#pragma pack(push, 1)
struct ISFSParams
{
Common::BigEndianValue<Uid> uid;
Common::BigEndianValue<Gid> gid;
char path[64];
Modes modes;
FileAttribute attribute;
};
struct ISFSNandStats
{
Common::BigEndianValue<u32> cluster_size;
Common::BigEndianValue<u32> free_clusters;
Common::BigEndianValue<u32> used_clusters;
Common::BigEndianValue<u32> bad_clusters;
Common::BigEndianValue<u32> reserved_clusters;
Common::BigEndianValue<u32> free_inodes;
Common::BigEndianValue<u32> used_inodes;
};
struct ISFSFileStats
{
Common::BigEndianValue<u32> size;
Common::BigEndianValue<u32> seek_position;
};
#pragma pack(pop)
template <typename T>
static Result<T> GetParams(Memory::MemoryManager& memory, const IOCtlRequest& request)
{
if (request.buffer_in_size < sizeof(T))
return ResultCode::Invalid;
T params;
memory.CopyFromEmu(¶ms, request.buffer_in, sizeof(params));
return params;
}
std::optional<IPCReply> FSDevice::IOCtl(const IOCtlRequest& request)
{
const auto it = m_core.m_fd_map.find(request.fd);
if (it == m_core.m_fd_map.end())
return IPCReply(ConvertResult(ResultCode::Invalid));
switch (request.request)
{
case ISFS_IOCTL_FORMAT:
return Format(it->second, request);
case ISFS_IOCTL_GETSTATS:
return GetStats(it->second, request);
case ISFS_IOCTL_CREATEDIR:
return CreateDirectory(it->second, request);
case ISFS_IOCTL_SETATTR:
return SetAttribute(it->second, request);
case ISFS_IOCTL_GETATTR:
return GetAttribute(it->second, request);
case ISFS_IOCTL_DELETE:
return DeleteFile(it->second, request);
case ISFS_IOCTL_RENAME:
return RenameFile(it->second, request);
case ISFS_IOCTL_CREATEFILE:
return CreateFile(it->second, request);
case ISFS_IOCTL_SETFILEVERCTRL:
return SetFileVersionControl(it->second, request);
case ISFS_IOCTL_GETFILESTATS:
return GetFileStats(it->second, request);
case ISFS_IOCTL_SHUTDOWN:
return Shutdown(it->second, request);
default:
return GetFSReply(ConvertResult(ResultCode::Invalid));
}
}
std::optional<IPCReply> FSDevice::IOCtlV(const IOCtlVRequest& request)
{
const auto it = m_core.m_fd_map.find(request.fd);
if (it == m_core.m_fd_map.end())
return IPCReply(ConvertResult(ResultCode::Invalid));
switch (request.request)
{
case ISFS_IOCTLV_READDIR:
return ReadDirectory(it->second, request);
case ISFS_IOCTLV_GETUSAGE:
return GetUsage(it->second, request);
default:
return GetFSReply(ConvertResult(ResultCode::Invalid));
}
}
IPCReply FSDevice::Format(const Handle& handle, const IOCtlRequest& request)
{
if (handle.uid != 0)
return GetFSReply(ConvertResult(ResultCode::AccessDenied));
const ResultCode result = m_ios.GetFS()->Format(handle.uid);
return GetReplyForSuperblockOperation(m_ios.GetVersion(), result);
}
IPCReply FSDevice::GetStats(const Handle& handle, const IOCtlRequest& request)
{
if (request.buffer_out_size < sizeof(ISFSNandStats))
return GetFSReply(ConvertResult(ResultCode::Invalid));
const Result<NandStats> stats = m_ios.GetFS()->GetNandStats();
LogResult(stats, "GetNandStats");
if (!stats)
return IPCReply(ConvertResult(stats.Error()));
auto& system = GetSystem();
auto& memory = system.GetMemory();
ISFSNandStats out;
out.cluster_size = stats->cluster_size;
out.free_clusters = stats->free_clusters;
out.used_clusters = stats->used_clusters;
out.bad_clusters = stats->bad_clusters;
out.reserved_clusters = stats->reserved_clusters;
out.free_inodes = stats->free_inodes;
out.used_inodes = stats->used_inodes;
memory.CopyToEmu(request.buffer_out, &out, sizeof(out));
return IPCReply(IPC_SUCCESS);
}
IPCReply FSDevice::CreateDirectory(const Handle& handle, const IOCtlRequest& request)
{
const auto params = GetParams<ISFSParams>(GetSystem().GetMemory(), request);
if (!params)
return GetFSReply(ConvertResult(params.Error()));
const ResultCode result = m_ios.GetFS()->CreateDirectory(handle.uid, handle.gid, params->path,
params->attribute, params->modes);
LogResult(result, "CreateDirectory({})", params->path);
return GetReplyForSuperblockOperation(m_ios.GetVersion(), result);
}
IPCReply FSDevice::ReadDirectory(const Handle& handle, const IOCtlVRequest& request)
{
if (request.in_vectors.empty() || request.in_vectors.size() != request.io_vectors.size() ||
request.in_vectors.size() > 2 || request.in_vectors[0].size != 64)
{
return GetFSReply(ConvertResult(ResultCode::Invalid));
}
auto& system = GetSystem();
auto& memory = system.GetMemory();
u32 file_list_address, file_count_address, max_count;
if (request.in_vectors.size() == 2)
{
if (request.in_vectors[1].size != 4 || request.io_vectors[1].size != 4)
return GetFSReply(ConvertResult(ResultCode::Invalid));
max_count = memory.Read_U32(request.in_vectors[1].address);
file_count_address = request.io_vectors[1].address;
file_list_address = request.io_vectors[0].address;
if (request.io_vectors[0].size != 13 * max_count)
return GetFSReply(ConvertResult(ResultCode::Invalid));
memory.Write_U32(max_count, file_count_address);
}
else
{
if (request.io_vectors[0].size != 4)
return GetFSReply(ConvertResult(ResultCode::Invalid));
max_count = memory.Read_U32(request.io_vectors[0].address);
file_count_address = request.io_vectors[0].address;
file_list_address = 0;
}
const std::string directory = memory.GetString(request.in_vectors[0].address, 64);
const Result<std::vector<std::string>> list =
m_ios.GetFS()->ReadDirectory(handle.uid, handle.gid, directory);
LogResult(list, "ReadDirectory({})", directory);
if (!list)
return GetFSReply(ConvertResult(list.Error()));
if (!file_list_address)
{
memory.Write_U32(static_cast<u32>(list->size()), file_count_address);
return GetFSReply(IPC_SUCCESS);
}
for (size_t i = 0; i < list->size() && i < max_count; ++i)
{
memory.Memset(file_list_address, 0, 13);
memory.CopyToEmu(file_list_address, (*list)[i].data(), (*list)[i].size());
memory.Write_U8(0, file_list_address + 12);
file_list_address += static_cast<u32>((*list)[i].size()) + 1;
}
// Write the actual number of entries in the buffer.
memory.Write_U32(std::min(max_count, static_cast<u32>(list->size())), file_count_address);
return GetFSReply(IPC_SUCCESS);
}
IPCReply FSDevice::SetAttribute(const Handle& handle, const IOCtlRequest& request)
{
const auto params = GetParams<ISFSParams>(GetSystem().GetMemory(), request);
if (!params)
return GetFSReply(ConvertResult(params.Error()));
const ResultCode result = m_ios.GetFS()->SetMetadata(
handle.uid, params->path, params->uid, params->gid, params->attribute, params->modes);
LogResult(result, "SetMetadata({})", params->path);
return GetReplyForSuperblockOperation(m_ios.GetVersion(), result);
}
IPCReply FSDevice::GetAttribute(const Handle& handle, const IOCtlRequest& request)
{
if (request.buffer_in_size < 64 || request.buffer_out_size < sizeof(ISFSParams))
return GetFSReply(ConvertResult(ResultCode::Invalid));
auto& system = GetSystem();
auto& memory = system.GetMemory();
const std::string path = memory.GetString(request.buffer_in, 64);
const auto ticks = EstimateFileLookupTicks(path, FileLookupMode::Split);
const Result<Metadata> metadata = m_ios.GetFS()->GetMetadata(handle.uid, handle.gid, path);
LogResult(metadata, "GetMetadata({})", path);
if (!metadata)
return GetFSReply(ConvertResult(metadata.Error()), ticks);
// Yes, the other members aren't copied at all. Actually, IOS does not even memset
// the struct at all, which means uninitialised bytes from the stack are returned.
// For the sake of determinism, let's just zero initialise the struct.
ISFSParams out{};
out.uid = metadata->uid;
out.gid = metadata->gid;
out.attribute = metadata->attribute;
out.modes = metadata->modes;
memory.CopyToEmu(request.buffer_out, &out, sizeof(out));
return GetFSReply(IPC_SUCCESS, ticks);
}
FS::ResultCode FSCore::DeleteFile(FS::Uid uid, FS::Gid gid, const std::string& path, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const ResultCode result = m_ios.GetFS()->Delete(uid, gid, path);
ticks.Add(GetSuperblockWriteTbTicks(m_ios.GetVersion()));
LogResult(result, "Delete({})", path);
return result;
}
IPCReply FSDevice::DeleteFile(const Handle& handle, const IOCtlRequest& request)
{
if (request.buffer_in_size < 64)
return GetFSReply(ConvertResult(ResultCode::Invalid));
auto& system = GetSystem();
auto& memory = system.GetMemory();
const std::string path = memory.GetString(request.buffer_in, 64);
return MakeIPCReply([&](Ticks ticks) {
return ConvertResult(m_core.DeleteFile(handle.uid, handle.gid, path, ticks));
});
}
FS::ResultCode FSCore::RenameFile(FS::Uid uid, FS::Gid gid, const std::string& old_path,
const std::string& new_path, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const ResultCode result = m_ios.GetFS()->Rename(uid, gid, old_path, new_path);
ticks.Add(GetSuperblockWriteTbTicks(m_ios.GetVersion()));
LogResult(result, "Rename({}, {})", old_path, new_path);
return result;
}
IPCReply FSDevice::RenameFile(const Handle& handle, const IOCtlRequest& request)
{
if (request.buffer_in_size < 64 * 2)
return GetFSReply(ConvertResult(ResultCode::Invalid));
auto& system = GetSystem();
auto& memory = system.GetMemory();
const std::string old_path = memory.GetString(request.buffer_in, 64);
const std::string new_path = memory.GetString(request.buffer_in + 64, 64);
return MakeIPCReply([&](Ticks ticks) {
return ConvertResult(m_core.RenameFile(handle.uid, handle.gid, old_path, new_path, ticks));
});
}
FS::ResultCode FSCore::CreateFile(FS::Uid uid, FS::Gid gid, const std::string& path,
FS::FileAttribute attribute, FS::Modes modes, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const ResultCode result = m_ios.GetFS()->CreateFile(uid, gid, path, attribute, modes);
ticks.Add(GetSuperblockWriteTbTicks(m_ios.GetVersion()));
LogResult(result, "CreateFile({})", path);
return result;
}
IPCReply FSDevice::CreateFile(const Handle& handle, const IOCtlRequest& request)
{
const auto params = GetParams<ISFSParams>(GetSystem().GetMemory(), request);
if (!params)
return GetFSReply(ConvertResult(params.Error()));
return MakeIPCReply([&](Ticks ticks) {
return ConvertResult(
m_core.CreateFile(handle.uid, handle.gid, params->path, params->attribute, params->modes));
});
}
IPCReply FSDevice::SetFileVersionControl(const Handle& handle, const IOCtlRequest& request)
{
const auto params = GetParams<ISFSParams>(GetSystem().GetMemory(), request);
if (!params)
return GetFSReply(ConvertResult(params.Error()));
// FS_SetFileVersionControl(ctx->uid, params->path, params->attribute)
ERROR_LOG_FMT(IOS_FS, "SetFileVersionControl({}, {:#x}): Stubbed", params->path,
params->attribute);
return GetFSReply(IPC_SUCCESS);
}
IPCReply FSDevice::GetFileStats(const Handle& handle, const IOCtlRequest& request)
{
if (request.buffer_out_size < 8)
return GetFSReply(ConvertResult(ResultCode::Invalid));
return MakeIPCReply([&](Ticks ticks) {
const Result<FileStatus> status = m_core.GetFileStatus(request.fd, ticks);
if (!status)
return ConvertResult(status.Error());
auto& system = GetSystem();
auto& memory = system.GetMemory();
ISFSFileStats out;
out.size = status->size;
out.seek_position = status->offset;
memory.CopyToEmu(request.buffer_out, &out, sizeof(out));
return IPC_SUCCESS;
});
}
FS::Result<FS::FileStatus> FSCore::GetFileStatus(u64 fd, Ticks ticks)
{
ticks.Add(IPC_OVERHEAD_TICKS);
const auto& handle = m_fd_map[fd];
if (handle.fs_fd == INVALID_FD)
return ResultCode::Invalid;
auto status = m_ios.GetFS()->GetFileStatus(handle.fs_fd);
LogResult(status, "GetFileStatus({})", handle.name.data());
return status;
}
IPCReply FSDevice::GetUsage(const Handle& handle, const IOCtlVRequest& request)
{
if (!request.HasNumberOfValidVectors(1, 2) || request.in_vectors[0].size != 64 ||
request.io_vectors[0].size != 4 || request.io_vectors[1].size != 4)
{
return GetFSReply(ConvertResult(ResultCode::Invalid));
}
auto& system = GetSystem();
auto& memory = system.GetMemory();
const std::string directory = memory.GetString(request.in_vectors[0].address, 64);
const Result<DirectoryStats> stats = m_ios.GetFS()->GetDirectoryStats(directory);
LogResult(stats, "GetDirectoryStats({})", directory);
if (!stats)
return GetFSReply(ConvertResult(stats.Error()));
memory.Write_U32(stats->used_clusters, request.io_vectors[0].address);
memory.Write_U32(stats->used_inodes, request.io_vectors[1].address);
return GetFSReply(IPC_SUCCESS);
}
IPCReply FSDevice::Shutdown(const Handle& handle, const IOCtlRequest& request)
{
INFO_LOG_FMT(IOS_FS, "Shutdown");
return GetFSReply(IPC_SUCCESS);
}
} // namespace IOS::HLE