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
40 : t -> size:int -> unit
43 { considered_files : int ref
44 ; considered_bytes : int ref
46 ; ignored_files : int ref
47 ; ignored_bytes : int ref
48 ; unique_size_files : int ref
49 ; unique_size_bytes : int ref
50 ; unique_sample_files : int ref
51 ; unique_sample_bytes : int ref
52 ; sampled_files : int ref
53 ; sampled_bytes : int ref
54 ; hashed_files : int ref
55 ; hashed_bytes : int ref
57 ; redundant_data : int ref
61 { considered_files = ref 0
62 ; considered_bytes = ref 0
64 ; ignored_files = ref 0
65 ; ignored_bytes = ref 0
66 ; unique_size_files = ref 0
67 ; unique_size_bytes = ref 0
68 ; sampled_files = ref 0
69 ; sampled_bytes = ref 0
70 ; hashed_files = ref 0
71 ; hashed_bytes = ref 0
72 ; unique_sample_files = ref 0
73 ; unique_sample_bytes = ref 0
75 ; redundant_data = ref 0
81 let file_considered t ~size =
82 incr t.considered_files;
83 add t.considered_bytes size
85 let file_ignored {ignored_files; ignored_bytes; _} ~size =
87 add ignored_bytes size
92 let chunk_read t ~size =
93 add t.sampled_bytes size
98 let file_unique_size t ~size =
99 incr t.unique_size_files;
100 add t.unique_size_bytes size
102 let file_unique_sample t ~size =
103 incr t.unique_sample_files;
104 add t.unique_sample_bytes size
106 let file_hashed t ~size =
108 add t.hashed_bytes size
113 let redundant_data t ~size =
114 add t.redundant_data size
121 ~time_group_by_digest
123 let b_to_mb b = (float_of_int b) /. 1024. /. 1024. in
124 let b_to_gb b = (b_to_mb b) /. 1024. in
125 eprintf "Time : %8.2f seconds\n%!"
127 eprintf "Considered : %8d files %6.2f Gb\n%!"
128 !(t.considered_files)
129 (b_to_gb !(t.considered_bytes));
130 eprintf "Sampled : %8d files %6.2f Gb\n%!"
132 (b_to_gb !(t.sampled_bytes));
133 eprintf "Hashed : %8d files %6.2f Gb %6.2f seconds\n%!"
135 (b_to_gb !(t.hashed_bytes))
136 time_group_by_digest;
137 eprintf "Digests : %8d\n%!"
139 eprintf "Duplicates (Hashed - Digests): %8d files %6.2f Gb\n%!"
140 (!(t.hashed_files) - !(t.digests))
141 (b_to_gb !(t.redundant_data));
142 eprintf "Skipped due to 0 size : %8d files\n%!" !(t.empty);
143 eprintf "Skipped due to unique size : %8d files %6.2f Gb %6.2f seconds\n%!"
144 !(t.unique_size_files)
145 (b_to_gb !(t.unique_size_bytes))
147 eprintf "Skipped due to unique sample : %8d files %6.2f Gb %6.2f seconds\n%!"
148 !(t.unique_sample_files)
149 (b_to_gb !(t.unique_sample_bytes))
151 eprintf "Ignored due to regex match : %8d files %6.2f Gb\n%!"
153 (b_to_gb !(t.ignored_bytes))
161 val create : (unit -> 'a option) -> 'a t
163 val of_queue : 'a Queue.t -> 'a t
165 val iter : 'a t -> f:('a -> unit) -> unit
167 val map : 'a t -> f:('a -> 'b) -> 'b t
169 val filter : 'a t -> f:('a -> bool) -> 'a t
171 val concat : ('a t) list -> 'a t
173 val group_by : 'a t -> f:('a -> 'b) -> ('b * int * 'a list) t
178 {mutable streams : ('a S.t) list}
181 {streams = [S.from (fun _ -> f ())]}
185 match Queue.take q with
186 | exception Queue.Empty ->
198 | exception Stream.Failure ->
199 t.streams <- streams;
209 | Some x -> Some (f x)
225 List.iter t.streams ~f:(S.iter f)
228 {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
231 let groups_tbl = Hashtbl.create 1_000_000 in
235 match Hashtbl.find_opt groups_tbl group with
241 Hashtbl.replace groups_tbl group members
243 iter t ~f:group_update;
244 let groups = Queue.create () in
246 (fun name (length, members) -> Queue.add (name, length, members) groups)
251 module In_channel : sig
252 val lines : in_channel -> string Stream.t
255 Stream.create (fun () ->
256 match input_line ic with
257 | exception End_of_file ->
270 val find : string -> t Stream.t
271 (** Find all files in the directory tree, starting from the given root path *)
273 val lookup : string Stream.t -> t Stream.t
274 (** Lookup file info for given paths *)
276 val filter_out_unique_sizes : t Stream.t -> metrics:M.t -> t Stream.t
277 val filter_out_unique_heads : t Stream.t -> len:int -> metrics:M.t -> t Stream.t
285 Stream.map paths ~f:(fun path ->
286 let {Unix.st_size = size; _} = Unix.lstat path in
291 let dirs = Queue.create () in
292 let files = Queue.create () in
294 Array.iter (Sys.readdir parent) ~f:(fun child ->
295 let path = Filename.concat parent child in
296 let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
299 let file = {path; size = st_size} in
313 match Queue.is_empty files, Queue.is_empty dirs with
314 | false, _ -> Some (Queue.take files)
315 | true , true -> None
317 explore (Queue.take dirs);
322 let filter_out_singletons files ~group ~handle_singleton =
323 let q = Queue.create () in
324 Stream.iter (Stream.group_by files ~f:group) ~f:(fun group ->
325 let (_, n, members) = group in
327 List.iter members ~f:(fun m -> Queue.add m q)
329 handle_singleton group
333 let filter_out_unique_sizes files ~metrics =
334 filter_out_singletons
336 ~group:(fun {size; _} -> size)
337 ~handle_singleton:(fun (size, _, _) -> M.file_unique_size metrics ~size)
339 let head path ~len ~metrics =
340 let buf = Bytes.make len ' ' in
341 let ic = open_in_bin path in
342 let rec read pos len =
347 let chunk_size = input ic buf pos len in
348 M.chunk_read metrics ~size:chunk_size;
349 if chunk_size = 0 then (* EOF *)
352 read (pos + chunk_size) (len - chunk_size)
359 let filter_out_unique_heads files ~len ~metrics =
360 filter_out_singletons
362 ~group:(fun {path; _} ->
363 M.file_sampled metrics;
364 head path ~len ~metrics
366 ~handle_singleton:(fun (_, _, files) ->
367 let {size; _} = List.hd files in (* Guaranteed non-empty *)
368 M.file_unique_sample metrics ~size
374 | Directories of string list
378 | Directory of string
383 ; ignore : string -> bool
387 let make_input_stream input ignore ~metrics =
391 File.lookup (In_channel.lines stdin)
392 | Directories paths ->
393 let paths = StrSet.elements (StrSet.of_list paths) in
394 Stream.concat (List.map paths ~f:File.find)
396 Stream.filter input ~f:(fun {File.path; size} ->
397 M.file_considered metrics ~size;
398 let empty = size = 0 in
399 let ignored = ignore path in
400 if empty then M.file_empty metrics;
401 if ignored then M.file_ignored metrics ~size;
402 (not empty) && (not ignored)
405 let make_output_fun = function
407 fun digest n_files files ->
408 printf "%s %d\n%!" (Digest.to_hex digest) n_files;
409 List.iter files ~f:(fun {File.path; _} ->
410 printf " %S\n%!" path
413 fun digest _ files ->
414 let digest = Digest.to_hex digest in
415 let dir = Filename.concat dir (String.sub digest 0 2) in
416 Unix.mkdir dir ~perm:0o700;
417 let oc = open_out (Filename.concat dir digest) in
418 List.iter files ~f:(fun {File.path; _} ->
419 output_string oc (sprintf "%S\n%!" path)
423 let main {input; output; ignore; sample = sample_len} =
424 let t0_all = Sys.time () in
425 let metrics = M.init () in
426 let output = make_output_fun output in
427 let input = make_input_stream input ignore ~metrics in
428 (* TODO: Make a nice(r) abstraction to re-assemble pieces in the pipeline:
430 * from input to files_by_size
431 * from files_by_size to files_by_sample
432 * from files_by_sample to files_by_digest
433 * from files_by_digest to output
435 * input |> files_by_size |> files_by_sample |> files_by_digest |> output
440 let t0_group_by_size = Sys.time () in
441 let files = File.filter_out_unique_sizes files ~metrics in
442 let t1_group_by_size = Sys.time () in
444 let t0_group_by_sample = t1_group_by_size in
445 let files = File.filter_out_unique_heads files ~len:sample_len ~metrics in
446 let t1_group_by_sample = Sys.time () in
448 let t0_group_by_digest = t1_group_by_sample in
450 Stream.group_by files ~f:(fun {File.path; size} ->
451 M.file_hashed metrics ~size;
455 let t1_group_by_digest = Sys.time () in
457 Stream.iter groups ~f:(fun (d, n, files) ->
460 M.redundant_data metrics ~size:(n * (List.hd files).File.size);
464 let t1_all = Sys.time () in
467 ~time_all: (t1_all -. t0_all)
468 ~time_group_by_size: (t1_group_by_size -. t0_group_by_size)
469 ~time_group_by_head: (t1_group_by_sample -. t0_group_by_sample)
470 ~time_group_by_digest:(t1_group_by_digest -. t0_group_by_digest)
472 let get_opt () : opt =
473 let assert_ test x msg =
474 if not (test x) then begin
475 eprintf "%s\n%!" msg;
479 let assert_file_exists path =
480 assert_ Sys.file_exists path (sprintf "File does not exist: %S" path)
482 let assert_file_is_dir path =
483 assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path)
485 let input = ref Stdin in
486 let output = ref Stdout in
487 let ignore = ref (fun _ -> false) in
488 let sample = ref 256 in
491 , Arg.String (fun path ->
492 assert_file_exists path;
493 assert_file_is_dir path;
494 output := Directory path
496 , " Output to this directory instead of stdout."
499 , Arg.String (fun regexp ->
500 let regexp = Str.regexp regexp in
501 ignore := fun string -> Str.string_match regexp string 0)
502 , " Ignore file paths which match this regexp pattern (see Str module)."
506 , (sprintf " Byte size of file samples to use. Default: %d" !sample)
513 assert_file_exists path;
514 assert_file_is_dir path;
517 input := Directories [path]
518 | Directories paths ->
519 input := Directories (path :: paths)
525 (sprintf "Sample size cannot be negative: %d" !sample);