vfs_posix_rename old_path points to the same as new_path
Hi,
During my investigation of vfs posix MPY 1.15, I found that there is issue in logic as following, although I have not tested it yet. Please refer to file https://github.com/micropython/micropython/blob/master/extmod/vfs_posix.c, line 271.
At first, old_path points to root buffer, member of mp_obj_vfs_posix_t .
After that, new_path also points to root buffer with new value "new_path_in". At this point, both old_path and new_path points to the same string in root buffer with the latest value "new_path_in".
So when executing rename(old_path, new_path) will be failed.
STATIC mp_obj_t vfs_posix_rename(mp_obj_t self_in, mp_obj_t old_path_in, mp_obj_t new_path_in) {
mp_obj_vfs_posix_t *self = MP_OBJ_TO_PTR(self_in);
const char *old_path = vfs_posix_get_path_str(self, old_path_in);
const char *new_path = vfs_posix_get_path_str(self, new_path_in);
MP_THREAD_GIL_EXIT();
int ret = rename(old_path, new_path);
MP_THREAD_GIL_ENTER();
if (ret != 0) {
mp_raise_OSError(errno);
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_3(vfs_posix_rename_obj, vfs_posix_rename);
FatFs's os.rename() is not POSIX compliant
Trying to do os.rename("old", "new") with FatFs fails if "new" already exists with OSError: [Errno 17] EEXIST. This is not POSIX compliant:
If newpath already exists it will be atomically replaced (subject to a few conditions; see ERRORS below), so that there is no point at which another process attempting to access newpath will find it missing.
(from man 2 rename)
https://docs.python.org/3/library/os.html#os.rename says on this:
Rename the file or directory src to dst. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. On Windows, if dst already exists, OSError will be raised even if it is a file.
So, we could leave it raising OSError, but that would be emulating Windows, and we should emulate POSIX apparently.