Skip to content

Commit

Permalink
Reindentation and file mode change.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
  • Loading branch information
kc284 committed Dec 14, 2017
1 parent ff95604 commit fc0653a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
Empty file modified configure.ml
100755 → 100644
Empty file.
110 changes: 55 additions & 55 deletions lib/inventory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,80 +41,80 @@ let inventory_m = Mutex.create ()

(* Compute the minimum necessary inventory file contents *)
let minimum_default_entries () =
let host_uuid = Uuidm.to_string (Uuidm.create `V4) in
let dom0_uuid = Uuidm.to_string (Uuidm.create `V4) in
[
_installation_uuid, host_uuid;
_control_domain_uuid, dom0_uuid;
_management_interface, "";
_management_address_type, "IPv4";
_build_number, "0"
]
let host_uuid = Uuidm.to_string (Uuidm.create `V4) in
let dom0_uuid = Uuidm.to_string (Uuidm.create `V4) in
[
_installation_uuid, host_uuid;
_control_domain_uuid, dom0_uuid;
_management_interface, "";
_management_address_type, "IPv4";
_build_number, "0"
]

(* trim any quotes off the ends *)
let strip_quotes v =
if String.length v >= 2
&& v.[0] = '\''
&& v.[String.length v - 1] = '\''
then String.sub v 1 (String.length v - 2)
else v
if String.length v >= 2
&& v.[0] = '\''
&& v.[String.length v - 1] = '\''
then String.sub v 1 (String.length v - 2)
else v

let parse_inventory_entry line =
match Astring.String.cuts ~empty:false ~sep:"=" line with
| [k; v] ->
(* trim whitespace *)
Some (k, v |> strip_quotes |> String.trim)
| _ -> None
match Astring.String.cuts ~empty:false ~sep:"=" line with
| [k; v] ->
(* trim whitespace *)
Some (k, v |> strip_quotes |> String.trim)
| _ -> None

let string_of_table h =
let lines = List.fold_left (fun acc (k, v) ->
Printf.sprintf "%s='%s'\n" k v :: acc) [] h in
String.concat "" lines
let lines = List.fold_left (fun acc (k, v) ->
Printf.sprintf "%s='%s'\n" k v :: acc) [] h in
String.concat "" lines

let read_inventory_contents () =
if not (Sys.file_exists !inventory_filename) then begin
Unixext.write_string_to_file !inventory_filename (
string_of_table (minimum_default_entries ()))
end;
(* Perhaps we should blank the old inventory before we read the new one?
What is the desired behaviour? *)
Unixext.file_lines_iter (fun line ->
match parse_inventory_entry line with
| Some (k, v) -> Hashtbl.add inventory k v
| None -> ())
!inventory_filename;
loaded_inventory := true
if not (Sys.file_exists !inventory_filename) then begin
Unixext.write_string_to_file !inventory_filename (
string_of_table (minimum_default_entries ()))
end;
(* Perhaps we should blank the old inventory before we read the new one?
What is the desired behaviour? *)
Unixext.file_lines_iter (fun line ->
match parse_inventory_entry line with
| Some (k, v) -> Hashtbl.add inventory k v
| None -> ())
!inventory_filename;
loaded_inventory := true

let read_inventory () = Mutex.execute inventory_m read_inventory_contents
let reread_inventory () = Mutex.execute inventory_m (fun () ->
Hashtbl.clear inventory;
read_inventory_contents ())
Hashtbl.clear inventory;
read_inventory_contents ())

exception Missing_inventory_key of string

let lookup ?default key =
Mutex.execute inventory_m (fun () ->
(if not (!loaded_inventory) then read_inventory_contents ());
if (Hashtbl.mem inventory key)
then
Hashtbl.find inventory key
else
match default with
| None -> raise (Missing_inventory_key key)
| Some v -> v)
Mutex.execute inventory_m (fun () ->
(if not (!loaded_inventory) then read_inventory_contents ());
if (Hashtbl.mem inventory key)
then
Hashtbl.find inventory key
else
match default with
| None -> raise (Missing_inventory_key key)
| Some v -> v)

let flush_to_disk_locked () =
let h = Hashtbl.fold (fun k v acc -> (k, v) :: acc) inventory [] in
Unixext.write_string_to_file !inventory_filename (string_of_table h)
let h = Hashtbl.fold (fun k v acc -> (k, v) :: acc) inventory [] in
Unixext.write_string_to_file !inventory_filename (string_of_table h)

let update key value = Mutex.execute inventory_m (fun () ->
Hashtbl.clear inventory;
read_inventory_contents ();
Hashtbl.replace inventory key value;
flush_to_disk_locked ())
Hashtbl.clear inventory;
read_inventory_contents ();
Hashtbl.replace inventory key value;
flush_to_disk_locked ())

let remove key = Mutex.execute inventory_m (fun () ->
Hashtbl.clear inventory;
read_inventory_contents ();
Hashtbl.remove inventory key;
flush_to_disk_locked ())
Hashtbl.clear inventory;
read_inventory_contents ();
Hashtbl.remove inventory key;
flush_to_disk_locked ())

0 comments on commit fc0653a

Please # to comment.