diff --git a/lib/block.ml b/lib/block.ml index a5d9246..12d6c09 100644 --- a/lib/block.ml +++ b/lib/block.ml @@ -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 = [ @@ -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 @@ -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 -> diff --git a/lib/block.mli b/lib/block.mli index 4f1ff33..034d725 100644 --- a/lib/block.mli +++ b/lib/block.mli @@ -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" *) @@ -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 @@ -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] *)