Change vfs mount ordering such that the root is always last in the

linked list. Its also the only one statically allocated and made
available over USB.
This commit is contained in:
Scott Shawcroft 2017-06-28 14:46:49 -07:00
parent 5ddbf26b62
commit d6a24afd71
3 changed files with 19 additions and 23 deletions

View File

@ -42,15 +42,13 @@
#define VFS_INDEX 0 #define VFS_INDEX 0
// The root FS is always at the end of the list.
static fs_user_mount_t* get_vfs(int index) { static fs_user_mount_t* get_vfs(int index) {
mp_vfs_mount_t* current_mount = MP_STATE_VM(vfs_mount_table); mp_vfs_mount_t* current_mount = MP_STATE_VM(vfs_mount_table);
for (uint8_t i = 0; current_mount != NULL; i++) { while (current_mount->next != NULL) {
if (i == VFS_INDEX) {
return (fs_user_mount_t *) current_mount->obj;
}
current_mount = current_mount->next; current_mount = current_mount->next;
} }
return NULL; return current_mount->obj;
} }
//! This function tests memory state, and starts memory initialization //! This function tests memory state, and starts memory initialization

View File

@ -117,18 +117,22 @@ void mp_init(void) {
#if MICROPY_VFS #if MICROPY_VFS
#if MICROPY_FATFS_NUM_PERSISTENT > 0 #if MICROPY_FATFS_NUM_PERSISTENT > 0
// We preserve the last MICROPY_FATFS_NUM_PERSISTENT mounts because newer
// mounts are put at the front of the list.
mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table); mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table);
if (vfs != NULL) { // Count how many mounts we have.
MP_STATE_VM(vfs_cur) = vfs; uint8_t count = 0;
} while (vfs != NULL) {
// Skip forward until the vfs->next pointer is the first vfs that shouldn't
// persist.
uint8_t i = 1;
while (vfs != NULL && i < MICROPY_FATFS_NUM_PERSISTENT) {
vfs = vfs->next; vfs = vfs->next;
i++; count++;
} }
vfs->next = NULL; // Find the vfs MICROPY_FATFS_NUM_PERSISTENT mounts from the end.
vfs = MP_STATE_VM(vfs_mount_table);
for (uint8_t j = 0; j < count - MICROPY_FATFS_NUM_PERSISTENT; j++) {
vfs = vfs->next;
}
MP_STATE_VM(vfs_mount_table) = vfs;
MP_STATE_VM(vfs_cur) = vfs;
#else #else
// initialise the VFS sub-system // initialise the VFS sub-system
MP_STATE_VM(vfs_cur) = NULL; MP_STATE_VM(vfs_cur) = NULL;

View File

@ -78,16 +78,10 @@ void common_hal_storage_mount(mp_obj_t vfs_obj, const char* mount_path, bool rea
} }
} }
// insert the vfs into the mount table // Insert the vfs into the mount table by pushing it onto the front of the
// mount table.
mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table); mp_vfs_mount_t **vfsp = &MP_STATE_VM(vfs_mount_table);
while (*vfsp != NULL) { vfs->next = *vfsp;
if ((*vfsp)->len == 1) {
// make sure anything mounted at the root stays at the end of the list
vfs->next = *vfsp;
break;
}
vfsp = &(*vfsp)->next;
}
*vfsp = vfs; *vfsp = vfs;
} }