8000 Prevent other filesystem changes besides file writes · rhwlo/circuitpython@6c01aba · GitHub
[go: up one dir, main page]

Skip to content

Commit 6c01aba

Browse files
committed
Prevent other filesystem changes besides file writes
Such as moving files, adding directories and changing the label.
1 parent 7482870 commit 6c01aba

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

extmod/vfs_fat.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "lib/oofatfs/ff.h"
4040
#include "extmod/vfs_fat.h"
4141
#include "lib/timeutils/timeutils.h"
42-
42+
#include "supervisor/filesystem.h"
4343
#include "supervisor/shared/translate.h"
4444

4545
#if _MAX_SS == _MIN_SS
@@ -99,6 +99,12 @@ STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, const
9999
return MP_OBJ_FROM_PTR(vfs);
100100
}
101101

102+
STATIC void verify_fs_writable(fs_user_mount_t *vfs) {
103+
if (!filesystem_is_writable_by_python(vfs)) {
104+
mp_raise_OSError(MP_EROFS);
105+
}
106+
}
107+
102108
#if _FS_REENTRANT
103109
STATIC mp_obj_t fat_vfs_del(mp_obj_t self_in) {
104110
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(self_in);
@@ -201,6 +207,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_i
201207

202208
STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
203209
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
210+
verify_fs_writable(self);
204211
const char *path = mp_obj_str_get_str(path_in);
205212

206213
FILINFO fno;
@@ -235,6 +242,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
235242

236243
STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) {
237244
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
245+
verify_fs_writable(self);
238246
const char *old_path = mp_obj_str_get_str(path_in);
239247
const char *new_path = mp_obj_str_get_str(path_out);
240248

@@ -271,6 +279,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
271279

272280
STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
273281
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
282+
verify_fs_writable(self);
274283
const char *path = mp_obj_str_get_str(path_o);
275284
FRESULT res = f_mkdir(&self->fatfs, path);
276285
if (res == FR_OK) {
@@ -435,6 +444,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getlabel_obj, vfs_fat_getlabel);
435444

436445
STATIC mp_obj_t vfs_fat_setlabel(mp_obj_t self_in, mp_obj_t label_in) {
437446
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
447+
verify_fs_writable(self);
438448
const char *label_str = mp_obj_str_get_str(label_in);
439449
FRESULT res = f_setlabel(&self->fatfs, label_str);
440450
if (res != FR_OK) {

0 commit comments

Comments
 (0)
0