Skip to content

Add filesystem limit. #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/board/fs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Size as per C implementation.
const maxSize = 31.5 * 1024;

export class FileSystem {
// Each entry is an FsFile object. The indexes are used as identifiers.
// When a file is deleted the entry becomes ['', null] and can be reused.
private _content: Array<FsFile | null> = [];
private _size = 0;

create(name: string) {
let free_idx = -1;
Expand Down Expand Up @@ -49,21 +53,29 @@ export class FileSystem {
}

remove(idx: number) {
this._content[idx] = null;
const file = this._content[idx];
if (file) {
this._size -= file.size();
this._content[idx] = null;
}
}

readbyte(idx: number, offset: number) {
const file = this._content[idx];
return file ? file.readbyte(offset) : -1;
}

write(idx: number, data: Uint8Array) {
write(idx: number, data: Uint8Array, force: boolean = false): boolean {
const file = this._content[idx];
if (!file) {
throw new Error("File must exist");
}
if (!force && this._size + data.length > maxSize) {
return false;
}
this._size += data.length;
file.append(data);
return data.length;
return true;
}

clear() {
Expand Down
39 changes: 25 additions & 14 deletions src/board/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ export class Board {
*/
private modulePromise: Promise<ModuleWrapper> | undefined;
/**
* Flag to trigger a reset after start finishes.
* If undefined, then when main finishes we stay stopped.
* Otherwise we perform the action then clear this field.
*/
private resetWhenDone: boolean = false;
private afterStopped: (() => void) | undefined;

constructor(
private notifications: Notifications,
Expand Down Expand Up @@ -339,17 +340,20 @@ export class Board {
if (panicCode !== undefined) {
this.displayPanic(panicCode);
} else {
if (this.resetWhenDone) {
this.resetWhenDone = false;
if (this.afterStopped) {
this.afterStopped();
this.afterStopped = undefined;
setTimeout(() => this.start(), 0);
} else {
this.displayStoppedState();
}
}
}

async stop(reset: boolean = false): Promise<void> {
this.resetWhenDone = reset;
async stop(
afterStopped: (() => void) | undefined = undefined
): Promise<void> {
this.afterStopped = afterStopped;
if (this.panicTimeout) {
clearTimeout(this.panicTimeout);
this.panicTimeout = null;
Expand All @@ -366,17 +370,24 @@ export class Board {
}

async reset(): Promise<void> {
this.stop(true);
const noChangeRestart = () => {};
this.stop(noChangeRestart);
}

async flash(filesystem: Record<string, Uint8Array>): Promise<void> {
await this.stop();
this.fs.clear();
Object.entries(filesystem).forEach(([name, value]) => {
const idx = this.fs.create(name);
this.fs.write(idx, value);
});
this.dataLogging.delete();
const flashFileSystem = () => {
this.fs.clear();
Object.entries(filesystem).forEach(([name, value]) => {
const idx = this.fs.create(name);
this.fs.write(idx, value, true);
});
this.dataLogging.delete();
};
if (this.modulePromise) {
// If it's running then we need to stop before flash.
return this.stop(flashFileSystem);
}
flashFileSystem();
return this.start();
}

Expand Down
8 changes: 8 additions & 0 deletions src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
);
break;
}
case "internal_error": {
console.error("Simulator internal error:");
console.error(e.data.error);
break;
}
default: {
// Ignore unknown message
}
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/jshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int mp_js_hal_filesystem_name(int idx, char *buf);
int mp_js_hal_filesystem_size(int idx);
void mp_js_hal_filesystem_remove(int idx);
int mp_js_hal_filesystem_readbyte(int idx, size_t offset);
int mp_js_hal_filesystem_write(int idx, const char *buf, size_t len);
bool mp_js_hal_filesystem_write(int idx, const char *buf, size_t len);

void mp_js_hal_panic(int code);
void mp_js_hal_reset(void);
Expand Down
2 changes: 1 addition & 1 deletion src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mergeInto(LibraryManager.library, {
},

mp_js_hal_filesystem_name: function (idx, buf) {
const name = fs.name(idx);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was recently broken by #51.
This PR seems to be working well so I'm going to go go ahead and merge shortly.

const name = Module.fs.name(idx);
if (name === undefined) {
return -1;
}
Expand Down
7 changes: 6 additions & 1 deletion src/microbitfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ STATIC mp_uint_t microbit_file_write(mp_obj_t self_in, const void *buf, mp_uint_
*errcode = MP_EBADF;
return MP_STREAM_ERROR;
}
return mp_js_hal_filesystem_write(self->idx, buf, size);
bool success = mp_js_hal_filesystem_write(self->idx, buf, size);
if (!success) {
*errcode = MP_ENOSPC;
return MP_STREAM_ERROR;
}
return size;
}

STATIC mp_uint_t microbit_file_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
Expand Down