Move the existing check from Index_parser in virt-builder, so it can be used also in other contexts (the Source reader, for example, which currently does not do any duplicate check). --- builder/index_parser.ml | 22 ++-------------------- builder/sources.ml | 3 ++- mllib/ini_reader.ml | 35 ++++++++++++++++++++++++++++++++--- mllib/ini_reader.mli | 2 +- 4 files changed, 37 insertions(+), 25 deletions(-) diff --git a/builder/index_parser.ml b/builder/index_parser.ml index 219003a..fad0781 100644 --- a/builder/index_parser.ml +++ b/builder/index_parser.ml @@ -127,7 +127,8 @@ let get_index ~downloader ~sigchecker Sigchecker.verify sigchecker tmpfile; (* Try parsing the file. *) - let sections = Ini_reader.read_ini ~real_uri:uri tmpfile in + let sections = Ini_reader.read_ini ~check_duplicated_fields:true + ~real_uri:uri tmpfile in if delete_tmpfile then (try Unix.unlink tmpfile with _ -> ()); @@ -152,25 +153,6 @@ let get_index ~downloader ~sigchecker Hashtbl.add nseen id true ) name_arch_map; - (* Check for repeated fields. *) - List.iter ( - fun (n, fields) -> - let fseen = Hashtbl.create 13 in - List.iter ( - fun (field, subkey, _) -> - let hashkey = (field, subkey) in - if Hashtbl.mem fseen hashkey then ( - (match subkey with - | Some value -> - eprintf (f_"%s: index is corrupt: %s: field '%s[%s]' appears two or more times\n") prog n field value - | None -> - eprintf (f_"%s: index is corrupt: %s: field '%s' appears two or more times\n") prog n field); - corrupt_file () - ); - Hashtbl.add fseen hashkey true - ) fields - ) sections; - (* Turn the sections into the final index. *) let entries = List.map ( diff --git a/builder/sources.ml b/builder/sources.ml index b774762..7cd522c 100644 --- a/builder/sources.ml +++ b/builder/sources.ml @@ -35,7 +35,8 @@ let parse_conf file = if verbose () then ( printf (f_"%s: trying to read %s\n") prog file; ); - let sections = Ini_reader.read_ini ~error_suffix:"[ignored]" file in + let sections = Ini_reader.read_ini ~error_suffix:"[ignored]" + ~check_duplicated_fields:true file in let sources = List.fold_right ( fun (n, fields) acc -> diff --git a/mllib/ini_reader.ml b/mllib/ini_reader.ml index 6d1d347..ea3bbda 100644 --- a/mllib/ini_reader.ml +++ b/mllib/ini_reader.ml @@ -16,6 +16,7 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) +open Common_gettext.Gettext open Common_utils type sections = section list @@ -31,10 +32,38 @@ and c_fields = field array (* Calls yyparse in the C code. *) external parse_index : prog:string -> error_suffix:string -> string -> c_sections = "virt_builder_parse_index" -let read_ini ?(error_suffix = "") ?real_uri file = +let read_ini ?(error_suffix = "") ?real_uri + ?(check_duplicated_fields = false) file = let sections = parse_index ~prog ~error_suffix file in let sections = Array.to_list sections in - List.map ( + let sections = List.map ( fun (n, fields) -> n, Array.to_list fields - ) sections + ) sections in + + let uri = + match real_uri with + | None -> file + | Some uri -> uri in + + (* Check for repeated fields. *) + if check_duplicated_fields then ( + List.iter ( + fun (n, fields) -> + let fseen = Hashtbl.create 13 in + List.iter ( + fun (field, subkey, _) -> + let hashkey = (field, subkey) in + if Hashtbl.mem fseen hashkey then ( + match subkey with + | Some value -> + error (f_"%s is corrupt: %s: field '%s[%s]' appears two or more times") uri n field value + | None -> + error (f_"%s is corrupt: %s: field '%s' appears two or more times") uri n field + ); + Hashtbl.add fseen hashkey true + ) fields + ) sections + ); + + sections diff --git a/mllib/ini_reader.mli b/mllib/ini_reader.mli index 7005e62..ceda015 100644 --- a/mllib/ini_reader.mli +++ b/mllib/ini_reader.mli @@ -21,4 +21,4 @@ and section = string * fields (* [name] + fields *) and fields = field list and field = string * string option * string (* key + subkey + value *) -val read_ini : ?error_suffix:string -> ?real_uri:string -> string -> sections +val read_ini : ?error_suffix:string -> ?real_uri:string -> ?check_duplicated_fields:bool -> string -> sections -- 2.1.0