dbuenzli / bos

Basic OS interaction for OCaml
http://erratique.ch/software/bos
ISC License
64 stars 16 forks source link

OS.File.read: support streaming files #79

Closed rizo closed 5 years ago

rizo commented 6 years ago

This adds support for reading the content of character devices and named pipes with Bos.OS.File.read. Of course, feel free to make any changes you see necessary, or reject entirely.

Note that currently Bos.OS.File.read_lines already supports those files (indirectly via Pervasives.input_line), so this would make Bos.OS.File.read consistent with that behaviour.

There's an obvious risk of a potential memory leak in case someone uses a device that does not terminate (i.e. /dev/random or mkfifo pipe; yes > pipe). But I think this is generally understandable.

The implementation relies on Unix.stat to obtain the file kind. It's called twice (when the file is opened and when it's closed). This (neglectable?) cost could be avoided, but I didn't want to change the code too much.

rizo commented 5 years ago

This is now ready for review, @dbuenzli.

As discussed in #78, the read function decides how to input the file by checking the length with lseek and catching ESPIPE.

dbuenzli commented 5 years ago

Thanks your patch is in as 61526bf67f218fd730db8ac1914994