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.unix/block.ml.html

Source file block.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
77
78
79
let error_msgf fmt = Fmt.kstr (fun msg -> Error (`Msg msg)) fmt

type t = { fd: Unix.file_descr; sector_size: int; length: int; mutex: Mutex.t }

let sector_size { sector_size; _ } = sector_size
let length { length; _ } = length

let pread fd buf ~off =
  let _ = Unix.lseek fd off Unix.SEEK_SET in
  let len = Bytes.length buf in
  let rec go dst_off =
    if dst_off < len then
      match Unix.read fd buf dst_off (len - dst_off) with
      | 0 -> Bytes.fill buf dst_off (len - dst_off) '\000'
      | n -> go (dst_off + n)
  in
  go 0

let pwrite fd buf ~off =
  let _ = Unix.lseek fd off Unix.SEEK_SET in
  let len = Bytes.length buf in
  let rec go src_off =
    if src_off < len then
      let n = Unix.write fd buf src_off (len - src_off) in
      go (src_off + n)
  in
  go 0

let atomic_read t ~src_off ?(dst_off = 0) bstr =
  if src_off land (t.sector_size - 1) <> 0 then
    Fmt.invalid_arg "Block_unix.atomic_read: unaligned offset (%d)" src_off;
  let buf = Bytes.create t.sector_size in
  Mutex.protect t.mutex (fun () -> pread t.fd buf ~off:src_off);
  Bstr.blit_from_bytes buf ~src_off:0 bstr ~dst_off ~len:t.sector_size

let atomic_write t ?(src_off = 0) ~dst_off bstr =
  if dst_off land (t.sector_size - 1) <> 0 then
    Fmt.invalid_arg "Block_unix.atomic_write: unaligned offset (%d)" dst_off;
  let buf = Bytes.create t.sector_size in
  Bstr.blit_to_bytes bstr ~src_off buf ~dst_off:0 ~len:t.sector_size;
  Mutex.protect t.mutex (fun () -> pwrite t.fd buf ~off:dst_off)

let of_fd ~sector_size fd =
  let stat = Unix.fstat fd in
  let length = stat.Unix.st_size land lnot (sector_size - 1) in
  { fd; sector_size; length; mutex= Mutex.create () }

let check_sector_size sector_size =
  if sector_size <= 0 || sector_size land (sector_size - 1) <> 0 then
    error_msgf "Invalid sector size: %d (must be a power of two)" sector_size
  else Ok ()

let load ?(sector_size = 0x1000) filename =
  match check_sector_size sector_size with
  | Error _ as err -> err
  | Ok () ->
      begin match Unix.openfile filename Unix.[ O_RDWR ] 0o644 with
      | fd -> Ok (of_fd ~sector_size fd)
      | exception Unix.Unix_error (err, _, _) ->
          error_msgf "%s: %s" filename (Unix.error_message err)
      end

let create ?(sector_size = 0x1000) filename length =
  match check_sector_size sector_size with
  | Error _ as err -> err
  | Ok () when length land (sector_size - 1) <> 0 ->
      error_msgf "%s: length (%d) is not sector-aligned (%d)" filename length
        sector_size
  | Ok () ->
      let m = Unix.[ O_RDWR; O_CREAT; O_TRUNC ] in
      begin match Unix.openfile filename m 0o644 with
      | fd ->
          Unix.ftruncate fd length;
          Ok (of_fd ~sector_size fd)
      | exception Unix.Unix_error (err, _, _) ->
          error_msgf "%s: %s" filename (Unix.error_message err)
      end

let close t = Unix.close t.fd