package qcow-types

  1. Overview
  2. Docs

Source file qcow_config.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
(*
 * Copyright (C) 2017 David Scott <dave@recoil.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *)

type t = {
    id: string
  ; discard: bool
  ; keep_erased: int64 option
  ; compact_after_unmaps: int64 option
  ; check_on_connect: bool
  ; runtime_asserts: bool
  ; read_only: bool
}

let fresh_id =
  let id = ref 0 in
  fun () ->
    let result = "unknown_" ^ string_of_int !id in
    incr id ; result

let create ?(id = fresh_id ()) ?(discard = false) ?keep_erased
    ?compact_after_unmaps ?(check_on_connect = true) ?(runtime_asserts = false)
    ?(read_only = false) () =
  {
    id
  ; discard
  ; keep_erased
  ; compact_after_unmaps
  ; check_on_connect
  ; runtime_asserts
  ; read_only
  }

let to_string t =
  Printf.sprintf
    "id=%s;discard=%b;keep_erased=%scompact_after_unmaps=%s;check_on_connect=%b;runtime_asserts=%b;read_only=%b"
    t.id t.discard
    (match t.keep_erased with None -> "0" | Some x -> Int64.to_string x)
    ( match t.compact_after_unmaps with
    | None ->
        "0"
    | Some x ->
        Int64.to_string x
    )
    t.check_on_connect t.runtime_asserts t.read_only

let default () =
  {
    id= fresh_id ()
  ; discard= false
  ; keep_erased= None
  ; compact_after_unmaps= None
  ; check_on_connect= true
  ; runtime_asserts= false
  ; read_only= false
  }

let of_string txt =
  let open Astring in
  try
    let strings = String.cuts ~sep:";" txt in
    Ok
      (List.fold_left
         (fun t line ->
           match String.cut ~sep:"=" line with
           | None ->
               t
           | Some (k, v) -> (
             match String.Ascii.lowercase k with
             | "id" ->
                 {t with id= v}
             | "discard" ->
                 {t with discard= bool_of_string v}
             | "keep_erased" ->
                 let keep_erased =
                   if v = "0" then None else Some (Int64.of_string v)
                 in
                 {t with keep_erased}
             | "compact_after_unmaps" ->
                 let compact_after_unmaps =
                   if v = "0" then None else Some (Int64.of_string v)
                 in
                 {t with compact_after_unmaps}
             | "check_on_connect" ->
                 {t with check_on_connect= bool_of_string v}
             | "runtime_asserts" ->
                 {t with runtime_asserts= bool_of_string v}
             | "read_only" ->
                 {t with read_only= bool_of_string v}
             | x ->
                 failwith ("Unknown qcow configuration key: " ^ x)
           )
         )
         (default ()) strings
      )
  with e -> Error (`Msg (Printexc.to_string e))