Newer
Older
Tardis / src / Filesystem.cpp
@Jookia Jookia on 19 Mar 2023 1 KB Initial commit
#include "mbed.h"
#include <errno.h>
#include <functional>
#include "Filesystem.h"
#include "MainBD.h"

// Maximum number of elements in buffer
#define BUFFER_MAX_LEN 10
#define FORCE_REFORMAT false

// function to attempt to mount the filesystem and
// reformat and mount it again should it fail
void mountFilesystem(mbed::BlockDevice& bd, LittleFileSystem2& fs) {
	int err;
	// Try to mount the filesystem
	printf("Mounting the filesystem... ");
	fflush(stdout);
	err = fs.mount(&bd);
	printf("%s\n", (err ? "Fail :(" : "OK"));
	if (err || FORCE_REFORMAT) {
		// Reformat if we can't mount the filesystem
		printf("formatting... ");
		fflush(stdout);
		err = fs.reformat(&bd);
		printf("%s\n", (err ? "Fail :(" : "OK"));
		if (err) {
			error("error: %s (%d)\n", strerror(-err), err);
		}
	// Try to mount the filesystem again
	printf("Mounting the filesystem... ");
	fflush(stdout);
	err = fs.mount(&bd);
	printf("%s\n", (err ? "Fail :(" : "OK"));
		if (err) {
			error("error: %s (%d)\n", strerror(-err), err);
		}
	}
}

// function to unmount the filesystem
void unmountFilesystem(mbed::BlockDevice& bd, LittleFileSystem2& fs) {
	// Try to unmount the filesystem
	printf("Unmounting the filesystem... ");
	fflush(stdout);
	int err = fs.unmount();
	printf("%s\n", (err ? "Fail :(" : "OK"));
	if (err) {
		error("error: %s (%d)\n", strerror(-err), err);
	}
}