summaryrefslogtreecommitdiff
path: root/extmod/fsusermount.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-02-10 00:50:07 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-02-10 00:50:07 +0200
commit5b85a86ce3aaabac49083e2699c90d011a1e4de3 (patch)
tree9f97817e6fcc2945171e00a051ef2048afacdea7 /extmod/fsusermount.c
parenta2e5e4c3d8e470f5bcc8a827b71f5670acd4851d (diff)
extmod/fsusermount: Introduce separate mkfs() function.
Per the previously discussed plan. mount() still stays backward-compatible, and new mkfs() is rought and takes more args than needed. But is a step in a forward direction.
Diffstat (limited to 'extmod/fsusermount.c')
-rw-r--r--extmod/fsusermount.c30
1 files changed, 26 insertions, 4 deletions
diff --git a/extmod/fsusermount.c b/extmod/fsusermount.c
index fa4a4c445..847c8e029 100644
--- a/extmod/fsusermount.c
+++ b/extmod/fsusermount.c
@@ -32,7 +32,7 @@
#include "lib/fatfs/ff.h"
#include "fsusermount.h"
-STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+STATIC mp_obj_t fatfs_mount_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, bool mkfs) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_readonly, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
{ MP_QSTR_mkfs, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
@@ -85,15 +85,28 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
vfs->writeblocks[0] = MP_OBJ_NULL;
}
- // mount the block device
- FRESULT res = f_mount(&vfs->fatfs, vfs->str, 1);
+ // mount the block device (if mkfs, only pre-mount)
+ FRESULT res = f_mount(&vfs->fatfs, vfs->str, !mkfs);
// check the result
if (res == FR_OK) {
+ if (mkfs) {
+ goto mkfs;
+ }
} else if (res == FR_NO_FILESYSTEM && args[1].u_bool) {
+mkfs:
res = f_mkfs(vfs->str, 1, 0);
if (res != FR_OK) {
+mkfs_error:
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mkfs"));
}
+ if (mkfs) {
+ // If requested to only mkfs, unmount pre-mounted device
+ res = f_mount(NULL, vfs->str, 0);
+ if (res != FR_OK) {
+ goto mkfs_error;
+ }
+ MP_STATE_PORT(fs_user_mount) = NULL;
+ }
} else {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't mount"));
}
@@ -112,6 +125,15 @@ STATIC mp_obj_t pyb_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *
}
return mp_const_none;
}
-MP_DEFINE_CONST_FUN_OBJ_KW(pyb_mount_obj, 2, pyb_mount);
+
+STATIC mp_obj_t fatfs_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ return fatfs_mount_mkfs(n_args, pos_args, kw_args, false);
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount);
+
+STATIC mp_obj_t fatfs_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ return fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mkfs_obj, 2, fatfs_mkfs);
#endif // MICROPY_FSUSERMOUNT