Skip to content

Commit 9a95184

Browse files
committed
Fix review comment
- Add function name in error log - Use sscanf to get index of ethx - Handle the exception explicitly when reading previous inventory Signed-off-by: Changlei Li <[email protected]>
1 parent f6df121 commit 9a95184

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

ocaml/networkd/bin/network_server.ml

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ let write_config () =
3636
with Network_config.Write_error -> ()
3737

3838
let get_index_from_ethx name =
39-
if String.starts_with ~prefix:"eth" name then
40-
let index = String.sub name 3 (String.length name - 3) in
41-
int_of_string_opt index
42-
else
43-
None
39+
try Scanf.sscanf name "eth%d%!" Option.some with _ -> None
4440

4541
let sort_based_on_ethx () =
4642
Sysfs.list ()
@@ -53,20 +49,32 @@ let sort_based_on_ethx () =
5349

5450
let read_previous_inventory () =
5551
let previous_inventory = "/var/tmp/.previousInventory" in
56-
Xapi_stdext_unix.Unixext.file_lines_fold
57-
(fun acc line ->
58-
match Inventory.parse_inventory_entry line with
59-
| Some ("MANAGEMENT_INTERFACE", iface) ->
60-
info "get management interface from previous inventory: %s" iface ;
61-
(iface, snd acc)
62-
| Some ("MANAGEMENT_ADDRESS_TYPE", addr_type) ->
63-
info "get management address type from previous inventory: %s"
64-
addr_type ;
65-
(fst acc, addr_type)
66-
| _ ->
67-
acc
68-
)
69-
("", "") previous_inventory
52+
try
53+
Xapi_stdext_unix.Unixext.file_lines_fold
54+
(fun acc line ->
55+
match Inventory.parse_inventory_entry line with
56+
| Some ("MANAGEMENT_INTERFACE", iface) ->
57+
info "get management interface from previous inventory: %s" iface ;
58+
(iface, snd acc)
59+
| Some ("MANAGEMENT_ADDRESS_TYPE", addr_type) ->
60+
info "get management address type from previous inventory: %s"
61+
addr_type ;
62+
(fst acc, addr_type)
63+
| _ ->
64+
acc
65+
)
66+
("", "") previous_inventory
67+
with e ->
68+
error "Failed to read previous inventory %s: %s" previous_inventory
69+
(Printexc.to_string e) ;
70+
raise
71+
(Network_error
72+
(Internal_error
73+
(Printf.sprintf "Failed to read previous inventory: %s"
74+
previous_inventory
75+
)
76+
)
77+
)
7078

7179
let update_inventory () =
7280
let iface, addr_type = read_previous_inventory () in
@@ -118,7 +126,7 @@ let sort last_order =
118126
(None, [])
119127
| true, Some _ ->
120128
(* Impossible *)
121-
error "device renamed but order is not None" ;
129+
error "%s: device renamed but order is not None" __FUNCTION__ ;
122130
raise
123131
(Network_error (Internal_error "device renamed but order is not None"))
124132
| false, None ->

0 commit comments

Comments
 (0)