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 print "Masking %s %d\n" fqp (String.length fqp);
38 Hashtbl.replace masks fqp true
42 let unmask_watch fqp =
43 if (Hashtbl.mem masks fqp) then
45 Hashtbl.remove masks fqp
48 logprint "WARNING: %s -- Unpaired unmask\n" fqp
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->logprint "Unhandled watch descriptor\n"
74 let fqp = String.concat "/" [dirname;purestr] in
75 let mask_filter = Hashtbl.mem masks fqp in
77 (*print "Received event for - %s\n"
79 if ((not mask_filter) && false) then
80 handler wd dirname evlist purestr
82 print "%s is masked\n" fqp
89 Fdwatcher.add_fd (None,fd) (None,fd) receive_event