Mbed OS Reference
Loading...
Searching...
No Matches
LittleFileSystem2.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2017 ARM Limited
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/** \addtogroup storage */
19/** @{*/
20
21#ifndef MBED_LFS2FILESYSTEM_H
22#define MBED_LFS2FILESYSTEM_H
23
24#include "FileSystem.h"
25#include "BlockDevice.h"
26#include "rtos/Mutex.h"
27#include "lfs2.h"
28
29namespace mbed {
30
31/**
32 * LittleFileSystem2, a little file system
33 *
34 * Synchronization level: Thread safe
35 */
37public:
38 /** Lifetime of the LittleFileSystem2
39 *
40 * @param name Name of the file system in the tree.
41 * @param bd Block device to mount. Mounted immediately if not NULL.
42 * @param block_size
43 * Size of a logical block. This does not impact ram consumption and
44 * may be larger than the physical erase block. If the physical erase
45 * block is larger, littlefs will use that instead. Larger values will
46 * be faster but waste more storage when files are not aligned to a
47 * block size.
48 * @param block_cycles
49 * Number of erase cycles before a block is forcefully evicted. Larger
50 * values are more efficient but cause less even wear distribution. 0
51 * disables dynamic wear-leveling.
52 * @param cache_size
53 * Size of read/program caches. Each file uses 1 cache, and littlefs
54 * allocates 2 caches for internal operations. Larger values should be
55 * faster but uses more RAM.
56 * @param lookahead
57 * Size of the lookahead buffer. A larger lookahead reduces the
58 * allocation scans and results in a faster filesystem but uses
59 * more RAM.
60 */
61 LittleFileSystem2(const char *name = NULL, mbed::BlockDevice *bd = NULL,
62 lfs2_size_t block_size = MBED_LFS2_BLOCK_SIZE,
63 uint32_t block_cycles = MBED_LFS2_BLOCK_CYCLES,
64 lfs2_size_t cache_size = MBED_LFS2_CACHE_SIZE,
65 lfs2_size_t lookahead = MBED_LFS2_LOOKAHEAD_SIZE);
66
67 virtual ~LittleFileSystem2();
68
69 /** Format a block device with the LittleFileSystem2.
70 *
71 * The block device to format should be mounted when this function is called.
72 *
73 * @param bd This is the block device that will be formatted.
74 * @param block_size
75 * Size of a logical block. This does not impact ram consumption and
76 * may be larger than the physical erase block. If the physical erase
77 * block is larger, littlefs will use that instead. Larger values will
78 * be faster but waste more storage when files are not aligned to a
79 * block size.
80 * @param block_cycles
81 * Number of erase cycles before a block is forcefully evicted. Larger
82 * values are more efficient but cause less even wear distribution. 0
83 * disables dynamic wear-leveling.
84 * @param cache_size
85 * Size of read/program caches. Each file uses 1 cache, and littlefs
86 * allocates 2 caches for internal operations. Larger values should be
87 * faster but uses more RAM.
88 * @param lookahead_size
89 * Size of the lookahead buffer. A larger lookahead reduces the
90 * allocation scans and results in a faster filesystem but uses
91 * more RAM.
92 */
93 static int format(mbed::BlockDevice *bd,
94 lfs2_size_t block_size = MBED_LFS2_BLOCK_SIZE,
95 uint32_t block_cycles = MBED_LFS2_BLOCK_CYCLES,
96 lfs2_size_t cache_size = MBED_LFS2_CACHE_SIZE,
97 lfs2_size_t lookahead_size = MBED_LFS2_LOOKAHEAD_SIZE);
98
99 /** Mount a file system to a block device.
100 *
101 * @param bd Block device to mount to.
102 * @return 0 on success, negative error code on failure.
103 */
104 virtual int mount(mbed::BlockDevice *bd);
105
106 /** Unmount a file system from the underlying block device.
107 *
108 * @return 0 on success, negative error code on failure
109 */
110 virtual int unmount();
111
112 /** Reformat a file system. Results in an empty and mounted file system.
113 *
114 * @param bd
115 * Block device to reformat and mount. If NULL, the mounted
116 * Block device is used.
117 * Note: If mount fails, bd must be provided.
118 * Default: NULL
119 *
120 * @return 0 on success, negative error code on failure
121 */
122 virtual int reformat(mbed::BlockDevice *bd);
123
124 /** Remove a file from the file system.
125 *
126 * @param path The name of the file to remove.
127 * @return 0 on success, negative error code on failure
128 */
129 virtual int remove(const char *path);
130
131 /** Rename a file in the file system.
132 *
133 * @param path The name of the file to rename.
134 * @param newpath The name to rename it to.
135 * @return 0 on success, negative error code on failure
136 */
137 virtual int rename(const char *path, const char *newpath);
138
139 /** Store information about the file in a stat structure
140 *
141 * @param path The name of the file to find information about.
142 * @param st The stat buffer to write to.
143 * @return 0 on success, negative error code on failure
144 */
145 virtual int stat(const char *path, struct stat *st);
146
147 /** Create a directory in the file system.
148 *
149 * @param path The name of the directory to create.
150 * @param mode The permissions with which to create the directory.
151 * @return 0 on success, negative error code on failure
152 */
153 virtual int mkdir(const char *path, mode_t mode);
154
155 /** Store information about the mounted file system in a statvfs structure.
156 *
157 * @param path The name of the file to find information about.
158 * @param buf The stat buffer to write to.
159 * @return 0 on success, negative error code on failure
160 */
161 virtual int statvfs(const char *path, struct statvfs *buf);
162
163protected:
164#if !(DOXYGEN_ONLY)
165 /** Open a file on the file system.
166 *
167 * @param file Destination of the newly created handle to the referenced file.
168 * @param path The name of the file to open.
169 * @param flags The flags that trigger opening of the file. These flags are O_RDONLY, O_WRONLY, and O_RDWR,
170 * with an O_CREAT, O_TRUNC, or O_APPEND bitwise OR operator.
171 * @return 0 on success, negative error code on failure.
172 */
173 virtual int file_open(mbed::fs_file_t *file, const char *path, int flags);
174
175 /** Close a file
176 *
177 * @param file File handle.
178 * return 0 on success, negative error code on failure
179 */
180 virtual int file_close(mbed::fs_file_t file);
181
182 /** Read the contents of a file into a buffer
183 *
184 * @param file File handle.
185 * @param buffer The buffer to read in to.
186 * @param size The number of bytes to read.
187 * @return The number of bytes read, 0 at end of file, negative error on failure
188 */
189 virtual ssize_t file_read(mbed::fs_file_t file, void *buffer, size_t size);
190
191 /** Write the contents of a buffer to a file
192 *
193 * @param file File handle.
194 * @param buffer The buffer to write from.
195 * @param size The number of bytes to write.
196 * @return The number of bytes written, negative error on failure
197 */
198 virtual ssize_t file_write(mbed::fs_file_t file, const void *buffer, size_t size);
199
200 /** Flush any buffers associated with the file
201 *
202 * @param file File handle.
203 * @return 0 on success, negative error code on failure
204 */
205 virtual int file_sync(mbed::fs_file_t file);
206
207 /** Move the file position to a given offset from a given location
208 *
209 * @param file File handle.
210 * @param offset The offset from whence to move to.
211 * @param whence The start of where to seek.
212 * SEEK_SET to start from beginning of file,
213 * SEEK_CUR to start from current position in file,
214 * SEEK_END to start from end of file.
215 * @return The new offset of the file
216 */
217 virtual off_t file_seek(mbed::fs_file_t file, off_t offset, int whence);
218
219 /** Get the file position of the file
220 *
221 * @param file File handle.
222 * @return The current offset in the file
223 */
224 virtual off_t file_tell(mbed::fs_file_t file);
225
226 /** Get the size of the file
227 *
228 * @param file File handle.
229 * @return Size of the file in bytes
230 */
231 virtual off_t file_size(mbed::fs_file_t file);
232
233 /** Truncate or extend a file.
234 *
235 * The file's length is set to the specified value. The seek pointer is
236 * not changed. If the file is extended, the extended area appears as if
237 * it were zero-filled.
238 *
239 * @param file File handle.
240 * @param length The requested new length for the file
241 *
242 * @return Zero on success, negative error code on failure
243 */
244 virtual int file_truncate(mbed::fs_file_t file, off_t length);
245
246 /** Open a directory on the file system.
247 *
248 * @param dir Destination for the handle to the directory.
249 * @param path Name of the directory to open.
250 * @return 0 on success, negative error code on failure
251 */
252 virtual int dir_open(mbed::fs_dir_t *dir, const char *path);
253
254 /** Close a directory
255 *
256 * @param dir Dir handle.
257 * return 0 on success, negative error code on failure
258 */
259 virtual int dir_close(mbed::fs_dir_t dir);
260
261 /** Read the next directory entry
262 *
263 * @param dir Dir handle.
264 * @param ent The directory entry to fill out.
265 * @return 1 on reading a filename, 0 at end of directory, negative error on failure
266 */
267 virtual ssize_t dir_read(mbed::fs_dir_t dir, struct dirent *ent);
268
269 /** Set the current position of the directory
270 *
271 * @param dir Dir handle.
272 * @param offset Offset of the location to seek to,
273 * must be a value returned from dir_tell
274 */
275 virtual void dir_seek(mbed::fs_dir_t dir, off_t offset);
276
277 /** Get the current position of the directory
278 *
279 * @param dir Dir handle.
280 * @return Position of the directory that can be passed to dir_rewind
281 */
282 virtual off_t dir_tell(mbed::fs_dir_t dir);
283
284 /** Rewind the current position to the beginning of the directory
285 *
286 * @param dir Dir handle
287 */
288 virtual void dir_rewind(mbed::fs_dir_t dir);
289#endif //!(DOXYGEN_ONLY)
290
291private:
292 lfs2_t _lfs; // The actual file system
293 struct lfs2_config _config;
294 mbed::BlockDevice *_bd; // The block device
295
296 // thread-safe locking
297 rtos::Mutex _mutex;
298};
299
300} // namespace mbed
301
302// Added "using" for backwards compatibility
303#ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
305#endif
306
307#endif
308
309/** @}*/
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
A file system object.
Definition: FileSystem.h:50
LittleFileSystem2, a little file system.
virtual int mkdir(const char *path, mode_t mode)
Create a directory in the file system.
virtual int reformat(mbed::BlockDevice *bd)
Reformat a file system.
virtual int stat(const char *path, struct stat *st)
Store information about the file in a stat structure.
LittleFileSystem2(const char *name=NULL, mbed::BlockDevice *bd=NULL, lfs2_size_t block_size=MBED_LFS2_BLOCK_SIZE, uint32_t block_cycles=MBED_LFS2_BLOCK_CYCLES, lfs2_size_t cache_size=MBED_LFS2_CACHE_SIZE, lfs2_size_t lookahead=MBED_LFS2_LOOKAHEAD_SIZE)
Lifetime of the LittleFileSystem2.
virtual int remove(const char *path)
Remove a file from the file system.
virtual int unmount()
Unmount a file system from the underlying block device.
virtual int statvfs(const char *path, struct statvfs *buf)
Store information about the mounted file system in a statvfs structure.
static int format(mbed::BlockDevice *bd, lfs2_size_t block_size=MBED_LFS2_BLOCK_SIZE, uint32_t block_cycles=MBED_LFS2_BLOCK_CYCLES, lfs2_size_t cache_size=MBED_LFS2_CACHE_SIZE, lfs2_size_t lookahead_size=MBED_LFS2_LOOKAHEAD_SIZE)
Format a block device with the LittleFileSystem2.
virtual int rename(const char *path, const char *newpath)
Rename a file in the file system.
virtual int mount(mbed::BlockDevice *bd)
Mount a file system to a block device.
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
Definition: lfs2.h:364