fatfs: small optimization

This commit is contained in:
Matheus
2025-10-04 20:04:37 -03:00
parent 2eb7d332c2
commit 5814a33cb6
5 changed files with 20 additions and 3 deletions
+1
View File
@@ -956,6 +956,7 @@ fatnode_t *fatfs_allocate_node(vfs_t *vfs, int type) {
fatnode->cluster = 0;
fatnode->parent_dir = NULL;
fatnode->dent_disk_offset = 0;
fatnode->saved_cluster = 0;
return fatnode;
}
-1
View File
@@ -477,7 +477,6 @@ int fatfs_update_dent(fatfs_t *fatfs, fatnode_t *fatnode) {
}
static int get_free_dents(fatfs_t *fatfs, fatnode_t *fatnode, size_t dent_count, size_t *dent_offset) {
// TODO add a first free search
size_t offset = 0;
fatfs_dent_t dent;
size_t free_count = 0;
+4
View File
@@ -326,6 +326,7 @@ int fatfs_resize_file(fatfs_t *fatfs, fatnode_t *fatnode, size_t new_size) {
return error;
fatnode->cluster = 0;
fatnode->saved_cluster = 0;
} else {
// truncate a part of the file
fatfs_cluster_t cluster;
@@ -335,6 +336,9 @@ int fatfs_resize_file(fatfs_t *fatfs, fatnode_t *fatnode, size_t new_size) {
__assert(cluster != FATFS_CLUSTER_FREE && fatfs_is_cluster_eof(fatfs, cluster) == false);
error = fatfs_cut_chain(fatfs, cluster);
// no need to update the saved index/cluster as the previour fatfs_get_cluster_from_index call will have
// set the index and cluster properly
}
if (error == 0) {
+12 -2
View File
@@ -1,9 +1,13 @@
#include <kernel/fatfs.h>
#include <logging.h>
// TODO optimize this by keeping some metadata in the fatfs node
int fatfs_get_cluster_from_index(fatfs_t *fs, fatnode_t *node, uintmax_t index, fatfs_cluster_t *cluster) {
fatfs_cluster_t iterator = node->cluster;
if (node->saved_cluster == 0 || node->saved_index > index) {
node->saved_cluster = node->cluster;
node->saved_index = 0;
}
fatfs_cluster_t iterator = node->saved_cluster;
// empty file?
if (iterator == 0) {
@@ -42,6 +46,12 @@ int fatfs_get_cluster_from_index(fatfs_t *fs, fatnode_t *node, uintmax_t index,
}
}
if (fatfs_is_cluster_eof(fs, iterator) == false) {
// update the last found index
node->saved_index = index;
node->saved_cluster = iterator;
}
*cluster = iterator;
return 0;
}