Skip to content

Commit 1a09452

Browse files
authored
xapi_vdi_helpers: actually write raw vdi when possible (#6334)
An `if ... then raise exn;`was misread and make the code after impossible to execute, when that was not the intention. Remove all the ignore_<type> functions from stdext: a plain ignore with a type annotation can replace these. We should start using those for all ignores (there are too many of them, and can't be easily automated to do it in this PR) Passes internal tests: 213465 (one failure due to the recent vlan + clustering issue)
2 parents c75fd8b + 4e88aba commit 1a09452

File tree

13 files changed

+88
-138
lines changed

13 files changed

+88
-138
lines changed

dune-project

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
(package
4141
(name tgroup)
42-
(depends
42+
(depends
4343
xapi-log
4444
xapi-stdext-unix)
4545
)
@@ -502,6 +502,21 @@ This package provides an Lwt compatible interface to the library.")
502502

503503
(package
504504
(name stunnel)
505+
(synopsis "Library used by xapi to herd stunnel processes")
506+
(description "This library allows xapi to configure, launch and terminate stunnel processes that act as clients and servers.")
507+
(depends
508+
astring
509+
(forkexec (= :version))
510+
(safe-resources (= :version))
511+
(uuid (= :version))
512+
(xapi-consts (= :version))
513+
xapi-inventory
514+
(xapi-log (= :version))
515+
(xapi-stdext-pervasives (= :version))
516+
(xapi-stdext-threads (= :version))
517+
(xapi-stdext-unix (= :version))
518+
(odoc :with-doc)
519+
)
505520
)
506521

507522
(package

ocaml/database/block_device_io.ml

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ let get_pointer half =
184184
(* Lay out a blank double-buffered redo log on the given block device. *)
185185
(* May raise Unixext.Timeout exception *)
186186
let initialise_redo_log block_dev_fd target_response_time =
187-
ignore_int (Unixext.seek_to block_dev_fd 0) ;
187+
ignore (Unixext.seek_to block_dev_fd 0 : int) ;
188188
Unixext.time_limited_write_substring block_dev_fd magic_size magic
189189
target_response_time ;
190190
Unixext.time_limited_write_substring block_dev_fd 2 "\0000"
@@ -221,7 +221,7 @@ let open_block_device block_dev target_response_time =
221221

222222
(* Within the given block device, seek to the position of the validity byte. *)
223223
let seek_to_validity_byte block_dev_fd =
224-
ignore_int (Unixext.seek_to block_dev_fd pos_validity_byte)
224+
ignore (Unixext.seek_to block_dev_fd pos_validity_byte : int)
225225

226226
(* Read the validity byte from the given block device. *)
227227
let read_validity_byte block_dev_fd target_response_time =
@@ -279,14 +279,15 @@ let read_database block_dev_fd target_response_time =
279279
let db_fn f =
280280
let prev_pos = Unixext.current_cursor_pos block_dev_fd in
281281
(* Seek to the position of the database *)
282-
ignore_int (Unixext.seek_to block_dev_fd cur_pos) ;
282+
ignore (Unixext.seek_to block_dev_fd cur_pos : int) ;
283283
(* Read 'len' bytes from the block device and send them to the function we were given *)
284-
ignore_int (Unixext.read_data_in_string_chunks f ~max_bytes:len block_dev_fd) ;
284+
ignore
285+
(Unixext.read_data_in_string_chunks f ~max_bytes:len block_dev_fd : int) ;
285286
(* Seek back to where we were before *)
286-
ignore_int (Unixext.seek_to block_dev_fd prev_pos)
287+
ignore (Unixext.seek_to block_dev_fd prev_pos : int)
287288
in
288289
(* For now, skip over where the database is *)
289-
ignore_int (Unixext.seek_rel block_dev_fd len) ;
290+
ignore (Unixext.seek_rel block_dev_fd len : int) ;
290291
(* Read the generation count and marker *)
291292
let generation_count = Int64.of_string (read generation_size) in
292293
let marker_end = read marker_size in
@@ -471,7 +472,7 @@ let action_writedb block_dev_fd client datasock target_response_time =
471472
(* if neither half is valid, use the first half *)
472473
in
473474
(* Seek to the start of the chosen half *)
474-
ignore_int (Unixext.seek_to block_dev_fd (start_of_half half_to_use)) ;
475+
ignore (Unixext.seek_to block_dev_fd (start_of_half half_to_use) : int) ;
475476
(* Check that we've got enough space for two markers, a length and a generation count. This is the smallest possible size for a db record. *)
476477
let min_space_needed = (marker_size * 2) + size_size + generation_size in
477478
let available_space = Db_globs.redo_log_length_of_half in
@@ -487,7 +488,7 @@ let action_writedb block_dev_fd client datasock target_response_time =
487488
R.debug "Cursor position to which the length will be written is %d"
488489
pos_to_write_length ;
489490
(* Seek forwards to the position to write the data *)
490-
ignore_int (Unixext.seek_rel block_dev_fd size_size) ;
491+
ignore (Unixext.seek_rel block_dev_fd size_size : int) ;
491492
(* Read the data from the data channel and write this directly into block_dev_fd *)
492493
let remaining_space =
493494
Db_globs.redo_log_length_of_half - marker_size - size_size
@@ -523,7 +524,7 @@ let action_writedb block_dev_fd client datasock target_response_time =
523524
(Bytes.make trample_size '\000')
524525
target_response_time ;
525526
(* Seek backwards in the block device to where the length is supposed to go and write it *)
526-
ignore_int (Unixext.seek_to block_dev_fd pos_to_write_length) ;
527+
ignore (Unixext.seek_to block_dev_fd pos_to_write_length : int) ;
527528
let total_length_str = Printf.sprintf "%016d" total_length in
528529
Unixext.time_limited_write_substring block_dev_fd size_size total_length_str
529530
target_response_time ;
@@ -680,7 +681,7 @@ let action_read block_dev_fd client datasock target_response_time =
680681
(* the log is empty *)
681682

682683
(* Seek to the start of the chosen half *)
683-
ignore_int (Unixext.seek_to block_dev_fd (start_of_half half_to_use)) ;
684+
ignore (Unixext.seek_to block_dev_fd (start_of_half half_to_use) : int) ;
684685
(* Attempt to read a database record *)
685686
let length, db_fn, generation_count, marker =
686687
read_database block_dev_fd target_response_time
@@ -783,7 +784,7 @@ let _ =
783784
(fun half ->
784785
Printf.printf "*** [Half %s] Entering half.\n" (half_to_string half) ;
785786
(* Seek to the start of the chosen half *)
786-
ignore_int (Unixext.seek_to block_dev_fd (start_of_half half)) ;
787+
ignore (Unixext.seek_to block_dev_fd (start_of_half half) : int) ;
787788
(* Attempt to read a database record *)
788789
try
789790
let length, db_fn, generation_count, marker =

ocaml/libs/stunnel/stunnel.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
module D = Debug.Make (struct let name = "stunnel" end)
1717

1818
open Printf
19-
open Xapi_stdext_pervasives.Pervasiveext
2019
open Xapi_stdext_unix
2120
open Safe_resources
2221

@@ -87,8 +86,8 @@ module Unsafe = struct
8786
try
8887
pre_exec () ;
8988
(* CA-18955: xapi now runs with priority -3. We then set his sons priority to 0. *)
90-
ignore_int (Unix.nice (-Unix.nice 0)) ;
91-
ignore_int (Unix.setsid ()) ;
89+
ignore (Unix.nice (-Unix.nice 0) : int) ;
90+
ignore (Unix.setsid () : int) ;
9291
match env with
9392
| None ->
9493
Unix.execv argv0 args

ocaml/libs/xapi-stdext/lib/xapi-stdext-pervasives/pervasiveext.ml

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,3 @@ let finally fct clean_f =
4242

4343
(** execute fct ignoring exceptions *)
4444
let ignore_exn fct = try fct () with _ -> ()
45-
46-
(* non polymorphic ignore function *)
47-
let ignore_int v =
48-
let (_ : int) = v in
49-
()
50-
51-
let ignore_int64 v =
52-
let (_ : int64) = v in
53-
()
54-
55-
let ignore_int32 v =
56-
let (_ : int32) = v in
57-
()
58-
59-
let ignore_string v =
60-
let (_ : string) = v in
61-
()
62-
63-
let ignore_float v =
64-
let (_ : float) = v in
65-
()
66-
67-
let ignore_bool v =
68-
let (_ : bool) = v in
69-
()

ocaml/libs/xapi-stdext/lib/xapi-stdext-pervasives/pervasiveext.mli

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,3 @@ val finally : (unit -> 'a) -> (unit -> unit) -> 'a
1717
[g ()] even if [f ()] throws an exception. *)
1818

1919
val ignore_exn : (unit -> unit) -> unit
20-
21-
val ignore_int : int -> unit
22-
23-
val ignore_int32 : int32 -> unit
24-
25-
val ignore_int64 : int64 -> unit
26-
27-
val ignore_string : string -> unit
28-
29-
val ignore_float : float -> unit
30-
31-
val ignore_bool : bool -> unit

ocaml/libs/xapi-stdext/lib/xapi-stdext-unix/unixext.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ let copy_file_internal ?limit reader writer =
219219
let num = reader buffer 0 (Int64.to_int requested) in
220220
let num64 = Int64.of_int num in
221221
limit := Option.map (fun x -> Int64.sub x num64) !limit ;
222-
ignore_int (writer buffer 0 num) ;
222+
ignore (writer buffer 0 num : int) ;
223223
total_bytes := Int64.add !total_bytes num64 ;
224224
finished := num = 0 || !limit = Some 0L
225225
done ;

ocaml/rrd2csv/src/rrd2csv.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,6 @@ let _ =
655655
R2.2. Ability to specify period of sampling on the command-line (in seconds)
656656
*)
657657
let user_filters, sampling_period, show_name, show_uuid =
658-
let open Xapi_stdext_pervasives.Pervasiveext in
659658
(* R2.1.1. If none are specified, assume that all enabled data-sources are of
660659
interest *)
661660
let ds = ref [] and s = ref None and n = ref false and u = ref false in
@@ -690,15 +689,15 @@ let _ =
690689
; ( "-help"
691690
, Arg.Unit
692691
(fun () ->
693-
ignore_int (Sys.command "man -M /opt/xensource/man rrd2csv") ;
692+
ignore (Sys.command "man -M /opt/xensource/man rrd2csv" : int) ;
694693
exit 0
695694
)
696695
, " display help"
697696
)
698697
; ( "--help"
699698
, Arg.Unit
700699
(fun () ->
701-
ignore_int (Sys.command "man -M /opt/xensource/man rrd2csv") ;
700+
ignore (Sys.command "man -M /opt/xensource/man rrd2csv" : int) ;
702701
exit 0
703702
)
704703
, " display help"

ocaml/xapi/repository_helpers.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ let get_remote_pool_coordinator_ip url =
242242
raise Api_errors.(Server_error (invalid_base_url, [url]))
243243

244244
let assert_remote_pool_url_is_valid ~url =
245-
get_remote_pool_coordinator_ip url
246-
|> Xapi_stdext_pervasives.Pervasiveext.ignore_string
245+
ignore (get_remote_pool_coordinator_ip url : string)
247246

248247
let with_pool_repositories f =
249248
Xapi_stdext_pervasives.Pervasiveext.finally

ocaml/xapi/xapi_host.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,8 +1970,7 @@ let attach_static_vdis ~__context ~host:_ ~vdi_reason_map =
19701970
&& v.Static_vdis_list.currently_attached
19711971
in
19721972
if not (List.exists check static_vdis) then
1973-
Pervasiveext.ignore_string
1974-
(Static_vdis.permanent_vdi_attach ~__context ~vdi ~reason)
1973+
ignore (Static_vdis.permanent_vdi_attach ~__context ~vdi ~reason : string)
19751974
in
19761975
List.iter attach vdi_reason_map
19771976

ocaml/xapi/xapi_vdi_helpers.ml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -261,27 +261,27 @@ module VDI_CStruct = struct
261261
end
262262

263263
let write_raw ~__context ~vdi ~text =
264-
if String.length text >= VDI_CStruct.(vdi_size - vdi_format_length) then (
265-
let error_msg =
266-
Printf.sprintf "Cannot write %d bytes to raw VDI. Capacity = %d bytes"
267-
(String.length text)
268-
VDI_CStruct.(vdi_size - vdi_format_length)
269-
in
270-
ignore (failwith error_msg) ;
271-
Helpers.call_api_functions ~__context (fun rpc session_id ->
272-
Sm_fs_ops.with_open_block_attached_device __context rpc session_id vdi
273-
`RW (fun fd ->
274-
let contents = Unixext.really_read_string fd VDI_CStruct.vdi_size in
275-
let cstruct = Cstruct.of_string contents in
276-
if VDI_CStruct.get_magic_number cstruct <> VDI_CStruct.magic_number
277-
then
278-
VDI_CStruct.format cstruct ;
279-
VDI_CStruct.write cstruct text (String.length text) ;
280-
Unix.ftruncate fd 0 ;
281-
Unixext.seek_to fd 0 |> ignore ;
282-
Unixext.really_write_string fd (VDI_CStruct.read cstruct)
283-
)
284-
)
264+
( if String.length text >= VDI_CStruct.(vdi_size - vdi_format_length) then
265+
let error_msg =
266+
Printf.sprintf "Cannot write %d bytes to raw VDI. Capacity = %d bytes"
267+
(String.length text)
268+
VDI_CStruct.(vdi_size - vdi_format_length)
269+
in
270+
failwith error_msg
271+
) ;
272+
Helpers.call_api_functions ~__context (fun rpc session_id ->
273+
Sm_fs_ops.with_open_block_attached_device __context rpc session_id vdi `RW
274+
(fun fd ->
275+
let contents = Unixext.really_read_string fd VDI_CStruct.vdi_size in
276+
let cstruct = Cstruct.of_string contents in
277+
if VDI_CStruct.get_magic_number cstruct <> VDI_CStruct.magic_number
278+
then
279+
VDI_CStruct.format cstruct ;
280+
VDI_CStruct.write cstruct text (String.length text) ;
281+
Unix.ftruncate fd 0 ;
282+
ignore (Unixext.seek_to fd 0 : int) ;
283+
Unixext.really_write_string fd (VDI_CStruct.read cstruct)
284+
)
285285
)
286286

287287
let read_raw ~__context ~vdi =

ocaml/xcp-rrdd/bin/rrdd/xcp_rrdd.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ let start (xmlrpc_path, http_fwd_path) process =
109109
|> Http.Request.t_of_rpc
110110
in
111111
req.Http.Request.close <- true ;
112-
ignore_bool (Http_svr.handle_one server received_fd () req)
112+
ignore (Http_svr.handle_one server received_fd () req : bool)
113113
)
114114
(fun _ -> Unix.close received_fd)
115115
) ;

stunnel.opam

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
# This file is generated by dune, edit dune-project instead
2-
license: "LGPL-2.1-only WITH OCaml-LGPL-linking-exception"
32
opam-version: "2.0"
4-
maintainer: "[email protected]"
5-
3+
synopsis: "Library used by xapi to herd stunnel processes"
4+
description:
5+
"This library allows xapi to configure, launch and terminate stunnel processes that act as clients and servers."
6+
maintainer: ["Xapi project maintainers"]
7+
authors: ["[email protected]"]
8+
license: "LGPL-2.1-only WITH OCaml-LGPL-linking-exception"
69
homepage: "https://xapi-project.github.io/"
7-
bug-reports: "https://github.com/xapi-project/xen-api.git"
8-
dev-repo: "git+https://github.com/xapi-project/xen-api.git"
9-
build: [[ "dune" "build" "-p" name "-j" jobs ]]
10-
11-
available: [ os = "linux" ]
10+
bug-reports: "https://github.com/xapi-project/xen-api/issues"
1211
depends: [
13-
"ocaml"
1412
"dune" {>= "3.15"}
1513
"astring"
16-
"forkexec"
17-
"safe-resources"
18-
"uuid"
19-
"xapi-consts"
20-
"xapi-log"
14+
"forkexec" {= version}
15+
"safe-resources" {= version}
16+
"uuid" {= version}
17+
"xapi-consts" {= version}
2118
"xapi-inventory"
22-
"xapi-stdext-pervasives"
23-
"xapi-stdext-threads"
24-
"xapi-stdext-unix"
19+
"xapi-log" {= version}
20+
"xapi-stdext-pervasives" {= version}
21+
"xapi-stdext-threads" {= version}
22+
"xapi-stdext-unix" {= version}
23+
"odoc" {with-doc}
2524
]
26-
synopsis: "Library required by xapi"
27-
description: """
28-
These libraries are provided for backwards compatibility only.
29-
No new code should use these libraries."""
30-
url {
31-
src:
32-
"https://github.com/xapi-project/xen-api/archive/master.tar.gz"
33-
}
25+
build: [
26+
["dune" "subst"] {dev}
27+
[
28+
"dune"
29+
"build"
30+
"-p"
31+
name
32+
"-j"
33+
jobs
34+
"@install"
35+
"@runtest" {with-test}
36+
"@doc" {with-doc}
37+
]
38+
]
39+
dev-repo: "git+https://github.com/xapi-project/xen-api.git"

stunnel.opam.template

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)