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
12 type 'a handlertype = Nohandler | Activehandler of 'a | Maskedhandler of 'a
14 let wdmap = Hashtbl.create 1024
16 let fd = Inotify.init ()
18 let handle_dir_event dirname evlist str =
19 let fname = String.concat "/" [dirname;str] in
20 fprintf logfd "File: %s. " fname;List.iter
22 fprintf logfd "Event: %s\n" (string_of_event e))
26 let add_watch dir events handler =
27 let wd = Inotify.add_watch fd dir events in
28 Hashtbl.add wdmap wd (dir,Activehandler(handler))
31 let (dirname,handler) = try Hashtbl.find wdmap wd with Not_found->("",Nohandler)
34 | Activehandler(func)->
35 Hashtbl.replace wdmap wd (dirname,Maskedhandler(func))
39 let unmask_events wd =
40 let (dirname,handler) = try Hashtbl.find wdmap wd with Not_found->("",Nohandler)
43 | Maskedhandler(func)->
44 Hashtbl.replace wdmap wd (dirname,Activehandler(func))
51 fprintf logfd "Removing watch for %s\n" dir;flush logfd;
52 let wd = Inotify.rm_watch fd dir in
53 Hashtbl.remove wdmap wd
57 let rec findfirstnul str idx len =
59 (str.[idx]==(char_of_int 0))) then idx
61 findfirstnul str (idx+1) len
63 let nulterm = findfirstnul s 0 (String.length s) in
64 String.sub s 0 nulterm
66 let receive_event (eventdescriptor:fname_and_fd) (bla:fname_and_fd) =
67 let (_,fd) = eventdescriptor in
68 let evs = Inotify.read fd in
71 | (wd,evlist,_,Some(str)) ->
72 let purestr = asciiz(str) in
73 let (dirname,handler) =
74 try Hashtbl.find wdmap wd with Not_found->("",Nohandler)
78 | Nohandler->fprintf logfd "Unhandled watch descriptor\n";flush logfd
79 | Activehandler(handler)->handler wd dirname evlist purestr
80 | Maskedhandler(_)->()
86 Fdwatcher.add_fd (None,fd) (None,fd) receive_event