libnx
fs.h
Go to the documentation of this file.
1 /**
2  * @file fs.h
3  * @brief Filesystem (fsp-srv) service IPC wrapper.
4  * Normally applications should just use standard stdio not FS-serv directly. However this can be used if obtaining a FsFileSystem, FsFile, or FsStorage, for mounting with fs_dev/romfs_dev, etc.
5  * @author plutoo
6  * @author yellows8
7  * @copyright libnx Authors
8  */
9 #pragma once
10 #include "../types.h"
11 #include "../services/sm.h"
12 
13 // We use wrapped handles for type safety.
14 
15 #define FS_MAX_PATH 0x301
16 
17 /// For use with FsSave.
18 #define FS_SAVEDATA_CURRENT_TITLEID 0
19 
20 /// For use with \ref FsSave and \ref FsSaveDataInfo.
21 #define FS_SAVEDATA_USERID_COMMONSAVE 0
22 
23 typedef struct {
24  Service s;
25 } FsFileSystem;
26 
27 typedef struct {
28  Service s;
29 } FsFile;
30 
31 typedef struct {
32  Service s;
33 } FsDir;
34 
35 typedef struct {
36  Service s;
37 } FsStorage;
38 
39 typedef struct {
40  Service s;
42 
43 typedef struct {
44  Service s;
46 
47 typedef struct {
48  Service s;
50 
51 /// Directory entry.
52 typedef struct
53 {
54  char name[FS_MAX_PATH]; ///< Entry name.
55  u8 pad[3];
56  s8 type; ///< See FsEntryType.
57  u8 pad2[3]; ///< ?
58  u64 fileSize; ///< File size.
60 
61 /// Save Struct
62 typedef struct
63 {
64  u64 titleID; ///< titleID of the savedata to access when accessing other titles' savedata via SaveData, otherwise FS_SAVEDATA_CURRENT_TITLEID.
65  u128 userID; ///< userID of the user-specific savedata to access, otherwise FS_SAVEDATA_USERID_COMMONSAVE. See account.h.
66  u64 saveID; ///< saveID, 0 for SaveData.
67  u64 SaveDataType; ///< See \ref FsSaveDataType.
68  u64 unk_x28; ///< 0 for SystemSaveData/SaveData.
69  u64 unk_x30; ///< 0 for SystemSaveData/SaveData.
70  u64 unk_x38; ///< 0 for SystemSaveData/SaveData.
71 } PACKED FsSave;
72 
73 typedef struct
74 {
75  u64 saveID_unk;
76  u8 SaveDataSpaceId; ///< See \ref FsSaveDataSpaceId.
77  u8 SaveDataType; ///< See \ref FsSaveDataType.
78  u8 pad[6];
79  u128 userID; ///< See userID for \ref FsSave.
80  u64 saveID; ///< See saveID for \ref FsSave.
81  u64 titleID; ///< titleID for FsSaveDataType_SaveData.
82  u64 size; ///< Raw saveimage size.
83  u8 unk_x38[0x28]; ///< Unknown. Usually zeros?
85 
86 typedef enum {
87  ENTRYTYPE_DIR = 0,
88  ENTRYTYPE_FILE = 1
89 } FsEntryType;
90 
91 typedef enum
92 {
93  FS_OPEN_READ = BIT(0), ///< Open for reading.
94  FS_OPEN_WRITE = BIT(1), ///< Open for writing.
95  FS_OPEN_APPEND = BIT(2), ///< Append file.
96 } FsFileFlags;
97 
98 /// For use with fsFsOpenDirectory.
99 typedef enum
100 {
101  FS_DIROPEN_DIRECTORY = BIT(0), ///< Enable reading directory entries.
102  FS_DIROPEN_FILE = BIT(1), ///< Enable reading file entries.
104 
105 typedef enum
106 {
107  FsStorageId_None = 0,
108  FsStorageId_Host = 1,
109  FsStorageId_GameCard = 2,
110  FsStorageId_NandSystem = 3,
111  FsStorageId_NandUser = 4,
112  FsStorageId_SdCard = 5,
113 } FsStorageId;
114 
115 typedef enum
116 {
117  FS_CONTENTSTORAGEID_NandSystem = 0,
118  FS_CONTENTSTORAGEID_NandUser = 1,
119  FS_CONTENTSTORAGEID_SdCard = 2,
120 } FsContentStorageId;
121 
122 typedef enum
123 {
124  FsSaveDataSpaceId_NandSystem = 0,
125  FsSaveDataSpaceId_NandUser = 1,
126  FsSaveDataSpaceId_SdCard = 2,
127  FsSaveDataSpaceId_TemporaryStorage = 3,
128 
129  FsSaveDataSpaceId_All = -1, ///< Pseudo value for fsOpenSaveDataIterator().
131 
132 typedef enum
133 {
134  FsSaveDataType_SystemSaveData = 0,
135  FsSaveDataType_SaveData = 1,
136  FsSaveDataType_BcatDeliveryCacheStorage = 2,
137  FsSaveDataType_DeviceSaveData = 3,
139  FsSaveDataType_CacheStorage = 5, ///< [3.0.0+]
141 
142 Result fsInitialize(void);
143 void fsExit(void);
144 
145 Service* fsGetServiceSession(void);
146 
147 /// Do not call this directly, see fs_dev.h.
149 
150 Result fsMountSaveData(FsFileSystem* out, u8 inval, FsSave *save);
151 Result fsMountSystemSaveData(FsFileSystem* out, u8 inval, FsSave *save);
152 Result fsOpenSaveDataIterator(FsSaveDataIterator* out, s32 SaveDataSpaceId);
153 Result fsOpenDataStorageByCurrentProcess(FsStorage* out);
154 Result fsOpenDeviceOperator(FsDeviceOperator* out);
155 Result fsOpenSdCardDetectionEventNotifier(FsEventNotifier* out);
156 // todo: Rest of commands here
157 
158 /// FsFileSystem can be mounted with fs_dev for use with stdio, see fs_dev.h.
159 
160 /// Wrapper(s) for fsMountSaveData.
161 /// See FsSave for titleID and userID.
162 Result fsMount_SaveData(FsFileSystem* out, u64 titleID, u128 userID);
163 
164 /// Wrapper for fsMountSystemSaveData.
165 /// WARNING: You can brick when writing to SystemSaveData, if the data is corrupted etc.
167 
168 // IFileSystem
169 Result fsFsCreateFile(FsFileSystem* fs, const char* path, size_t size, int flags);
170 Result fsFsDeleteFile(FsFileSystem* fs, const char* path);
171 Result fsFsCreateDirectory(FsFileSystem* fs, const char* path);
172 Result fsFsDeleteDirectory(FsFileSystem* fs, const char* path);
173 Result fsFsDeleteDirectoryRecursively(FsFileSystem* fs, const char* path);
174 Result fsFsRenameFile(FsFileSystem* fs, const char* path0, const char* path1);
175 Result fsFsRenameDirectory(FsFileSystem* fs, const char* path0, const char* path1);
176 Result fsFsGetEntryType(FsFileSystem* fs, const char* path, FsEntryType* out);
177 Result fsFsOpenFile(FsFileSystem* fs, const char* path, int flags, FsFile* out);
178 Result fsFsOpenDirectory(FsFileSystem* fs, const char* path, int flags, FsDir* out);
179 Result fsFsCommit(FsFileSystem* fs);
180 Result fsFsGetFreeSpace(FsFileSystem* fs, const char* path, u64* out);
181 Result fsFsGetTotalSpace(FsFileSystem* fs, const char* path, u64* out);
182 void fsFsClose(FsFileSystem* fs);
183 
184 // IFile
185 Result fsFileRead(FsFile* f, u64 off, void* buf, size_t len, size_t* out);
186 Result fsFileWrite(FsFile* f, u64 off, const void* buf, size_t len);
187 Result fsFileFlush(FsFile* f);
188 Result fsFileSetSize(FsFile* f, u64 sz);
189 Result fsFileGetSize(FsFile* f, u64* out);
190 void fsFileClose(FsFile* f);
191 
192 // IDirectory
193 Result fsDirRead(FsDir* d, u64 inval, size_t* total_entries, size_t max_entries, FsDirectoryEntry *buf);
194 Result fsDirGetEntryCount(FsDir* d, u64* count);
195 void fsDirClose(FsDir* d);
196 
197 // IStorage
198 Result fsStorageRead(FsStorage* s, u64 off, void* buf, size_t len);
199 void fsStorageClose(FsStorage* s);
200 
201 // ISaveDataInfoReader
202 
203 /// Read FsSaveDataInfo data into the buf array.
204 Result fsSaveDataIteratorRead(FsSaveDataIterator *s, FsSaveDataInfo* buf, size_t max_entries, size_t* total_entries);
205 void fsSaveDataIteratorClose(FsSaveDataIterator *s);
206 
207 // IEventNotifier
208 Result fsEventNotifierGetEventHandle(FsEventNotifier* e, Handle* out);
209 void fsEventNotifierClose(FsEventNotifier* e);
210 
211 // IDeviceOperator
212 Result fsDeviceOperatorIsSdCardInserted(FsDeviceOperator* d, bool* out);
213 void fsDeviceOperatorClose(FsDeviceOperator* d);
Result fsMountSdcard(FsFileSystem *out)
Do not call this directly, see fs_dev.h.
u64 titleID
titleID for FsSaveDataType_SaveData.
Definition: fs.h:81
u128 userID
See userID for FsSave.
Definition: fs.h:79
u128 userID
userID of the user-specific savedata to access, otherwise FS_SAVEDATA_USERID_COMMONSAVE. See account.h.
Definition: fs.h:65
int8_t s8
8-bit signed integer.
Definition: types.h:27
#define PACKED
Packs a struct (and other types?) so it won&#39;t include padding bytes.
Definition: types.h:56
u8 SaveDataSpaceId
See FsSaveDataSpaceId.
Definition: fs.h:76
s8 type
See FsEntryType.
Definition: fs.h:56
u8 SaveDataType
See FsSaveDataType.
Definition: fs.h:77
Result fsMount_SystemSaveData(FsFileSystem *out, u64 saveID)
Wrapper for fsMountSystemSaveData.
Open for reading.
Definition: fs.h:93
u64 titleID
titleID of the savedata to access when accessing other titles&#39; savedata via SaveData, otherwise FS_SAVEDATA_CURRENT_TITLEID.
Definition: fs.h:64
u64 saveID
See saveID for FsSave.
Definition: fs.h:80
Definition: fs.h:43
u32 Handle
Kernel object handle.
Definition: types.h:45
Service object structure.
Definition: sm.h:23
Save Struct.
Definition: fs.h:62
u32 Result
Function error code result type.
Definition: types.h:46
uint8_t u8
8-bit unsigned integer.
Definition: types.h:21
uint64_t u64
64-bit unsigned integer.
Definition: types.h:24
FsDirectoryFlags
For use with fsFsOpenDirectory.
Definition: fs.h:99
u64 SaveDataType
See FsSaveDataType.
Definition: fs.h:67
u64 unk_x38
0 for SystemSaveData/SaveData.
Definition: fs.h:70
Enable reading file entries.
Definition: fs.h:102
Enable reading directory entries.
Definition: fs.h:101
Result fsSaveDataIteratorRead(FsSaveDataIterator *s, FsSaveDataInfo *buf, size_t max_entries, size_t *total_entries)
Read FsSaveDataInfo data into the buf array.
u64 size
Raw saveimage size.
Definition: fs.h:82
u64 unk_x30
0 for SystemSaveData/SaveData.
Definition: fs.h:69
int32_t s32
32-bit signed integer.
Definition: types.h:29
FsFileFlags
Definition: fs.h:91
__uint128_t u128
128-bit unsigned integer.
Definition: types.h:25
FsSaveDataSpaceId
Definition: fs.h:122
Definition: fs.h:47
Definition: fs.h:73
Append file.
Definition: fs.h:95
[3.0.0+]
Definition: fs.h:138
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:51
u64 saveID
saveID, 0 for SaveData.
Definition: fs.h:66
FsSaveDataType
Definition: fs.h:132
Definition: fs.h:39
u64 unk_x28
0 for SystemSaveData/SaveData.
Definition: fs.h:68
Definition: fs.h:27
[3.0.0+]
Definition: fs.h:139
Open for writing.
Definition: fs.h:94
Definition: fs.h:23
Definition: fs.h:35
Result fsMount_SaveData(FsFileSystem *out, u64 titleID, u128 userID)
FsFileSystem can be mounted with fs_dev for use with stdio, see fs_dev.h.
Definition: fs.h:31
u64 fileSize
File size.
Definition: fs.h:58
Pseudo value for fsOpenSaveDataIterator().
Definition: fs.h:129
Directory entry.
Definition: fs.h:52