Moved folder moving inside itself checks into f_rename

This commit is contained in:
Melissa LeBlanc-Williams 2022-09-13 08:42:35 -07:00
parent e85bb9febb
commit 9e13e8e991
3 changed files with 19 additions and 29 deletions

View File

@ -225,22 +225,7 @@ 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);
// Check to see if we're moving a directory into itself. This occurs when we're moving a
// directory where the old path is a prefix of the new and the next character is a "/" and thus
// preserves the original directory name.
FILINFO fno;
FRESULT res = f_stat(&self->fatfs, old_path, &fno);
if (res != FR_OK) {
mp_raise_OSError_fresult(res);
}
if ((fno.fattrib & AM_DIR) != 0 &&
strlen(new_path) > strlen(old_path) &&
new_path[strlen(old_path)] == '/' &&
strncmp(old_path, new_path, strlen(old_path)) == 0) {
mp_raise_OSError(MP_EINVAL);
}
res = f_rename(&self->fatfs, old_path, new_path);
FRESULT res = f_rename(&self->fatfs, 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(vfs_in, path_out, 0); // 0 == file attribute
@ -249,6 +234,8 @@ STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_
}
if (res == FR_OK) {
return mp_const_none;
} else if (res == FR_NO_PATH) {
mp_raise_OSError(MP_EINVAL);
} else {
mp_raise_OSError_fresult(res);
}

View File

@ -4816,6 +4816,21 @@ FRESULT f_rename (
DEF_NAMBUF
// Check to see if we're moving a directory into itself. This occurs when we're moving a
// directory where the old path is a prefix of the new and the next character is a "/" and thus
// preserves the original directory name.
FILINFO fno;
res = f_stat(fs, path_old, &fno);
if (res != FR_OK) {
return res;
}
if ((fno.fattrib & AM_DIR) != 0 &&
strlen(path_new) > strlen(path_old) &&
path_new[strlen(path_old)] == '/' &&
strncmp(path_old, path_new, strlen(path_old)) == 0) {
return FR_NO_PATH;
}
res = find_volume(fs, FA_WRITE); /* Get logical drive of the old object */
if (res == FR_OK) {
djo.obj.fs = fs;

View File

@ -1080,19 +1080,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
destination[destinationlen - 1] = '\0';
}
// Check to see if we're moving a directory into itself.
FILINFO file;
FRESULT result = f_stat(fs, path, &file);
if (result == FR_OK) {
if ((file.fattrib & AM_DIR) != 0 &&
strlen(destination) > strlen(path) &&
destination[strlen(path)] == '/' &&
strncmp(path, destination, strlen(path)) == 0) {
result = FR_NO_PATH;
} else {
result = f_rename(fs, path, destination);
}
}
FRESULT result = f_rename(fs, path, destination);
#if CIRCUITPY_USB_MSC
usb_msc_unlock();
#endif