Skip to content

Commit b781e65

Browse files
committed
codal_port/microbitfs: 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. Fixes issue #105. Signed-off-by: Damien George <[email protected]>
1 parent 0f0766f commit b781e65

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/codal_port/microbitfs.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
* THE SOFTWARE.
2626
*/
2727

28-
// This is a copy of the file micropython:ports/nrf/modules/uos/microbitfs.c with
29-
// a call to `microbit_file_opened_for_writing` added in `microbit_file_open`.
28+
// This is a copy of the file micropython:ports/nrf/modules/uos/microbitfs.c with:
29+
// - a call to `microbit_file_opened_for_writing` added in `microbit_file_open`
30+
// - a fix to `find_chunk_and_erase` to sweep the filesystem if any free chunks are found
3031

3132
#include <string.h>
3233
#include <stdio.h>
@@ -83,10 +84,6 @@
8384
/** Must be such that sizeof(file_header) < DATA_PER_CHUNK */
8485
#define MAX_FILENAME_LENGTH 120
8586

86-
//Minimum number of free chunks to justify sweeping.
87-
//If this is too low it may cause excessive wear
88-
#define MIN_CHUNKS_FOR_SWEEP (FLASH_PAGESIZE / CHUNK_SIZE)
89-
9087
#define FILE_NOT_FOUND ((uint8_t)-1)
9188

9289
/** Maximum number of chunks allowed in filesystem. 252 chunks is 31.5kB */
@@ -271,9 +268,9 @@ STATIC uint8_t microbit_find_file(const char *name, int name_len) {
271268
// Search the chunks:
272269
// 1 If an UNUSED chunk is found, then return that.
273270
// 2. If an entire page of FREED chunks is found, then erase the page and return the first chunk
274-
// 3. If the number of FREED chunks is >= MIN_CHUNKS_FOR_SWEEP, then
271+
// 3. If the number of FREED chunks is > 0, then
275272
// 3a. Sweep the filesystem and restart.
276-
// 3b. Fail and return FILE_NOT_FOUND
273+
// 3b. Otherwise, fail and return FILE_NOT_FOUND.
277274
//
278275
STATIC uint8_t find_chunk_and_erase(void) {
279276
// Start search at a random chunk to spread the wear more evenly.
@@ -314,7 +311,7 @@ STATIC uint8_t find_chunk_and_erase(void) {
314311
if (index == chunks_in_file_system+1) index = 1;
315312
} while (index != start_index);
316313
DEBUG(("FILE DEBUG: %lu free chunks\r\n", freed_chunks));
317-
if (freed_chunks < MIN_CHUNKS_FOR_SWEEP) {
314+
if (freed_chunks == 0) {
318315
return FILE_NOT_FOUND;
319316
}
320317
// No freed pages, so sweep file system.

0 commit comments

Comments
 (0)