<commit_before>(*
 * Copyright (C) Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)

open Int64

let kib = 1024L
let mib = mul kib kib
let gib = mul kib mib
let mib_minus_1 = sub mib 1L
let two_mib = mul mib 2L
let max_size = mul gib 2040L

let roundup v block =
  mul block (div (sub (add v block) 1L) block)

let create path size =
  let size = roundup size two_mib in
  if size < mib or size > max_size
  then failwith (Printf.sprintf "VDI size must be between 1 MiB and %Ld MiB" max_size)

let destroy path =
  (* TODO: GC *)
  try Unix.unlink path with _ -> ()

let my_context = ref (Tapctl.create ())
let ctx () = !my_context

let t_detach t = Tapctl.detach (ctx ()) t; Tapctl.free (ctx ()) (Tapctl.get_minor t)
let t_pause t =  Tapctl.pause (ctx ()) t
let t_unpause t = Tapctl.unpause (ctx ()) t
let get_paused t = Tapctl.is_paused (ctx ()) t
let get_activated t = Tapctl.is_active (ctx ()) t

let attach _ =
  let minor = Tapctl.allocate (ctx ()) in
  let tid = Tapctl.spawn (ctx ()) in
  let dev = Tapctl.attach (ctx ()) tid minor in
  let dest = Tapctl.devnode (ctx ()) (Tapctl.get_minor dev) in
  dest

let activate dev file ty =<commit_msg>Throw a VDI_SIZE Backend_error if the requested VDI has a bad size.<commit_after>(*
 * Copyright (C) Citrix Systems Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *)

open Storage_interface
open Common
open Int64

let kib = 1024L
let mib = mul kib kib
let gib = mul kib mib
let mib_minus_1 = sub mib 1L
let two_mib = mul mib 2L
let max_size = mul gib 2040L

let roundup v block =
  mul block (div (sub (add v block) 1L) block)

let create path size =
  let size = roundup size two_mib in
  if size < mib or size > max_size then begin
    error "Cannot create vhd with virtual_size = %Ld MiB (must be between 1 MiB and %Ld MiB)" (div size mib) (div max_size mib);
    raise (Backend_error("VDI_SIZE", [ to_string size; to_string mib; to_string (div max_size mib) ]))
  end;
  Vhd.create path size (Vhd.Ty_dynamic) max_size []

let destroy path =
  (* TODO: GC *)
  try Unix.unlink path with _ -> ()

let my_context = ref (Tapctl.create ())
let ctx () = !my_context

let t_detach t = Tapctl.detach (ctx ()) t; Tapctl.free (ctx ()) (Tapctl.get_minor t)
let t_pause t =  Tapctl.pause (ctx ()) t
let t_unpause t = Tapctl.unpause (ctx ()) t
let get_paused t = Tapctl.is_paused (ctx ()) t
let get_activated t = Tapctl.is_active (ctx ()) t

let attach _ =
  let minor = Tapctl.allocate (ctx ()) in
  let tid = Tapctl.spawn (ctx ()) in
  let dev = Tapctl.attach (ctx ()) tid minor in
  let dest = Tapctl.devnode (ctx ()) (Tapctl.get_minor dev) in
  dest

let activate dev file ty =
{'fraction_non_alphanumeric': 0.08655973451327434, 'fraction_numerical': 0.009402654867256638, 'mean_word_length': 3.6017811704834606, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3250463', 'n_tokens_mistral': 1257, 'n_tokens_neox': 1195, 'n_words': 526}