v86 icon indicating copy to clipboard operation
v86 copied to clipboard

read_file returns null for empty file

Open stasoid opened this issue 3 years ago • 1 comments

If an empty file is created by a command, FS.read_file returns null instead of empty Uint8Array. Empty files created by create_file doesn't have this issue.

This happens because of this line in get_data, but I don't know if it can be changed to return new Uint8Array. There is similar code in get_buffer, which also claims to @return {!Promise<Uint8Array>}.

How to reproduce: execute command touch /mnt/f in the following VM, then press read f button. Result: null is printed to js console.

<!doctype html>
<script src="../build/libv86.js"></script>

<script>
onload = function()
{
	emulator = new V86Starter({
		wasm_path:  "../build/v86.wasm",
		bios:       { url: "../bios/seabios.bin" },
		vga_bios:   { url: "../bios/vgabios.bin" },
		bzimage:    { url: "../images/buildroot-bzimage.bin" },
		filesystem: {},
		screen_container: screen_container,
		autostart: true
	});
}
async function read_f()
{
	let data = await emulator.fs9p.read_file("f");
	console.log(data);
}
</script>

<!-- A minimal structure for the ScreenAdapter defined in browser/screen.js -->
<div id=screen_container>
	<div style="white-space: pre; font: 14px monospace"></div>
	<canvas></canvas>
</div>

<button onclick="read_f()">read f</button>

stasoid avatar May 22 '22 18:05 stasoid

This happens because of this line in get_data, but I don't know if it can be changed to return new Uint8Array. There is similar code in get_buffer, which also claims to @return {!Promise<Uint8Array>}.

I don't think it can be changed to return new Uint8Array, since that would be returned for non-existing files. Rather, CreateFile should ensure that inodedata is set (similar Linux's behavious, where close(open(..., O_CREAT)) creates an empty file).

A PR (with a test case showing the current incorrect behaviour, see https://github.com/copy/v86/blob/master/tests/devices/virtio_9p.js) would be welcome.

copy avatar May 23 '22 11:05 copy