6262STATIC uint32_t os_num_mounted_devices ;
6363STATIC os_term_dup_obj_t os_term_dup_obj ;
6464
65+ /******************************************************************************
66+ DECLARE PRIVATE FUNCTIONS
67+ ******************************************************************************/
68+ STATIC void unmount (os_fs_mount_t * mount_obj );
69+ STATIC bool path_equal (const char * path , const char * path_canonical );
70+ STATIC void append_dir_item (mp_obj_t dirlist , const char * item , bool string );
71+ STATIC void mount (mp_obj_t device , const char * path , uint pathlen , bool readonly );
72+
6573/******************************************************************************
6674 DEFINE PUBLIC FUNCTIONS
6775 ******************************************************************************/
@@ -102,6 +110,13 @@ os_fs_mount_t *osmount_find_by_device (mp_obj_t device) {
102110 return NULL ;
103111}
104112
113+ void osmount_unmount_all (void ) {
114+ for (mp_uint_t i = 0 ; i < MP_STATE_PORT (mount_obj_list ).len ; i ++ ) {
115+ os_fs_mount_t * mount_obj = ((os_fs_mount_t * )(MP_STATE_PORT (mount_obj_list ).items [i ]));
116+ unmount (mount_obj );
117+ }
118+ }
119+
105120/******************************************************************************
106121 DEFINE PRIVATE FUNCTIONS
107122 ******************************************************************************/
@@ -188,19 +203,11 @@ STATIC void mount (mp_obj_t device, const char *path, uint pathlen, bool readonl
188203 os_num_mounted_devices ++ ;
189204}
190205
191- STATIC void unmount (const char * path ) {
192- if (FR_OK != f_mount (NULL , path , 1 )) {
193- nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_operation_failed ));
194- }
195-
196- // remove from the list after the actual unmount
197- os_fs_mount_t * mount_obj ;
198- if ((mount_obj = osmount_find_by_path (path ))) {
199- mp_obj_list_remove (& MP_STATE_PORT (mount_obj_list ), mount_obj );
200- os_num_mounted_devices -- ;
201- } else {
202- nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
203- }
206+ STATIC void unmount (os_fs_mount_t * mount_obj ) {
207+ // remove it from the list and then call FatFs
208+ f_mount (NULL , mount_obj -> path , 1 );
209+ mp_obj_list_remove (& MP_STATE_PORT (mount_obj_list ), mount_obj );
210+ os_num_mounted_devices -- ;
204211}
205212
206213/******************************************************************************/
@@ -487,14 +494,20 @@ STATIC mp_obj_t os_unmount(mp_obj_t path_o) {
487494 }
488495
489496 // now unmount it
490- unmount (path );
497+ os_fs_mount_t * mount_obj ;
498+ if ((mount_obj = osmount_find_by_path (path ))) {
499+ unmount (mount_obj );
500+ } else {
501+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
502+ }
491503
492504 return mp_const_none ;
493505}
494506STATIC MP_DEFINE_CONST_FUN_OBJ_1 (os_unmount_obj , os_unmount );
495507
496508STATIC mp_obj_t os_mkfs (mp_obj_t device ) {
497509 const char * path = "/__mkfs__mnt__" ;
510+ os_fs_mount_t * mount_obj = NULL ;
498511 bool unmt = false;
499512 FRESULT res ;
500513
@@ -505,29 +518,26 @@ STATIC mp_obj_t os_mkfs(mp_obj_t device) {
505518 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_value_invalid_arguments ));
506519 }
507520 } else {
508- // mount it and unmount it briefly
509- unmt = true;
521+ // mount it briefly
510522 mount (device , path , strlen (path ), false);
523+ unmt = true;
511524 }
512525
513526 byte sfd = 0 ;
514527 if (!memcmp (path , "/flash" , strlen ("/flash" ))) {
515528 sfd = 1 ;
516- } else {
517- os_fs_mount_t * mount_obj ;
518- if ((mount_obj = osmount_find_by_path (path ))) {
519- if (mount_obj -> device != (mp_obj_t )& pybsd_obj &&
520- mp_obj_get_int (mp_call_method_n_kw (0 , 0 , mount_obj -> count )) < 2048 ) {
521- sfd = 1 ;
522- }
529+ } else if ((mount_obj = osmount_find_by_path (path ))) {
530+ if (mount_obj -> device != (mp_obj_t )& pybsd_obj &&
531+ mp_obj_get_int (mp_call_method_n_kw (0 , 0 , mount_obj -> count )) < 2048 ) {
532+ sfd = 1 ;
523533 }
524534 }
525535
526536 // now format the device
527537 res = f_mkfs (path , sfd , 0 );
528538
529- if (unmt ) {
530- unmount (path );
539+ if (unmt && mount_obj ) {
540+ unmount (mount_obj );
531541 }
532542
533543 if (res != FR_OK ) {
0 commit comments