Mbed OS Reference
Loading...
Searching...
No Matches
FATFileSystem.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2020 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
19#ifndef MBED_FATFILESYSTEM_H
20#define MBED_FATFILESYSTEM_H
21
22#include "filesystem/FileSystem.h"
23#include "blockdevice/BlockDevice.h"
24#include "FileHandle.h"
25#include <stdint.h>
26#include "rtos/Mutex.h"
27#include "storage/filesystem/fat/ChaN/ff.h"
28
29namespace mbed {
30
31/** \defgroup filesystem Filesystem
32 * \ingroup storage
33 * @{
34 */
35
36/**
37 * FAT file system based on ChaN's FAT file system library v0.8
38 *
39 * Synchronization level: Thread safe
40 */
41class FATFileSystem : public FileSystem {
42public:
43 /** Lifetime of the FAT file system.
44 *
45 * @param name Name of the file system in the tree.
46 * @param bd Block device to mount. Mounted immediately if not NULL.
47 */
48 FATFileSystem(const char *name = NULL, BlockDevice *bd = NULL);
49 virtual ~FATFileSystem();
50
51 /** Format a logical drive, FDISK partitioning rule.
52 *
53 * The block device to format should be mounted when this function is called.
54 *
55 * @param bd
56 * This is the block device that will be formatted.
57 *
58 * @param cluster_size
59 * This is the number of bytes per cluster. A larger cluster size decreases
60 * the overhead of the FAT table, but also increases the minimum file size. The
61 * cluster size must be a multiple of the underlying device's allocation unit
62 * and is currently limited to a max of 32,768 bytes. If the cluster size is set to zero, a cluster size
63 * is determined from the device's allocation unit. Defaults to zero.
64 *
65 * @return 0 on success, negative error code on failure.
66 */
67 static int format(BlockDevice *bd, bd_size_t cluster_size = 0);
68
69 /** Mount a file system to a block device.
70 *
71 * @param bd Block device to mount to.
72 * @return 0 on success, negative error code on failure.
73 */
74 virtual int mount(BlockDevice *bd);
75
76 /** Unmount a file system from the underlying block device.
77 *
78 * @return 0 on success, negative error code on failure.
79 */
80 virtual int unmount();
81
82 /** Reformat a file system, results in an empty and mounted file system.
83 *
84 * @param bd
85 * Block device to reformat and mount. If NULL, the mounted
86 * Block device is used.
87 * Note: If mount fails, bd must be provided.
88 * Default: NULL
89 *
90 * @param allocation_unit
91 * This is the number of bytes per cluster size. The valid value is N
92 * times the sector size. N is a power of 2, from 1 to 128, for the FAT
93 * volume, and up to 16MiB for the exFAT volume. If zero is given,
94 * the default allocation unit size is selected by the underlying
95 * file system, which depends on the volume size.
96 *
97 * @return 0 on success, negative error code on failure.
98 */
99 virtual int reformat(BlockDevice *bd, int allocation_unit);
100
101 /** Reformat a file system, results in an empty and mounted file system.
102 *
103 * @param bd Block device to reformat and mount. If NULL, the mounted
104 * Block device is used.
105 * Note: If mount fails, bd must be provided.
106 * Default: NULL
107 * @return 0 on success, negative error code on failure.
108 */
109 virtual int reformat(BlockDevice *bd = NULL)
110 {
111 // Required for virtual inheritance shenanigans.
112 return reformat(bd, 0);
113 }
114
115 /** Remove a file from the file system.
116 *
117 * @param path The name of the file to remove.
118 * @return 0 on success, negative error code on failure.
119 */
120 virtual int remove(const char *path);
121
122 /** Rename a file in the file system.
123 *
124 * @param path The current name of the file to rename.
125 * @param newpath The new name of the file.
126 * @return 0 on success, negative error code on failure.
127 */
128 virtual int rename(const char *path, const char *newpath);
129
130 /** Store information about the file in a stat structure.
131 *
132 * @param path The name of the file to store information about.
133 * @param st The stat buffer to write to.
134 * @return 0 on success, negative error code on failure.
135 */
136 virtual int stat(const char *path, struct stat *st);
137
138 /** Create a directory in the file system.
139 *
140 * @param path The name of the directory to create.
141 * @param mode The permissions with which to create the directory.
142 * @return 0 on success, negative error code on failure.
143 */
144 virtual int mkdir(const char *path, mode_t mode);
145
146 /** Store information about the mounted file system in a statvfs structure.
147 *
148 * @param path The name of the file to store information about.
149 * @param buf The stat buffer to write to.
150 * @return 0 on success, negative error code on failure.
151 */
152 virtual int statvfs(const char *path, struct statvfs *buf);
153
154protected:
155#if !(DOXYGEN_ONLY)
156 /** Open a file on the file system.
157 *
158 * @param file Destination for the handle to a newly created file.
159 * @param path The name of the file to open.
160 * @param flags The flags that trigger opening of the file. These flags are O_RDONLY, O_WRONLY, and O_RDWR,
161 * with an O_CREAT, O_TRUNC, or O_APPEND bitwise OR operator.
162 * @return 0 on success, negative error code on failure.
163 */
164 virtual int file_open(fs_file_t *file, const char *path, int flags);
165
166 /** Close a file
167 *
168 * @param file File handle.
169 * @return 0 on success, negative error code on failure.
170 */
171 virtual int file_close(fs_file_t file);
172
173 /** Read the contents of a file into a buffer.
174 *
175 * @param file File handle.
176 * @param buffer The buffer to read into.
177 * @param len The number of bytes to read.
178 * @return The number of bytes read; 0 at the end of the file, negative error on failure.
179 */
180 virtual ssize_t file_read(fs_file_t file, void *buffer, size_t len);
181
182 /** Write the contents of a buffer to a file.
183 *
184 * @param file File handle.
185 * @param buffer The buffer to write from.
186 * @param len The number of bytes to write.
187 * @return The number of bytes written, negative error on failure.
188 */
189 virtual ssize_t file_write(fs_file_t file, const void *buffer, size_t len);
190
191 /** Flush any buffers associated with the file.
192 *
193 * @param file File handle.
194 * @return 0 on success, negative error code on failure.
195 */
196 virtual int file_sync(fs_file_t file);
197
198 /** Move the file position to a given offset from a given location
199 *
200 * @param file File handle.
201 * @param offset The offset from whence to move to.
202 * @param whence The start of where to seek.
203 * SEEK_SET to start from the beginning of the file,
204 * SEEK_CUR to start from the current position in the file,
205 * SEEK_END to start from the end of the file.
206 * @return The new offset of the file.
207 */
208 virtual off_t file_seek(fs_file_t file, off_t offset, int whence);
209
210 /** Get the file position of the file.
211 *
212 * @param file File handle.
213 * @return The current offset in the file.
214 */
215 virtual off_t file_tell(fs_file_t file);
216
217 /** Get the size of the file.
218 *
219 * @param file File handle.
220 * @return Size of the file in bytes.
221 */
222 virtual off_t file_size(fs_file_t file);
223
224 /** Truncate or extend a file.
225 *
226 * The file's length is set to the specified value. The seek pointer is
227 * not changed. If the file is extended, the extended area appears as if
228 * it were zero-filled.
229 *
230 * @param file File handle.
231 * @param length The requested new length for the file.
232 *
233 * @return 0 on success, negative error code on failure.
234 */
235 virtual int file_truncate(mbed::fs_file_t file, off_t length);
236
237 /** Open a directory on the file system.
238 *
239 * @param dir Destination for the handle to the directory.
240 * @param path Name of the directory to open.
241 * @return 0 on success, negative error code on failure.
242 */
243 virtual int dir_open(fs_dir_t *dir, const char *path);
244
245 /** Close a directory
246 *
247 * @param dir Dir handle.
248 * @return 0 on success, negative error code on failure.
249 */
250 virtual int dir_close(fs_dir_t dir);
251
252 /** Read the next directory entry
253 *
254 * @param dir Dir handle.
255 * @param ent The directory entry to fill out.
256 * @return 1 on reading a filename, 0 at the end of the directory, negative error on failure.
257 */
258 virtual ssize_t dir_read(fs_dir_t dir, struct dirent *ent);
259
260 /** Set the current position of the directory.
261 *
262 * @param dir Dir handle.
263 * @param offset Offset of the location to seek to.
264 * Must be a value returned by dir_tell.
265 */
266 virtual void dir_seek(fs_dir_t dir, off_t offset);
267
268 /** Get the current position of the directory.
269 *
270 * @param dir Dir handle.
271 * @return Directory position, which can be passed to dir_rewind.
272 */
273 virtual off_t dir_tell(fs_dir_t dir);
274
275 /** Rewind the current position to the beginning of the directory.
276 *
277 * @param dir Dir handle.
278 */
279 virtual void dir_rewind(fs_dir_t dir);
280#endif //!(DOXYGEN_ONLY)
281
282private:
283 FATFS _fs; // Work area (file system object) for logical drive.
284 char _fsid[sizeof("0:")];
285 int _id;
286
287protected:
288 virtual void lock();
289 virtual void unlock();
290 virtual int mount(BlockDevice *bd, bool mount);
291};
292
293/** @}
294 */
295
296} // namespace mbed
297
298// Added "using" for backwards compatibility.
299#ifndef MBED_NO_GLOBAL_USING_DIRECTIVE
301#endif
302
303#endif
304
A hardware device capable of writing and reading blocks.
Definition: BlockDevice.h:53
FAT file system based on ChaN's FAT file system library v0.8.
Definition: FATFileSystem.h:41
FATFileSystem(const char *name=NULL, BlockDevice *bd=NULL)
Lifetime of the FAT file system.
virtual int mkdir(const char *path, mode_t mode)
Create a directory in the file system.
static int format(BlockDevice *bd, bd_size_t cluster_size=0)
Format a logical drive, FDISK partitioning rule.
virtual int reformat(BlockDevice *bd, int allocation_unit)
Reformat a file system, results in an empty and mounted file system.
virtual int mount(BlockDevice *bd)
Mount a file system to a block device.
virtual int stat(const char *path, struct stat *st)
Store information about the file in a stat structure.
virtual int reformat(BlockDevice *bd=NULL)
Reformat a file system, results in an empty and mounted file system.
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.
virtual int rename(const char *path, const char *newpath)
Rename a file in the file system.
A file system object.
Definition: FileSystem.h:50
uint64_t bd_size_t
Type representing a quantity of 8-bit bytes.
Definition: BlockDevice.h:48
Definition: ff.h:132