|
31 | 31 | #include <string.h> |
32 | 32 | #include "py/nlr.h" |
33 | 33 | #include "py/runtime.h" |
| 34 | +#include "py/mperrno.h" |
34 | 35 | #include "lib/fatfs/ff.h" |
35 | 36 | #include "lib/fatfs/diskio.h" |
36 | 37 | #include "extmod/vfs_fat_file.h" |
@@ -76,24 +77,42 @@ STATIC mp_obj_t fat_vfs_listdir_func(size_t n_args, const mp_obj_t *args) { |
76 | 77 | } |
77 | 78 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_listdir_obj, 1, 2, fat_vfs_listdir_func); |
78 | 79 |
|
79 | | -STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) { |
80 | | - (void)vfs_in; |
| 80 | +STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) { |
81 | 81 | const char *path = mp_obj_str_get_str(path_in); |
82 | | - // TODO check that path is actually a file before trying to unlink it |
83 | | - FRESULT res = f_unlink(path); |
84 | | - if (res == FR_OK) { |
| 82 | + |
| 83 | + FILINFO fno; |
| 84 | +#if _USE_LFN |
| 85 | + fno.lfname = NULL; |
| 86 | + fno.lfsize = 0; |
| 87 | +#endif |
| 88 | + FRESULT res = f_stat(path, &fno); |
| 89 | + |
| 90 | + if (res != FR_OK) { |
| 91 | + mp_raise_OSError(fresult_to_errno_table[res]); |
| 92 | + } |
| 93 | + |
| 94 | + // check if path is a file or directory |
| 95 | + if ((fno.fattrib & AM_DIR) == attr) { |
| 96 | + res = f_unlink(path); |
| 97 | + |
| 98 | + if (res != FR_OK) { |
| 99 | + mp_raise_OSError(fresult_to_errno_table[res]); |
| 100 | + } |
85 | 101 | return mp_const_none; |
86 | 102 | } else { |
87 | | - mp_raise_OSError(fresult_to_errno_table[res]); |
| 103 | + mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR); |
88 | 104 | } |
89 | 105 | } |
| 106 | + |
| 107 | +STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) { |
| 108 | + (void)vfs_in; |
| 109 | + return fat_vfs_remove_internal(path_in, 0); // 0 == file attribute |
| 110 | +} |
90 | 111 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove); |
91 | 112 |
|
92 | 113 | STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) { |
93 | | - // TODO: Currently just redirects to fat_vfs_remove(), which are |
94 | | - // backed by the same underlying FatFs function. Should at least |
95 | | - // check that path is actually a dir. |
96 | | - return fat_vfs_remove(vfs_in, path_in); |
| 114 | + (void) vfs_in; |
| 115 | + return fat_vfs_remove_internal(path_in, AM_DIR); |
97 | 116 | } |
98 | 117 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir); |
99 | 118 |
|
|
0 commit comments