From e0821830b044f736619906e1b4f974b7d43263db Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 29 Feb 2016 01:22:38 +0200 Subject: [PATCH] extmod/vfs_fat: Add .rename() method. --- extmod/vfs_fat.c | 16 ++++++++++++++++ py/qstrdefs.h | 1 + 2 files changed, 17 insertions(+) diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index d21f4812e9..c52adfbe08 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -89,6 +89,21 @@ STATIC mp_obj_t fat_vfs_remove(mp_obj_t vfs_in, mp_obj_t path_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_remove_obj, fat_vfs_remove); +STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) { + (void)vfs_in; + const char *old_path = mp_obj_str_get_str(path_in); + const char *new_path = mp_obj_str_get_str(path_out); + FRESULT res = f_rename(old_path, new_path); + switch (res) { + case FR_OK: + return mp_const_none; + default: + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error renaming file '%s' to '%s'", old_path, new_path)); + } + +} +STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename); + STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) { (void)vfs_in; const char *path = mp_obj_str_get_str(path_o); @@ -111,6 +126,7 @@ STATIC const mp_rom_map_elem_t fat_vfs_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_listdir), MP_ROM_PTR(&fat_vfs_listdir_obj) }, { MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&fat_vfs_mkdir_obj) }, { MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&fat_vfs_remove_obj) }, + { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&fat_vfs_rename_obj) }, }; STATIC MP_DEFINE_CONST_DICT(fat_vfs_locals_dict, fat_vfs_locals_dict_table); diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 93eca3e823..c983569706 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -719,6 +719,7 @@ Q(umount) Q(readonly) Q(mkfs) Q(listdir) +Q(rename) Q(readblocks) Q(writeblocks) Q(ioctl)