| 1 |
(* |
| 2 |
* Copyright (C) 2006-2008 Vincent Hanquez <[email protected]> |
| 3 |
* |
| 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.1 only. with the special |
| 7 |
* exception on linking described in file LICENSE. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* Inotify OCaml binding |
| 15 |
*) |
| 16 |
|
| 17 |
type selector = |
| 18 |
| S_Access |
| 19 |
| S_Attrib |
| 20 |
| S_Close_write |
| 21 |
| S_Close_nowrite |
| 22 |
| S_Create |
| 23 |
| S_Delete |
| 24 |
| S_Delete_self |
| 25 |
| S_Modify |
| 26 |
| S_Move_self |
| 27 |
| S_Moved_from |
| 28 |
| S_Moved_to |
| 29 |
| S_Open |
| 30 |
| S_Dont_follow |
| 31 |
| S_Mask_add |
| 32 |
| S_Oneshot |
| 33 |
| S_Onlydir |
| 34 |
(* convenience *) |
| 35 |
| S_Move |
| 36 |
| S_Close |
| 37 |
| S_All |
| 38 |
|
| 39 |
type event_kind = |
| 40 |
| Access |
| 41 |
| Attrib |
| 42 |
| Close_write |
| 43 |
| Close_nowrite |
| 44 |
| Create |
| 45 |
| Delete |
| 46 |
| Delete_self |
| 47 |
| Modify |
| 48 |
| Move_self |
| 49 |
| Moved_from |
| 50 |
| Moved_to |
| 51 |
| Open |
| 52 |
| Ignored |
| 53 |
| Isdir |
| 54 |
| Q_overflow |
| 55 |
| Unmount |
| 56 |
|
| 57 |
let string_of_event_kind = function |
| 58 |
| Access -> "ACCESS" |
| 59 |
| Attrib -> "ATTRIB" |
| 60 |
| Close_write -> "CLOSE_WRITE" |
| 61 |
| Close_nowrite -> "CLOSE_NOWRITE" |
| 62 |
| Create -> "CREATE" |
| 63 |
| Delete -> "DELETE" |
| 64 |
| Delete_self -> "DELETE_SELF" |
| 65 |
| Modify -> "MODIFY" |
| 66 |
| Move_self -> "MOVE_SELF" |
| 67 |
| Moved_from -> "MOVED_FROM" |
| 68 |
| Moved_to -> "MOVED_TO" |
| 69 |
| Open -> "OPEN" |
| 70 |
| Ignored -> "IGNORED" |
| 71 |
| Isdir -> "ISDIR" |
| 72 |
| Q_overflow -> "Q_OVERFLOW" |
| 73 |
| Unmount -> "UNMOUNT" |
| 74 |
|
| 75 |
type watch = int |
| 76 |
type event = watch * event_kind list * int32 * string option |
| 77 |
|
| 78 |
external create : unit -> Unix.file_descr = "caml_inotify_init" |
| 79 |
external add_watch : Unix.file_descr -> string -> selector list -> watch |
| 80 |
= "caml_inotify_add_watch" |
| 81 |
external rm_watch : Unix.file_descr -> watch -> unit = "caml_inotify_rm_watch" |
| 82 |
|
| 83 |
external convert : Bytes.t -> (watch * event_kind list * int32 * int) |
| 84 |
= "caml_inotify_convert" |
| 85 |
external struct_size : unit -> int = "caml_inotify_struct_size" |
| 86 |
external name_max : unit -> int = "caml_inotify_name_max" |
| 87 |
|
| 88 |
let int_of_watch watch = watch |
| 89 |
|
| 90 |
let watch_of_int watch = watch |
| 91 |
|
| 92 |
let string_of_event (watch, events, cookie, name) = |
| 93 |
Printf.sprintf "watch=%d cookie=%ld events=%s%s" |
| 94 |
watch cookie |
| 95 |
(String.concat "|" (List.map string_of_event_kind events)) |
| 96 |
(match name with |
| 97 |
| None -> "" |
| 98 |
| Some name' -> Printf.sprintf " %S" name') |
| 99 |
|
| 100 |
let read fd = |
| 101 |
(* Turns out that reading from blocking descriptors always requires a buffer |
| 102 |
of the maximum size, which is, from the inotify man page: |
| 103 |
|
| 104 |
The behavior when the buffer given to read(2) is too small to return |
| 105 |
information about the next event depends on the kernel version: in |
| 106 |
kernels before 2.6.21, read(2) returns 0; since kernel 2.6.21, |
| 107 |
read(2) fails with the error EINVAL. Specifying a buffer of size |
| 108 |
|
| 109 |
sizeof(struct inotify_event) + NAME_MAX + 1 |
| 110 |
*) |
| 111 |
let event_size = struct_size () in |
| 112 |
|
| 113 |
let buf_size = event_size + (name_max ()) + 1 in |
| 114 |
let buf = Bytes.create buf_size in |
| 115 |
let bytes_read = Unix.read fd buf 0 buf_size in |
| 116 |
|
| 117 |
let read_c_string pos = |
| 118 |
Bytes.sub_string buf pos ((Bytes.index_from buf pos '\x00') - pos) |
| 119 |
in |
| 120 |
|
| 121 |
let rec read_one pos rest = |
| 122 |
if bytes_read < pos + event_size then rest |
| 123 |
else |
| 124 |
let watch, mask, cookie, len = convert (Bytes.sub buf pos event_size) in |
| 125 |
if bytes_read < pos + event_size + len then rest |
| 126 |
else |
| 127 |
let name = if len > 0 then Some (read_c_string (pos + event_size)) else None in |
| 128 |
read_one (pos + event_size + len) ((watch, mask, cookie, name) :: rest) |
| 129 |
in |
| 130 |
|
| 131 |
List.rev (read_one 0 []) |
| 132 |
|
| 133 |
external supported_by_the_os : unit -> bool = "caml_inotify_is_supported" |