1 /*
2  * Copyright (c) 2020 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_FS_FS_SYS_H_
8 #define ZEPHYR_INCLUDE_FS_FS_SYS_H_
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /**
15  * @ingroup file_system_api
16  * @{
17  */
18 
19 /**
20  * @brief File System interface structure
21  */
22 struct fs_file_system_t {
23 	/**
24 	 * @name File operations
25 	 * @{
26 	 */
27 	/**
28 	 * Opens or creates a file, depending on flags given.
29 	 *
30 	 * @param filp File to open/create.
31 	 * @param fs_path Path to the file.
32 	 * @param flags Flags for opening/creating the file.
33 	 * @return 0 on success, negative errno code on fail.
34 	 */
35 	int (*open)(struct fs_file_t *filp, const char *fs_path,
36 		    fs_mode_t flags);
37 	/**
38 	 * Reads nbytes number of bytes.
39 	 *
40 	 * @param filp File to read from.
41 	 * @param dest Destination buffer.
42 	 * @param nbytes Number of bytes to read.
43 	 * @return Number of bytes read on success, negative errno code on fail.
44 	 */
45 	ssize_t (*read)(struct fs_file_t *filp, void *dest, size_t nbytes);
46 	/**
47 	 * Writes nbytes number of bytes.
48 	 *
49 	 * @param filp File to write to.
50 	 * @param src Source buffer.
51 	 * @param nbytes Number of bytes to write.
52 	 * @return Number of bytes written on success, negative errno code on fail.
53 	 */
54 	ssize_t (*write)(struct fs_file_t *filp,
55 					const void *src, size_t nbytes);
56 	/**
57 	 * Moves the file position to a new location in the file.
58 	 *
59 	 * @param filp File to move.
60 	 * @param off Relative offset from the position specified by whence.
61 	 * @param whence Position in the file. Possible values: SEEK_CUR, SEEK_SET, SEEK_END.
62 	 * @return New position in the file or negative errno code on fail.
63 	 */
64 	int (*lseek)(struct fs_file_t *filp, off_t off, int whence);
65 	/**
66 	 * Retrieves the current position in the file.
67 	 *
68 	 * @param filp File to get the current position from.
69 	 * @return Current position in the file or negative errno code on fail.
70 	 */
71 	off_t (*tell)(struct fs_file_t *filp);
72 	/**
73 	 * Truncates/expands the file to the new length.
74 	 *
75 	 * @param filp File to truncate/expand.
76 	 * @param length New length of the file.
77 	 * @return 0 on success, negative errno code on fail.
78 	 */
79 	int (*truncate)(struct fs_file_t *filp, off_t length);
80 	/**
81 	 * Flushes the cache of an open file.
82 	 *
83 	 * @param filp File to flush.
84 	 * @return 0 on success, negative errno code on fail.
85 	 */
86 	int (*sync)(struct fs_file_t *filp);
87 	/**
88 	 * Flushes the associated stream and closes the file.
89 	 *
90 	 * @param filp File to close.
91 	 * @return 0 on success, negative errno code on fail.
92 	 */
93 	int (*close)(struct fs_file_t *filp);
94 	/** @} */
95 
96 	/**
97 	 * @name Directory operations
98 	 * @{
99 	 */
100 	/**
101 	 * Opens an existing directory specified by the path.
102 	 *
103 	 * @param dirp Directory to open.
104 	 * @param fs_path Path to the directory.
105 	 * @return 0 on success, negative errno code on fail.
106 	 */
107 	int (*opendir)(struct fs_dir_t *dirp, const char *fs_path);
108 	/**
109 	 * Reads directory entries of an open directory.
110 	 *
111 	 * @param dirp Directory to read from.
112 	 * @param entry Next directory entry in the dirp directory.
113 	 * @return 0 on success, negative errno code on fail.
114 	 */
115 	int (*readdir)(struct fs_dir_t *dirp, struct fs_dirent *entry);
116 	/**
117 	 * Closes an open directory.
118 	 *
119 	 * @param dirp Directory to close.
120 	 * @return 0 on success, negative errno code on fail.
121 	 */
122 	int (*closedir)(struct fs_dir_t *dirp);
123 	/** @} */
124 
125 	/**
126 	 * @name File system level operations
127 	 * @{
128 	 */
129 	/**
130 	 * Mounts a file system.
131 	 *
132 	 * @param mountp Mount point.
133 	 * @return 0 on success, negative errno code on fail.
134 	 */
135 	int (*mount)(struct fs_mount_t *mountp);
136 	/**
137 	 * Unmounts a file system.
138 	 *
139 	 * @param mountp Mount point.
140 	 * @return 0 on success, negative errno code on fail.
141 	 */
142 	int (*unmount)(struct fs_mount_t *mountp);
143 	/**
144 	 * Deletes the specified file or directory.
145 	 *
146 	 * @param mountp Mount point.
147 	 * @param name Path to the file or directory to delete.
148 	 * @return 0 on success, negative errno code on fail.
149 	 */
150 	int (*unlink)(struct fs_mount_t *mountp, const char *name);
151 	/**
152 	 * Renames a file or directory.
153 	 *
154 	 * @param mountp Mount point.
155 	 * @param from Path to the file or directory to rename.
156 	 * @param to New name of the file or directory.
157 	 * @return 0 on success, negative errno code on fail.
158 	 */
159 	int (*rename)(struct fs_mount_t *mountp, const char *from,
160 					const char *to);
161 	/**
162 	 * Creates a new directory using specified path.
163 	 *
164 	 * @param mountp Mount point.
165 	 * @param name Path to the directory to create.
166 	 * @return 0 on success, negative errno code on fail.
167 	 */
168 	int (*mkdir)(struct fs_mount_t *mountp, const char *name);
169 	/**
170 	 * Checks the status of a file or directory specified by the path.
171 	 *
172 	 * @param mountp Mount point.
173 	 * @param path Path to the file or directory.
174 	 * @param entry Directory entry.
175 	 * @return 0 on success, negative errno code on fail.
176 	 */
177 	int (*stat)(struct fs_mount_t *mountp, const char *path,
178 					struct fs_dirent *entry);
179 	/**
180 	 * Returns the total and available space on the file system volume.
181 	 *
182 	 * @param mountp Mount point.
183 	 * @param path Path to the file or directory.
184 	 * @param stat File system statistics.
185 	 * @return 0 on success, negative errno code on fail.
186 	 */
187 	int (*statvfs)(struct fs_mount_t *mountp, const char *path,
188 					struct fs_statvfs *stat);
189 #if defined(CONFIG_FILE_SYSTEM_MKFS) || defined(__DOXYGEN__)
190 	/**
191 	 * Formats a device to specified file system type.
192 	 * Available only if @kconfig{CONFIG_FILE_SYSTEM_MKFS} is enabled.
193 	 *
194 	 * @param dev_id Device identifier.
195 	 * @param cfg File system configuration.
196 	 * @param flags Formatting flags.
197 	 * @return 0 on success, negative errno code on fail.
198 	 *
199 	 * @note This operation destroys existing data on the target device.
200 	 */
201 	int (*mkfs)(uintptr_t dev_id, void *cfg, int flags);
202 #endif
203 	/** @} */
204 };
205 
206 /**
207  * @}
208  */
209 
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif /* ZEPHYR_INCLUDE_FS_FS_SYS_H_ */
215