]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/library/libraryNoDb.ml
0a03a4a7e4ed6f480a56ab42efcf6b5f0b634d59
[helm.git] / helm / ocaml / library / libraryNoDb.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 exception Checksum_failure of string
29 exception Corrupt_metadata of string
30 exception Version_mismatch of string
31
32 let magic = 1
33
34 type metadata =
35   | Dependency of string  (* baseuri without trailing slash *)
36
37 let eq_metadata (m1:metadata) (m2:metadata) = m1 = m2
38
39 let marshal_flags = []
40
41 (** .metadata file format
42  * - an integer -- magic number -- denoting the version of the dumped data
43  *   structure. Different magic numbers stand for incompatible data structures
44  * - an integer -- checksum -- denoting the hash value (computed with
45  *   Hashtbl.hash) of the string representation of the dumped data structur
46  * - marshalled data: list of metadata
47  *)
48
49 let save_metadata ~fname metadata =
50  let ensure_path_exists path =
51    let dir = Filename.dirname path in
52    HExtlib.mkdir dir
53  in
54  ensure_path_exists fname;
55  let oc = open_out fname in
56  let marshalled = Marshal.to_string metadata marshal_flags in
57  let checksum = Hashtbl.hash marshalled in
58  output_binary_int oc magic;
59  output_binary_int oc checksum;
60  output_string oc marshalled;
61  close_out oc
62
63 let load_metadata ~fname =
64   let ic = open_in fname in
65   HExtlib.finally
66     (fun () -> close_in ic)
67     (fun () ->
68       try
69         let file_magic = input_binary_int ic in
70         if file_magic <> magic then raise (Version_mismatch fname);
71         let file_checksum = input_binary_int ic in
72         let marshalled = HExtlib.input_all ic in
73         let checksum = Hashtbl.hash marshalled in
74         if checksum <> file_checksum then raise (Checksum_failure fname);
75         let (metadata:metadata list) = Marshal.from_string marshalled 0 in
76         metadata
77       with End_of_file -> raise (Corrupt_metadata fname))
78     ()
79