From 0f8dffb7a47c53c0dd22cd3aa488cf4fefa8d95a Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 14 Aug 2017 17:05:40 -0500 Subject: [PATCH 1/2] fs: Added FileSystem::reformat This function provides a shortcut to reformatting a mounted filesystem. Also, since this function is a virtual member of the FileSystem class, the user does not need to know the underlying filesystem to reformat the underlying storage. --- features/filesystem/FileSystem.cpp | 5 ++++ features/filesystem/FileSystem.h | 10 +++++++ features/filesystem/fat/FATFileSystem.cpp | 30 +++++++++++++++++++++ features/filesystem/fat/FATFileSystem.h | 33 +++++++++++++++++++++++ 4 files changed, 78 insertions(+) diff --git a/features/filesystem/FileSystem.cpp b/features/filesystem/FileSystem.cpp index dfae7a02e6..4fea36f1a0 100644 --- a/features/filesystem/FileSystem.cpp +++ b/features/filesystem/FileSystem.cpp @@ -24,6 +24,11 @@ FileSystem::FileSystem(const char *name) { } +int FileSystem::reformat(BlockDevice *bd) +{ + return -ENOSYS; +} + int FileSystem::remove(const char *path) { return -ENOSYS; diff --git a/features/filesystem/FileSystem.h b/features/filesystem/FileSystem.h index 6b948b2fa6..06659f3830 100644 --- a/features/filesystem/FileSystem.h +++ b/features/filesystem/FileSystem.h @@ -68,6 +68,16 @@ public: */ virtual int unmount() = 0; + /** 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 = NULL); + /** Remove a file from the filesystem. * * @param path The name of the file to remove. diff --git a/features/filesystem/fat/FATFileSystem.cpp b/features/filesystem/fat/FATFileSystem.cpp index 3fa35ecaaf..5015db0255 100644 --- a/features/filesystem/fat/FATFileSystem.cpp +++ b/features/filesystem/fat/FATFileSystem.cpp @@ -318,6 +318,36 @@ int FATFileSystem::format(BlockDevice *bd, int allocation_unit) { return 0; } +int FATFileSystem::reformat(BlockDevice *bd, int allocation_unit) { + lock(); + if (_id != -1) { + if (!bd) { + bd = _ffs[_id]; + } + + int err = unmount(); + if (err) { + unlock(); + return err; + } + } + + if (!bd) { + unlock(); + return -ENODEV; + } + + int err = FATFileSystem::format(bd, allocation_unit); + if (err) { + unlock(); + return err; + } + + err = mount(bd); + unlock(); + return err; +} + int FATFileSystem::remove(const char *path) { Deferred fpath = fat_path_prefix(_id, path); diff --git a/features/filesystem/fat/FATFileSystem.h b/features/filesystem/fat/FATFileSystem.h index d46535b9c4..c49393b446 100644 --- a/features/filesystem/fat/FATFileSystem.h +++ b/features/filesystem/fat/FATFileSystem.h @@ -75,6 +75,39 @@ 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 + * + * @param allocation_unit + * This is the number of bytes per cluster size. The valid value is N + * times the sector size. N is a power of 2 from 1 to 128 for FAT + * volume and upto 16MiB for exFAT volume. If zero is given, + * the default allocation unit size is selected by the underlying + * filesystem, which depends on the volume size. + * + * @return 0 on success, negative error code on failure + */ + virtual int reformat(BlockDevice *bd, int allocation_unit); + + /** 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 = NULL) + { + // required for virtual inheritance shenanigans + return reformat(bd, 0); + } + /** Remove a file from the filesystem. * * @param path The name of the file to remove. From cb2306c085026c3e80b57f87df5b80d5901ca209 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Mon, 14 Aug 2017 17:32:08 -0500 Subject: [PATCH 2/2] retarget: Added ENODEV to error codes --- platform/mbed_retarget.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/platform/mbed_retarget.h b/platform/mbed_retarget.h index 85c89fe3de..90d19fb094 100644 --- a/platform/mbed_retarget.h +++ b/platform/mbed_retarget.h @@ -120,6 +120,9 @@ extern "C" { #undef EXDEV #define EXDEV 18 /* Cross-device link */ +#undef ENODEV +#define ENODEV 19 + #undef EINVAL #define EINVAL 22 /* Invalid argument */