Skip 0-sized files in the shell version
[dups.git] / dups.ml
diff --git a/dups.ml b/dups.ml
index 6628d99..2521b97 100644 (file)
--- a/dups.ml
+++ b/dups.ml
@@ -283,51 +283,48 @@ end = struct
   end
 
   let lord t ~njobs ~vassals ~ic ~ocs =
-    eprintf "[debug] [lord] started\n%!";
     let active_vassals = ref njobs in
     let results = Queue.create () in
-    let rec dispatch () =
-      match Ipc.recv ic with
-      | ((Exiting i) : ('input, 'output) msg_from_vassal) ->
+    let rec loop () =
+      match ((Ipc.recv ic) : ('input, 'output) msg_from_vassal) with
+      | Exiting i ->
           close_out ocs.(i);
           decr active_vassals;
-          if !active_vassals = 0 then
-            ()
-          else
-            dispatch ()
-      | ((Ready i) : ('input, 'output) msg_from_vassal) ->
+          if !active_vassals = 0 then () else loop ()
+      | Ready i ->
           Ipc.send ocs.(i) (Job (next t));
-          dispatch ()
-      | ((Result (i, result)) : ('input, 'output) msg_from_vassal) ->
+          loop ()
+      | Result (i, result) ->
           Queue.add result results;
           Ipc.send ocs.(i) (Job (next t));
-          dispatch ()
+          loop ()
     in
     let rec wait = function
-      | [] -> ()
+      | [] ->
+          ()
       | vassals ->
           let pid, _process_status = Unix.wait () in
           (* TODO: handle process_status *)
           wait (List.filter vassals ~f:(fun p -> p <> pid))
     in
-    dispatch ();
+    loop ();
     close_in ic;
     wait vassals;
     of_queue results
 
   let vassal i ~f ~vassal_pipe_r ~lord_pipe_w =
-    eprintf "[debug] [vassal %d] started\n%!" i;
     let ic = Unix.in_channel_of_descr vassal_pipe_r in
     let oc = Unix.out_channel_of_descr lord_pipe_w in
-    let rec work msg =
-      Ipc.send oc msg;
-      match Ipc.recv ic with
-      | (Job (Some x) : 'input msg_from_lord) ->
-          work (Result (i, (x, f x)))
-      | (Job None : 'input msg_from_lord) ->
+    let rec loop () =
+      match (Ipc.recv ic : 'input msg_from_lord) with
+      | Job (Some x) ->
+          Ipc.send oc (Result (i, (x, f x)));
+          loop ()
+      | Job None ->
           Ipc.send oc (Exiting i)
     in
-    work (Ready i);
+    Ipc.send oc (Ready i);
+    loop ();
     close_in ic;
     close_out oc;
     exit 0
@@ -357,16 +354,27 @@ end = struct
 end
 
 module In_channel : sig
-  val lines : in_channel -> string Stream.t
+  val lines : ?delim_null:bool -> in_channel -> string Stream.t
 end = struct
-  let lines ic =
-    Stream.create (fun () ->
-      match input_line ic with
-      | exception End_of_file ->
-          None
-      | line ->
-          Some line
-    )
+  let read_until_newline ic () =
+    match input_line ic with
+    | exception End_of_file ->
+        None
+    | line ->
+        Some line
+
+  let read_until_null ic =
+    let lexbuf = Lexing.from_channel ic in
+    fun () -> Input_delim.by_null lexbuf
+
+  let lines ?(delim_null=false) ic =
+    let reader =
+      if delim_null then
+        read_until_null ic
+      else
+        read_until_newline ic
+    in
+    Stream.create reader
 end
 
 module File : sig
@@ -491,13 +499,14 @@ type opt =
   ; ignore : string -> bool
   ; sample : int
   ; njobs  : int
+  ; delim_null : bool
   }
 
-let make_input_stream input ignore ~metrics =
+let make_input_stream input ignore ~metrics ~delim_null =
   let input =
     match input with
     | Stdin ->
-        File.lookup (In_channel.lines stdin)
+        File.lookup (In_channel.lines stdin ~delim_null)
     | Directories paths ->
         let paths = StrSet.elements (StrSet.of_list paths) in
         Stream.concat (List.map paths ~f:File.find)
@@ -535,12 +544,12 @@ let time_wall () =
 let time_proc () =
   Sys.time ()
 
-let main {input; output; ignore; sample = sample_len; njobs} =
+let main {input; output; ignore; sample = sample_len; njobs; delim_null} =
   let wt0_all = time_wall () in
   let pt0_all = time_proc () in
   let metrics = M.init () in
   let output = make_output_fun  output in
-  let input  = make_input_stream input ignore ~metrics in
+  let input  = make_input_stream input ignore ~metrics ~delim_null in
   (* TODO: Make a nice(r) abstraction to re-assemble pieces in the pipeline:
    *
    * from input           to files_by_size
@@ -610,9 +619,10 @@ let main {input; output; ignore; sample = sample_len; njobs} =
   eprintf "[debug] reporting\n%!";
   Stream.iter groups ~f:(fun (d, n, files) ->
     M.digest metrics;
-    if n > 1 then
+    if n > 1 then begin
       M.redundant_data metrics ~size:(n * (List.hd files).File.size);
       output d n files
+    end
   );
 
   let pt1_all = time_proc () in
@@ -646,6 +656,7 @@ let get_opt () : opt =
   let ignore = ref (fun _ -> false) in
   let sample = ref 512 in
   let njobs  = ref 6 in
+  let input_delim_null = ref false in
   let spec =
     [ ( "-out"
       , Arg.String (fun path ->
@@ -669,6 +680,16 @@ let get_opt () : opt =
       , Arg.Set_int njobs
       , (sprintf " Number of parallel jobs. Default: %d" !njobs)
       )
+    ; ( "-0"
+      , Arg.Set input_delim_null
+      , ( sprintf
+            ( " Delimit input paths by null character instead of a newline."
+            ^^" Meaningful only when reading candidate paths from stdin."
+            ^^" Default: %B"
+            )
+            !input_delim_null
+        )
+      )
     ]
   in
   Arg.parse
@@ -692,6 +713,7 @@ let get_opt () : opt =
   ; ignore = !ignore
   ; sample = !sample
   ; njobs  = !njobs
+  ; delim_null = !input_delim_null
   }
 
 let () =
This page took 0.021412 seconds and 4 git commands to generate.