diff options
author | Damien George <damien.p.george@gmail.com> | 2016-12-02 15:06:09 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2016-12-02 15:06:09 +1100 |
commit | b7df3e541a49a1cb72862c4015b6a883a63e1e29 (patch) | |
tree | 34c338dbb2ea6da52398feccc1b17672abe92608 /extmod/vfs_fat.c | |
parent | 08bd7d1d31a95331dad403adce8834c2d39ab3e8 (diff) |
extmod/vfs_fat: Implement POSIX behaviour of rename, allow to overwrite.
If the destination of os.rename() exists then it will be overwritten if it
is a file. This is the POSIX behaviour, which is also the CPython
behaviour, and so we follow suit.
See issue #2598 for discussion.
Diffstat (limited to 'extmod/vfs_fat.c')
-rw-r--r-- | extmod/vfs_fat.c | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/extmod/vfs_fat.c b/extmod/vfs_fat.c index 6e827fc66..bd88bcf1b 100644 --- a/extmod/vfs_fat.c +++ b/extmod/vfs_fat.c @@ -121,6 +121,12 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_ 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); + if (res == FR_EXIST) { + // if new_path exists then try removing it (but only if it's a file) + fat_vfs_remove_internal(path_out, 0); // 0 == file attribute + // try to rename again + res = f_rename(old_path, new_path); + } if (res == FR_OK) { return mp_const_none; } else { |