-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from nojb/seek
Add API for seekable read/writes
- Loading branch information
Showing
9 changed files
with
115 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
(** A file opened for reading. *) | ||
class virtual ro = object (_ : <Generic.t; Flow.source; ..>) | ||
method probe _ = None | ||
method read_methods = [] | ||
method virtual pread : file_offset:Optint.Int63.t -> Cstruct.t list -> int | ||
end | ||
|
||
(** A file opened for reading and writing. *) | ||
class virtual rw = object (_ : <Generic.t; Flow.source; Flow.sink; ..>) | ||
inherit ro | ||
method virtual pwrite : file_offset:Optint.Int63.t -> Cstruct.t list -> int | ||
end | ||
|
||
(** [pread t ~file_offset bufs] performs a single read of [t] at [file_offset] into [bufs]. | ||
It returns the number of bytes read, which may be less than the space in [bufs], | ||
even if more bytes are available. Use {!pread_exact} instead if you require | ||
the buffer to be filled. | ||
To read at the current offset, use {!Flow.read} instead. *) | ||
let pread (t : #ro) ~file_offset bufs = | ||
let got = t#pread ~file_offset bufs in | ||
assert (got > 0 && got <= Cstruct.lenv bufs); | ||
got | ||
|
||
(** [pread_exact t ~file_offset bufs] reads from [t] into [bufs] until [bufs] is full. | ||
@raise End_of_file if the buffer could not be filled. *) | ||
let rec pread_exact (t : #ro) ~file_offset bufs = | ||
if Cstruct.lenv bufs > 0 then ( | ||
let got = t#pread ~file_offset bufs in | ||
let file_offset = Optint.Int63.add file_offset (Optint.Int63.of_int got) in | ||
pread_exact t ~file_offset (Cstruct.shiftv bufs got) | ||
) | ||
|
||
(** [pwrite_single t ~file_offset bufs] performs a single write operation, writing | ||
data from [bufs] to location [file_offset] in [t]. | ||
It returns the number of bytes written, which may be less than the length of [bufs]. | ||
In most cases, you will want to use {!pwrite_all} instead. *) | ||
let pwrite_single (t : #rw) ~file_offset bufs = | ||
let got = t#pwrite ~file_offset bufs in | ||
assert (got > 0 && got <= Cstruct.lenv bufs); | ||
got | ||
|
||
(** [pwrite_all t ~file_offset bufs] writes all the data in [bufs] to location [file_offset] in [t]. *) | ||
let rec pwrite_all (t : #rw) ~file_offset bufs = | ||
if Cstruct.lenv bufs > 0 then ( | ||
let got = t#pwrite ~file_offset bufs in | ||
let file_offset = Optint.Int63.add file_offset (Optint.Int63.of_int got) in | ||
pwrite_all t ~file_offset (Cstruct.shiftv bufs got) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters