Skip to content

Commit

Permalink
Merge connect_from_config into connect with optional arguments
Browse files Browse the repository at this point in the history
Existing code which calls `Block.connect path` will still work.

New code can now request different behaviour by supplying the optional
arguments ?buffered and ?sync.

This patch also renames `get_config` to `to_config` so we also have:

  val to_config: t -> Config.t
  (** [to_config t] returns the configuration of a device *)

  val of_config: Config.t -> [ `Ok of t | `Error of error ] io
  (** [of_config config] creates a fresh device from [config] *)

Signed-off-by: David Scott <dave@recoil.org>
  • Loading branch information
djs55 committed Sep 20, 2016
1 parent c61e37c commit c145b50
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
14 changes: 8 additions & 6 deletions lib/block.ml
Expand Up @@ -100,7 +100,7 @@ type t = {
use_fsync_on_flush: bool;
}

let get_config { config } = config
let to_config { config } = config

module Result = struct
type ('a, 'b) result = [
Expand Down 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_from_config ({ Config.buffered; sync; path } as config) =
let of_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 @@ -178,10 +178,12 @@ let remove_prefix prefix x =
then true, String.sub x prefix' (x' - prefix')
else false, x

let connect name =
let buffered, path = remove_prefix buffered_prefix name in
let config = { Config.buffered; sync = false; path } in
connect_from_config config
let connect ?buffered ?sync name =
let legacy_buffered, path = remove_prefix buffered_prefix name in
(* Keep support for the legacy buffered: prefix until version 3.x.y *)
let buffered = if legacy_buffered then Some true else buffered in
let config = Config.create ?buffered ?sync name in
of_config config

let disconnect t = match t.fd with
| Some fd ->
Expand Down
17 changes: 10 additions & 7 deletions lib/block.mli
Expand Up @@ -32,8 +32,6 @@ val blkgetsize: string -> Unix.file_descr -> [ `Ok of int64 | `Error of error ]
given by [fd]. [path] is only used to construct a human-readable error
message. *)

val connect : string -> [`Ok of t | `Error of error] io

module Config: sig
type t = {
buffered: bool; (** true if I/O hits the OS disk caches, false if "direct" *)
Expand All @@ -54,9 +52,11 @@ module Config: sig
(** Parse the result of a previous [to_string] invocation *)
end

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 connect : ?buffered:bool -> ?sync:bool -> string -> [`Ok of t | `Error of error] io
(** [connect ?buffered ?sync path] connects to a block device on the filesystem
at [path]. By default I/O is unbuffered and fully synchronous. These defaults
can be changed by supplying the optional arguments [~buffered:true] and
[~sync:false] *)

val resize : t -> int64 -> [ `Ok of unit | `Error of error ] io
(** [resize t new_size_sectors] attempts to resize the connected device
Expand All @@ -76,5 +76,8 @@ val seek_mapped: t -> int64 -> [ `Ok of int64 | `Error of error ] io
device which may have data in it (typically this is the next mapped
region) *)

val get_config: t -> Config.t
(** [get_config t] returns the configuration of a device *)
val to_config: t -> Config.t
(** [to_config t] returns the configuration of a device *)

val of_config: Config.t -> [ `Ok of t | `Error of error ] io
(** [of_config config] creates a fresh device from [config] *)

0 comments on commit c145b50

Please sign in to comment.