package mgit

Build not yet checked
  1. Overview
  2. Docs
Git in OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

mgit-0.1.0.tbz
sha256=6fad812ce53e69e045c4d53d958fc0f12c13900841348dd37f0ba6dd0db2ba8d
sha512=3c2427bc9ee42748aa99540582125aea989dca574493ef46cae70d67ab005c517f079c6c537507b3cb53544811864d52d30df22993b7c297f6a6521f6484ff82

doc/src/mgit.blk/append.ml.html

Source file append.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(* NOTE(dinosaure): mainly from mkernel-memtrace *)

exception Out_of_space

module type S = sig
  type t
  type blk

  val append : t -> ?off:int -> ?len:int -> string -> unit
  val flush : t -> unit
  val create : blk -> ?off:int -> int -> t
  val position : t -> int
  val written : t -> int
  val full : t -> bool
  val sink : init:(unit -> t) -> (string, unit) Flux.sink
end

module Make (Block : Device.S) = struct
  type blk = Block.t

  type t = {
      blk: Block.t
    ; sector: Bstr.t
    ; limit: int
    ; mutable cursor: int
    ; mutable fill: int
    ; mutable written: int
  }

  let position t = t.cursor + t.fill
  let written t = t.written
  let full t = t.cursor + t.fill >= t.limit

  let issue t =
    Block.atomic_write t.blk ~dst_off:t.cursor t.sector;
    t.cursor <- t.cursor + Bstr.length t.sector;
    t.fill <- 0

  let rec fill_sectors t ~off ~len str =
    if len > 0 then begin
      let sector_size = Bstr.length t.sector in
      if t.cursor + t.fill + len > t.limit then raise Out_of_space;
      let can = Int.min len (sector_size - t.fill) in
      Bstr.blit_from_string str ~src_off:off t.sector ~dst_off:t.fill ~len:can;
      t.fill <- t.fill + can;
      t.written <- t.written + can;
      if t.fill = sector_size then issue t;
      fill_sectors t ~off:(off + can) ~len:(len - can) str
    end

  let append t ?(off = 0) ?len str =
    let len =
      match len with Some len -> len | None -> String.length str - off
    in
    if off < 0 || len < 0 || off > String.length str - len then
      invalid_arg "Append.append";
    fill_sectors t ~off ~len str

  let flush t =
    if t.fill > 0 then begin
      let sector_size = Bstr.length t.sector in
      Bstr.fill t.sector ~off:t.fill ~len:(sector_size - t.fill) '\000';
      issue t
    end

  let create blk ?(off = 0) limit =
    let sector_size = Block.sector_size blk in
    if off land (sector_size - 1) <> 0 then
      invalid_arg "Append.make: off must be sector-aligned";
    let sector = Bstr.create sector_size in
    { blk; sector; limit; cursor= off; fill= 0; written= 0 }

  let sink ~init =
    let push t str = append t str; t in
    Flux.Sink { init; push; full; stop= flush }
end