3 module Array = ArrayLabels
4 module List = ListLabels
5 module StrSet = Set.Make(String)
6 module Unix = UnixLabels
11 val create : (unit -> 'a option) -> 'a t
13 val iter : 'a t -> f:('a -> unit) -> unit
15 val map : 'a t -> f:('a -> 'b) -> 'b t
17 val filter : 'a t -> f:('a -> bool) -> 'a t
19 val concat : ('a t) list -> 'a t
24 {mutable streams : ('a S.t) list}
27 {streams = [S.from (fun _ -> f ())]}
35 | exception Stream.Failure ->
46 | Some x -> Some (f x)
62 List.iter t.streams ~f:(S.iter f)
65 {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
68 module In_channel : sig
69 val lines : in_channel -> string Stream.t
72 Stream.create (fun () ->
73 match input_line ic with
74 | exception End_of_file ->
87 val find : string -> t Stream.t
88 (** Find all files in the directory tree, starting from the given root path *)
90 val lookup : string Stream.t -> t Stream.t
91 (** Lookup file info for given paths *)
93 module Set : sig include Set.S with type elt := t end
100 let compare {path=p1; _} {path=p2; _} =
104 Stream.map paths ~f:(fun path ->
105 let {Unix.st_size = size; _} = Unix.lstat path in
110 let dirs = Queue.create () in
111 let files = Queue.create () in
113 Array.iter (Sys.readdir parent) ~f:(fun child ->
114 let path = Filename.concat parent child in
115 let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
118 let file = {path; size = st_size} in
132 match Queue.is_empty files, Queue.is_empty dirs with
133 | false, _ -> Some (Queue.take files)
134 | true , true -> None
136 explore (Queue.take dirs);
141 module Set = Set.Make(struct
144 let compare = compare
150 | Directories of string list
154 | Directory of string
159 ; ignore : Str.regexp option
164 { considered_files : int ref
165 ; considered_bytes : int ref
167 ; ignored_files : int ref
168 ; ignored_bytes : int ref
169 ; unique_size_files : int ref
170 ; unique_size_bytes : int ref
171 ; unique_sample_files : int ref
172 ; unique_sample_bytes : int ref
173 ; sampled_files : int ref
174 ; sampled_bytes : int ref
175 ; hashed_files : int ref
176 ; hashed_bytes : int ref
183 let make_input_stream input ignore count =
187 File.lookup (In_channel.lines stdin)
188 | Directories paths ->
189 let paths = StrSet.elements (StrSet.of_list paths) in
190 Stream.concat (List.map paths ~f:File.find)
192 Stream.filter input ~f:(fun {File.path; size} ->
193 incr count.considered_files;
194 add count.considered_bytes size;
195 let empty = size = 0 in
198 | Some regexp when (Str.string_match regexp path 0) ->
199 incr count.ignored_files;
200 add count.ignored_bytes size;
205 if empty then incr count.empty;
206 (not empty) && (not ignored)
209 let make_output_fun = function
211 fun digest n_files files ->
212 printf "%s %d\n%!" (Digest.to_hex digest) n_files;
213 List.iter (File.Set.elements files) ~f:(fun {File.path; _} ->
214 printf " %S\n%!" path
217 fun digest _ files ->
218 let digest = Digest.to_hex digest in
219 let dir = Filename.concat dir (String.sub digest 0 2) in
220 Unix.mkdir dir ~perm:0o700;
221 let oc = open_out (Filename.concat dir digest) in
222 List.iter (File.Set.elements files) ~f:(fun {File.path; _} ->
223 output_string oc (sprintf "%S\n%!" path)
227 let sample path ~len ~count =
228 let buf = Bytes.make len ' ' in
229 let ic = open_in_bin path in
230 let rec read pos len =
235 let chunk_size = input ic buf pos len in
236 add count.sampled_bytes chunk_size;
237 if chunk_size = 0 then (* EOF *)
240 read (pos + chunk_size) (len - chunk_size)
247 let main {input; output; ignore; sample = sample_len} =
248 let t0 = Sys.time () in
250 { considered_files = ref 0
251 ; considered_bytes = ref 0
253 ; ignored_files = ref 0
254 ; ignored_bytes = ref 0
255 ; unique_size_files = ref 0
256 ; unique_size_bytes = ref 0
257 ; sampled_files = ref 0
258 ; sampled_bytes = ref 0
259 ; hashed_files = ref 0
260 ; hashed_bytes = ref 0
261 ; unique_sample_files = ref 0
262 ; unique_sample_bytes = ref 0
266 let output = make_output_fun output in
267 let input = make_input_stream input ignore count in
268 let files_by_size = Hashtbl.create 1_000_000 in
269 let files_by_sample = Hashtbl.create 1_000_000 in
270 let files_by_digest = Hashtbl.create 1_000_000 in
271 let process tbl ~group ~file =
273 match Hashtbl.find_opt tbl group with
279 Hashtbl.replace tbl group (count + 1, File.Set.add file files)
281 (* TODO: Make a nice(r) abstraction to re-assemble pieces in the pipeline:
283 * from input to files_by_size
284 * from files_by_size to files_by_sample
285 * from files_by_sample to files_by_digest
286 * from files_by_digest to output
288 * input |> files_by_size |> files_by_sample |> files_by_digest |> output
290 let t0_group_by_size = Sys.time () in
291 Stream.iter input ~f:(fun ({File.size; _} as file) ->
292 process files_by_size ~group:size ~file
294 let t1_group_by_size = Sys.time () in
295 let t0_group_by_sample = Sys.time () in
298 (* Skip files with unique sizes *)
301 (fun ({File.path; _} as file) ->
302 incr count.sampled_files;
305 ~group:(sample path ~len:sample_len ~count)
311 (fun {File.size; _} ->
312 incr count.unique_size_files;
313 add count.unique_size_bytes size
318 let t1_group_by_sample = Sys.time () in
319 let t0_group_by_digest = Sys.time () in
322 (* Skip files with unique samples *)
325 (fun ({File.path; size} as file) ->
326 incr count.hashed_files;
327 add count.hashed_bytes size;
328 process files_by_digest ~group:(Digest.file path) ~file
333 (fun {File.size; _} ->
334 incr count.unique_sample_files;
335 add count.unique_sample_bytes size;
340 let t1_group_by_digest = Sys.time () in
348 let t1 = Sys.time () in
349 let b_to_mb b = (float_of_int b) /. 1024. /. 1024. in
350 let b_to_gb b = (b_to_mb b) /. 1024. in
351 eprintf "Time : %8.2f seconds\n%!" (t1 -. t0);
352 eprintf "Considered : %8d files %6.2f Gb\n%!"
353 !(count.considered_files)
354 (b_to_gb !(count.considered_bytes));
355 eprintf "Sampled : %8d files %6.2f Gb\n%!"
356 !(count.sampled_files)
357 (b_to_gb !(count.sampled_bytes));
358 eprintf "Hashed : %8d files %6.2f Gb %6.2f seconds\n%!"
359 !(count.hashed_files)
360 (b_to_gb !(count.hashed_bytes))
361 (t1_group_by_digest -. t0_group_by_digest);
362 eprintf "Digests : %8d\n%!"
364 eprintf "Duplicates (Hashed - Digests): %8d\n%!"
365 (!(count.hashed_files) - !(count.digests));
366 eprintf "Skipped due to 0 size : %8d files\n%!" !(count.empty);
367 eprintf "Skipped due to unique size : %8d files %6.2f Gb %6.2f seconds\n%!"
368 !(count.unique_size_files)
369 (b_to_gb !(count.unique_size_bytes))
370 (t1_group_by_size -. t0_group_by_size);
371 eprintf "Skipped due to unique sample : %8d files %6.2f Gb %6.2f seconds\n%!"
372 !(count.unique_sample_files)
373 (b_to_gb !(count.unique_sample_bytes))
374 (t1_group_by_sample -. t0_group_by_sample);
375 eprintf "Ignored due to regex match : %8d files %6.2f Gb\n%!"
376 !(count.ignored_files)
377 (b_to_gb !(count.ignored_bytes))
379 let get_opt () : opt =
380 let assert_ test x msg =
381 if not (test x) then begin
382 eprintf "%s\n%!" msg;
386 let assert_file_exists path =
387 assert_ Sys.file_exists path (sprintf "File does not exist: %S" path)
389 let assert_file_is_dir path =
390 assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path)
392 let input = ref Stdin in
393 let output = ref Stdout in
394 let ignore = ref None in
395 let sample = ref 256 in
398 , Arg.String (fun path ->
399 assert_file_exists path;
400 assert_file_is_dir path;
401 output := Directory path
403 , " Output to this directory instead of stdout."
406 , Arg.String (fun regexp -> ignore := Some (Str.regexp regexp))
407 , " Ignore file paths which match this regexp pattern (see Str module)."
411 , (sprintf " Byte size of file samples to use. Default: %d" !sample)
418 assert_file_exists path;
419 assert_file_is_dir path;
422 input := Directories [path]
423 | Directories paths ->
424 input := Directories (path :: paths)
430 (sprintf "Sample size cannot be negative: %d" !sample);