2 * Copyright (C) 2006 Vincent Hanquez <vincent@snarc.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; version 2 only.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * Inotify OCaml binding
56 let string_of_event = function
59 | Close_write -> "CLOSE_WRITE"
60 | Close_nowrite -> "CLOSE_NOWRITE"
63 | Delete_self -> "DELETE_SELF"
65 | Move_self -> "MOVE_SELF"
66 | Moved_from -> "MOVED_FROM"
67 | Moved_to -> "MOVED_TO"
69 | Ignored -> "IGNORED"
71 | Q_overflow -> "Q_OVERFLOW"
72 | Unmount -> "UNMOUNT"
77 type event = wd * type_event list * int32 * string option
79 external init : unit -> Unix.file_descr = "stub_inotify_init"
80 external add_watch : Unix.file_descr -> string -> select_event list -> wd
81 = "stub_inotify_add_watch"
82 external rm_watch : Unix.file_descr -> wd -> unit = "stub_inotify_rm_watch"
83 external convert : string -> (wd * type_event list * int32 * int)
84 = "stub_inotify_convert"
85 external struct_size : unit -> int = "stub_inotify_struct_size"
87 external to_read : Unix.file_descr -> int = "stub_inotify_ioctl_fionread"
90 let ss = struct_size () in
91 let toread = to_read fd in
94 let buf = String.make toread '\000' in
95 let toread = Unix.read fd buf 0 toread in
101 let wd, l, cookie, len = convert (String.sub buf !i ss) in
102 let s = if len > 0 then Some (String.sub buf (!i + ss) len) else None in
103 ret := (wd, l, cookie, s) :: !ret;
104 i := !i + (ss + len);