The file system API provides a common interface for implementing a file system on a block-based storage device. The file system API is a class-based interface, but implementing the file system API provides the standard POSIX file API familiar to C users.
Here is what it looks like to use the file system in mbed OS:
Declaring a file system
The FileSystem class provides the core API for file system operations. You must provide a block device to back the file system. When you declare a file system with a name, you can open files on the file system through standard POSIX functions (see open or fopen.
Existing file systems:
The FAT file system is a standard file system.
The BlockDevice class provides the underlying API for representing block-based storage that can be used to back a file system. mbed OS provides standard interfaces for the more common storage media, and you can extend the BlockDevice class to provide support for unsupported storage.
Existing block devices:
- SDBlockDevice - block device for SD cards.
- SPIFBlockDevice - block device for NOR based SPI flash devices.
- I2CEEBlockDevice - block device for EEPROM based I2C devices.
- HeapBlockDevice - block device backed by the heap for quick testing.
Additionally, two utility block devices give you better control over how storage is allocated. The slicing block device allows you to partition storage into smaller block devices that you can use independentally, and the chaining block device allows you to chain multiple block devices together and extend the usable amount of storage.
Note: Some file systems may provide a format function for cleanly initializing a file system on an underlying block device or require external tools to set up the file system before the first use.
The FileSystem class provides the core user interface with general functions that map directly to their global POSIX counterparts. mbed OS provides File and Dir classes that represent files and directories in a C++ API that uses object-oriented features in C++.
To implement a new file system in mbed OS, an implementor just needs to provide the abstract functions in the file system interface. The FAT file system provides an excellent example, you can find tests of the POSIX API here.
Minimal file system operations required:
- rename - POSIX, user/implementation API.
- remove - POSIX, user/implementation API.
- stat - POSIX, user/implementation API.
- mkdir - POSIX, user/implementation API.
Minimal file operations required:
- file open - POSIX, user API, implementation API.
- file close - POSIX, user API, implementation API.
- file read - POSIX, user API, implementation API.
- file write - POSIX, user API, implementation API.
- file seek - POSIX, user API, implementation API.
Minimal directory operations required: