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?
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.
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?
I recommend opening a feature request in https://github.com/WebAssembly/wasi-filesystem/
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.
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
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?
Would it make sense for stream to have a skip?
for something like protobuf you sometimes want to interleave reads and seeks
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
wasi-io streams do already have a skip
wow - I thought it did and I could have sworn I looked; thanks
Seems like skip is probably sufficient for 90%+ of (read) seeks?
Though an implementer might feel like they can't implement skip as a seek based on "Behaves identical to read"?
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)
skip cant go backwards, seek can
Ah OK I was maybe looking at 0.3 before.
Oh duh, there was no builtin stream in 0.2 :face_palm:
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