<commit_before>open Stringext
open Listext
open Threadext
open Forkhelpers

type tapdev = {
	minor : int;
	tapdisk_pid : int;
} with rpc

type t = tapdev * string * (string * string) option 


type context = {
	host_local_dir: string;
	dummy: bool;
}

let create () = { host_local_dir = ""; dummy = false }
let create_dummy dir = 
	{host_local_dir=dir; dummy=true }

let get_devnode_dir ctx =
	let d = Printf.sprintf "%s/dev/xen/blktap-2" ctx.host_local_dir in
	Unixext.mkdir_rec d 0o755;
	d
let get_blktapstem ctx = Printf.sprintf "%s/dev/xen/blktap-2/blktap" ctx.host_local_dir
let get_tapdevstem ctx = Printf.sprintf "%s/dev/xen/blktap-2/tapdev" ctx.host_local_dir

type driver = | Vhd | Aio

let string_of_driver = function
| Vhd -> "vhd"
| Aio -> "aio"

(* DUMMY MODE FUNCTIONS *)

let get_minor tapdev = tapdev.minor
let get_tapdisk_pid tapdev = tapdev.tapdisk_pid

module Dummy = struct 
	type dummy_tap = {
		d_minor : int option;
		d_pid : int option;
		d_state : string option;
		d_args : string option;
	} and dummy_tap_list = dummy_tap list with rpc
			
	let d_lock = Mutex.create ()
		
	let get_dummy_tapdisk_list_filename ctx =
		let file = Printf.sprintf "%s/dev/tapdisks" ctx.host_local_dir in
		Unixext.mkdir_rec (Filename.dirname file) 0o777;
		file
			
	let get_dummy_tapdisk_list ctx =
		let filename = get_dummy_tapdisk_list_filename ctx in
		try
			dummy_tap_list_of_rpc (Jsonrpc.of_string (Unixext.string_of_file filename))
		with _ -> []
			
	let write_dummy_tapdisk_list ctx list =
		let filename = get_dummy_tapdisk_list_filename ctx in
		let str = Jsonrpc.to_string (rpc_of_dummy_tap_list list) in
		Unixext.write_string_to_file filename str
			
	let find_next_unused_number list =
		if List.length list = 0 then 0 else
			let list_plus_one = List.map ((+) 1) list in
			let diff = List.set_difference list_plus_one list in
			List.hd diff
				
	let find_next_unused_minor list =
		let minors = List.filter_map (fun t -> t.d_minor) list in
		find_next_unused_number minors
			
	let find_next_unused_pid list =
		let pids = List.filter_map (fun t -> t.d_pid) list in
		find_next_unused_number pids
			
	let get_entry_from_pid pid list =
		try Some (List.find (fun entry -> entry.d_pid = Some pid) list) with _ -> None
			
	let get_entry_from_minor minor list =
		try Some (List.find (fun entry -> entry.d_minor = Some minor) list) with _ -> None
			
	let allocate ctx =
		Mutex.execute d_lock (fun () -> 
			let list = get_dummy_tapdisk_list ctx in
			let minor = find_next_unused_minor list in
			let entry = {
				d_minor = Some minor;
				d_pid = None;
				d_state = None;
				d_args = None;
			} in
			write_dummy_tapdisk_list ctx (entry::list);
			minor
		)
			
	let spawn ctx =<commit_msg>Fix a problem with dummy mode in tapctl<commit_after>open Stringext
open Listext
open Threadext
open Forkhelpers

type tapdev = {
	minor : int;
	tapdisk_pid : int;
} with rpc

type t = tapdev * string * (string * string) option 


type context = {
	host_local_dir: string;
	dummy: bool;
}

let create () = { host_local_dir = ""; dummy = false }
let create_dummy dir = 
	{host_local_dir=dir; dummy=true }

let get_devnode_dir ctx =
	let d = Printf.sprintf "%s/dev/xen/blktap-2" ctx.host_local_dir in
	Unixext.mkdir_rec d 0o755;
	d
let get_blktapstem ctx = Printf.sprintf "%s/dev/xen/blktap-2/blktap" ctx.host_local_dir
let get_tapdevstem ctx = Printf.sprintf "%s/dev/xen/blktap-2/tapdev" ctx.host_local_dir

type driver = | Vhd | Aio

let string_of_driver = function
| Vhd -> "vhd"
| Aio -> "aio"

(* DUMMY MODE FUNCTIONS *)

let get_minor tapdev = tapdev.minor
let get_tapdisk_pid tapdev = tapdev.tapdisk_pid

module Dummy = struct 
	type dummy_tap = {
		d_minor : int option;
		d_pid : int option;
		d_state : string option;
		d_args : string option;
	} and dummy_tap_list = dummy_tap list with rpc
			
	let d_lock = Mutex.create ()
		
	let get_dummy_tapdisk_list_filename ctx =
		let file = Printf.sprintf "%s/dev/tapdisks" ctx.host_local_dir in
		Unixext.mkdir_rec (Filename.dirname file) 0o777;
		file
			
	let get_dummy_tapdisk_list ctx =
		let filename = get_dummy_tapdisk_list_filename ctx in
		try
			dummy_tap_list_of_rpc (Jsonrpc.of_string (Unixext.string_of_file filename))
		with _ -> []
			
	let write_dummy_tapdisk_list ctx list =
		let filename = get_dummy_tapdisk_list_filename ctx in
		let str = Jsonrpc.to_string (rpc_of_dummy_tap_list list) in
		Unixext.write_string_to_file filename str
			
	let find_next_unused_number list =
		if List.length list = 0 then 0 else
			let list_plus_one = List.map ((+) 1) list in
			let diff = List.set_difference list_plus_one list in
			List.hd diff
				
	let find_next_unused_minor list =
		let minors = List.filter_map (fun t -> t.d_minor) list in
		find_next_unused_number minors
			
	let find_next_unused_pid list =
		let pids = List.filter_map (fun t -> t.d_pid) list in
		find_next_unused_number pids
			
	let get_entry_from_pid pid list =
		try Some (List.find (fun entry -> entry.d_pid = Some pid) list) with _ -> None
			
	let get_entry_from_minor minor list =
		try Some (List.find (fun entry -> entry.d_minor = Some minor) list) with _ -> None
			
	let allocate ctx =
		Mutex.execute d_lock (fun () -> 
			let list = get_dummy_tapdisk_list ctx in
			let minor = find_next_unused_minor list in
			let entry = {
				d_minor = Some minor;
				d_pid = None;
				d_state = None;
				d_args = None;
			} in
			let stem = get_tapdevstem ctx in
			let dummy_device = Printf.sprintf "%s%d" stem minor in
			Unixext.unlink_safe dummy_device;
			Unixext.touch_file dummy_device;
			write_dummy_tapdisk_list ctx (entry::list);
			minor
		)
			
	let spawn ctx =
{'fraction_non_alphanumeric': 0.08216503992901508, 'fraction_numerical': 0.004968944099378882, 'mean_word_length': 3.6008163265306123, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '6226792', 'n_tokens_mistral': 2345, 'n_tokens_neox': 2083, 'n_words': 569}