Skip to content

Commit cc43fa0

Browse files
Julowjonludlam
authored andcommitted
Safer index.m lookup and marshalling
1 parent 0b65038 commit cc43fa0

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/driver/compile.ml

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,35 @@ open Eio.Std
7070

7171
type partial = (string * compiled) list * Packages.modulety Util.StringMap.t
7272

73-
let unmarshal filename =
73+
let unmarshal filename : partial =
7474
let ic = open_in_bin (Fpath.to_string filename) in
75-
let (v : partial) = Marshal.from_channel ic in
76-
close_in ic;
77-
v
75+
Fun.protect
76+
~finally:(fun () -> close_in ic)
77+
(fun () -> Marshal.from_channel ic)
7878

7979
let marshal (v : partial) filename =
8080
let p = Fpath.parent filename in
8181
Util.mkdir_p p;
8282
let oc = open_out_bin (Fpath.to_string filename) in
83-
Marshal.to_channel oc v [];
84-
close_out oc
83+
Fun.protect
84+
~finally:(fun () -> close_out oc)
85+
(fun () -> Marshal.to_channel oc v [])
8586

8687
let find_partials odoc_dir =
8788
let tbl = Hashtbl.create 1000 in
8889
let hashes_result =
89-
Bos.OS.Dir.fold_contents ~dotfiles:false
90+
Bos.OS.Dir.fold_contents ~dotfiles:false ~elements:`Dirs
9091
(fun p hashes ->
91-
if Fpath.filename p = "index.m" then (
92-
let tbl', hashes' = unmarshal p in
93-
List.iter
94-
(fun (k, v) ->
95-
Hashtbl.replace tbl k (Promise.create_resolved (Ok v)))
96-
tbl';
97-
Util.StringMap.union (fun _x o1 _o2 -> Some o1) hashes hashes')
98-
else hashes)
92+
let index_m = Fpath.( / ) p "index.m" in
93+
match Bos.OS.File.exists index_m with
94+
| Ok true ->
95+
let tbl', hashes' = unmarshal index_m in
96+
List.iter
97+
(fun (k, v) ->
98+
Hashtbl.replace tbl k (Promise.create_resolved (Ok v)))
99+
tbl';
100+
Util.StringMap.union (fun _x o1 _o2 -> Some o1) hashes hashes'
101+
| _ -> hashes)
99102
Util.StringMap.empty odoc_dir
100103
in
101104
match hashes_result with

0 commit comments

Comments
 (0)