package inotify

  1. Overview
  2. Docs
Inotify bindings for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v2.4.1.tar.gz
md5=5bb1c5754d305acf9c1f10611f49aae2
sha512=3e114ee0e8b5b9c7c996df0d2cd8f03e0efdfb9837c1990f98c256868c0e2ad275f91be1adcfbbbcf1bdab801f18e82efa483510d2566e0d12cb303dfc91e4e5

doc/src/inotify.lwt/lwt_inotify.ml.html

Source file lwt_inotify.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
open Lwt

type t = {
  queue   : Inotify.event Queue.t;
  unix_fd : Unix.file_descr;
  lwt_fd  : Lwt_unix.file_descr;
}

let create () =
  try
    let unix_fd = Inotify.create () in
    return {
      queue   = Queue.create ();
      lwt_fd  = Lwt_unix.of_unix_file_descr unix_fd;
      unix_fd; }
  with exn ->
    Lwt.fail exn

let add_watch inotify path selector =
  try
    return (Inotify.add_watch inotify.unix_fd path selector)
  with exn ->
    Lwt.fail exn

let rm_watch inotify wd =
  try
    return (Inotify.rm_watch inotify.unix_fd wd)
  with exn ->
    Lwt.fail exn

let rec read inotify =
  try
    return (Queue.take inotify.queue)
  with Queue.Empty ->
    Lwt_unix.wait_read inotify.lwt_fd >>= fun () ->
    begin try
      let events = Inotify.read inotify.unix_fd in
      List.iter (fun event -> Queue.push event inotify.queue) events;
      return_unit
    with exn ->
      Lwt.fail exn
    end >>= fun () ->
    read inotify

let rec try_read inotify =
  try
    return (Some (Queue.take inotify.queue))
  with Queue.Empty ->
    if Lwt_unix.readable inotify.lwt_fd
    then read inotify >|= fun x -> Some x
    else return_none

let close inotify =
  Lwt_unix.close inotify.lwt_fd