X-Git-Url: https://git.xandkar.net/?p=dups.git;a=blobdiff_plain;f=dups.ml;h=941d8fa34d1e43a0e0f8b0c656faccc4a6607194;hp=0bcc83a8a312f52ff3dbd91dab32493e67538dd8;hb=8673c3a56eacade031b713b19cf04068387cb6db;hpb=e13e9ef5acbfd0affadcf6d23223fa61ffd8cd6b diff --git a/dups.ml b/dups.ml index 0bcc83a..941d8fa 100644 --- a/dups.ml +++ b/dups.ml @@ -2,6 +2,7 @@ open Printf module Array = ArrayLabels module List = ListLabels +module StrSet= Set.Make(String) module Stream : sig type 'a t @@ -9,17 +10,22 @@ module Stream : sig val create : (unit -> 'a option) -> 'a t val iter : 'a t -> f:('a -> unit) -> unit + + val concat : ('a t) list -> 'a t end = struct module S = Stream type 'a t = - 'a S.t + ('a S.t) list let create f = - S.from (fun _ -> f ()) + [S.from (fun _ -> f ())] let iter t ~f = - S.iter f t + List.iter t ~f:(S.iter f) + + let concat ts = + List.concat ts end module In_channel : sig @@ -35,13 +41,12 @@ end = struct ) end -module Directory : sig +module Directory_tree : sig val find_files : string -> string Stream.t 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,35 +64,30 @@ end = struct () ) in - let next_dir () = - match Queue.take dirs with - | exception Queue.Empty -> - () - | dir -> - explore dir - in - let next_file () = - match Queue.take files with - | exception Queue.Empty -> - None - | file_path -> - Some file_path + 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 - Stream.create (fun () -> - next_dir (); - next_file () - ) + Stream.create next end type input = - | Root_path of string + | Root_paths of string list | Paths_on_stdin let main input = let paths = match input with - | Paths_on_stdin -> In_channel.lines stdin - | Root_path root -> Directory.find_files root + | Paths_on_stdin -> + In_channel.lines stdin + | Root_paths paths -> + let paths = StrSet.elements (StrSet.of_list paths) in + Stream.concat (List.map paths ~f:Directory_tree.find_files) in let paths_by_digest = Hashtbl.create 1_000_000 in let path_count = ref 0 in @@ -99,20 +99,20 @@ let main input = let paths = match Hashtbl.find_opt paths_by_digest digest with | None -> - [] + StrSet.empty | Some paths -> paths in - Hashtbl.replace paths_by_digest digest (path :: paths) + Hashtbl.replace paths_by_digest digest (StrSet.add path paths) with Sys_error e -> eprintf "WARNING: Failed to process %S: %S\n%!" path e ); Hashtbl.iter (fun digest paths -> - let n_paths = List.length paths in + let n_paths = StrSet.cardinal paths in if n_paths > 1 then begin printf "%s %d\n%!" (Digest.to_hex digest) n_paths; - List.iter paths ~f:(fun path -> printf " %s\n%!" path) + List.iter (StrSet.elements paths) ~f:(printf " %s\n%!") end ) paths_by_digest; @@ -121,5 +121,19 @@ let main input = let () = let input = ref Paths_on_stdin in - Arg.parse [] (fun path -> input := Root_path path) ""; + Arg.parse + [] + (function + | path when Sys.file_exists path -> + (match !input with + | Paths_on_stdin -> + input := Root_paths [path] + | Root_paths paths -> + input := Root_paths (path :: paths) + ) + | path -> + eprintf "File does not exist: %S\n%!" path; + exit 1 + ) + ""; main !input