Debian Codegraph @

lib/objFile.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Pp
12 open System
13
14 let magic_number = 0x436F7121l (* "Coq!" *)
15
16 let error_corrupted file s =
17 CErrors.user_err (str file ++ str ": " ++ str s ++ str ". Try to rebuild it.")
18
19 let open_trapping_failure name =
20 try open_out_bin name
21 with e when CErrors.noncritical e ->
22 CErrors.user_err (str "Can't open " ++ str name ++ spc() ++ str "(" ++ CErrors.print e ++ str ").")
23
24 (*
25
26 int32: big-endian, 4 bytes
27 int64: big-endian, 8 bytes
28
29 -- string --
30 int32 | length of the next field
31 data |
32
33 -- segment summary --
34 string | name
35 int64 | absolute position
36 int64 | length (without hash)
37 hash | MD5 (16 bytes)
38
39 -- segment --
40 ... | binary data
41 hash | MD5 (16 bytes)
42
43 -- summary --
44 int32 | number of segment summaries
45 s1 |
46 ... | segment summaries
47 sn |
48
49 -- vo --
50 int32 | magic number
51 int32 | Coq version
52 int64 | absolute position of the summary
53 ... | segments
54 summary |
55
56 *)
57
58 type 'a segment = {
59 name : string;
60 pos : int64;
61 len : int64;
62 hash : Digest.t;
63 }
64
65 type in_handle = {
66 in_filename : string;
67 in_channel : in_channel;
68 in_segments : Obj.t segment CString.Map.t;
69 }
70
71 type out_handle = {
72 out_filename : string;
73 out_channel : out_channel;
74 mutable out_segments : Obj.t segment CString.Map.t;
75 }
76
77 type 'a id = { id : string }
78
79 let make_id id = { id }
80
81 let input_segment_summary ch =
82 let nlen = input_int32 ch in
83 let name = really_input_string ch (Int32.to_int nlen) in
84 let pos = input_int64 ch in
85 let len = input_int64 ch in
86 let hash = Digest.input ch in
87 { name; pos; len; hash }
88
89 let output_segment_summary ch seg =
90 let nlen = Int32.of_int (String.length seg.name) in
91 let () = output_int32 ch nlen in
92 let () = output_string ch seg.name in
93 let () = output_int64 ch seg.pos in
94 let () = output_int64 ch seg.len in
95 let () = Digest.output ch seg.hash in
96 ()
97
98 let rec input_segment_summaries ch n accu =
99 if Int32.equal n 0l then accu
100 else
101 let s = input_segment_summary ch in
102 let accu = CString.Map.add s.name s accu in
103 input_segment_summaries ch (Int32.pred n) accu
104
105 let marshal_in_segment (type a) h ~segment : a * Digest.t =
106 let { in_channel = ch } = h in
107 let s = CString.Map.find segment.id h.in_segments in
108 let () = LargeFile.seek_in ch s.pos in
109 let (v : a) = marshal_in h.in_filename ch in
110 let () = assert (Int64.equal (LargeFile.pos_in ch) (Int64.add s.pos s.len)) in
111 let h = Digest.input ch in
112 let () = assert (String.equal h s.hash) in
113 (v, s.hash)
114
115 let marshal_out_segment h ~segment v =
116 let { out_channel = ch } = h in
117 let () = assert (not (CString.Map.mem segment.id h.out_segments)) in
118 let pos = LargeFile.pos_out ch in
119 let () = Marshal.to_channel ch v [] in
120 let () = flush ch in
121 let pos' = LargeFile.pos_out ch in
122 let len = Int64.sub pos' pos in
123 let hash =
124 let in_ch = open_in_bin h.out_filename in
125 let () = LargeFile.seek_in in_ch pos in
126 let digest = Digest.channel in_ch (Int64.to_int len) in
127 let () = close_in in_ch in
128 digest
129 in
130 let () = Digest.output ch hash in
131 let s = { name = segment.id; pos; len; hash } in
132 let () = h.out_segments <- CString.Map.add segment.id s h.out_segments in
133 ()
134
135 let marshal_out_binary h ~segment =
136 let { out_channel = ch } = h in
137 let () = assert (not (CString.Map.mem segment.id h.out_segments)) in
138 let pos = LargeFile.pos_out ch in
139 let finish () =
140 let () = flush ch in
141 let pos' = LargeFile.pos_out ch in
142 let len = Int64.sub pos' pos in
143 let hash =
144 let in_ch = open_in_bin h.out_filename in
145 let () = LargeFile.seek_in in_ch pos in
146 let digest = Digest.channel in_ch (Int64.to_int len) in
147 let () = close_in in_ch in
148 digest
149 in
150 let () = Digest.output ch hash in
151 let s = { name = segment.id; pos; len; hash } in
152 h.out_segments <- CString.Map.add segment.id s h.out_segments
153 in
154 ch, finish
155
156 let open_in ~file =
157 try
158 let ch = open_in_bin file in
159 let magic = input_int32 ch in
160 let version = input_int32 ch in
161 let () =
162 if not (Int32.equal magic magic_number) then
163 let e = { filename = file; actual = magic; expected = magic_number } in
164 raise (Bad_magic_number e)
165 in
166 let () =
167 let expected = Coq_config.vo_version in
168 if not (Int32.equal version expected) then
169 let e = { filename = file; actual = version; expected } in
170 raise (Bad_version_number e)
171 in
172 let summary_pos = input_int64 ch in
173 let () = LargeFile.seek_in ch summary_pos in
174 let nsum = input_int32 ch in
175 let seg = input_segment_summaries ch nsum CString.Map.empty in
176 { in_filename = file; in_channel = ch; in_segments = seg }
177 with
178 | End_of_file -> error_corrupted file "premature end of file"
179 | Failure s | Sys_error s -> error_corrupted file s
180
181 let close_in ch =
182 close_in ch.in_channel
183
184 let get_segment (type a) ch ~(segment : a id) : a segment =
185 (CString.Map.find segment.id ch.in_segments :> a segment)
186
187 let segments ch = ch.in_segments
188
189 let open_out ~file =
190 let ch = open_trapping_failure file in
191 let () = output_int32 ch magic_number in
192 let () = output_int32 ch Coq_config.vo_version in
193 let () = output_int64 ch 0L (* placeholder *) in
194 { out_channel = ch; out_segments = CString.Map.empty; out_filename = file }
195
196 let close_out { out_channel = ch; out_segments = seg } =
197 let () = flush ch in
198 let pos = LargeFile.pos_out ch in
199 (* Write the segment summary *)
200 let () = output_int32 ch (Int32.of_int (CString.Map.cardinal seg)) in
201 let iter _ s = output_segment_summary ch s in
202 let () = CString.Map.iter iter seg in
203 (* Overwrite the position place holder *)
204 let () = LargeFile.seek_out ch 8L in
205 let () = output_int64 ch pos in
206 let () = flush ch in
207 close_out ch