mirror of
https://github.com/Mathewnd/Astral.git
synced 2026-10-27 12:48:22 +00:00
fatfs: small optimization
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -59,6 +59,9 @@ typedef struct fatnode_t {
|
||||
|
||||
size_t size;
|
||||
|
||||
size_t saved_index;
|
||||
size_t saved_cluster;
|
||||
|
||||
struct fatnode_t *parent_dir;
|
||||
size_t dent_disk_offset;
|
||||
} fatnode_t;
|
||||
|
||||
Reference in New Issue
Block a user