Skip to content

Commit 9cc668b

Browse files
committed
Revert most changes. Only change web workflow writable
1 parent 95c4ffb commit 9cc668b

File tree

6 files changed

+24
-52
lines changed

6 files changed

+24
-52
lines changed

extmod/vfs_fat.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ STATIC mp_obj_t fat_vfs_make_new(const mp_obj_type_t *type, size_t n_args, size_
9898
return MP_OBJ_FROM_PTR(vfs);
9999
}
100100

101-
STATIC void filesystem_lock_raise(fs_user_mount_t *vfs) {
102-
if (!filesystem_lock(vfs)) {
101+
STATIC void verify_fs_writable(fs_user_mount_t *vfs) {
102+
if (!filesystem_is_writable_by_python(vfs)) {
103103
mp_raise_OSError(MP_EROFS);
104104
}
105105
}
@@ -218,6 +218,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fat_vfs_ilistdir_obj, 1, 2, fat_vfs_i
218218

219219
STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_int_t attr) {
220220
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
221+
verify_fs_writable(self);
221222
const char *path = mp_obj_str_get_str(path_in);
222223

223224
FILINFO fno;
@@ -229,9 +230,7 @@ STATIC mp_obj_t fat_vfs_remove_internal(mp_obj_t vfs_in, mp_obj_t path_in, mp_in
229230

230231
// check if path is a file or directory
231232
if ((fno.fattrib & AM_DIR) == attr) {
232-
filesystem_lock_raise(self);
233233
res = f_unlink(&self->fatfs, path);
234-
filesystem_unlock(self);
235234

236235
if (res != FR_OK) {
237236
mp_raise_OSError_fresult(res);
@@ -254,18 +253,17 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(fat_vfs_rmdir_obj, fat_vfs_rmdir);
254253

255254
STATIC mp_obj_t fat_vfs_rename(mp_obj_t vfs_in, mp_obj_t path_in, mp_obj_t path_out) {
256255
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
256+
verify_fs_writable(self);
257257
const char *old_path = mp_obj_str_get_str(path_in);
258258
const char *new_path = mp_obj_str_get_str(path_out);
259259

260-
filesystem_lock_raise(self);
261260
FRESULT res = f_rename(&self->fatfs, old_path, new_path);
262261
if (res == FR_EXIST) {
263262
// if new_path exists then try removing it (but only if it's a file)
264263
fat_vfs_remove_internal(vfs_in, path_out, 0); // 0 == file attribute
265264
// try to rename again
266265
res = f_rename(&self->fatfs, old_path, new_path);
267266
}
268-
filesystem_unlock(self);
269267
if (res == FR_OK) {
270268
return mp_const_none;
271269
} else {
@@ -277,10 +275,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_rename_obj, fat_vfs_rename);
277275

278276
STATIC mp_obj_t fat_vfs_mkdir(mp_obj_t vfs_in, mp_obj_t path_o) {
279277
mp_obj_fat_vfs_t *self = MP_OBJ_TO_PTR(vfs_in);
278+
verify_fs_writable(self);
280279
const char *path = mp_obj_str_get_str(path_o);
281-
filesystem_lock_raise(self);
282280
FRESULT res = f_mkdir(&self->fatfs, path);
283-
filesystem_unlock(self);
284281
if (res == FR_OK) {
285282
return mp_const_none;
286283
} else {
@@ -466,11 +463,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(fat_vfs_utime_obj, vfs_fat_utime);
466463

467464
STATIC mp_obj_t vfs_fat_getreadonly(mp_obj_t self_in) {
468465
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
469-
bool writable = filesystem_lock(self);
470-
if (writable) {
471-
filesystem_unlock(self);
472-
}
473-
return mp_obj_new_bool(!writable);
466+
return mp_obj_new_bool(!filesystem_is_writable_by_python(self));
474467
}
475468
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getreadonly_obj, vfs_fat_getreadonly);
476469
STATIC const mp_obj_property_t fat_vfs_readonly_obj = {
@@ -494,10 +487,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(fat_vfs_getlabel_obj, vfs_fat_getlabel);
494487

495488
STATIC mp_obj_t vfs_fat_setlabel(mp_obj_t self_in, mp_obj_t label_in) {
496489
fs_user_mount_t *self = MP_OBJ_TO_PTR(self_in);
490+
verify_fs_writable(self);
497491
const char *label_str = mp_obj_str_get_str(label_in);
498-
filesystem_lock_raise(self);
499492
FRESULT res = f_setlabel(&self->fatfs, label_str);
500-
filesystem_unlock(self);
501493
if (res != FR_OK) {
502494
if (res == FR_WRITE_PROTECTED) {
503495
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("Read-only filesystem"));

extmod/vfs_fat.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ MP_DECLARE_CONST_FUN_OBJ_3(fat_vfs_open_obj);
5050
typedef struct _pyb_file_obj_t {
5151
mp_obj_base_t base;
5252
FIL fp;
53-
// CIRCUITPY-CHANGE: We need to unlock the fs on file close.
54-
fs_user_mount_t *fs_mount;
5553
} pyb_file_obj_t;
5654

5755
#endif // MICROPY_INCLUDED_EXTMOD_VFS_FAT_H

extmod/vfs_fat_file.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ STATIC mp_uint_t file_obj_ioctl(mp_obj_t o_in, mp_uint_t request, uintptr_t arg,
127127
} else if (request == MP_STREAM_CLOSE) {
128128
// if fs==NULL then the file is closed and in that case this method is a no-op
129129
if (self->fp.obj.fs != NULL) {
130-
if (self->fp.flag & FA_WRITE) {
131-
filesystem_unlock(self->fs_mount);
132-
}
133130
FRESULT res = f_close(&self->fp);
134131
if (res != FR_OK) {
135132
*errcode = fresult_to_errno_table[res];
@@ -246,14 +243,13 @@ STATIC mp_obj_t fat_vfs_open(mp_obj_t self_in, mp_obj_t path_in, mp_obj_t mode_i
246243
}
247244

248245
assert(self != NULL);
249-
if ((mode & FA_WRITE) != 0 && !filesystem_lock(self)) {
246+
if ((mode & FA_WRITE) != 0 && !filesystem_is_writable_by_python(self)) {
250247
mp_raise_OSError(MP_EROFS);
251248
}
252249

253250

254251
pyb_file_obj_t *o = m_new_obj_with_finaliser(pyb_file_obj_t);
255252
o->base.type = type;
256-
o->fs_mount = self;
257253

258254
const char *fname = mp_obj_str_get_str(path_in);
259255
FRESULT res = f_open(&self->fatfs, &o->fp, fname, mode);

supervisor/filesystem.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void filesystem_set_internal_concurrent_write_protection(bool concurrent_write_p
4242
void filesystem_set_writable_by_usb(fs_user_mount_t *vfs, bool usb_writable);
4343
void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concurrent_write_protection);
4444

45+
// Whether user code can modify the filesystem. It doesn't depend on the state
46+
// of USB. Don't use this for a workflow. In workflows, grab the shared file
47+
// system lock.
48+
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs);
49+
4550
// This controls whether USB tries to grab the underlying block device lock
4651
// during enumeration. If another workflow is modifying the filesystem when this
4752
// happens, then USB will be readonly.
@@ -52,7 +57,7 @@ fs_user_mount_t *filesystem_for_path(const char *path_in, const char **path_unde
5257
bool filesystem_native_fatfs(fs_user_mount_t *fs_mount);
5358

5459
// We have two levels of locking. filesystem_* calls grab a shared blockdev lock to allow
55-
// CircuitPython's fatfs code to edit the blocks. blockdev_* class grab a lock to mutate blocks
60+
// CircuitPython's fatfs code to edit the blocks. blockdev_* calls grab a lock to mutate blocks
5661
// directly, excluding any filesystem_* locks.
5762

5863
bool filesystem_lock(fs_user_mount_t *fs_mount);

supervisor/shared/filesystem.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ void filesystem_set_writable_by_usb(fs_user_mount_t *vfs, bool usb_writable) {
208208
}
209209
}
210210

211+
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs) {
212+
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
213+
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) == 0;
214+
}
215+
211216
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs) {
212217
return (vfs->blockdev.flags & MP_BLOCKDEV_FLAG_CONCURRENT_WRITE_PROTECTED) == 0 ||
213218
(vfs->blockdev.flags & MP_BLOCKDEV_FLAG_USB_WRITABLE) != 0;

supervisor/stub/filesystem.c

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ void filesystem_set_writable_by_usb(fs_user_mount_t *vfs, bool usb_writable) {
5555
return;
5656
}
5757

58+
bool filesystem_is_writable_by_python(fs_user_mount_t *vfs) {
59+
(void)vfs;
60+
return true;
61+
}
62+
5863
bool filesystem_is_writable_by_usb(fs_user_mount_t *vfs) {
5964
return true;
6065
}
@@ -73,32 +78,3 @@ void filesystem_set_concurrent_write_protection(fs_user_mount_t *vfs, bool concu
7378
bool filesystem_present(void) {
7479
return false;
7580
}
76-
77-
bool filesystem_lock(fs_user_mount_t *fs_mount) {
78-
if (fs_mount->lock_count == 0 && !blockdev_lock(fs_mount)) {
79-
return false;
80-
}
81-
fs_mount->lock_count += 1;
82-
return true;
83-
}
84-
85-
void filesystem_unlock(fs_user_mount_t *fs_mount) {
86-
assert(fs_mount->lock_count > 0);
87-
fs_mount->lock_count -= 1;
88-
if (fs_mount->lock_count == 0) {
89-
blockdev_unlock(fs_mount);
90-
}
91-
}
92-
93-
bool blockdev_lock(fs_user_mount_t *fs_mount) {
94-
if ((fs_mount->blockdev.flags & MP_BLOCKDEV_FLAG_LOCKED) != 0) {
95-
return false;
96-
}
97-
fs_mount->blockdev.flags |= MP_BLOCKDEV_FLAG_LOCKED;
98-
return true;
99-
}
100-
101-
void blockdev_unlock(fs_user_mount_t *fs_mount) {
102-
assert(fs_mount->blockdev.flags & MP_BLOCKDEV_FLAG_LOCKED);
103-
fs_mount->blockdev.flags &= ~MP_BLOCKDEV_FLAG_LOCKED;
104-
}

0 commit comments

Comments
 (0)