Merge pull request #4908 from geky/fs-reformat

fs: Add FileSystem::reformat
pull/4886/merge
Jimmy Brisson 2017-08-21 10:38:00 -05:00 committed by GitHub
commit a457fe3bdd
5 changed files with 81 additions and 0 deletions

View File

@ -24,6 +24,11 @@ FileSystem::FileSystem(const char *name)
{
}
int FileSystem::reformat(BlockDevice *bd)
{
return -ENOSYS;
}
int FileSystem::remove(const char *path)
{
return -ENOSYS;

View File

@ -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.

View File

@ -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);

View File

@ -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.

View File

@ -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 */