summaryrefslogtreecommitdiffstatshomepage
path: root/extmod/vfs_fat.c
diff options
context:
space:
mode:
authorAlex March <alex.march.dev@gmail.com>2016-09-28 14:51:35 +0100
committerDamien George <damien.p.george@gmail.com>2016-10-11 16:03:52 +1100
commitd02f3a57f47b44bf31769fb089f1f18060ea3b4d (patch)
tree32cf154e12468baf2afc3661c2e9ad4a9f2d7329 /extmod/vfs_fat.c
parenteaef6b5324fa2ff425802d4abeea45aa945bfc14 (diff)
downloadmicropython-d02f3a57f47b44bf31769fb089f1f18060ea3b4d.tar.gz
micropython-d02f3a57f47b44bf31769fb089f1f18060ea3b4d.zip
extmod/vfs_fat: Add file and directory checks for remove and rmdir.
Diffstat (limited to 'extmod/vfs_fat.c')
-rw-r--r--extmod/vfs_fat.c39
1 files changed, 29 insertions, 10 deletions
diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c
index 3a17533e43..6e827fc664 100644
--- a/extmod/vfs_fat.c
+++ b/extmod/vfs_fat.c
@@ -31,6 +31,7 @@
#include <string.h>
#include "py/nlr.h"
#include "py/runtime.h"
+#include "py/mperrno.h"
#include "lib/fatfs/ff.h"
#include "lib/fatfs/diskio.h"
#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) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_listdir_obj, 1, 2, fat_vfs_listdir_func);
-STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
- (void)vfs_in;
+STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t path_in, mp_int_t attr) {
const char *path = mp_obj_str_get_str(path_in);
- // TODO check that path is actually a file before trying to unlink it
- FRESULT res = f_unlink(path);
- if (res == FR_OK) {
+
+ FILINFO fno;
+#if _USE_LFN
+ fno.lfname = NULL;
+ fno.lfsize = 0;
+#endif
+ FRESULT res = f_stat(path, &fno);
+
+ if (res != FR_OK) {
+ mp_raise_OSError(fresult_to_errno_table[res]);
+ }
+
+ // check if path is a file or directory
+ if ((fno.fattrib & AM_DIR) == attr) {
+ res = f_unlink(path);
+
+ if (res != FR_OK) {
+ mp_raise_OSError(fresult_to_errno_table[res]);
+ }
return mp_const_none;
} else {
- mp_raise_OSError(fresult_to_errno_table[res]);
+ mp_raise_OSError(attr ? MP_ENOTDIR : MP_EISDIR);
}
}
+
+STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) {
+ (void)vfs_in;
+ return fat_vfs_remove_internal(path_in, 0); // 0 == file attribute
+}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove);
STATIC mp_obj_t fat_vfs_rmdir(mp_obj_t vfs_in, mp_obj_t path_in) {
- // TODO: Currently just redirects to fat_vfs_remove(), which are
- // backed by the same underlying FatFs function. Should at least
- // check that path is actually a dir.
- return fat_vfs_remove(vfs_in, path_in);
+ (void) vfs_in;
+ return fat_vfs_remove_internal(path_in, AM_DIR);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);