3 module Array = ArrayLabels
4 module List = ListLabels
5 module StrSet = Set.Make(String)
6 module Unix = UnixLabels
16 -> time_group_by_size:float
17 -> time_group_by_head:float
18 -> time_group_by_digest:float
22 : t -> size:int -> unit
24 : t -> size:int -> unit
30 : t -> size:int -> unit
32 : t -> size:int -> unit
33 val file_unique_sample
34 : t -> size:int -> unit
36 : t -> size:int -> unit
41 { considered_files : int ref
42 ; considered_bytes : int ref
44 ; ignored_files : int ref
45 ; ignored_bytes : int ref
46 ; unique_size_files : int ref
47 ; unique_size_bytes : int ref
48 ; unique_sample_files : int ref
49 ; unique_sample_bytes : int ref
50 ; sampled_files : int ref
51 ; sampled_bytes : int ref
52 ; hashed_files : int ref
53 ; hashed_bytes : int ref
58 { considered_files = ref 0
59 ; considered_bytes = ref 0
61 ; ignored_files = ref 0
62 ; ignored_bytes = ref 0
63 ; unique_size_files = ref 0
64 ; unique_size_bytes = ref 0
65 ; sampled_files = ref 0
66 ; sampled_bytes = ref 0
67 ; hashed_files = ref 0
68 ; hashed_bytes = ref 0
69 ; unique_sample_files = ref 0
70 ; unique_sample_bytes = ref 0
77 let file_considered t ~size =
78 incr t.considered_files;
79 add t.considered_bytes size
81 let file_ignored {ignored_files; ignored_bytes; _} ~size =
83 add ignored_bytes size
88 let chunk_read t ~size =
89 add t.sampled_bytes size
94 let file_unique_size t ~size =
95 incr t.unique_size_files;
96 add t.unique_size_bytes size
98 let file_unique_sample t ~size =
99 incr t.unique_sample_files;
100 add t.unique_sample_bytes size
102 let file_hashed t ~size =
104 add t.hashed_bytes size
114 ~time_group_by_digest
116 let b_to_mb b = (float_of_int b) /. 1024. /. 1024. in
117 let b_to_gb b = (b_to_mb b) /. 1024. in
118 eprintf "Time : %8.2f seconds\n%!"
120 eprintf "Considered : %8d files %6.2f Gb\n%!"
121 !(t.considered_files)
122 (b_to_gb !(t.considered_bytes));
123 eprintf "Sampled : %8d files %6.2f Gb\n%!"
125 (b_to_gb !(t.sampled_bytes));
126 eprintf "Hashed : %8d files %6.2f Gb %6.2f seconds\n%!"
128 (b_to_gb !(t.hashed_bytes))
129 time_group_by_digest;
130 eprintf "Digests : %8d\n%!"
132 eprintf "Duplicates (Hashed - Digests): %8d\n%!"
133 (!(t.hashed_files) - !(t.digests));
134 eprintf "Skipped due to 0 size : %8d files\n%!" !(t.empty);
135 eprintf "Skipped due to unique size : %8d files %6.2f Gb %6.2f seconds\n%!"
136 !(t.unique_size_files)
137 (b_to_gb !(t.unique_size_bytes))
139 eprintf "Skipped due to unique sample : %8d files %6.2f Gb %6.2f seconds\n%!"
140 !(t.unique_sample_files)
141 (b_to_gb !(t.unique_sample_bytes))
143 eprintf "Ignored due to regex match : %8d files %6.2f Gb\n%!"
145 (b_to_gb !(t.ignored_bytes))
153 val create : (unit -> 'a option) -> 'a t
155 val of_queue : 'a Queue.t -> 'a t
157 val iter : 'a t -> f:('a -> unit) -> unit
159 val map : 'a t -> f:('a -> 'b) -> 'b t
161 val filter : 'a t -> f:('a -> bool) -> 'a t
163 val concat : ('a t) list -> 'a t
165 val group_by : 'a t -> f:('a -> 'b) -> ('b * int * 'a list) t
170 {mutable streams : ('a S.t) list}
173 {streams = [S.from (fun _ -> f ())]}
177 match Queue.take q with
178 | exception Queue.Empty ->
190 | exception Stream.Failure ->
191 t.streams <- streams;
201 | Some x -> Some (f x)
217 List.iter t.streams ~f:(S.iter f)
220 {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
223 let groups_tbl = Hashtbl.create 1_000_000 in
227 match Hashtbl.find_opt groups_tbl group with
233 Hashtbl.replace groups_tbl group members
235 iter t ~f:group_update;
236 let groups = Queue.create () in
238 (fun name (length, members) -> Queue.add (name, length, members) groups)
243 module In_channel : sig
244 val lines : in_channel -> string Stream.t
247 Stream.create (fun () ->
248 match input_line ic with
249 | exception End_of_file ->
262 val find : string -> t Stream.t
263 (** Find all files in the directory tree, starting from the given root path *)
265 val lookup : string Stream.t -> t Stream.t
266 (** Lookup file info for given paths *)
268 val filter_out_unique_sizes : t Stream.t -> metrics:M.t -> t Stream.t
269 val filter_out_unique_heads : t Stream.t -> len:int -> metrics:M.t -> t Stream.t
277 Stream.map paths ~f:(fun path ->
278 let {Unix.st_size = size; _} = Unix.lstat path in
283 let dirs = Queue.create () in
284 let files = Queue.create () in
286 Array.iter (Sys.readdir parent) ~f:(fun child ->
287 let path = Filename.concat parent child in
288 let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
291 let file = {path; size = st_size} in
305 match Queue.is_empty files, Queue.is_empty dirs with
306 | false, _ -> Some (Queue.take files)
307 | true , true -> None
309 explore (Queue.take dirs);
314 let filter_out_singletons files ~group ~handle_singleton =
315 let q = Queue.create () in
316 Stream.iter (Stream.group_by files ~f:group) ~f:(fun group ->
317 let (_, n, members) = group in
319 List.iter members ~f:(fun m -> Queue.add m q)
321 handle_singleton group
325 let filter_out_unique_sizes files ~metrics =
326 filter_out_singletons
328 ~group:(fun {size; _} -> size)
329 ~handle_singleton:(fun (size, _, _) -> M.file_unique_size metrics ~size)
331 let head path ~len ~metrics =
332 let buf = Bytes.make len ' ' in
333 let ic = open_in_bin path in
334 let rec read pos len =
339 let chunk_size = input ic buf pos len in
340 M.chunk_read metrics ~size:chunk_size;
341 if chunk_size = 0 then (* EOF *)
344 read (pos + chunk_size) (len - chunk_size)
351 let filter_out_unique_heads files ~len ~metrics =
352 filter_out_singletons
354 ~group:(fun {path; _} ->
355 M.file_sampled metrics;
356 head path ~len ~metrics
358 ~handle_singleton:(fun (_, _, files) ->
359 let {size; _} = List.hd files in (* Guaranteed non-empty *)
360 M.file_unique_sample metrics ~size
366 | Directories of string list
370 | Directory of string
375 ; ignore : Str.regexp option
379 let make_input_stream input ignore ~metrics =
383 File.lookup (In_channel.lines stdin)
384 | Directories paths ->
385 let paths = StrSet.elements (StrSet.of_list paths) in
386 Stream.concat (List.map paths ~f:File.find)
388 Stream.filter input ~f:(fun {File.path; size} ->
389 M.file_considered metrics ~size;
390 let empty = size = 0 in
391 if empty then M.file_empty metrics;
394 | Some regexp when (Str.string_match regexp path 0) ->
395 M.file_ignored metrics ~size;
400 (not empty) && (not ignored)
403 let make_output_fun = function
405 fun digest n_files files ->
406 printf "%s %d\n%!" (Digest.to_hex digest) n_files;
407 List.iter files ~f:(fun {File.path; _} ->
408 printf " %S\n%!" path
411 fun digest _ files ->
412 let digest = Digest.to_hex digest in
413 let dir = Filename.concat dir (String.sub digest 0 2) in
414 Unix.mkdir dir ~perm:0o700;
415 let oc = open_out (Filename.concat dir digest) in
416 List.iter files ~f:(fun {File.path; _} ->
417 output_string oc (sprintf "%S\n%!" path)
421 let main {input; output; ignore; sample = sample_len} =
422 let t0_all = Sys.time () in
423 let metrics = M.init () in
424 let output = make_output_fun output in
425 let input = make_input_stream input ignore ~metrics in
426 (* TODO: Make a nice(r) abstraction to re-assemble pieces in the pipeline:
428 * from input to files_by_size
429 * from files_by_size to files_by_sample
430 * from files_by_sample to files_by_digest
431 * from files_by_digest to output
433 * input |> files_by_size |> files_by_sample |> files_by_digest |> output
438 let t0_group_by_size = Sys.time () in
439 let files = File.filter_out_unique_sizes files ~metrics in
440 let t1_group_by_size = Sys.time () in
442 let t0_group_by_sample = t1_group_by_size in
443 let files = File.filter_out_unique_heads files ~len:sample_len ~metrics in
444 let t1_group_by_sample = Sys.time () in
446 let t0_group_by_digest = t1_group_by_sample in
448 Stream.group_by files ~f:(fun {File.path; size} ->
449 M.file_hashed metrics ~size;
453 let t1_group_by_digest = Sys.time () in
455 Stream.iter groups ~f:(fun (d, n, files) ->
457 if n > 1 then output d n files
460 let t1_all = Sys.time () in
463 ~time_all: (t1_all -. t0_all)
464 ~time_group_by_size: (t1_group_by_size -. t0_group_by_size)
465 ~time_group_by_head: (t1_group_by_sample -. t0_group_by_sample)
466 ~time_group_by_digest:(t1_group_by_digest -. t0_group_by_digest)
468 let get_opt () : opt =
469 let assert_ test x msg =
470 if not (test x) then begin
471 eprintf "%s\n%!" msg;
475 let assert_file_exists path =
476 assert_ Sys.file_exists path (sprintf "File does not exist: %S" path)
478 let assert_file_is_dir path =
479 assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path)
481 let input = ref Stdin in
482 let output = ref Stdout in
483 let ignore = ref None in
484 let sample = ref 256 in
487 , Arg.String (fun path ->
488 assert_file_exists path;
489 assert_file_is_dir path;
490 output := Directory path
492 , " Output to this directory instead of stdout."
495 , Arg.String (fun regexp -> ignore := Some (Str.regexp regexp))
496 , " Ignore file paths which match this regexp pattern (see Str module)."
500 , (sprintf " Byte size of file samples to use. Default: %d" !sample)
507 assert_file_exists path;
508 assert_file_is_dir path;
511 input := Directories [path]
512 | Directories paths ->
513 input := Directories (path :: paths)
519 (sprintf "Sample size cannot be negative: %d" !sample);