| 1 | open Printf |
| 2 | |
| 3 | module Array = ArrayLabels |
| 4 | module List = ListLabels |
| 5 | module StrSet = Set.Make(String) |
| 6 | module Unix = UnixLabels |
| 7 | |
| 8 | module Metrics : sig |
| 9 | type t |
| 10 | |
| 11 | val init |
| 12 | : unit -> t |
| 13 | val report |
| 14 | : t |
| 15 | -> time_all:float |
| 16 | -> time_group_by_size:float |
| 17 | -> time_group_by_head:float |
| 18 | -> time_group_by_digest:float |
| 19 | -> unit |
| 20 | |
| 21 | val file_considered |
| 22 | : t -> size:int -> unit |
| 23 | val file_ignored |
| 24 | : t -> size:int -> unit |
| 25 | val file_empty |
| 26 | : t -> unit |
| 27 | val file_sampled |
| 28 | : t -> unit |
| 29 | val chunk_read |
| 30 | : t -> size:int -> unit |
| 31 | val file_unique_size |
| 32 | : t -> size:int -> unit |
| 33 | val file_unique_sample |
| 34 | : t -> size:int -> unit |
| 35 | val file_hashed |
| 36 | : t -> size:int -> unit |
| 37 | val digest |
| 38 | : t -> unit |
| 39 | val redundant_data |
| 40 | : t -> size:int -> unit |
| 41 | end = struct |
| 42 | type t = |
| 43 | { considered_files : int ref |
| 44 | ; considered_bytes : int ref |
| 45 | ; empty : 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 |
| 56 | ; digests : int ref |
| 57 | ; redundant_data : int ref |
| 58 | } |
| 59 | |
| 60 | let init () = |
| 61 | { considered_files = ref 0 |
| 62 | ; considered_bytes = ref 0 |
| 63 | ; empty = 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 |
| 74 | ; digests = ref 0 |
| 75 | ; redundant_data = ref 0 |
| 76 | } |
| 77 | |
| 78 | let add sum addend = |
| 79 | sum := !sum + addend |
| 80 | |
| 81 | let file_considered t ~size = |
| 82 | incr t.considered_files; |
| 83 | add t.considered_bytes size |
| 84 | |
| 85 | let file_ignored {ignored_files; ignored_bytes; _} ~size = |
| 86 | incr ignored_files; |
| 87 | add ignored_bytes size |
| 88 | |
| 89 | let file_empty t = |
| 90 | incr t.empty |
| 91 | |
| 92 | let chunk_read t ~size = |
| 93 | add t.sampled_bytes size |
| 94 | |
| 95 | let file_sampled t = |
| 96 | incr t.sampled_files |
| 97 | |
| 98 | let file_unique_size t ~size = |
| 99 | incr t.unique_size_files; |
| 100 | add t.unique_size_bytes size |
| 101 | |
| 102 | let file_unique_sample t ~size = |
| 103 | incr t.unique_sample_files; |
| 104 | add t.unique_sample_bytes size |
| 105 | |
| 106 | let file_hashed t ~size = |
| 107 | incr t.hashed_files; |
| 108 | add t.hashed_bytes size |
| 109 | |
| 110 | let digest t = |
| 111 | incr t.digests |
| 112 | |
| 113 | let redundant_data t ~size = |
| 114 | add t.redundant_data size |
| 115 | |
| 116 | let report |
| 117 | t |
| 118 | ~time_all |
| 119 | ~time_group_by_size |
| 120 | ~time_group_by_head |
| 121 | ~time_group_by_digest |
| 122 | = |
| 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%!" |
| 126 | time_all; |
| 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%!" |
| 131 | !(t.sampled_files) |
| 132 | (b_to_gb !(t.sampled_bytes)); |
| 133 | eprintf "Hashed : %8d files %6.2f Gb %6.2f seconds\n%!" |
| 134 | !(t.hashed_files) |
| 135 | (b_to_gb !(t.hashed_bytes)) |
| 136 | time_group_by_digest; |
| 137 | eprintf "Digests : %8d\n%!" |
| 138 | !(t.digests); |
| 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)) |
| 146 | time_group_by_size; |
| 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)) |
| 150 | time_group_by_head; |
| 151 | eprintf "Ignored due to regex match : %8d files %6.2f Gb\n%!" |
| 152 | !(t.ignored_files) |
| 153 | (b_to_gb !(t.ignored_bytes)) |
| 154 | end |
| 155 | |
| 156 | module M = Metrics |
| 157 | |
| 158 | module Stream : sig |
| 159 | type 'a t |
| 160 | |
| 161 | val create : (unit -> 'a option) -> 'a t |
| 162 | |
| 163 | val of_queue : 'a Queue.t -> 'a t |
| 164 | |
| 165 | val iter : 'a t -> f:('a -> unit) -> unit |
| 166 | |
| 167 | val map : 'a t -> f:('a -> 'b) -> 'b t |
| 168 | |
| 169 | val filter : 'a t -> f:('a -> bool) -> 'a t |
| 170 | |
| 171 | val concat : ('a t) list -> 'a t |
| 172 | |
| 173 | val group_by : 'a t -> f:('a -> 'b) -> ('b * int * 'a list) t |
| 174 | end = struct |
| 175 | module S = Stream |
| 176 | |
| 177 | type 'a t = |
| 178 | {mutable streams : ('a S.t) list} |
| 179 | |
| 180 | let create f = |
| 181 | {streams = [S.from (fun _ -> f ())]} |
| 182 | |
| 183 | let of_queue q = |
| 184 | create (fun () -> |
| 185 | match Queue.take q with |
| 186 | | exception Queue.Empty -> |
| 187 | None |
| 188 | | x -> |
| 189 | Some x |
| 190 | ) |
| 191 | |
| 192 | let rec next t = |
| 193 | match t.streams with |
| 194 | | [] -> |
| 195 | None |
| 196 | | s :: streams -> |
| 197 | (match S.next s with |
| 198 | | exception Stream.Failure -> |
| 199 | t.streams <- streams; |
| 200 | next t |
| 201 | | x -> |
| 202 | Some x |
| 203 | ) |
| 204 | |
| 205 | let map t ~f = |
| 206 | create (fun () -> |
| 207 | match next t with |
| 208 | | None -> None |
| 209 | | Some x -> Some (f x) |
| 210 | ) |
| 211 | |
| 212 | let filter t ~f = |
| 213 | let rec filter () = |
| 214 | match next t with |
| 215 | | None -> |
| 216 | None |
| 217 | | Some x when f x -> |
| 218 | Some x |
| 219 | | Some _ -> |
| 220 | filter () |
| 221 | in |
| 222 | create filter |
| 223 | |
| 224 | let iter t ~f = |
| 225 | List.iter t.streams ~f:(S.iter f) |
| 226 | |
| 227 | let concat ts = |
| 228 | {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))} |
| 229 | |
| 230 | let group_by t ~f = |
| 231 | let groups_tbl = Hashtbl.create 1_000_000 in |
| 232 | let group_update x = |
| 233 | let group = f x in |
| 234 | let members = |
| 235 | match Hashtbl.find_opt groups_tbl group with |
| 236 | | None -> |
| 237 | (1, [x]) |
| 238 | | Some (n, xs) -> |
| 239 | (succ n, x :: xs) |
| 240 | in |
| 241 | Hashtbl.replace groups_tbl group members |
| 242 | in |
| 243 | iter t ~f:group_update; |
| 244 | let groups = Queue.create () in |
| 245 | Hashtbl.iter |
| 246 | (fun name (length, members) -> Queue.add (name, length, members) groups) |
| 247 | groups_tbl; |
| 248 | of_queue groups |
| 249 | end |
| 250 | |
| 251 | module In_channel : sig |
| 252 | val lines : in_channel -> string Stream.t |
| 253 | end = struct |
| 254 | let lines ic = |
| 255 | Stream.create (fun () -> |
| 256 | match input_line ic with |
| 257 | | exception End_of_file -> |
| 258 | None |
| 259 | | line -> |
| 260 | Some line |
| 261 | ) |
| 262 | end |
| 263 | |
| 264 | module File : sig |
| 265 | type t = |
| 266 | { path : string |
| 267 | ; size : int |
| 268 | } |
| 269 | |
| 270 | val find : string -> t Stream.t |
| 271 | (** Find all files in the directory tree, starting from the given root path *) |
| 272 | |
| 273 | val lookup : string Stream.t -> t Stream.t |
| 274 | (** Lookup file info for given paths *) |
| 275 | |
| 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 |
| 278 | end = struct |
| 279 | type t = |
| 280 | { path : string |
| 281 | ; size : int |
| 282 | } |
| 283 | |
| 284 | let lookup paths = |
| 285 | Stream.map paths ~f:(fun path -> |
| 286 | let {Unix.st_size = size; _} = Unix.lstat path in |
| 287 | {path; size} |
| 288 | ) |
| 289 | |
| 290 | let find root = |
| 291 | let dirs = Queue.create () in |
| 292 | let files = Queue.create () in |
| 293 | let explore parent = |
| 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 |
| 297 | match file_kind with |
| 298 | | Unix.S_REG -> |
| 299 | let file = {path; size = st_size} in |
| 300 | Queue.add file files |
| 301 | | Unix.S_DIR -> |
| 302 | Queue.add path dirs |
| 303 | | Unix.S_CHR |
| 304 | | Unix.S_BLK |
| 305 | | Unix.S_LNK |
| 306 | | Unix.S_FIFO |
| 307 | | Unix.S_SOCK -> |
| 308 | () |
| 309 | ) |
| 310 | in |
| 311 | explore root; |
| 312 | let rec next () = |
| 313 | match Queue.is_empty files, Queue.is_empty dirs with |
| 314 | | false, _ -> Some (Queue.take files) |
| 315 | | true , true -> None |
| 316 | | true , false -> |
| 317 | explore (Queue.take dirs); |
| 318 | next () |
| 319 | in |
| 320 | Stream.create next |
| 321 | |
| 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 |
| 326 | if n > 1 then |
| 327 | List.iter members ~f:(fun m -> Queue.add m q) |
| 328 | else |
| 329 | handle_singleton group |
| 330 | ); |
| 331 | Stream.of_queue q |
| 332 | |
| 333 | let filter_out_unique_sizes files ~metrics = |
| 334 | filter_out_singletons |
| 335 | files |
| 336 | ~group:(fun {size; _} -> size) |
| 337 | ~handle_singleton:(fun (size, _, _) -> M.file_unique_size metrics ~size) |
| 338 | |
| 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 = |
| 343 | assert (len >= 0); |
| 344 | if len = 0 then |
| 345 | () |
| 346 | else begin |
| 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 *) |
| 350 | () |
| 351 | else |
| 352 | read (pos + chunk_size) (len - chunk_size) |
| 353 | end |
| 354 | in |
| 355 | read 0 len; |
| 356 | close_in ic; |
| 357 | Bytes.to_string buf |
| 358 | |
| 359 | let filter_out_unique_heads files ~len ~metrics = |
| 360 | filter_out_singletons |
| 361 | files |
| 362 | ~group:(fun {path; _} -> |
| 363 | M.file_sampled metrics; |
| 364 | head path ~len ~metrics |
| 365 | ) |
| 366 | ~handle_singleton:(fun (_, _, files) -> |
| 367 | let {size; _} = List.hd files in (* Guaranteed non-empty *) |
| 368 | M.file_unique_sample metrics ~size |
| 369 | ) |
| 370 | end |
| 371 | |
| 372 | type input = |
| 373 | | Stdin |
| 374 | | Directories of string list |
| 375 | |
| 376 | type output = |
| 377 | | Stdout |
| 378 | | Directory of string |
| 379 | |
| 380 | type opt = |
| 381 | { input : input |
| 382 | ; output : output |
| 383 | ; ignore : Str.regexp option |
| 384 | ; sample : int |
| 385 | } |
| 386 | |
| 387 | let make_input_stream input ignore ~metrics = |
| 388 | let input = |
| 389 | match input with |
| 390 | | Stdin -> |
| 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) |
| 395 | in |
| 396 | Stream.filter input ~f:(fun {File.path; size} -> |
| 397 | M.file_considered metrics ~size; |
| 398 | let empty = size = 0 in |
| 399 | if empty then M.file_empty metrics; |
| 400 | let ignored = |
| 401 | match ignore with |
| 402 | | Some regexp when (Str.string_match regexp path 0) -> |
| 403 | M.file_ignored metrics ~size; |
| 404 | true |
| 405 | | Some _ | None -> |
| 406 | false |
| 407 | in |
| 408 | (not empty) && (not ignored) |
| 409 | ) |
| 410 | |
| 411 | let make_output_fun = function |
| 412 | | Stdout -> |
| 413 | fun digest n_files files -> |
| 414 | printf "%s %d\n%!" (Digest.to_hex digest) n_files; |
| 415 | List.iter files ~f:(fun {File.path; _} -> |
| 416 | printf " %S\n%!" path |
| 417 | ) |
| 418 | | Directory dir -> |
| 419 | fun digest _ files -> |
| 420 | let digest = Digest.to_hex digest in |
| 421 | let dir = Filename.concat dir (String.sub digest 0 2) in |
| 422 | Unix.mkdir dir ~perm:0o700; |
| 423 | let oc = open_out (Filename.concat dir digest) in |
| 424 | List.iter files ~f:(fun {File.path; _} -> |
| 425 | output_string oc (sprintf "%S\n%!" path) |
| 426 | ); |
| 427 | close_out oc |
| 428 | |
| 429 | let main {input; output; ignore; sample = sample_len} = |
| 430 | let t0_all = Sys.time () in |
| 431 | let metrics = M.init () in |
| 432 | let output = make_output_fun output in |
| 433 | let input = make_input_stream input ignore ~metrics in |
| 434 | (* TODO: Make a nice(r) abstraction to re-assemble pieces in the pipeline: |
| 435 | * |
| 436 | * from input to files_by_size |
| 437 | * from files_by_size to files_by_sample |
| 438 | * from files_by_sample to files_by_digest |
| 439 | * from files_by_digest to output |
| 440 | * |
| 441 | * input |> files_by_size |> files_by_sample |> files_by_digest |> output |
| 442 | *) |
| 443 | |
| 444 | let files = input in |
| 445 | |
| 446 | let t0_group_by_size = Sys.time () in |
| 447 | let files = File.filter_out_unique_sizes files ~metrics in |
| 448 | let t1_group_by_size = Sys.time () in |
| 449 | |
| 450 | let t0_group_by_sample = t1_group_by_size in |
| 451 | let files = File.filter_out_unique_heads files ~len:sample_len ~metrics in |
| 452 | let t1_group_by_sample = Sys.time () in |
| 453 | |
| 454 | let t0_group_by_digest = t1_group_by_sample in |
| 455 | let groups = |
| 456 | Stream.group_by files ~f:(fun {File.path; size} -> |
| 457 | M.file_hashed metrics ~size; |
| 458 | Digest.file path |
| 459 | ) |
| 460 | in |
| 461 | let t1_group_by_digest = Sys.time () in |
| 462 | |
| 463 | Stream.iter groups ~f:(fun (d, n, files) -> |
| 464 | M.digest metrics; |
| 465 | if n > 1 then |
| 466 | M.redundant_data metrics ~size:(n * (List.hd files).File.size); |
| 467 | output d n files |
| 468 | ); |
| 469 | |
| 470 | let t1_all = Sys.time () in |
| 471 | |
| 472 | M.report metrics |
| 473 | ~time_all: (t1_all -. t0_all) |
| 474 | ~time_group_by_size: (t1_group_by_size -. t0_group_by_size) |
| 475 | ~time_group_by_head: (t1_group_by_sample -. t0_group_by_sample) |
| 476 | ~time_group_by_digest:(t1_group_by_digest -. t0_group_by_digest) |
| 477 | |
| 478 | let get_opt () : opt = |
| 479 | let assert_ test x msg = |
| 480 | if not (test x) then begin |
| 481 | eprintf "%s\n%!" msg; |
| 482 | exit 1 |
| 483 | end |
| 484 | in |
| 485 | let assert_file_exists path = |
| 486 | assert_ Sys.file_exists path (sprintf "File does not exist: %S" path) |
| 487 | in |
| 488 | let assert_file_is_dir path = |
| 489 | assert_ Sys.is_directory path (sprintf "File is not a directory: %S" path) |
| 490 | in |
| 491 | let input = ref Stdin in |
| 492 | let output = ref Stdout in |
| 493 | let ignore = ref None in |
| 494 | let sample = ref 256 in |
| 495 | let spec = |
| 496 | [ ( "-out" |
| 497 | , Arg.String (fun path -> |
| 498 | assert_file_exists path; |
| 499 | assert_file_is_dir path; |
| 500 | output := Directory path |
| 501 | ) |
| 502 | , " Output to this directory instead of stdout." |
| 503 | ) |
| 504 | ; ( "-ignore" |
| 505 | , Arg.String (fun regexp -> ignore := Some (Str.regexp regexp)) |
| 506 | , " Ignore file paths which match this regexp pattern (see Str module)." |
| 507 | ) |
| 508 | ; ( "-sample" |
| 509 | , Arg.Set_int sample |
| 510 | , (sprintf " Byte size of file samples to use. Default: %d" !sample) |
| 511 | ) |
| 512 | ] |
| 513 | in |
| 514 | Arg.parse |
| 515 | (Arg.align spec) |
| 516 | (fun path -> |
| 517 | assert_file_exists path; |
| 518 | assert_file_is_dir path; |
| 519 | match !input with |
| 520 | | Stdin -> |
| 521 | input := Directories [path] |
| 522 | | Directories paths -> |
| 523 | input := Directories (path :: paths) |
| 524 | ) |
| 525 | ""; |
| 526 | assert_ |
| 527 | (fun x -> x > 0) |
| 528 | !sample |
| 529 | (sprintf "Sample size cannot be negative: %d" !sample); |
| 530 | { input = !input |
| 531 | ; output = !output |
| 532 | ; ignore = !ignore |
| 533 | ; sample = !sample |
| 534 | } |
| 535 | |
| 536 | let () = |
| 537 | main (get_opt ()) |