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 *)
99 Stream.map paths ~f:(fun path ->
100 let {Unix.st_size = size; _} = Unix.lstat path in
105 let dirs = Queue.create () in
106 let files = Queue.create () in
108 Array.iter (Sys.readdir parent) ~f:(fun child ->
109 let path = Filename.concat parent child in
110 let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
113 let file = {path; size = st_size} in
127 match Queue.is_empty files, Queue.is_empty dirs with
128 | false, _ -> Some (Queue.take files)
129 | true , true -> None
131 explore (Queue.take dirs);
139 | Directories of string list
143 | Directory of string
148 ; ignore : Str.regexp option
153 { considered : int ref
156 ; unique_size : int ref
157 ; unique_sample : int ref
161 let make_input_stream input ignore count =
165 File.lookup (In_channel.lines stdin)
166 | Directories paths ->
167 let paths = StrSet.elements (StrSet.of_list paths) in
168 Stream.concat (List.map paths ~f:File.find)
170 Stream.filter input ~f:(fun {File.path; size} ->
171 incr count.considered;
172 let empty = size = 0 in
175 | Some regexp when (Str.string_match regexp path 0) ->
180 if empty then incr count.empty;
181 if ignored then incr count.ignored;
182 (not empty) && (not ignored)
185 let make_output_fun = function
187 fun digest n_paths paths ->
188 printf "%s %d\n%!" (Digest.to_hex digest) n_paths;
189 List.iter (StrSet.elements paths) ~f:(printf " %S\n%!")
191 fun digest _ paths ->
192 let digest = Digest.to_hex digest in
193 let dir = Filename.concat dir (String.sub digest 0 2) in
194 Unix.mkdir dir ~perm:0o700;
195 let oc = open_out (Filename.concat dir digest) in
196 List.iter (StrSet.elements paths) ~f:(fun path ->
197 output_string oc (sprintf "%S\n%!" path)
201 let sample path ~len =
202 let buf = Bytes.make len ' ' in
203 let ic = open_in_bin path in
204 let rec read pos len =
209 let chunk_size = input ic buf pos len in
210 if chunk_size = 0 then (* EOF *)
213 read (pos + chunk_size) (len - chunk_size)
220 let main {input; output; ignore; sample = sample_len} =
221 let t0 = Sys.time () in
226 ; unique_size = ref 0
228 ; unique_sample = ref 0
231 let output = make_output_fun output in
232 let input = make_input_stream input ignore count in
233 let paths_by_size = Hashtbl.create 1_000_000 in
234 let paths_by_sample = Hashtbl.create 1_000_000 in
235 let paths_by_digest = Hashtbl.create 1_000_000 in
236 let process tbl path ~f =
239 match Hashtbl.find_opt tbl key with
245 Hashtbl.replace tbl key (count + 1, StrSet.add path paths)
247 Stream.iter input ~f:(fun {File.path; size} ->
248 process paths_by_size path ~f:(fun _ -> size)
252 (* Skip files with unique sizes *)
256 process paths_by_sample path ~f:(sample ~len:sample_len)
260 incr count.unique_size;
265 (* Skip files with unique samples *)
270 process paths_by_digest path ~f:Digest.file
274 incr count.unique_sample;
277 Hashtbl.iter (fun d (n, ps) -> if n > 1 then output d n ps) paths_by_digest;
278 let t1 = Sys.time () in
279 eprintf "Time : %f seconds\n%!" (t1 -. t0);
280 eprintf "Considered : %d\n%!" !(count.considered);
281 eprintf "Hashed : %d\n%!" !(count.hashed);
282 eprintf "Skipped due to 0 size : %d\n%!" !(count.empty);
283 eprintf "Skipped due to unique size : %d\n%!" !(count.unique_size);
284 eprintf "Skipped due to unique sample : %d\n%!" !(count.unique_sample);
285 eprintf "Ignored due to regex match : %d\n%!" !(count.ignored)
287 let get_opt () : opt =
288 let assert_ test x msg =
289 if not (test x) then begin
290 eprintf "%s\n%!" msg;
294 let assert_file_exists path =
295 assert_ Sys.file_exists path (sprintf "File does not exist: %S" path)
297 let assert_file_is_dir path =
298 assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path)
300 let input = ref Stdin in
301 let output = ref Stdout in
302 let ignore = ref None in
303 let sample = ref 256 in
306 , Arg.String (fun path ->
307 assert_file_exists path;
308 assert_file_is_dir path;
309 output := Directory path
311 , " Output to this directory instead of stdout."
314 , Arg.String (fun regexp -> ignore := Some (Str.regexp regexp))
315 , " Ignore file paths which match this regexp pattern (see Str module)."
319 , (sprintf " Byte size of file samples to use. Default: %d" !sample)
326 assert_file_exists path;
327 assert_file_is_dir path;
330 input := Directories [path]
331 | Directories paths ->
332 input := Directories (path :: paths)
338 (sprintf "Sample size cannot be negative: %d" !sample);