1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
open Stdune
open Result.O
open Cache_intf
type t =
{ root : Path.t
; build_root : Path.t option
; info : User_message.Style.t Pp.t list -> unit
; warn : User_message.Style.t Pp.t list -> unit
; repositories : repository list
; command_handler : command -> unit
; duplication_mode : Duplication_mode.t
; temp_dir : Path.t
}
module Trimming_result = struct
type t = { trimmed_bytes : int64 }
let empty = { trimmed_bytes = 0L }
let add t ~(bytes : int) =
{ trimmed_bytes = Int64.add t.trimmed_bytes (Int64.of_int bytes) }
end
let default_root () =
Path.L.relative
(Path.of_filename_relative_to_initial_cwd Xdg.cache_dir)
[ "dune"; "db" ]
let file_store_root cache = Path.L.relative cache.root [ "files"; "v4" ]
let metadata_store_root cache = Path.L.relative cache.root [ "meta"; "v4" ]
module type FSScheme = sig
val path : root:Path.t -> Digest.t -> Path.t
val list : root:Path.t -> Path.t list
end
module FSSchemeImpl = FirstTwoCharsSubdir
let metadata_path cache key =
FSSchemeImpl.path ~root:(metadata_store_root cache) key
let file_path cache key = FSSchemeImpl.path ~root:(file_store_root cache) key
module V3 = struct
let file_store_root cache = Path.L.relative cache.root [ "files"; "v3" ]
let metadata_store_root cache = Path.L.relative cache.root [ "meta"; "v3" ]
let file_path cache key = FSSchemeImpl.path ~root:(file_store_root cache) key
end
module Metadata_file = struct
type t =
{ metadata : Sexp.t list
; files : File.t list
}
let to_sexp { metadata; files } =
let open Sexp in
let f ({ path; digest } : File.t) =
Sexp.List
[ Sexp.Atom (Path.Local.to_string (Path.Build.local path))
; Sexp.Atom (Digest.to_string digest)
]
in
List
[ List (Atom "metadata" :: metadata)
; List (Atom "files" :: List.map ~f files)
]
let of_sexp = function
| Sexp.List
[ List (Atom "metadata" :: metadata); List (Atom "files" :: produced) ]
->
let+ files =
Result.List.map produced ~f:(function
| List [ Atom path; Atom digest ] ->
let path = Path.Build.of_string path in
let+ digest =
match Digest.from_hex digest with
| Some digest -> Ok digest
| None -> Error "Invalid digest in cache metadata"
in
{ File.path; digest }
| _ -> Error "Invalid list of produced files in cache metadata")
in
{ metadata; files }
| _ -> Error "Invalid cache metadata"
let of_string s =
match Csexp.parse_string s with
| Ok sexp -> of_sexp sexp
| Error (_, msg) -> Error msg
let to_string f = to_sexp f |> Csexp.to_string
let parse path = Io.with_file_in path ~f:Csexp.input >>= of_sexp
end
let tmp_path cache name =
let res = Path.relative cache.temp_dir name in
Path.mkdir_p res;
res
let make_path cache path =
match cache.build_root with
| Some p -> Result.ok (Path.append_local p path)
| None ->
Result.Error
(sprintf "relative path %s while no build root was set"
(Path.Local.to_string_maybe_quoted path))
let with_repositories cache repositories = Result.Ok { cache with repositories }
let duplicate ?(duplication = None) cache ~src ~dst =
match Option.value ~default:cache.duplication_mode duplication with
| Copy -> Io.copy_file ~src ~dst ()
| Hardlink -> Path.link src dst
let retrieve cache (file : File.t) =
let src = file_path cache file.digest in
let dst = Path.build file.path in
cache.info
[ Pp.textf "retrieve %s from cache" (Path.to_string_maybe_quoted dst) ];
duplicate cache ~src ~dst;
dst
let deduplicate cache (file : File.t) =
match cache.duplication_mode with
| Copy -> ()
| Hardlink -> (
let path = Path.build file.path in
let path_in_cache = file_path cache file.digest in
let tmpname = Path.build (Path.Build.of_string ".dedup") in
cache.info
[ Pp.textf "deduplicate %s from %s" (Path.to_string path)
(Path.to_string path_in_cache)
];
try
Path.unlink_no_err tmpname;
Path.link path_in_cache tmpname;
Path.rename tmpname path
with
| Unix.Unix_error (e, syscall, _) ->
Path.unlink_no_err tmpname;
cache.warn
[ Pp.textf "error handling dune-cache command: %s: %s" syscall
(Unix.error_message e)
])
let apply ~f o v =
match o with
| Some o -> f v o
| None -> v
let promote_sync cache paths key metadata ~repository ~duplication =
let open Result.O in
let* repo =
match repository with
| None -> Result.Ok None
| Some idx -> (
match List.nth cache.repositories idx with
| None -> Result.Error (Printf.sprintf "repository out of range: %i" idx)
| repo -> Result.Ok repo)
in
let metadata =
apply
~f:(fun metadata repository ->
metadata
@ [ Sexp.List [ Sexp.Atom "repo"; Sexp.Atom repository.remote ]
; Sexp.List [ Sexp.Atom "commit_id"; Sexp.Atom repository.commit ]
])
repo metadata
in
let promote (path, expected_digest) =
let* abs_path = make_path cache (Path.Build.local path) in
cache.info [ Pp.textf "promote %s" (Path.to_string abs_path) ];
let stat = Path.lstat abs_path in
let* stat =
if stat.st_kind = S_REG then
Result.Ok stat
else
Result.Error
(Format.sprintf "invalid file type: %s"
(Path.string_of_file_kind stat.st_kind))
in
Temp.with_temp_path ~dir:cache.temp_dir ~prefix:"temp" ~suffix:"data"
~f:(function
| Error exn ->
let message =
sprintf "Failed to create a temp file: %s"
(Exn.to_dyn exn |> Dyn.to_string)
in
cache.info [ Pp.text message ];
Result.Error message
| Ok tmp -> (
duplicate ~duplication cache ~src:abs_path ~dst:tmp;
let effective_digest = Digest.file_with_stats tmp (Path.stat tmp) in
if Digest.compare effective_digest expected_digest != Ordering.Eq then (
let message =
Printf.sprintf "digest mismatch: %s != %s"
(Digest.to_string effective_digest)
(Digest.to_string expected_digest)
in
cache.info [ Pp.text message ];
Result.Error message
) else
let in_the_cache = file_path cache effective_digest in
match Path.exists in_the_cache with
| true ->
Path.touch in_the_cache;
Result.Ok (Already_promoted { path; digest = effective_digest })
| false ->
Path.mkdir_p (Path.parent_exn in_the_cache);
Path.rename tmp in_the_cache;
Path.chmod in_the_cache ~mode:(stat.st_perm land 0o555);
Result.Ok (Promoted { path; digest = effective_digest })))
in
let+ promoted = Result.List.map ~f:promote paths in
let metadata_path = metadata_path cache key
and metadata_tmp_path = Path.relative cache.temp_dir "metadata"
and files =
List.map promoted ~f:(function
| Already_promoted f
| Promoted f
-> f)
in
let metadata_file : Metadata_file.t = { metadata; files } in
let metadata = Csexp.to_string (Metadata_file.to_sexp metadata_file) in
Io.write_file metadata_tmp_path metadata;
let () =
match Io.read_file metadata_path with
| contents ->
if contents <> metadata then
User_warning.emit
[ Pp.textf "non reproductible collision on rule %s"
(Digest.to_string key)
]
| exception Sys_error _ -> Path.mkdir_p (Path.parent_exn metadata_path)
in
Path.rename metadata_tmp_path metadata_path;
(match cache.duplication_mode with
| Copy -> ()
| Hardlink ->
List.iter promoted ~f:(function
| Already_promoted file -> cache.command_handler (Dedup file)
| _ -> ()));
(metadata_file, promoted)
let promote cache paths key metadata ~repository ~duplication =
Result.map ~f:ignore
(promote_sync cache paths key metadata ~repository ~duplication)
let search cache key =
let path = metadata_path cache key in
let* sexp =
try Io.with_file_in path ~f:Csexp.input with
| Sys_error _ -> Error "no cached file"
in
let+ metadata = Metadata_file.of_sexp sexp in
let () =
let f (file : File.t) =
try Path.touch ~create:false (file_path cache file.digest) with
| Unix.(Unix_error (ENOENT, _, _)) -> ()
in
List.iter ~f metadata.files
in
(metadata.metadata, metadata.files)
let set_build_dir cache p = Result.Ok { cache with build_root = Some p }
let teardown cache = Temp.destroy Dir cache.temp_dir
let hint _ _ = Result.Ok ()
let detect_duplication_mode root =
let () = Path.mkdir_p root in
let beacon = Path.relative root "beacon"
and target = Path.relative Path.build_dir ".cache-beacon" in
let () = Path.touch beacon in
let rec test () =
match Path.link beacon target with
| exception Unix.Unix_error (Unix.EEXIST, _, _) ->
Path.unlink_no_err target;
test ()
| exception Unix.Unix_error _ -> Duplication_mode.Copy
| () -> Duplication_mode.Hardlink
in
test ()
let make ?(root = default_root ())
?(duplication_mode = detect_duplication_mode root)
?(log = Dune_util.Log.info) ?(warn = fun pp -> User_warning.emit pp)
~command_handler () =
let res =
{ root
; build_root = None
; info = log
; warn
; repositories = []
; command_handler
; duplication_mode
; temp_dir =
Temp.temp_in_dir ~perms:0o700 Temp.Dir ~dir:root ~prefix:"promoting."
~suffix:("." ^ string_of_int (Unix.getpid ()))
}
in
match
Path.mkdir_p @@ file_store_root res;
Path.mkdir_p @@ metadata_store_root res
with
| () -> Ok res
| exception exn ->
Error
("Unable to set up the cache root directory: " ^ Printexc.to_string exn)
let duplication_mode cache = cache.duplication_mode
let trim_bad_metadata_files ~metadata_files ~trimmed_so_far ~file_path cache =
List.fold_left metadata_files ~init:trimmed_so_far
~f:(fun trimmed_so_far path ->
let should_be_removed =
match Metadata_file.parse path with
| Result.Error msg ->
cache.warn
[ Pp.textf "remove invalid metadata file %s: %s"
(Path.to_string_maybe_quoted path)
msg
];
true
| Result.Ok { Metadata_file.files; _ } ->
let is_broken =
List.exists files ~f:(fun { File.digest; _ } ->
let reference = file_path cache digest in
not (Path.exists reference))
in
if is_broken then
cache.info
[ Pp.textf "remove metadata file %s as it refers to missing files"
(Path.to_string_maybe_quoted path)
];
is_broken
in
match should_be_removed with
| true ->
let bytes = (Path.stat path).st_size in
Path.unlink_no_err path;
Trimming_result.add trimmed_so_far ~bytes
| false -> trimmed_so_far)
let garbage_collect_impl ~trimmed_so_far cache =
let metadata_files = FSSchemeImpl.list ~root:(V3.metadata_store_root cache) in
let trimmed_so_far =
trim_bad_metadata_files ~metadata_files ~trimmed_so_far
~file_path:V3.file_path cache
in
let metadata_files = FSSchemeImpl.list ~root:(metadata_store_root cache) in
trim_bad_metadata_files ~metadata_files ~trimmed_so_far ~file_path cache
let garbage_collect = garbage_collect_impl ~trimmed_so_far:Trimming_result.empty
let file_exists_and_is_unused ~stats = stats.Unix.st_nlink = 1
let files_in_cache_for_all_supported_versions cache =
let files = FSSchemeImpl.list ~root:(file_store_root cache) in
let files_v3 = FSSchemeImpl.list ~root:(V3.file_store_root cache) in
files @ files_v3
let trim cache ~goal =
let files = files_in_cache_for_all_supported_versions cache in
let f path =
let stats = Path.stat path in
if file_exists_and_is_unused ~stats then
Some (path, stats.st_size, stats.st_ctime)
else
None
and compare (_, _, t1) (_, _, t2) = Ordering.of_int (Stdlib.compare t1 t2) in
let files = List.sort ~compare (List.filter_map ~f files)
and delete (trimmed_so_far : Trimming_result.t) (path, bytes, _) =
if trimmed_so_far.trimmed_bytes >= goal then
trimmed_so_far
else (
Path.unlink path;
Trimming_result.add trimmed_so_far ~bytes
)
in
let trimmed_so_far =
List.fold_left ~init:Trimming_result.empty ~f:delete files
in
garbage_collect_impl cache ~trimmed_so_far
let overhead_size cache =
let files = files_in_cache_for_all_supported_versions cache in
let stats =
let f p =
try
let stats = Path.stat p in
if file_exists_and_is_unused ~stats then
Int64.of_int stats.st_size
else
0L
with
| Unix.Unix_error (Unix.ENOENT, _, _) -> 0L
in
List.map ~f files
in
List.fold_left ~f:Int64.add ~init:0L stats