Skip to content

Commit 7c004e7

Browse files
robert-hhpfalcon
authored andcommitted
extmod/vfs_fat*: Replace text error messages by POSIX error numbers.
These changes are in line with similar changes in other modules, and with standard Python interface.
1 parent 751e3b7 commit 7c004e7

2 files changed

Lines changed: 18 additions & 21 deletions

File tree

extmod/vfs_fat.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
7979
const char *path = mp_obj_str_get_str(path_in);
8080
// TODO check that path is actually a file before trying to unlink it
8181
FRESULT res = f_unlink(path);
82-
switch (res) {
83-
case FR_OK:
84-
return mp_const_none;
85-
default:
86-
// TODO: standard errno's
87-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error removing file '%s'", path));
82+
if (res == FR_OK) {
83+
return mp_const_none;
84+
} else {
85+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
86+
MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
8887
}
8988
}
9089
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
@@ -94,11 +93,11 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_
9493
const char *old_path = mp_obj_str_get_str(path_in);
9594
const char *new_path = mp_obj_str_get_str(path_out);
9695
FRESULT res = f_rename(old_path, new_path);
97-
switch (res) {
98-
case FR_OK:
99-
return mp_const_none;
100-
default:
101-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error renaming file '%s' to '%s'", old_path, new_path));
96+
if (res == FR_OK) {
97+
return mp_const_none;
98+
} else {
99+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
100+
MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
102101
}
103102

104103
}
@@ -108,14 +107,11 @@ STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
108107
(void)vfs_in;
109108
const char *path = mp_obj_str_get_str(path_o);
110109
FRESULT res = f_mkdir(path);
111-
switch (res) {
112-
case FR_OK:
113-
return mp_const_none;
114-
case FR_EXIST:
115-
// TODO should be FileExistsError
116-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "File exists: '%s'", path));
117-
default:
118-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error creating directory '%s'", path));
110+
if (res == FR_OK) {
111+
return mp_const_none;
112+
} else {
113+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
114+
MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
119115
}
120116
}
121117
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_mkdir_obj, fat_vfs_mkdir);

extmod/vfs_fat_misc.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ mp_obj_t fat_vfs_listdir(const char *path, bool is_str_type) {
5454

5555
res = f_opendir(&dir, path); /* Open the directory */
5656
if (res != FR_OK) {
57-
// TODO should be mp_type_FileNotFoundError
58-
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "No such file or directory: '%s'", path));
57+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError,
58+
MP_OBJ_NEW_SMALL_INT(fresult_to_errno_table[res])));
59+
5960
}
6061

6162
mp_obj_t dir_list = mp_obj_new_list(0, NULL);

0 commit comments

Comments
 (0)