Skip to content

Commit 070c865

Browse files
microbit-rosslowemicrobit-carlos
authored andcommitted
upy-fs-hex: Add methods to get used and remaining FS space (#10)
1 parent 44968ec commit 070c865

File tree

2 files changed

+62
-10
lines changed

2 files changed

+62
-10
lines changed

src/__tests__/micropython-fs-hex.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ describe('Test create method.', () => {
9595
});
9696
});
9797

98+
describe('Test size operations.', () => {
99+
it('Creates new files.', () => {
100+
const microbitFs = new MicropythonFsHex(uPyHexFile);
101+
102+
expect(microbitFs.getStorageUsed()).toEqual(0);
103+
expect(microbitFs.getStorageRemaining()).toEqual(27648);
104+
105+
microbitFs.create('chunk1.py', 'first 128 byte chunk');
106+
107+
expect(microbitFs.getStorageUsed()).toEqual(128);
108+
expect(microbitFs.getStorageRemaining()).toEqual(27648 - 128);
109+
110+
microbitFs.create('chunk2.py', 'second 128 byte chunk');
111+
112+
expect(microbitFs.getStorageUsed()).toEqual(256);
113+
expect(microbitFs.getStorageRemaining()).toEqual(27648 - 256);
114+
115+
microbitFs.create('chunk3.py', new Uint8Array(26800).fill(0x60));
116+
117+
expect(microbitFs.getStorageUsed()).toEqual(27648 - 128);
118+
expect(microbitFs.getStorageRemaining()).toEqual(128);
119+
120+
microbitFs.create('chunk4.py', 'fourth chunk');
121+
122+
expect(microbitFs.getStorageUsed()).toEqual(27648);
123+
expect(microbitFs.getStorageRemaining()).toEqual(0);
124+
});
125+
});
126+
98127
describe('Test write method.', () => {
99128
it('Throw error with invalid file name.', () => {
100129
const micropythonFs = new MicropythonFsHex(uPyHexFile);
@@ -504,7 +533,7 @@ describe('Test MicroPython hex filesystem size.', () => {
504533
it('Get how much available fs space there is in a MicroPython hex file.', () => {
505534
const micropythonFs = new MicropythonFsHex(uPyHexFile);
506535

507-
const totalSize = micropythonFs.getFsSize();
536+
const totalSize = micropythonFs.getStorageSize();
508537

509538
// Calculated by hand from the uPyHexFile v1.0.1 release.
510539
expect(totalSize).toEqual(27 * 1024);

src/micropython-fs-hex.ts

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,38 @@ export class MicropythonFsHex implements FsInterface {
169169
return files;
170170
}
171171

172+
/**
173+
* Calculate the MicroPython filesystem total size.
174+
*
175+
* @returns Size of the filesystem in bytes.
176+
*/
177+
getStorageSize(): number {
178+
return getIntelHexFsSize(this._intelHex);
179+
}
180+
181+
/**
182+
* @returns The total number of bytes currently used by files in the file system.
183+
*/
184+
getStorageUsed(): number {
185+
let total: number = 0;
186+
Object.values(this._files).forEach(
187+
(value) => (total += this.size(value.filename))
188+
);
189+
return total;
190+
}
191+
192+
/**
193+
* @returns The remaining storage of the file system in bytes.
194+
*/
195+
getStorageRemaining(): number {
196+
let total: number = 0;
197+
const capacity: number = this.getStorageSize();
198+
Object.values(this._files).forEach(
199+
(value) => (total += this.size(value.filename))
200+
);
201+
return capacity - total;
202+
}
203+
172204
/**
173205
* Read the files included in a MicroPython hex string and add them to this
174206
* instance.
@@ -228,13 +260,4 @@ export class MicropythonFsHex implements FsInterface {
228260
});
229261
return addIntelHexFiles(finalHex, files);
230262
}
231-
232-
/**
233-
* Calculate the MicroPython filesystem total size.
234-
*
235-
* @returns Size of the filesystem in bytes.
236-
*/
237-
getFsSize(): number {
238-
return getIntelHexFsSize(this._intelHex);
239-
}
240263
}

0 commit comments

Comments
 (0)