Skip to content

Commit 3846fd5

Browse files
committed
extmod/fsusermount: Implement separate umount() function.
1 parent 5b85a86 commit 3846fd5

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

extmod/fsusermount.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "py/mpconfig.h"
2828
#if MICROPY_FSUSERMOUNT
29+
#include <string.h>
30+
#include <errno.h>
2931

3032
#include "py/nlr.h"
3133
#include "py/runtime.h"
@@ -131,6 +133,34 @@ STATIC mp_obj_t fatfs_mount(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t
131133
}
132134
MP_DEFINE_CONST_FUN_OBJ_KW(fsuser_mount_obj, 2, fatfs_mount);
133135

136+
STATIC mp_obj_t fatfs_umount(mp_obj_t bdev_or_path_in) {
137+
if (MP_STATE_PORT(fs_user_mount) == NULL) {
138+
goto einval;
139+
}
140+
141+
if (MP_OBJ_IS_STR(bdev_or_path_in)) {
142+
mp_uint_t mnt_len;
143+
const char *mnt_str = mp_obj_str_get_data(bdev_or_path_in, &mnt_len);
144+
if (memcmp(mnt_str, MP_STATE_PORT(fs_user_mount)->str, mnt_len + 1)) {
145+
goto einval;
146+
}
147+
} else if (bdev_or_path_in != MP_STATE_PORT(fs_user_mount)->readblocks[1]) {
148+
goto einval;
149+
}
150+
151+
FRESULT res = f_mount(NULL, MP_STATE_PORT(fs_user_mount)->str, 0);
152+
m_del_obj(fs_user_mount_t, MP_STATE_PORT(fs_user_mount));
153+
MP_STATE_PORT(fs_user_mount) = NULL;
154+
if (res != FR_OK) {
155+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't umount"));
156+
}
157+
return mp_const_none;
158+
159+
einval:
160+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(EINVAL)));
161+
}
162+
MP_DEFINE_CONST_FUN_OBJ_1(fsuser_umount_obj, fatfs_umount);
163+
134164
STATIC mp_obj_t fatfs_mkfs(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
135165
return fatfs_mount_mkfs(n_args, pos_args, kw_args, true);
136166
}

0 commit comments

Comments
 (0)