Mistake on this page?
Report an issue in GitHub or email us

Storage

Storage support is split between file systems and their underlying block device support. The storage API page has more information on existing APIs in Mbed OS for both interface.

Block Device

Adding a block device implementation is required for backing filesystems on new hardware. You can extend the BlockDevice class to provide support for unsupported storage.

If you want to port a new file system to Mbed OS on existing storage options you can skip to the following section.

File systems

To implement a new file system in Mbed OS, an implementor needs to provide the abstract functions in the file system interface. The FAT file system provides an excellent example.

A minimal file system needs to provide the following functions:

  • file_open.
  • file_close.
  • file_read.
  • file_write.
  • file_seek.

Here is the full API that a file system may implement:

Public Member Functions
 FileSystem (const char *name=NULL)
 FileSystem lifetime. More...
virtual int mount (BlockDevice *bd)=0
 Mounts a filesystem to a block device. More...
virtual int unmount ()=0
 Unmounts a filesystem from the underlying block device. More...
virtual int reformat (BlockDevice *bd=NULL)
 Reformats a filesystem, results in an empty and mounted filesystem. More...
virtual int remove (const char *path)
 Remove a file from the filesystem. More...
virtual int rename (const char *path, const char *newpath)
 Rename a file in the filesystem. More...
virtual int stat (const char *path, struct stat *st)
 Store information about the file in a stat structure. More...
virtual int mkdir (const char *path, mode_t mode)
 Create a directory in the filesystem. More...
virtual int statvfs (const char *path, struct statvfs *buf)
 Store information about the mounted filesystem in a statvfs structure. More...
FileHandleopen (const char *path, int flags)
 Open a file on the filesystem. More...
DirHandleopendir (const char *path)
 Open a directory on the filesystem. More...
Static Public Member Functions
static FileSystemget_default_instance ()
 Return the default filesystem. More...
Protected Member Functions
virtual int file_open (fs_file_t *file, const char *path, int flags)=0
 Open a file on the filesystem. More...
virtual int file_close (fs_file_t file)=0
 Close a file. More...
virtual ssize_t file_read (fs_file_t file, void *buffer, size_t size)=0
 Read the contents of a file into a buffer. More...
virtual ssize_t file_write (fs_file_t file, const void *buffer, size_t size)=0
 Write the contents of a buffer to a file. More...
virtual int file_sync (fs_file_t file)
 Flush any buffers associated with the file. More...
virtual int file_isatty (fs_file_t file)
 Check if the file in an interactive terminal device If so, line buffered behaviour is used by default. More...
virtual off_t file_seek (fs_file_t file, off_t offset, int whence)=0
 Move the file position to a given offset from from a given location. More...
virtual off_t file_tell (fs_file_t file)
 Get the file position of the file. More...
virtual void file_rewind (fs_file_t file)
 Rewind the file position to the beginning of the file. More...
virtual off_t file_size (fs_file_t file)
 Get the size of the file. More...
virtual int dir_open (fs_dir_t *dir, const char *path)
 Open a directory on the filesystem. More...
virtual int dir_close (fs_dir_t dir)
 Close a directory. More...
virtual ssize_t dir_read (fs_dir_t dir, struct dirent *ent)
 Read the next directory entry. More...
virtual void dir_seek (fs_dir_t dir, off_t offset)
 Set the current position of the directory. More...
virtual off_t dir_tell (fs_dir_t dir)
 Get the current position of the directory. More...
virtual void dir_rewind (fs_dir_t dir)
 Rewind the current position to the beginning of the directory. More...
virtual size_t dir_size (fs_dir_t dir)
 Get the sizeof the directory. More...
virtual int open (FileHandle **file, const char *path, int flags)
 Open a file on the filesystem. More...
virtual int open (DirHandle **dir, const char *path)
 Open a directory on the filesystem. More...

File systems must be backed by a block device in Mbed OS. If you are using supported hardware, then you can use the Mbed OS block device classes. Otherwise, view the block device porting section earlier in this guide.

Related content

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.