1 (** Watches directories for events. Agnostic to vsys semantics of backends and
8 (* I don't know if a wd corresponding to a deleted directory is evicted or just
9 * leaks - fix implementation of rmdir accordingly
11 let wdmap = Hashtbl.create 1024
12 let masks = Hashtbl.create 1024
14 let fd = Inotify.init ()
16 let rec list_check lst elt =
19 | car::cdr -> if (car==elt) then true else list_check cdr elt
21 let handle_dir_event dirname evlist str =
22 let fname = String.concat "/" [dirname;str] in
23 fprintf logfd "File: %s. " fname;List.iter
25 fprintf logfd "Event: %s\n" (string_of_event e))
29 let add_watch dir events handler =
30 let evcheck = list_check events in
31 let wd = Inotify.add_watch fd dir events in
32 Hashtbl.add wdmap wd (dir,Some(handler))
33 (* Ignore the possibility that the whole directory can disappear and come
34 * back while it is masked *)
38 Hashtbl.replace masks fqp true
42 let unmask_watch fqp =
43 if (Hashtbl.mem masks fqp) then
45 Hashtbl.remove masks fqp
48 fprintf logfd "WARNING: %s -- Unpaired unmask\n" fqp;flush logfd
51 let rec findfirstnul str idx len =
53 (str.[idx]==(char_of_int 0))) then idx
55 findfirstnul str (idx+1) len
57 let nulterm = findfirstnul s 0 (String.length s) in
58 String.sub s 0 nulterm
60 let receive_event (eventdescriptor:fname_and_fd) (bla:fname_and_fd) =
61 let (_,fd) = eventdescriptor in
62 let evs = Inotify.read fd in
65 | (wd,evlist,_,Some(str)) ->
67 let purestr = asciiz(str) in
68 let (dirname,handler) =
69 try Hashtbl.find wdmap wd with Not_found->("",None)
72 | None->fprintf logfd "Unhandled watch descriptor\n";flush logfd
74 let fqp = String.concat "/" [dirname;purestr] in
75 let mask_filter = Hashtbl.mem masks fqp in
76 if (not mask_filter) then
78 handler wd dirname evlist purestr
85 Fdwatcher.add_fd (None,fd) (None,fd) receive_event