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
152 { considered : int ref
155 ; unique_size : int ref
159 let make_input_stream input ignore count =
163 File.lookup (In_channel.lines stdin)
164 | Directories paths ->
165 let paths = StrSet.elements (StrSet.of_list paths) in
166 Stream.concat (List.map paths ~f:File.find)
168 Stream.filter input ~f:(fun {File.path; size} ->
169 incr count.considered;
170 let empty = size = 0 in
173 | Some regexp when (Str.string_match regexp path 0) ->
178 if empty then incr count.empty;
179 if ignored then incr count.ignored;
180 (not empty) && (not ignored)
183 let make_output_fun = function
185 fun digest n_paths paths ->
186 printf "%s %d\n%!" (Digest.to_hex digest) n_paths;
187 List.iter (StrSet.elements paths) ~f:(printf " %S\n%!")
189 fun digest _ paths ->
190 let digest = Digest.to_hex digest in
191 let dir = Filename.concat dir (String.sub digest 0 2) in
192 Unix.mkdir dir ~perm:0o700;
193 let oc = open_out (Filename.concat dir digest) in
194 List.iter (StrSet.elements paths) ~f:(fun path ->
195 output_string oc (sprintf "%S\n%!" path)
199 let main {input; output; ignore} =
200 let t0 = Sys.time () in
205 ; unique_size = ref 0
209 let output = make_output_fun output in
210 let input = make_input_stream input ignore count in
211 let paths_by_size = Hashtbl.create 1_000_000 in
212 let paths_by_digest = Hashtbl.create 1_000_000 in
213 let process tbl path ~f =
216 match Hashtbl.find_opt tbl key with
222 Hashtbl.replace tbl key (count + 1, StrSet.add path paths)
224 Stream.iter input ~f:(fun {File.path; size} ->
225 process paths_by_size path ~f:(fun _ -> size)
229 (* Skip files with unique sizes *)
234 process paths_by_digest path ~f:Digest.file
238 incr count.unique_size;
241 Hashtbl.iter (fun d (n, ps) -> if n > 1 then output d n ps) paths_by_digest;
242 let t1 = Sys.time () in
243 eprintf "Time : %f seconds\n%!" (t1 -. t0);
244 eprintf "Considered : %d\n%!" !(count.considered);
245 eprintf "Hashed : %d\n%!" !(count.hashed);
246 eprintf "Skipped due to 0 size : %d\n%!" !(count.empty);
247 eprintf "Skipped due to unique size : %d\n%!" !(count.unique_size);
248 eprintf "Ignored due to regex match : %d\n%!" !(count.ignored)
250 let get_opt () : opt =
251 let assert_ test x msg =
252 if not (test x) then begin
253 eprintf "%s\n%!" msg;
257 let assert_file_exists path =
258 assert_ Sys.file_exists path (sprintf "File does not exist: %S" path)
260 let assert_file_is_dir path =
261 assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path)
263 let input = ref Stdin in
264 let output = ref Stdout in
265 let ignore = ref None in
268 , Arg.String (fun path ->
269 assert_file_exists path;
270 assert_file_is_dir path;
271 output := Directory path
273 , " Output to this directory instead of stdout."
276 , Arg.String (fun regexp -> ignore := Some (Str.regexp regexp))
277 , " Ignore file paths which match this regexp pattern (see Str module)."
284 assert_file_exists path;
285 assert_file_is_dir path;
288 input := Directories [path]
289 | Directories paths ->
290 input := Directories (path :: paths)