Stream: wasi

Topic: Seeking files in preview 3?


view this post on Zulip mainrs (May 12 2025 at 12:17):

As far as I can tell, there is no way to seek files (move the reading position forward or backwards) in WASI. Are there plans to include such an API in preview 3? If not, is there a possibility for a RFC process?

view this post on Zulip bjorn3 (May 12 2025 at 12:44):

Wasip2 doesn't have file positions at all I believe. You have to specify the position you want to start at when opening a read or write stream. As such changing file positions would need to be implemented in wasi-libc, which may be blocked on getting rid of wasi-libc's dependency on the wasip1 to wasip2 adapter.

view this post on Zulip mainrs (May 12 2025 at 12:58):

bjorn3 said:

Wasip2 doesn't have file positions at all I believe. You have to specify the position you want to start at when opening a read or write stream. As such changing file positions would need to be implemented in wasi-libc, which may be blocked on getting rid of wasi-libc's dependency on the wasip1 to wasip2 adapter.

My use case are small information extraction services that get read-only access to the file to extract metadata from them. I was hoping that they can be implemented efficiently using seeking. Else I would have to read the whole file.

There are some file formats that store metadata in the middle or at the end of the file. Or anywhere in between. Is there any other approach I can take except reading the whole file and doing dummy reads to simulate seeking?

Would opening new file streams at certain positions to simulate a single seek operation be worth it or is the overhead to high for that?

view this post on Zulip Lann Martin (May 12 2025 at 15:01):

I recommend opening a feature request in https://github.com/WebAssembly/wasi-filesystem/

Filesystem API for WASI. Contribute to WebAssembly/wasi-filesystem development by creating an account on GitHub.

view this post on Zulip bjorn3 (May 12 2025 at 15:30):

I think what you want is pread support rather than seeking support. Pread allows reading at an arbitrary position without updating the position. Wasi doesn't support pread right now however.

view this post on Zulip Joel Dice (May 12 2025 at 15:36):

Note that WASIp3 has a descriptor.read-via-stream method which takes an offset from which reading should start: https://github.com/WebAssembly/wasi-filesystem/blob/b17eb760d2f7f35c37752dcc636dc0806ac95185/wit-0.3.0-draft/types.wit#L295-L308

Filesystem API for WASI. Contribute to WebAssembly/wasi-filesystem development by creating an account on GitHub.

view this post on Zulip Joel Dice (May 12 2025 at 15:37):

I believe that could be considered a pread equivalent. Or is there something else that pread does which is not covered by the current API?

view this post on Zulip Lann Martin (May 12 2025 at 19:53):

Would it make sense for stream to have a skip?

view this post on Zulip Lann Martin (May 12 2025 at 19:55):

for something like protobuf you sometimes want to interleave reads and seeks

view this post on Zulip Lann Martin (May 12 2025 at 19:57):

it seems like that could even replace the offset there; it would be a slightly more complex implementation but could apply to other stream types

view this post on Zulip Dan Gohman (May 14 2025 at 16:52):

wasi-io streams do already have a skip

I/O Types proposal for WASI. Contribute to WebAssembly/wasi-io development by creating an account on GitHub.

view this post on Zulip Lann Martin (May 14 2025 at 17:07):

wow - I thought it did and I could have sworn I looked; thanks

view this post on Zulip Lann Martin (May 14 2025 at 17:08):

Seems like skip is probably sufficient for 90%+ of (read) seeks?

view this post on Zulip Lann Martin (May 14 2025 at 17:10):

Though an implementer might feel like they can't implement skip as a seek based on "Behaves identical to read"?

view this post on Zulip Pat Hickey (May 14 2025 at 18:42):

right, wasi-io streams have skip, but i dont think that the cm3 streams do - my recollection was those dropped extra features like skip, write-zeroes, splice to be post 0.3.0 (coming in some 0.3.x)

view this post on Zulip Pat Hickey (May 14 2025 at 18:43):

skip cant go backwards, seek can

view this post on Zulip Lann Martin (May 14 2025 at 19:18):

Ah OK I was maybe looking at 0.3 before.

view this post on Zulip Lann Martin (May 14 2025 at 19:25):

Oh duh, there was no builtin stream in 0.2 :face_palm:

view this post on Zulip Joel Dice (May 14 2025 at 20:08):

For p3 descriptors, seeking backwards or forwards just means dropping the current stream and calling descriptor.read-via-stream again, and should be roughly as efficient as e.g. fseek if implemented properly on the host side, I believe. In any case, once we add native p3 support to wasi-libc we'll find out how well pread, fseek etc. map to the new WASI interfaces.


Last updated: Dec 06 2025 at 06:05 UTC