Skip to content

Commit f7545b2

Browse files
committed
stmhal/moduos: Implement POSIX behaviour of rename, allow to overwrite.
1 parent b7df3e5 commit f7545b2

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

stmhal/moduos.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ STATIC mp_obj_t os_rename(mp_obj_t path_in, mp_obj_t path_out) {
183183
const char *old_path = mp_obj_str_get_str(path_in);
184184
const char *new_path = mp_obj_str_get_str(path_out);
185185
FRESULT res = f_rename(old_path, new_path);
186+
if (res == FR_EXIST) {
187+
// if new_path exists then try removing it
188+
res = f_unlink(new_path);
189+
if (res == FR_OK) {
190+
// try to rename again
191+
res = f_rename(old_path, new_path);
192+
}
193+
}
186194
switch (res) {
187195
case FR_OK:
188196
return mp_const_none;

0 commit comments

Comments
 (0)