Handle directories with no regular-file children
[dups.git] / dups.ml
diff --git a/dups.ml b/dups.ml
index 0bcc83a..46e8b91 100644 (file)
--- a/dups.ml
+++ b/dups.ml
@@ -41,7 +41,6 @@ end = struct
   let find_files root =
     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
@@ -59,24 +58,16 @@ end = struct
             ()
       )
     in
-    let next_dir () =
-      match Queue.take dirs with
-      | exception Queue.Empty ->
-          ()
-      | dir ->
-          explore dir
+    explore root;
+    let rec next () =
+      match Queue.is_empty files, Queue.is_empty dirs with
+      | false, _     -> Some (Queue.take files)
+      | true , true  -> None
+      | true , false ->
+          explore (Queue.take dirs);
+          next ()
     in
-    let next_file () =
-      match Queue.take files with
-      | exception Queue.Empty ->
-          None
-      | file_path ->
-          Some file_path
-    in
-    Stream.create (fun () ->
-      next_dir ();
-      next_file ()
-    )
+    Stream.create next
 end
 
 type input =
@@ -121,5 +112,12 @@ let main input =
 
 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
This page took 0.016971 seconds and 4 git commands to generate.