let list_directory path =
let f_stream = Lwt_unix.files_of_directory path in
let next s =
wrap_deferred ~on_exn:(fun e -> `System (`List_directory path, `Exn e))
Lwt.(fun () ->
catch (fun () -> Lwt_stream.next s >>= fun n -> return (Some n))
(function Lwt_stream.Empty -> return None | e -> fail e)) in
`Stream (fun () -> (next f_stream))