Handle root path missing or not a directory
[dups.git] / dups.ml
diff --git a/dups.ml b/dups.ml
index 8a555e8..a1dca83 100644 (file)
--- a/dups.ml
+++ b/dups.ml
@@ -5,8 +5,9 @@ module List  = ListLabels
 
 module Stream : sig
   type 'a t
 
 module Stream : sig
   type 'a t
-  val lines : in_channel -> string t
-  val rec_file_paths : root:string -> string t
+
+  val create : (unit -> 'a option) -> 'a t
+
   val iter : 'a t -> f:('a -> unit) -> unit
 end = struct
   module S = Stream
   val iter : 'a t -> f:('a -> unit) -> unit
 end = struct
   module S = Stream
@@ -14,10 +15,32 @@ end = struct
   type 'a t =
     'a S.t
 
   type 'a t =
     'a S.t
 
-  let rec_file_paths ~root =
+  let create f =
+    S.from (fun _ -> f ())
+
+  let iter t ~f =
+    S.iter f t
+end
+
+module In_channel : sig
+  val lines : 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
+    )
+end
+
+module Directory : sig
+  val find_files : string -> string Stream.t
+end = struct
+  let find_files root =
     let dirs  = Queue.create () in
     let files = Queue.create () in
     let dirs  = Queue.create () in
     let files = Queue.create () in
-    Queue.add root dirs;
     let explore parent =
       Array.iter (Sys.readdir parent) ~f:(fun child ->
         let path = Filename.concat parent child in
     let explore parent =
       Array.iter (Sys.readdir parent) ~f:(fun child ->
         let path = Filename.concat parent child in
@@ -49,22 +72,11 @@ end = struct
       | file_path ->
           Some file_path
     in
       | file_path ->
           Some file_path
     in
-    S.from (fun _ ->
+    explore root;
+    Stream.create (fun () ->
       next_dir ();
       next_file ()
     )
       next_dir ();
       next_file ()
     )
-
-  let lines ic =
-    S.from (fun _ ->
-      match input_line ic with
-      | exception End_of_file ->
-          None
-      | line ->
-          Some line
-    )
-
-  let iter t ~f =
-    S.iter f t
 end
 
 type input =
 end
 
 type input =
@@ -74,8 +86,8 @@ type input =
 let main input =
   let paths =
     match input with
 let main input =
   let paths =
     match input with
-    | Paths_on_stdin -> Stream.lines stdin
-    | Root_path root -> Stream.rec_file_paths ~root
+    | Paths_on_stdin -> In_channel.lines stdin
+    | Root_path root -> Directory.find_files root
   in
   let paths_by_digest = Hashtbl.create 1_000_000 in
   let path_count = ref 0 in
   in
   let paths_by_digest = Hashtbl.create 1_000_000 in
   let path_count = ref 0 in
@@ -109,5 +121,12 @@ let main input =
 
 let () =
   let input = ref Paths_on_stdin in
 
 let () =
   let input = ref Paths_on_stdin in
-  Arg.parse [] (fun path -> input := Root_path path) "";
+  Arg.parse [] (fun path ->
+    if Sys.file_exists path then
+      input := Root_path path
+    else begin
+      eprintf "File does not exist: %S\n%!" path;
+      exit 1
+    end
+  ) "";
   main !input
   main !input
This page took 0.020198 seconds and 4 git commands to generate.