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 logprint "File: %s. " fname;
26 logprint "Event: %s\n" (string_of_event e))
29 let add_watch dir events handler =
30 let wd = Inotify.add_watch fd dir events in
31 Hashtbl.add wdmap wd (dir,Some(handler))
32 (* Ignore the possibility that the whole directory can disappear and come
33 * back while it is masked *)
37 Hashtbl.replace masks fqp true
41 let unmask_watch fqp =
42 if (Hashtbl.mem masks fqp) then
44 Hashtbl.remove masks fqp
47 logprint "WARNING: %s -- Unpaired unmask\n" fqp
50 let rec findfirstnul str idx len =
52 (str.[idx]==(char_of_int 0))) then idx
54 findfirstnul str (idx+1) len
56 let nulterm = findfirstnul s 0 (String.length s) in
57 String.sub s 0 nulterm
59 let receive_event (eventdescriptor:fname_and_fd) (bla:fname_and_fd) =
60 let (_,fd) = eventdescriptor in
61 let evs = Inotify.read fd in
64 | (wd,evlist,_,Some(str)) ->
66 let purestr = asciiz(str) in
67 let (dirname,handler) =
68 try Hashtbl.find wdmap wd with Not_found->("",None)
71 | None->logprint "Unhandled watch descriptor\n"
73 let fqp = String.concat "/" [dirname;purestr] in
74 let mask_filter = Hashtbl.mem masks fqp in
76 if ((not mask_filter)) then
78 (*logprint "Received event for - %s\n"
80 handler wd dirname evlist
91 Fdwatcher.add_fd (None,fd) (None,fd) receive_event