diff --git a/LittleFileSystem.cpp b/LittleFileSystem.cpp index a827497d14..2a32683d4c 100644 --- a/LittleFileSystem.cpp +++ b/LittleFileSystem.cpp @@ -223,6 +223,31 @@ int LittleFileSystem::format(BlockDevice *bd, return 0; } +int LittleFileSystem::reformat(BlockDevice *bd) { + if (_bd) { + if (!bd) { + bd = _bd; + } + + int err = unmount(); + if (err) { + return err; + } + } + + if (!bd) { + return -ENODEV; + } + + int err = LittleFileSystem::format(bd, + _read_size, _prog_size, _block_size, _lookahead); + if (err) { + return err; + } + + return mount(bd); +} + int LittleFileSystem::remove(const char *filename) { int err = lfs_remove(&_lfs, filename); return lfs_toerror(err); diff --git a/LittleFileSystem.h b/LittleFileSystem.h index 6f1c173060..719ae7e741 100644 --- a/LittleFileSystem.h +++ b/LittleFileSystem.h @@ -65,7 +65,7 @@ public: lfs_size_t lookahead=MBED_LFS_LOOKAHEAD); virtual ~LittleFileSystem(); - /** Formats a logical drive, FDISK partitioning rule. + /** Formats a block device with the LittleFileSystem * * The block device to format should be mounted when this function is called. * @@ -107,6 +107,18 @@ public: */ virtual int unmount(); + /** Reformats a filesystem, results in an empty and mounted filesystem + * + * @param bd + * BlockDevice to reformat and mount. If NULL, the mounted + * block device will be used. + * Note: if mount fails, bd must be provided. + * Default: NULL + * + * @return 0 on success, negative error code on failure + */ + virtual int reformat(BlockDevice *bd); + /** Remove a file from the filesystem. * * @param path The name of the file to remove.