Skip to content

fs: Add FileSystem::reformat #4908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions features/filesystem/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ FileSystem::FileSystem(const char *name)
{
}

int FileSystem::reformat(BlockDevice *bd)
{
return -ENOSYS;
}

int FileSystem::remove(const char *path)
{
return -ENOSYS;
Expand Down
10 changes: 10 additions & 0 deletions features/filesystem/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ class FileSystem : public FileSystemLike {
*/
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.
Expand Down
30 changes: 30 additions & 0 deletions features/filesystem/fat/FATFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*> fpath = fat_path_prefix(_id, path);

Expand Down
33 changes: 33 additions & 0 deletions features/filesystem/fat/FATFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,39 @@ class FATFileSystem : public FileSystem {
*/
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.
Expand Down
3 changes: 3 additions & 0 deletions platform/mbed_retarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down