Skip to content

Commit 62f55d0

Browse files
committed
tapctl: Return Option instead of raising Not_found
Some of the users of the function did not handle exceptions correctly - make the "not found" case explicit with an option. Signed-off-by: Andrii Sultanov <[email protected]>
1 parent fcefacd commit 62f55d0

File tree

6 files changed

+15
-18
lines changed

6 files changed

+15
-18
lines changed

ocaml/tapctl/tapctl.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,11 +533,7 @@ let of_device ctx path =
533533
let major = stat.Unix.st_rdev / 256 in
534534
let minor = stat.Unix.st_rdev mod 256 in
535535
if driver_of_major major <> "tapdev" then raise Not_blktap ;
536-
match List.filter (fun (tapdev, _, _) -> tapdev.minor = minor) (list ctx) with
537-
| [t] ->
538-
t
539-
| _ ->
540-
raise Not_found
536+
List.find_opt (fun (tapdev, _, _) -> tapdev.minor = minor) (list ctx)
541537

542538
let find ctx ~pid ~minor =
543539
match list ~t:{minor; tapdisk_pid= pid} ctx with

ocaml/tapctl/tapctl.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ exception Not_blktap
9191
(** Thrown by [of_device x] when [x] is not a device *)
9292
exception Not_a_device
9393

94-
val of_device : context -> string -> t
94+
val of_device : context -> string -> t option
9595
(** Given a path to a device, return the corresponding tap information *)
9696

9797
val find : context -> pid:int -> minor:int -> t

ocaml/vhd-tool/cli/sparse_dd.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,14 @@ let with_paused_tapdisk path f =
244244
let path = find_backend_device path |> Opt.default path in
245245
let context = Tapctl.create () in
246246
match Tapctl.of_device context path with
247-
| tapdev, _, Some (_driver, path) ->
247+
| Some (tapdev, _, Some (_driver, path)) ->
248248
debug "pausing tapdisk for %s" path ;
249249
Tapctl.pause context tapdev ;
250250
after f (fun () ->
251251
debug "unpausing tapdisk for %s" path ;
252252
Tapctl.unpause context tapdev path Tapctl.Vhd
253253
)
254-
| _, _, _ ->
254+
| _ ->
255255
failwith (Printf.sprintf "Failed to pause tapdisk for %s" path)
256256

257257
(* Record when the binary started for performance measuring *)

ocaml/vhd-tool/src/image.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ let image_behind_nbd_device image =
6666

6767
let of_device path =
6868
match Tapctl.of_device (Tapctl.create ()) path with
69-
| _, _, Some ("vhd", vhd) ->
69+
| Some (_, _, Some ("vhd", vhd)) ->
7070
Some (`Vhd vhd)
71-
| _, _, Some ("aio", vhd) ->
71+
| Some (_, _, Some ("aio", vhd)) ->
7272
Some (`Raw vhd)
73-
| _, _, _ ->
73+
| _ ->
7474
None
7575
| exception Tapctl.Not_blktap ->
7676
get_nbd_device path |> image_behind_nbd_device

ocaml/xapi/storage_smapiv1_migrate.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ let tapdisk_of_attach_info (backend : Storage_interface.backend) =
108108
let path = blockdevice.Storage_interface.path in
109109
try
110110
match Tapctl.of_device (Tapctl.create ()) path with
111-
| tapdev, _, _ ->
111+
| Some (tapdev, _, _) ->
112112
Some tapdev
113+
| None ->
114+
D.debug "Device %s has an unknown driver" path ;
115+
None
113116
with
114117
| Tapctl.Not_blktap ->
115118
D.debug "Device %s is not controlled by blktap" path ;
116119
None
117120
| Tapctl.Not_a_device ->
118121
D.debug "%s is not a device" path ;
119122
None
120-
| _ ->
121-
D.debug "Device %s has an unknown driver" path ;
122-
None
123123
)
124124
| _, nbd :: _ -> (
125125
try
@@ -295,8 +295,8 @@ module Copy = struct
295295
perform_cleanup_actions !on_fail ;
296296
raise e
297297

298-
(** [copy_into_sr] does not requires a dest vdi to be provided, instead, it will
299-
find the nearest vdi on the [dest] sr, and if there is no such vdi, it will
298+
(** [copy_into_sr] does not requires a dest vdi to be provided, instead, it will
299+
find the nearest vdi on the [dest] sr, and if there is no such vdi, it will
300300
create one. *)
301301
let copy_into_sr ~task ~dbg ~sr ~vdi ~vm ~url ~dest ~verify_dest =
302302
D.debug "copy sr:%s vdi:%s url:%s dest:%s verify_dest:%B"

ocaml/xapi/xapi_vdi_helpers.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ let find_backend_device path =
399399
let backing_info_of_device path =
400400
let tapdisk_of_path path =
401401
try
402-
let _, _, backing_info = Tapctl.of_device (Tapctl.create ()) path in
402+
let ( let* ) = Option.bind in
403+
let* _, _, backing_info = Tapctl.of_device (Tapctl.create ()) path in
403404
backing_info
404405
with
405406
| Tapctl.Not_a_device ->

0 commit comments

Comments
 (0)