Skip to content

Commit

Permalink
Replace connect_uri with connect_from_config
Browse files Browse the repository at this point in the history
Signed-off-by: David Scott <[email protected]>
  • Loading branch information
djs55 committed Sep 20, 2016
1 parent db5a604 commit c61e37c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 35 deletions.
12 changes: 2 additions & 10 deletions lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ let get_file_size filename fd =
(`Unknown
(Printf.sprintf "get_file_size %s: neither a file nor a block device" filename))

let connect_common ({ Config.buffered; sync; path } as config) =
let connect_from_config ({ Config.buffered; sync; path } as config) =
let openfile, use_fsync_after_write = match buffered, is_win32 with
| true, _ -> Raw.openfile_buffered, false
| false, false -> Raw.openfile_unbuffered, false
Expand Down Expand Up @@ -181,15 +181,7 @@ let remove_prefix prefix x =
let connect name =
let buffered, path = remove_prefix buffered_prefix name in
let config = { Config.buffered; sync = false; path } in
connect_common config

let connect_uri uri =
let path = Uri.(pct_decode @@ path uri) in
let params = Uri.query uri in
let buffered = try List.assoc "buffered" params = [ "1" ] with Not_found -> false in
let sync = try List.assoc "sync" params = [ "1" ] with Not_found -> false in
let config = { Config.buffered; sync; path } in
connect_common config
connect_from_config config

let disconnect t = match t.fd with
| Some fd ->
Expand Down
10 changes: 3 additions & 7 deletions lib/block.mli
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ module Config: sig
(** Parse the result of a previous [to_string] invocation *)
end

val connect_uri : Uri.t -> [`Ok of t | `Error of error] io
(** [connect_uri uri] connects to [uri], respecting options provided as
query parameters:
buffered=(0|1): 1 means use the underlying host's buffer cache
sync=(0|1): 1 means `flush` will also flush any storage hardware caches
(which will be slow but writes will persist even over a power loss)
*)
val connect_from_config : Config.t -> [`Ok of t | `Error of error] io
(** [connect_from_config config] connects to the block device described by
[config]. *)

val resize : t -> int64 -> [ `Ok of unit | `Error of error ] io
(** [resize t new_size_sectors] attempts to resize the connected device
Expand Down
18 changes: 0 additions & 18 deletions lib_test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,6 @@ let test_parse_print_config config =
assert_equal ~printer:(fun x -> x) config.path config'.path;
)

let test_connect_uri query buffered sync () =
let t =
with_temp_file
(fun file ->
let uri = Uri.make ~scheme:"file" ~path:file ~query () in
Block.connect_uri uri >>= function
| `Error _ -> failwith (Printf.sprintf "Block.connect %s failed" (Uri.to_string uri))
| `Ok device1 ->
let config = Block.get_config device1 in
assert_equal ~printer:string_of_bool buffered config.Block.Config.buffered;
assert_equal ~printer:string_of_bool sync config.Block.Config.sync;
Block.disconnect device1
) in
Lwt_main.run t

let not_implemented_on_windows = [
"test resize" >:: test_resize;
]
Expand All @@ -244,9 +229,6 @@ let tests = [
"test flush" >:: test_flush;
test_parse_print_config { Block.Config.buffered = true; sync = false; path = "C:\\cygwin" };
test_parse_print_config { Block.Config.buffered = false; sync = true; path = "/var/tmp/foo.qcow2" };
"test connect_uri" >:: (test_connect_uri [] false false);
"test connect_uri buffered=1" >:: (test_connect_uri [ "buffered", ["1"] ] true false);
"test connect_uri buffered=1&sync=1" >:: (test_connect_uri [ "buffered", ["1"]; "sync", ["1"]] true true);
"test write then read" >:: test_write_read;
"test that writes fail if the buffer has a bad length" >:: test_buffer_wrong_length;
] @ (if Sys.os_type <> "Win32" then not_implemented_on_windows else [])
Expand Down

0 comments on commit c61e37c

Please sign in to comment.