Skip to content

Commit a457fe3

Browse files
Merge pull request #4908 from geky/fs-reformat
fs: Add FileSystem::reformat
2 parents 02baee3 + cb2306c commit a457fe3

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

features/filesystem/FileSystem.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ FileSystem::FileSystem(const char *name)
2424
{
2525
}
2626

27+
int FileSystem::reformat(BlockDevice *bd)
28+
{
29+
return -ENOSYS;
30+
}
31+
2732
int FileSystem::remove(const char *path)
2833
{
2934
return -ENOSYS;

features/filesystem/FileSystem.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ class FileSystem : public FileSystemLike {
6868
*/
6969
virtual int unmount() = 0;
7070

71+
/** Reformats a filesystem, results in an empty and mounted filesystem
72+
*
73+
* @param bd BlockDevice to reformat and mount. If NULL, the mounted
74+
* block device will be used.
75+
* Note: if mount fails, bd must be provided.
76+
* Default: NULL
77+
* @return 0 on success, negative error code on failure
78+
*/
79+
virtual int reformat(BlockDevice *bd = NULL);
80+
7181
/** Remove a file from the filesystem.
7282
*
7383
* @param path The name of the file to remove.

features/filesystem/fat/FATFileSystem.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,36 @@ int FATFileSystem::format(BlockDevice *bd, int allocation_unit) {
318318
return 0;
319319
}
320320

321+
int FATFileSystem::reformat(BlockDevice *bd, int allocation_unit) {
322+
lock();
323+
if (_id != -1) {
324+
if (!bd) {
325+
bd = _ffs[_id];
326+
}
327+
328+
int err = unmount();
329+
if (err) {
330+
unlock();
331+
return err;
332+
}
333+
}
334+
335+
if (!bd) {
336+
unlock();
337+
return -ENODEV;
338+
}
339+
340+
int err = FATFileSystem::format(bd, allocation_unit);
341+
if (err) {
342+
unlock();
343+
return err;
344+
}
345+
346+
err = mount(bd);
347+
unlock();
348+
return err;
349+
}
350+
321351
int FATFileSystem::remove(const char *path) {
322352
Deferred<const char*> fpath = fat_path_prefix(_id, path);
323353

features/filesystem/fat/FATFileSystem.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,39 @@ class FATFileSystem : public FileSystem {
7575
*/
7676
virtual int unmount();
7777

78+
/** Reformats a filesystem, results in an empty and mounted filesystem
79+
*
80+
* @param bd
81+
* BlockDevice to reformat and mount. If NULL, the mounted
82+
* block device will be used.
83+
* Note: if mount fails, bd must be provided.
84+
* Default: NULL
85+
*
86+
* @param allocation_unit
87+
* This is the number of bytes per cluster size. The valid value is N
88+
* times the sector size. N is a power of 2 from 1 to 128 for FAT
89+
* volume and upto 16MiB for exFAT volume. If zero is given,
90+
* the default allocation unit size is selected by the underlying
91+
* filesystem, which depends on the volume size.
92+
*
93+
* @return 0 on success, negative error code on failure
94+
*/
95+
virtual int reformat(BlockDevice *bd, int allocation_unit);
96+
97+
/** Reformats a filesystem, results in an empty and mounted filesystem
98+
*
99+
* @param bd BlockDevice to reformat and mount. If NULL, the mounted
100+
* block device will be used.
101+
* Note: if mount fails, bd must be provided.
102+
* Default: NULL
103+
* @return 0 on success, negative error code on failure
104+
*/
105+
virtual int reformat(BlockDevice *bd = NULL)
106+
{
107+
// required for virtual inheritance shenanigans
108+
return reformat(bd, 0);
109+
}
110+
78111
/** Remove a file from the filesystem.
79112
*
80113
* @param path The name of the file to remove.

platform/mbed_retarget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ extern "C" {
120120
#undef EXDEV
121121
#define EXDEV 18 /* Cross-device link */
122122

123+
#undef ENODEV
124+
#define ENODEV 19
125+
123126
#undef EINVAL
124127
#define EINVAL 22 /* Invalid argument */
125128

0 commit comments

Comments
 (0)