Skip to content

Commit 9e13e8e

Browse files
committed
Moved folder moving inside itself checks into f_rename
1 parent e85bb9f commit 9e13e8e

File tree

3 files changed

+19
-29
lines changed

3 files changed

+19
-29
lines changed

extmod/vfs_fat.c

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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_
225225
const char *old_path = mp_obj_str_get_str(path_in);
226226
const char *new_path = mp_obj_str_get_str(path_out);
227227

228-
// Check to see if we're moving a directory into itself. This occurs when we're moving a
229-
// directory where the old path is a prefix of the new and the next character is a "/" and thus
230-
// preserves the original directory name.
231-
FILINFO fno;
232-
FRESULT res = f_stat(&self->fatfs, old_path, &fno);
233-
if (res != FR_OK) {
234-
mp_raise_OSError_fresult(res);
235-
}
236-
if ((fno.fattrib & AM_DIR) != 0 &&
237-
strlen(new_path) > strlen(old_path) &&
238-
new_path[strlen(old_path)] == '/' &&
239-
strncmp(old_path, new_path, strlen(old_path)) == 0) {
240-
mp_raise_OSError(MP_EINVAL);
241-
}
242-
243-
res = f_rename(&self->fatfs, old_path, new_path);
228+
FRESULT res = f_rename(&self->fatfs, old_path, new_path);
244229
if (res == FR_EXIST) {
245230
// if new_path exists then try removing it (but only if it's a file)
246231
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_
249234
}
250235
if (res == FR_OK) {
251236
return mp_const_none;
237+
} else if (res == FR_NO_PATH) {
238+
mp_raise_OSError(MP_EINVAL);
252239
} else {
253240
mp_raise_OSError_fresult(res);
254241
}

lib/oofatfs/ff.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4816,6 +4816,21 @@ FRESULT f_rename (
48164816
DEF_NAMBUF
48174817

48184818

4819+
// Check to see if we're moving a directory into itself. This occurs when we're moving a
4820+
// directory where the old path is a prefix of the new and the next character is a "/" and thus
4821+
// preserves the original directory name.
4822+
FILINFO fno;
4823+
res = f_stat(fs, path_old, &fno);
4824+
if (res != FR_OK) {
4825+
return res;
4826+
}
4827+
if ((fno.fattrib & AM_DIR) != 0 &&
4828+
strlen(path_new) > strlen(path_old) &&
4829+
path_new[strlen(path_old)] == '/' &&
4830+
strncmp(path_old, path_new, strlen(path_old)) == 0) {
4831+
return FR_NO_PATH;
4832+
}
4833+
48194834
res = find_volume(fs, FA_WRITE); /* Get logical drive of the old object */
48204835
if (res == FR_OK) {
48214836
djo.obj.fs = fs;

supervisor/shared/web_workflow/web_workflow.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,19 +1080,7 @@ static bool _reply(socketpool_socket_obj_t *socket, _request *request) {
10801080
destination[destinationlen - 1] = '\0';
10811081
}
10821082

1083-
// Check to see if we're moving a directory into itself.
1084-
FILINFO file;
1085-
FRESULT result = f_stat(fs, path, &file);
1086-
if (result == FR_OK) {
1087-
if ((file.fattrib & AM_DIR) != 0 &&
1088-
strlen(destination) > strlen(path) &&
1089-
destination[strlen(path)] == '/' &&
1090-
strncmp(path, destination, strlen(path)) == 0) {
1091-
result = FR_NO_PATH;
1092-
} else {
1093-
result = f_rename(fs, path, destination);
1094-
}
1095-
}
1083+
FRESULT result = f_rename(fs, path, destination);
10961084
#if CIRCUITPY_USB_MSC
10971085
usb_msc_unlock();
10981086
#endif

0 commit comments

Comments
 (0)