Merge pull request #6883 from makermelissa/main

Prevent folder from trying to move inside itself
This commit is contained in:
Dan Halbert 2022-10-12 19:09:29 -04:00 committed by GitHub
commit 9540aed6a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 16 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

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_INVALID_NAME;
}
res = find_volume(fs, FA_WRITE); /* Get logical drive of the old object */
if (res == FR_OK) {
djo.obj.fs = fs;

View File

@ -70,6 +70,11 @@ try:
except OSError as e:
print(e.errno == uerrno.ENOENT)
try:
vfs.rename("foo_dir", "foo_dir/inside_itself")
except OSError as e:
print(e.errno == uerrno.EINVAL)
# file in dir
with open("foo_dir/file-in-dir.txt", "w+t") as f:
f.write("data in file")

View File

@ -1,6 +1,7 @@
True
True
True
True
b'data in file'
True
[('sub_file.txt', 32768, 0, 11), ('file.txt', 32768, 0, 12)]