diff --git a/src/bigarray/CCBigstring.ml b/src/bigarray/CCBigstring.ml index 7993d15c..efa37a74 100644 --- a/src/bigarray/CCBigstring.ml +++ b/src/bigarray/CCBigstring.ml @@ -207,13 +207,17 @@ let to_gen_slice a i len = let map_file_descr ?pos ?(shared=false) fd len = B.map_file fd ?pos Bigarray.char Bigarray.c_layout shared len -let with_map_file ?pos ?(mode=0o644) ?(flags=[Unix.O_RDONLY]) ?shared name len f = - let fd = Unix.openfile name flags mode in - let a = map_file_descr ?pos ?shared fd len in +let with_map_file ?pos ?len ?(mode=0o644) ?(flags=[Open_rdonly]) ?shared name f = + let ic = open_in_gen flags mode name in + let len = match len with + | None -> in_channel_length ic + | Some n -> n + in + let a = map_file_descr ?pos ?shared (Unix.descr_of_in_channel ic) len in try let x = f a in - Unix.close fd; + close_in ic; x with e -> - Unix.close fd; + close_in ic; raise e diff --git a/src/bigarray/CCBigstring.mli b/src/bigarray/CCBigstring.mli index 32283bd8..952de8a1 100644 --- a/src/bigarray/CCBigstring.mli +++ b/src/bigarray/CCBigstring.mli @@ -107,11 +107,12 @@ val to_gen_slice : t -> int -> int -> char gen (** {2 Memory-map} *) val with_map_file : - ?pos:int64 -> ?mode:int -> ?flags:Unix.open_flag list -> ?shared:bool -> - string -> int -> (t -> 'a) -> 'a -(** [with_map_file name len f] maps the file into memory, opening it, and - call [f] with a slice [pos.... pos+len] of the bytes of the file. When - [f] returns, the file is closed. + ?pos:int64 -> ?len:int -> ?mode:int -> ?flags:open_flag list -> ?shared:bool -> + string -> (t -> 'a) -> 'a +(** [with_map_file name f] maps the file into memory, opening it, and + call [f] with a slice [pos.... pos+len] of the bytes of the file + where [len] is the length of the file if not provided. + When [f] returns, the file is closed. @param pos offset in the file (default 0) @param shared if true, modifications are shared between processes that have mapped this file (requires the filedescr to be open in write mode).