Update README
[dups.git] / dups.ml
CommitLineData
cce97c27
SK
1open Printf
2
948ee900
SK
3module Array = ArrayLabels
4module List = ListLabels
8673c3a5 5module StrSet= Set.Make(String)
e09dff7f 6module Unix = UnixLabels
cce97c27
SK
7
8module Stream : sig
948ee900 9 type 'a t
e13e9ef5
SK
10
11 val create : (unit -> 'a option) -> 'a t
12
948ee900 13 val iter : 'a t -> f:('a -> unit) -> unit
8673c3a5 14
a9a56d74
SK
15 val map : 'a t -> f:('a -> 'b) -> 'b t
16
17 val filter : 'a t -> f:('a -> bool) -> 'a t
18
8673c3a5 19 val concat : ('a t) list -> 'a t
cce97c27
SK
20end = struct
21 module S = Stream
22
948ee900 23 type 'a t =
a9a56d74 24 {mutable streams : ('a S.t) list}
948ee900 25
e13e9ef5 26 let create f =
a9a56d74
SK
27 {streams = [S.from (fun _ -> f ())]}
28
29 let rec next t =
30 match t.streams with
31 | [] ->
32 None
33 | s :: streams ->
34 (match S.next s with
35 | exception Stream.Failure ->
36 t.streams <- streams;
37 next t
38 | x ->
39 Some x
40 )
41
42 let map t ~f =
43 create (fun () ->
44 match next t with
45 | None -> None
46 | Some x -> Some (f x)
47 )
48
49 let filter t ~f =
50 let rec filter () =
51 match next t with
52 | None ->
53 None
54 | Some x when f x ->
55 Some x
56 | Some _ ->
57 filter ()
58 in
59 create filter
e13e9ef5
SK
60
61 let iter t ~f =
a9a56d74 62 List.iter t.streams ~f:(S.iter f)
8673c3a5
SK
63
64 let concat ts =
a9a56d74 65 {streams = List.concat (List.map ts ~f:(fun {streams} -> streams))}
e13e9ef5
SK
66end
67
68module In_channel : sig
69 val lines : in_channel -> string Stream.t
70end = struct
71 let lines ic =
72 Stream.create (fun () ->
73 match input_line ic with
74 | exception End_of_file ->
75 None
76 | line ->
77 Some line
78 )
79end
80
a9a56d74
SK
81module File : sig
82 type t =
83 { path : string
84 ; size : int
85 }
86
87 val find : string -> t Stream.t
88 (** Find all files in the directory tree, starting from the given root path *)
89
90 val lookup : string Stream.t -> t Stream.t
91 (** Lookup file info for given paths *)
e13e9ef5 92end = struct
a9a56d74
SK
93 type t =
94 { path : string
95 ; size : int
96 }
97
98 let lookup paths =
99 Stream.map paths ~f:(fun path ->
100 let {Unix.st_size = size; _} = Unix.lstat path in
101 {path; size}
102 )
103
104 let find root =
948ee900
SK
105 let dirs = Queue.create () in
106 let files = Queue.create () in
948ee900
SK
107 let explore parent =
108 Array.iter (Sys.readdir parent) ~f:(fun child ->
109 let path = Filename.concat parent child in
a9a56d74 110 let {Unix.st_kind = file_kind; st_size; _} = Unix.lstat path in
948ee900
SK
111 match file_kind with
112 | Unix.S_REG ->
a9a56d74
SK
113 let file = {path; size = st_size} in
114 Queue.add file files
948ee900
SK
115 | Unix.S_DIR ->
116 Queue.add path dirs
117 | Unix.S_CHR
118 | Unix.S_BLK
119 | Unix.S_LNK
120 | Unix.S_FIFO
121 | Unix.S_SOCK ->
122 ()
123 )
124 in
1f130f74 125 explore root;
c66266c6
SK
126 let rec next () =
127 match Queue.is_empty files, Queue.is_empty dirs with
128 | false, _ -> Some (Queue.take files)
129 | true , true -> None
130 | true , false ->
131 explore (Queue.take dirs);
132 next ()
133 in
134 Stream.create next
cce97c27
SK
135end
136
948ee900 137type input =
cfcdf90a
SK
138 | Stdin
139 | Directories of string list
948ee900 140
e09dff7f
SK
141type output =
142 | Stdout
143 | Directory of string
144
a9a56d74
SK
145type count =
146 { considered : int ref
147 ; empty : int ref
148 ; ignored : int ref
149 ; unique_size : int ref
150 ; hashed : int ref
151 }
152
153let make_input_stream input ignore count =
154 let input =
155 match input with
156 | Stdin ->
157 File.lookup (In_channel.lines stdin)
158 | Directories paths ->
159 let paths = StrSet.elements (StrSet.of_list paths) in
160 Stream.concat (List.map paths ~f:File.find)
161 in
162 Stream.filter input ~f:(fun {File.path; size} ->
163 incr count.considered;
164 let empty = size = 0 in
165 let ignored =
166 match ignore with
167 | Some regexp when (Str.string_match regexp path 0) ->
168 true
169 | Some _ | None ->
170 false
171 in
172 if empty then incr count.empty;
173 if ignored then incr count.ignored;
174 (not empty) && (not ignored)
175 )
e09dff7f
SK
176
177let make_output_fun = function
178 | Stdout ->
179 fun digest n_paths paths ->
180 printf "%s %d\n%!" (Digest.to_hex digest) n_paths;
181 List.iter (StrSet.elements paths) ~f:(printf " %S\n%!")
182 | Directory dir ->
183 fun digest _ paths ->
184 let digest = Digest.to_hex digest in
185 let dir = Filename.concat dir (String.sub digest 0 2) in
186 Unix.mkdir dir ~perm:0o700;
187 let oc = open_out (Filename.concat dir digest) in
188 List.iter (StrSet.elements paths) ~f:(fun path ->
189 output_string oc (sprintf "%S\n%!" path)
190 );
191 close_out oc
192
34107832 193let main input output ignore =
a9a56d74
SK
194 let t0 = Sys.time () in
195 let count =
196 { considered = ref 0
197 ; empty = ref 0
198 ; ignored = ref 0
199 ; unique_size = ref 0
200 ; hashed = ref 0
201 }
202 in
e09dff7f 203 let output = make_output_fun output in
a9a56d74
SK
204 let input = make_input_stream input ignore count in
205 let paths_by_size = Hashtbl.create 1_000_000 in
cce97c27 206 let paths_by_digest = Hashtbl.create 1_000_000 in
a9a56d74
SK
207 let process tbl path ~f =
208 let key = f path in
209 let count, paths =
210 match Hashtbl.find_opt tbl key with
211 | None ->
212 (0, StrSet.empty)
213 | Some (n, paths) ->
214 (n, paths)
215 in
216 Hashtbl.replace tbl key (count + 1, StrSet.add path paths)
34107832 217 in
a9a56d74
SK
218 Stream.iter input ~f:(fun {File.path; size} ->
219 process paths_by_size path ~f:(fun _ -> size)
cce97c27 220 );
a9a56d74
SK
221 Hashtbl.iter
222 (fun _ (n, paths) ->
223 (* Skip files with unique sizes *)
224 if n > 1 then
225 StrSet.iter
226 (fun path ->
227 incr count.hashed;
228 process paths_by_digest path ~f:Digest.file
229 )
230 paths
231 else
232 incr count.unique_size;
233 )
234 paths_by_size;
e09dff7f 235 Hashtbl.iter (fun d (n, ps) -> if n > 1 then output d n ps) paths_by_digest;
f16e6ff1 236 let t1 = Sys.time () in
a9a56d74
SK
237 eprintf "Time : %f seconds\n%!" (t1 -. t0);
238 eprintf "Considered : %d\n%!" !(count.considered);
239 eprintf "Hashed : %d\n%!" !(count.hashed);
240 eprintf "Skipped due to 0 size : %d\n%!" !(count.empty);
241 eprintf "Skipped due to unique size : %d\n%!" !(count.unique_size);
242 eprintf "Ignored due to regex match : %d\n%!" !(count.ignored)
cce97c27
SK
243
244let () =
cfcdf90a 245 let input = ref Stdin in
e09dff7f 246 let output = ref Stdout in
34107832 247 let ignore = ref None in
e09dff7f
SK
248 let assert_file_exists path =
249 if Sys.file_exists path then
250 ()
251 else begin
252 eprintf "File does not exist: %S\n%!" path;
253 exit 1
254 end
255 in
256 let assert_file_is_dir path =
257 if Sys.is_directory path then
258 ()
259 else begin
260 eprintf "File is not a directory: %S\n%!" path;
261 exit 1
262 end
263 in
264 let spec =
265 [ ( "-out"
266 , Arg.String (fun path ->
267 assert_file_exists path;
268 assert_file_is_dir path;
269 output := Directory path
8673c3a5 270 )
e09dff7f
SK
271 , " Output to this directory instead of stdout."
272 )
34107832
SK
273 ; ( "-ignore"
274 , Arg.String (fun regexp -> ignore := Some (Str.regexp regexp))
275 , " Ignore file paths which match this regexp pattern (see Str module)."
276 )
e09dff7f
SK
277 ]
278 in
279 Arg.parse
280 (Arg.align spec)
281 (fun path ->
282 assert_file_exists path;
283 assert_file_is_dir path;
284 match !input with
cfcdf90a
SK
285 | Stdin ->
286 input := Directories [path]
287 | Directories paths ->
288 input := Directories (path :: paths)
61a05dbb
SK
289 )
290 "";
34107832 291 main !input !output !ignore
This page took 0.035679 seconds and 4 git commands to generate.