From 5ebaca3698e515d374484a17a4af1c917ad57c73 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 24 Aug 2022 12:12:16 +1000 Subject: [PATCH] microbit: Sweep the filesystem if any free chunks found. If there are any free chunks found then it's better to sweep the filesystem and use the available chunks, rather than error out with ENOSPC when there is in fact a bit of space remaining. Signed-off-by: Damien George --- inc/microbit/filesystem.h | 4 ---- source/microbit/filesystem.c | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/inc/microbit/filesystem.h b/inc/microbit/filesystem.h index 27ba103fb..f552a612d 100644 --- a/inc/microbit/filesystem.h +++ b/inc/microbit/filesystem.h @@ -76,10 +76,6 @@ typedef struct _file_descriptor_obj { /** Must be such that sizeof(file_header) < DATA_PER_CHUNK */ #define MAX_FILENAME_LENGTH 120 -//Minimum number of free chunks to justify sweeping. -//If this is too low it may cause excessive wear -#define MIN_CHUNKS_FOR_SWEEP 8 - typedef struct _file_header { uint8_t end_offset; uint8_t name_len; diff --git a/source/microbit/filesystem.c b/source/microbit/filesystem.c index 480596cd0..24a1da3e4 100644 --- a/source/microbit/filesystem.c +++ b/source/microbit/filesystem.c @@ -202,9 +202,9 @@ uint8_t microbit_find_file(const char *name, int name_len) { * Search the chunks: * 1 If an UNUSED chunk is found, then return that. * 2. If an entire page of FREED chunks is found, then erase the page and return the first chunk - * 3. If the number of FREED chunks is >= MIN_FREE_CHUNKS_FOR_SWEEP, then + * 3. If the number of FREED chunks is > 0, then * 3a. Sweep the filesystem and restart. - * 3b. Fail and return FILE_NOT_FOUND + * 3b. Otherwise, fail and return FILE_NOT_FOUND. */ static uint8_t find_chunk_and_erase(void) { // Start search at a random chunk to spread the wear more evenly. @@ -245,7 +245,7 @@ static uint8_t find_chunk_and_erase(void) { if (index == chunks_in_file_system+1) index = 1; } while (index != start_index); DEBUG(("FILE DEBUG: %lu free chunks\r\n", freed_chunks)); - if (freed_chunks < MIN_CHUNKS_FOR_SWEEP) { + if (freed_chunks == 0) { return FILE_NOT_FOUND; } // No freed pages, so sweep file system.