summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/filesystem/fsp/fs_i_filesystem.cpp
blob: 1e69d22b8c0006a7eecbf1a45ddb2241ee8e15df (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/string_util.h"
#include "core/hle/service/filesystem/fsp/fs_i_directory.h"
#include "core/hle/service/filesystem/fsp/fs_i_file.h"
#include "core/hle/service/filesystem/fsp/fs_i_filesystem.h"
#include "core/hle/service/ipc_helpers.h"

namespace Service::FileSystem {

IFileSystem::IFileSystem(Core::System& system_, FileSys::VirtualDir dir_, SizeGetter size_)
    : ServiceFramework{system_, "IFileSystem"},
      backend{std::make_unique<FileSys::Fsa::IFileSystem>(dir_)}, size{std::move(size_)} {
    static const FunctionInfo functions[] = {
        {0, &IFileSystem::CreateFile, "CreateFile"},
        {1, &IFileSystem::DeleteFile, "DeleteFile"},
        {2, &IFileSystem::CreateDirectory, "CreateDirectory"},
        {3, &IFileSystem::DeleteDirectory, "DeleteDirectory"},
        {4, &IFileSystem::DeleteDirectoryRecursively, "DeleteDirectoryRecursively"},
        {5, &IFileSystem::RenameFile, "RenameFile"},
        {6, nullptr, "RenameDirectory"},
        {7, &IFileSystem::GetEntryType, "GetEntryType"},
        {8, &IFileSystem::OpenFile, "OpenFile"},
        {9, &IFileSystem::OpenDirectory, "OpenDirectory"},
        {10, &IFileSystem::Commit, "Commit"},
        {11, &IFileSystem::GetFreeSpaceSize, "GetFreeSpaceSize"},
        {12, &IFileSystem::GetTotalSpaceSize, "GetTotalSpaceSize"},
        {13, &IFileSystem::CleanDirectoryRecursively, "CleanDirectoryRecursively"},
        {14, &IFileSystem::GetFileTimeStampRaw, "GetFileTimeStampRaw"},
        {15, nullptr, "QueryEntry"},
        {16, &IFileSystem::GetFileSystemAttribute, "GetFileSystemAttribute"},
    };
    RegisterHandlers(functions);
}

void IFileSystem::CreateFile(HLERequestContext& ctx) {
    IPC::RequestParser rp{ctx};

    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    const u64 file_mode = rp.Pop<u64>();
    const u32 file_size = rp.Pop<u32>();

    LOG_DEBUG(Service_FS, "called. file={}, mode=0x{:X}, size=0x{:08X}", name, file_mode,
              file_size);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(backend->CreateFile(path, file_size));
}

void IFileSystem::DeleteFile(HLERequestContext& ctx) {
    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    LOG_DEBUG(Service_FS, "called. file={}", name);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(backend->DeleteFile(path));
}

void IFileSystem::CreateDirectory(HLERequestContext& ctx) {
    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    LOG_DEBUG(Service_FS, "called. directory={}", name);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(backend->CreateDirectory(path));
}

void IFileSystem::DeleteDirectory(HLERequestContext& ctx) {
    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    LOG_DEBUG(Service_FS, "called. directory={}", name);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(backend->DeleteDirectory(path));
}

void IFileSystem::DeleteDirectoryRecursively(HLERequestContext& ctx) {
    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    LOG_DEBUG(Service_FS, "called. directory={}", name);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(backend->DeleteDirectoryRecursively(path));
}

void IFileSystem::CleanDirectoryRecursively(HLERequestContext& ctx) {
    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    LOG_DEBUG(Service_FS, "called. Directory: {}", name);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(backend->CleanDirectoryRecursively(path));
}

void IFileSystem::RenameFile(HLERequestContext& ctx) {
    const std::string src_name = Common::StringFromBuffer(ctx.ReadBuffer(0));
    const std::string dst_name = Common::StringFromBuffer(ctx.ReadBuffer(1));

    const auto src_path = FileSys::Path(src_name.c_str());
    const auto dst_path = FileSys::Path(dst_name.c_str());

    LOG_DEBUG(Service_FS, "called. file '{}' to file '{}'", src_name, dst_name);

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(backend->RenameFile(src_path, dst_path));
}

void IFileSystem::OpenFile(HLERequestContext& ctx) {
    IPC::RequestParser rp{ctx};

    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    const auto mode = static_cast<FileSys::OpenMode>(rp.Pop<u32>());

    LOG_DEBUG(Service_FS, "called. file={}, mode={}", name, mode);

    FileSys::VirtualFile vfs_file{};
    auto result = backend->OpenFile(&vfs_file, path, mode);
    if (result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    auto file = std::make_shared<IFile>(system, vfs_file);

    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
    rb.Push(ResultSuccess);
    rb.PushIpcInterface<IFile>(std::move(file));
}

void IFileSystem::OpenDirectory(HLERequestContext& ctx) {
    IPC::RequestParser rp{ctx};

    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());
    const auto mode = rp.PopRaw<FileSys::OpenDirectoryMode>();

    LOG_DEBUG(Service_FS, "called. directory={}, mode={}", name, mode);

    FileSys::VirtualDir vfs_dir{};
    auto result = backend->OpenDirectory(&vfs_dir, path, mode);
    if (result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    auto directory = std::make_shared<IDirectory>(system, vfs_dir, mode);

    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
    rb.Push(ResultSuccess);
    rb.PushIpcInterface<IDirectory>(std::move(directory));
}

void IFileSystem::GetEntryType(HLERequestContext& ctx) {
    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    LOG_DEBUG(Service_FS, "called. file={}", name);

    FileSys::DirectoryEntryType vfs_entry_type{};
    auto result = backend->GetEntryType(&vfs_entry_type, path);
    if (result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    IPC::ResponseBuilder rb{ctx, 3};
    rb.Push(ResultSuccess);
    rb.Push<u32>(static_cast<u32>(vfs_entry_type));
}

void IFileSystem::Commit(HLERequestContext& ctx) {
    LOG_WARNING(Service_FS, "(STUBBED) called");

    IPC::ResponseBuilder rb{ctx, 2};
    rb.Push(ResultSuccess);
}

void IFileSystem::GetFreeSpaceSize(HLERequestContext& ctx) {
    LOG_DEBUG(Service_FS, "called");

    IPC::ResponseBuilder rb{ctx, 4};
    rb.Push(ResultSuccess);
    rb.Push(size.get_free_size());
}

void IFileSystem::GetTotalSpaceSize(HLERequestContext& ctx) {
    LOG_DEBUG(Service_FS, "called");

    IPC::ResponseBuilder rb{ctx, 4};
    rb.Push(ResultSuccess);
    rb.Push(size.get_total_size());
}

void IFileSystem::GetFileTimeStampRaw(HLERequestContext& ctx) {
    const auto file_buffer = ctx.ReadBuffer();
    const std::string name = Common::StringFromBuffer(file_buffer);
    const auto path = FileSys::Path(name.c_str());

    LOG_WARNING(Service_FS, "(Partial Implementation) called. file={}", name);

    FileSys::FileTimeStampRaw vfs_timestamp{};
    auto result = backend->GetFileTimeStampRaw(&vfs_timestamp, path);
    if (result != ResultSuccess) {
        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(result);
        return;
    }

    IPC::ResponseBuilder rb{ctx, 10};
    rb.Push(ResultSuccess);
    rb.PushRaw(vfs_timestamp);
}

void IFileSystem::GetFileSystemAttribute(HLERequestContext& ctx) {
    LOG_WARNING(Service_FS, "(STUBBED) called");

    struct FileSystemAttribute {
        u8 dir_entry_name_length_max_defined;
        u8 file_entry_name_length_max_defined;
        u8 dir_path_name_length_max_defined;
        u8 file_path_name_length_max_defined;
        INSERT_PADDING_BYTES_NOINIT(0x5);
        u8 utf16_dir_entry_name_length_max_defined;
        u8 utf16_file_entry_name_length_max_defined;
        u8 utf16_dir_path_name_length_max_defined;
        u8 utf16_file_path_name_length_max_defined;
        INSERT_PADDING_BYTES_NOINIT(0x18);
        s32 dir_entry_name_length_max;
        s32 file_entry_name_length_max;
        s32 dir_path_name_length_max;
        s32 file_path_name_length_max;
        INSERT_PADDING_WORDS_NOINIT(0x5);
        s32 utf16_dir_entry_name_length_max;
        s32 utf16_file_entry_name_length_max;
        s32 utf16_dir_path_name_length_max;
        s32 utf16_file_path_name_length_max;
        INSERT_PADDING_WORDS_NOINIT(0x18);
        INSERT_PADDING_WORDS_NOINIT(0x1);
    };
    static_assert(sizeof(FileSystemAttribute) == 0xc0, "FileSystemAttribute has incorrect size");

    FileSystemAttribute savedata_attribute{};
    savedata_attribute.dir_entry_name_length_max_defined = true;
    savedata_attribute.file_entry_name_length_max_defined = true;
    savedata_attribute.dir_entry_name_length_max = 0x40;
    savedata_attribute.file_entry_name_length_max = 0x40;

    IPC::ResponseBuilder rb{ctx, 50};
    rb.Push(ResultSuccess);
    rb.PushRaw(savedata_attribute);
}

} // namespace Service::FileSystem