[Libguestfs] [PATCH 4/6] mllib: add duplicate sections check to Ini_reader.read_ini

Pino Toscano ptoscano at redhat.com
Wed Jul 8 14:42:16 UTC 2015


Not currently used, since repository configurations and indexes in
virt-builder allow more sections with the same name.
---
 mllib/ini_reader.ml  | 15 ++++++++++++++-
 mllib/ini_reader.mli |  2 +-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/mllib/ini_reader.ml b/mllib/ini_reader.ml
index ea3bbda..08f3ec7 100644
--- a/mllib/ini_reader.ml
+++ b/mllib/ini_reader.ml
@@ -33,7 +33,8 @@ and c_fields = field array
 external parse_index : prog:string -> error_suffix:string -> string -> c_sections = "virt_builder_parse_index"
 
 let read_ini ?(error_suffix = "") ?real_uri
-  ?(check_duplicated_fields = false) file =
+  ?(check_duplicated_sections = false) ?(check_duplicated_fields = false)
+  file =
   let sections = parse_index ~prog ~error_suffix file in
   let sections = Array.to_list sections in
   let sections = List.map (
@@ -46,6 +47,18 @@ let read_ini ?(error_suffix = "") ?real_uri
     | None -> file
     | Some uri -> uri in
 
+  (* Check for duplicated sections. *)
+  if check_duplicated_sections then (
+    let gseen = Hashtbl.create 13 in
+    List.iter (
+      fun (n, _) ->
+        if Hashtbl.mem gseen n then
+          error (f_"%s is corrupt: section '%s' appears two or more times")
+            uri n;
+        Hashtbl.add gseen n true
+    ) sections
+  );
+
   (* Check for repeated fields. *)
   if check_duplicated_fields then (
     List.iter (
diff --git a/mllib/ini_reader.mli b/mllib/ini_reader.mli
index ceda015..63e7572 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 -> ?check_duplicated_fields:bool -> string -> sections
+val read_ini : ?error_suffix:string -> ?real_uri:string -> ?check_duplicated_sections:bool -> ?check_duplicated_fields:bool -> string -> sections
-- 
2.1.0




More information about the Libguestfs mailing list