text
stringlengths 12
786k
|
---|
let wrap_env fenv ftree arg = let env = ! printing_env in set_printing_env ( fenv env ) ; let tree = ftree arg in set_printing_env env ; tree |
let filter_rem_sig item rem = match item , rem with | Sig_class _ , ctydecl :: tydecl1 :: tydecl2 :: rem -> ( [ ctydecl ; tydecl1 ; tydecl2 ] , rem ) | Sig_class_type _ , tydecl1 :: tydecl2 :: rem -> ( [ tydecl1 ; tydecl2 ] , rem ) | _ -> ( [ ] , rem ) |
let dummy = { type_params = [ ] ; type_arity = 0 ; type_kind = Type_abstract ; type_private = Public ; type_manifest = None ; type_variance = [ ] ; type_newtype_level = None ; type_loc = Location . none ; type_attributes = [ ] ; type_immediate = false ; type_unboxed = unboxed_false_default_false ; } |
let hide_rec_items = function | Sig_type ( id , _decl , rs ) :: rem when rs = Trec_first && not ! Clflags . real_paths -> let rec get_ids = function Sig_type ( id , _ , Trec_next ) :: rem -> id :: get_ids rem | _ -> [ ] in let ids = id :: get_ids rem in set_printing_env ( List . fold_right ( fun id -> Env . add_type ~ check : false ( Ident . rename id ) dummy ) ids ! printing_env ) | _ -> ( ) |
let rec tree_of_modtype ( ? ellipsis = false ) = function | Mty_ident p -> Omty_ident ( tree_of_path p ) | Mty_signature sg -> Omty_signature ( if ellipsis then [ Osig_ellipsis ] else tree_of_signature sg ) | Mty_functor ( param , ty_arg , ty_res ) -> let res = match ty_arg with None -> tree_of_modtype ~ ellipsis ty_res | Some mty -> wrap_env ( Env . add_module ~ arg : true param mty ) ( tree_of_modtype ~ ellipsis ) ty_res in Omty_functor ( Ident . name param , may_map ( tree_of_modtype ~ ellipsis : false ) ty_arg , res ) | Mty_alias ( _ , p ) -> Omty_alias ( tree_of_path p ) wrap_env ( fun env -> env ) ( tree_of_signature_rec ! printing_env false ) sg [ ] -> [ ] | item :: rem as items -> let in_type_group = match in_type_group , item with true , Sig_type ( _ , _ , Trec_next ) -> true | _ , Sig_type ( _ , _ , ( Trec_not | Trec_first ) ) -> set_printing_env env ' ; true | _ -> set_printing_env env ' ; false in let ( sg , rem ) = filter_rem_sig item rem in hide_rec_items items ; let trees = trees_of_sigitem item in let env ' = Env . add_signature ( item :: sg ) env ' in trees @ tree_of_signature_rec env ' in_type_group rem | Sig_value ( id , decl ) -> [ tree_of_value_description id decl ] | Sig_type ( id , _ , _ ) when is_row_name ( Ident . name id ) -> [ ] | Sig_type ( id , decl , rs ) -> [ tree_of_type_declaration id decl rs ] | Sig_typext ( id , ext , es ) -> [ tree_of_extension_constructor id ext es ] | Sig_module ( id , md , rs ) -> let ellipsis = List . exists ( function ( { txt " . . . " } , = Parsetree . PStr [ ] ) -> true | _ -> false ) md . md_attributes in [ tree_of_module id md . md_type rs ~ ellipsis ] | Sig_modtype ( id , decl ) -> [ tree_of_modtype_declaration id decl ] | Sig_class ( id , decl , rs ) -> [ tree_of_class_declaration id decl rs ] | Sig_class_type ( id , decl , rs ) -> [ tree_of_cltype_declaration id decl rs ] let mty = match decl . mtd_type with | None -> Omty_abstract | Some mty -> tree_of_modtype mty in Osig_modtype ( Ident . name id , mty ) Osig_module ( Ident . name id , tree_of_modtype ? ellipsis mty , tree_of_rec rs ) end |
module Exn = struct let protect f v ~ final = Spotlib . Exn . protect f v ~ finally : final end |
module Array = struct let shuffle ( ? random = Random . int ) a = let len = Array . length a - 1 in for i = 0 to len - 1 do let d = len - i in let j = random d + i in let x = Array . unsafe_get a i in let y = Array . unsafe_get a j in Array . unsafe_set a i y ; Array . unsafe_set a j x ; done end |
module Base = struct let ( !++ ) r = let v = ! r in incr r ; v end |
module Gc = struct open Gc let used_words ( ) = let c = get ( ) in set { c with minor_heap_size = 1000 ; } ; compact ( ) ; let s = stat ( ) in let res = s . live_words in set c ; res let with_big_compacts f v = let used_before = used_words ( ) in Spotlib . Spot . Exn . protect_with f v ~ finally ( : fun _ -> let used_after = used_words ( ) in used_before , used_after ) end |
let tnb data = try int_of_string ( List . hd ( String . split ' \ 000 ' data ) ) with _ -> 0 |
let do_test xb = let i = ref 0 in let disp op pkt = let tid , rid , ty , data = pkt in printf " % 3x % s % d ( % d ) % s " \% s " . \\ n " %! ! i op tid rid ( Xb . Op . to_string ty ) ( String . escaped data ) ; flush ( stdout ) ; in let y ( tid , ty , data ) = let spkt = ( tid , ! i , ty , data ) in disp " S : " spkt ; send_packet xb tid ! i ty data ; let rpkt = recv_packet xb in disp " R : " rpkt ; let ( _ , _ , _ , data ) = rpkt in data in let x spkt = ignore ( y spkt ) in x ( 0 , Xb . Op . Read , " \ 000 " ) ; x ( 0 , Xb . Op . Write , " \ 000 " ) ; x ( 0 , Xb . Op . Getdomainpath , " \ 000 " ) ; x ( 0 , Xb . Op . Directory , " \ 000 " ) ; x ( 0 , Xb . Op . Mkdir , " \ 000 " ) ; x ( 0 , Xb . Op . Getperms , " \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " \ 000 " ) ; x ( 0 , Xb . Op . Write , " / test \ 000xxx \ 000yyy \ 000 \ 000zzz " ) ; x ( 0 , Xb . Op . Read , " / test " ) ; x ( 0 , Xb . Op . Read , " / test \ 000 " ) ; x ( 0 , Xb . Op . Read , " / test \ 000some \ 000otherargs " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000spurious \ 000abc " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000spurious " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000someotherargs " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000someotherargs \ 000others " ) ; x ( 0 , Xb . Op . Write , " / test \ 000 " ) ; x ( 0 , Xb . Op . Write , " / test \ 000abc " ) ; x ( 0 , Xb . Op . Read , " / test " ) ; x ( 0 , Xb . Op . Read , " / test \ 000 " ) ; x ( 0 , Xb . Op . Read , " / test \ 000 \ 000 " ) ; x ( 0 , Xb . Op . Write , " / test " ) ; x ( 0 , Xb . Op . Read , " / test " ) ; x ( 0 , Xb . Op . Read , " / test \ 000 " ) ; x ( 0 , Xb . Op . Read , " / test \ 000 \ 000 " ) ; x ( 0 , Xb . Op . Write , " \ 000 " ) ; / x ( 0 , Xb . Op . Directory , " / test " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000 " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000 \ 000 " ) ; x ( 0 , Xb . Op . Rm , " / test " ) ; x ( 0 , Xb . Op . Directory , " / test " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000 " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000 \ 000 " ) ; x ( 0 , Xb . Op . Write , " / test / abc \ 000x1 " ) ; x ( 0 , Xb . Op . Write , " / test / def \ 000x2 " ) ; x ( 0 , Xb . Op . Write , " / test / xyz \ 000x3 " ) ; x ( 0 , Xb . Op . Write , " / test / xyz \ 000x4 " ) ; x ( 0 , Xb . Op . Directory , " / test " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000 " ) ; x ( 0 , Xb . Op . Directory , " / test \ 000spurious \ 000 " ) ; x ( 0 , Xb . Op . Getperms , " / test " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000spurious \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test \ 000 \ 000 " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test \ 000n0 \ 000 " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test \ 000n0 " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test \ 000n0 \ 000r1 " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test \ 000n0 \ 000r1 \ 000 " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; x ( 0 , Xb . Op . Setperms , " / test \ 000n0r2 \ 000spurious " ) ; x ( 0 , Xb . Op . Getperms , " / test \ 000 " ) ; let t1 = tnb ( y ( 0 , Xb . Op . Transaction_start , " \ 000 " ) ) in x ( t1 , Xb . Op . Transaction_end , " \ 000 " ) ; let t2 = tnb ( y ( 0 , Xb . Op . Transaction_start , " \ 000 " ) ) in x ( t2 , Xb . Op . Transaction_end , string_of_int t2 ^ " \ 000 " ) ; let t3 = tnb ( y ( 0 , Xb . Op . Transaction_start , " \ 000 \ 000 " ) ) in x ( t3 , Xb . Op . Transaction_end , " 0 \ 000 \ 000 " ) ; ( ) |
let ( ) = let xb = open_xb ( ) in finally ( fun ( ) -> do_test xb ) ( fun ( ) -> close_xb xb ) ; |
module type XStringSig = sig type xstring = UChar . t XArray . t val get : xstring -> int -> UChar . t val set : xstring -> int -> UChar . t -> unit val length : xstring -> int type index val look : xstring -> index -> UChar . t val nth : xstring -> int -> index val first : xstring -> index val last : xstring -> index val out_of_range : xstring -> index -> bool val next : xstring -> index -> index val prev : xstring -> index -> index val move : xstring -> index -> int -> index val compare_index : xstring -> index -> index -> int val make : ? bufsize : int -> int -> UChar . t -> xstring val clear : xstring -> unit val reset : xstring -> unit val copy : xstring -> xstring val sub : xstring -> int -> int -> xstring val add_char : xstring -> UChar . t -> unit val add_text : xstring -> ' a UText . text -> unit val add_xstring : xstring -> xstring -> unit val shrink : xstring -> int -> unit val append : xstring -> xstring -> xstring val utext_of : xstring -> UText . t val ustring_of : xstring -> UText . ustring val iter : ( UChar . t -> unit ) unit -> xstring -> unit val compare : xstring -> xstring -> int end |
module XStringAux : XStringSig = struct include XArray type xstring = UChar . t XArray . t let rec compare_aux i t1 t2 = if i >= length t1 then if i >= length t2 then 0 else ~- 1 else if i >= length t2 then 1 else match UChar . compare ( XArray . get t1 i ) i ( XArray . get t2 i ) i with 0 -> compare_aux ( i + 1 ) 1 t1 t2 | sgn -> sgn let compare t1 t2 = compare_aux 0 t1 t2 let add_xstring = add_xarray let add_char = add_element let add_text b t = UText . iter ( add_char b ) b t let ustring_of b = UText . init_ustring ( length b ) b ( get b ) b let utext_of b = UText . init ( length b ) b ( get b ) b end |
let init len f = XArray . init len ( UChar . chr_of_uint 0 ) 0 f |
module Buf = struct include XStringAux type buf = xstring let create bufsize = make ~ bufsize 0 ( UChar . chr_of_uint 0 ) 0 let contents x = x let add_string = add_xstring let add_buffer = add_xstring end |
module String = struct include String let of_char c = String . make 1 c let rev_map f string = let n = length string in String . init n ( fun i -> f string . [ n - i - 1 ] ) let rev_iter f string = for i = length string - 1 downto 0 do f string . [ i ] done let fold_left f accu string = let accu = ref accu in for i = 0 to length string - 1 do accu := f ! accu string . [ i ] done ; ! accu let fold_right f string accu = let accu = ref accu in for i = length string - 1 downto 0 do accu := f string . [ i ] ! accu done ; ! accu let explode string = fold_right ( fun h t -> h :: t ) string [ ] let implode list = concat " " ( List . map of_char list ) let endswith suffix x = let x_l = String . length x and suffix_l = String . length suffix in suffix_l <= x_l && String . sub x ( x_l - suffix_l ) suffix_l = suffix let startswith prefix x = let x_l = String . length x and prefix_l = String . length prefix in prefix_l <= x_l && String . sub x 0 prefix_l = prefix let isspace = function ' ' | ' \ n ' | ' \ r ' | ' \ t ' -> true | _ -> false let strip predicate string = let rec remove = function | [ ] -> [ ] | c :: cs -> if predicate c then remove cs else c :: cs in implode ( List . rev ( remove ( List . rev ( remove ( explode string ) ) ) ) ) let escaped ? rules string = match rules with | None -> String . escaped string | Some rules -> let aux h t = ( if List . mem_assoc h rules then List . assoc h rules else of_char h ) :: t in concat " " ( fold_right aux string [ ] ) let split_f p str = let not_p x = not ( p x ) in let rec split_one p acc = function | [ ] -> ( List . rev acc , [ ] ) | c :: cs -> if p c then split_one p ( c :: acc ) cs else ( List . rev acc , c :: cs ) in let rec alternate acc drop chars = if chars = [ ] then acc else let a , b = split_one ( if drop then p else not_p ) [ ] chars in alternate ( if drop then acc else a :: acc ) ( not drop ) b in List . rev ( List . map implode ( alternate [ ] true ( explode str ) ) ) let index_opt s c = let rec loop i = if String . length s = i then None else if s . [ i ] = c then Some i else loop ( i + 1 ) in loop 0 let rec split ( ? limit = - 1 ) c s = let i = match index_opt s c with Some x -> x | None -> - 1 in let nlimit = if limit = - 1 || limit = 0 then limit else limit - 1 in if i = - 1 || nlimit = 0 then [ s ] else let a = String . sub s 0 i and b = String . sub s ( i + 1 ) ( String . length s - i - 1 ) in a :: split ~ limit : nlimit c b let rtrim s = let n = String . length s in if n > 0 && s . [ n - 1 ] = ' \ n ' then String . sub s 0 ( n - 1 ) else s let has_substr str sub = if String . length sub > String . length str then false else let result = ref false in for start = 0 to String . length str - String . length sub do if String . sub str start ( String . length sub ) = sub then result := true done ; ! result let find_all needle haystack = let m = String . length needle and n = String . length haystack in if m > n then [ ] else let i = ref 0 and found = ref [ ] in while ! i < n - m + 1 do if String . sub haystack ! i m = needle then ( found := ! i :: ! found ; i := ! i + m ) else incr i done ; List . rev ! found let replace f t s = let indexes = find_all f s in let n = List . length indexes in if n > 0 then ( let len_f = String . length f and len_t = String . length t in let new_len = String . length s + ( n * len_t ) - ( n * len_f ) in let new_b = Bytes . make new_len ' \ 000 ' in let orig_offset = ref 0 and dest_offset = ref 0 in List . iter ( fun h -> let len = h - ! orig_offset in Bytes . blit_string s ! orig_offset new_b ! dest_offset len ; Bytes . blit_string t 0 new_b ( ! dest_offset + len ) len_t ; orig_offset := ! orig_offset + len + len_f ; dest_offset := ! dest_offset + len + len_t ) indexes ; Bytes . blit_string s ! orig_offset new_b ! dest_offset ( String . length s - ! orig_offset ) ; Bytes . unsafe_to_string new_b ) else s let filter_chars s valid = let badchars = ref false in let buf = Buffer . create 0 in for i = 0 to String . length s - 1 do if ! badchars then ( if valid s . [ i ] then Buffer . add_char buf s . [ i ] ) else if not ( valid s . [ i ] ) then ( Buffer . add_substring buf s 0 i ; badchars := true ) done ; if ! badchars then Buffer . contents buf else s let map_unlikely s f = let changed = ref false in let m = ref 0 in let buf = Buffer . create 0 in for i = 0 to String . length s - 1 do match f s . [ i ] with | None -> ( ) | Some n -> changed := true ; Buffer . add_substring buf s ! m ( i - ! m ) ; Buffer . add_string buf n ; m := i + 1 done ; if ! changed then ( Buffer . add_substring buf s ! m ( String . length s - ! m ) ; Buffer . contents buf ) else s let sub_to_end s start = let length = String . length s in String . sub s start ( length - start ) let sub_before c s = String . sub s 0 ( String . index s c ) let sub_after c s = sub_to_end s ( String . index s c + 1 ) end |
let test_boolean tested_f ( name , case , expected ) = let check ( ) = Alcotest . ( check bool ) name expected ( tested_f case ) in ( name , ` Quick , check ) |
let test_string tested_f ( name , case , expected ) = let check ( ) = Alcotest . ( check string ) name expected ( tested_f case ) in ( name , ` Quick , check ) |
let test_list tested_f ( name , case , expected ) = let check ( ) = Alcotest . ( check @@ list string ) name expected ( tested_f case ) in ( name , ` Quick , check ) |
let test_rev_map = let spec_rev = [ ( " " , " " ) ; ( " foo bar " , " rab oof " ) ] in let spec_func = [ ( " id " , Fun . id ) ; ( " uppercase_ascii " , Char . uppercase_ascii ) ] in let test ( f_name , f ) ( case , expected ) = let expected = String . map f expected in let name = Printf . sprintf { " |% s " produces " % s " ( % s ) } | case expected f_name in test_string ( XString . rev_map f ) ( name , case , expected ) in let tests = List . concat ( List . map ( fun func -> List . map ( test func ) spec_rev ) spec_func ) in ( " rev_map " , tests ) |
let test_split = let test ? limit ( splitter , splitted , expected ) = let split , name = match limit with | None -> let name = Printf . sprintf { ' |% c ' splits " % s " } | splitter splitted in let split = XString . split ~ limit ( :- 1 ) in ( split , name ) | Some limit -> let name = Printf . sprintf { ' |% c ' splits " % s " with limit % i } | splitter splitted limit in let split = XString . split ~ limit in ( split , name ) in test_list ( split splitter ) ( name , splitted , expected ) in let specs_no_limit = [ ( ' . ' , " . . . " , [ " " ; " " ; " " ; " " ] ) ; ( ' . ' , " foo . bar . baz " , [ " foo " ; " bar " ; " baz " ] ) ] in let tests_no_limit = List . map test specs_no_limit in let specs_limit = [ ( 0 , [ ( ' . ' , " . . . " , [ " . . . " ] ) ; ( ' . ' , " foo . bar . baz " , [ " foo . bar . baz " ] ) ] ) ; ( 1 , [ ( ' . ' , " . . . " , [ " . . . " ] ) ; ( ' . ' , " foo . bar . baz " , [ " foo . bar . baz " ] ) ] ) ; ( 2 , [ ( ' . ' , " . . . " , [ " " ; " . . " ] ) ; ( ' . ' , " foo . bar . baz " , [ " foo " ; " bar . baz " ] ) ] ) ; ( 3 , [ ( ' . ' , " . . . " , [ " " ; " " ; " . " ] ) ; ( ' . ' , " foo . bar . baz " , [ " foo " ; " bar " ; " baz " ] ) ] ) ; ( 4 , [ ( ' . ' , " . . . " , [ " " ; " " ; " " ; " " ] ) ] ) ] in let tests_limit = List . map ( fun ( limit , spec ) -> List . map ( test ~ limit ) spec ) specs_limit |> List . concat in ( " split " , List . concat [ tests_no_limit ; tests_limit ] ) |
let test_split_f = let specs = [ ( XString . isspace , " foo bar " , [ " foo " ; " bar " ] ) ; ( XString . isspace , " foo bar " , [ " foo " ; " bar " ] ) ; ( XString . isspace , " foo \ n \ t \ r bar " , [ " foo " ; " bar " ] ) ; ( XString . isspace , " foo bar " , [ " foo " ; " bar " ] ) ; ( XString . isspace , " " , [ ] ) ; ( XString . isspace , " " , [ ] ) ] in let test ( splitter , splitted , expected ) = let name = Printf . sprintf { " |% s " } | ( String . escaped splitted ) in test_list ( XString . split_f splitter ) ( name , splitted , expected ) in let tests = List . map test specs in ( " split_f " , tests ) |
let test_has_substr = let spec = [ ( " " , " " , true ) ; ( " " , " foo bar " , true ) ; ( " f " , " foof " , true ) ; ( " foofo " , " foof " , false ) ; ( " foof " , " foof " , true ) ; ( " f " , " foof " , true ) ; ( " fo " , " foof " , true ) ; ( " of " , " foof " , true ) ; ( " ff " , " foof " , false ) ] in let test ( contained , container , expected ) = let name = Printf . sprintf { " |% s " in " % s " } | contained container in test_boolean ( XString . has_substr container ) ( name , contained , expected ) in ( " has_substr " , List . map test spec ) |
let test_startswith = let spec = [ ( " " , " " , true ) ; ( " " , " foo bar " , true ) ; ( " foofo " , " foof " , false ) ; ( " foof " , " foof " , true ) ; ( " f " , " foof " , true ) ; ( " fo " , " foof " , true ) ; ( " of " , " foof " , false ) ; ( " ff " , " foof " , false ) ] in let test ( contained , container , expected ) = let name = Printf . sprintf { " |% s " starts with " % s " } | container contained in test_boolean ( XString . startswith contained ) ( name , container , expected ) in ( " startswith " , List . map test spec ) |
let test_endswith = let spec = [ ( " " , " " , true ) ; ( " " , " foo bar " , true ) ; ( " ofoof " , " foof " , false ) ; ( " foof " , " foof " , true ) ; ( " f " , " foof " , true ) ; ( " fo " , " foof " , false ) ; ( " of " , " foof " , true ) ; ( " ff " , " foof " , false ) ] in let test ( contained , container , expected ) = let name = Printf . sprintf { " |% s " ends with " % s " } | container contained in test_boolean ( XString . endswith contained ) ( name , container , expected ) in ( " endswith " , List . map test spec ) |
let test_rtrim = let spec = [ ( " " , " " ) ; ( " \ n " , " " ) ; ( " \ n \ n " , " \ n " ) ; ( " \ n " , " \ n " ) ; ( " foo \ n " , " foo " ) ; ( " fo \ no " , " fo \ no " ) ] in let test ( case , expected ) = let name = Printf . sprintf { " |% s " gets trimmed to " % s " } | ( String . escaped case ) ( String . escaped expected ) in test_string XString . rtrim ( name , case , expected ) in ( " rtrim " , List . map test spec ) |
let ( ) = Alcotest . run " Xstringext " [ test_rev_map ; test_split ; test_split_f ; test_has_substr ; test_startswith ; test_endswith ; test_rtrim ] |
let ( |> ) a b = b a |
let getdomainpath domid client = immediate client ( fun xs -> getdomainpath xs domid ) >>= fun dom_path -> return ( prefix ^ dom_path ) |
let readdir d client = Lwt . catch ( fun ( ) -> immediate client ( fun xs -> directory xs d ) ) ( function | Xs_protocol . Enoent _ -> return [ ] | e -> Lwt . fail e ) |
let read_opt path xs = Lwt . catch ( fun ( ) -> read xs path >>= fun x -> Lwt . return ( Some x ) ) ( function | Xs_protocol . Enoent _ -> return None | e -> Lwt . fail e ) |
let exists path xs = read_opt path xs >|= ( fun x -> x <> None ) |
module Device = struct type kind = Vif | Vbd | Tap | Pci | Vfs | Vfb | Vkbd let kind_of_string = function | " vif " -> Some Vif | " vbd " -> Some Vbd | " tap " -> Some Tap | " pci " -> Some Pci | " vfs " -> Some Vfs | " vfb " -> Some Vfb | " vkbd " -> Some Vkbd | x -> None let string_of_kind = function | Vif -> " vif " | Vbd -> " vbd " | Tap -> " tap " | Pci -> " pci " | Vfs -> " vfs " | Vfb -> " vfb " | Vkbd -> " vkbd " type devid = int type endpoint = { domid : int ; kind : kind ; devid : int } type device = { frontend : endpoint ; backend : endpoint } let parse_int i = try Some ( int_of_string i ) with _ -> None let rec split ? limit ( : limit ( =- 1 ) ) c s = let i = try String . index s c with Not_found -> - 1 in let nlimit = if limit = - 1 || limit = 0 then limit else limit - 1 in if i = - 1 || nlimit = 0 then [ s ] else let a = String . sub s 0 i and b = String . sub s ( i + 1 ) ( String . length s - i - 1 ) in a :: ( split ~ limit : nlimit c b ) let parse_backend_link x = match split ' ' / x with | [ " " ; " local " ; " domain " ; domid ; " backend " ; kind ; _ ; devid ] -> begin match parse_int domid , kind_of_string kind , parse_int devid with | Some domid , Some kind , Some devid -> Some { domid = domid ; kind = kind ; devid = devid } | _ , _ , _ -> None end | _ -> None let to_list xs = List . fold_left ( fun acc x -> match x with | Some x -> x :: acc | None -> acc ) [ ] xs let list_kinds dir client = readdir dir client >|= List . map kind_of_string >|= to_list let list_frontends domid client = getdomainpath domid client >>= fun dom_path -> let frontend_dir = dom_path ^ " / device " in list_kinds frontend_dir client >>= fun kinds -> Lwt_list . map_s ( fun k -> let dir = Printf . sprintf " % s /% s " frontend_dir ( string_of_kind k ) in readdir dir client >|= List . map parse_int >|= to_list >>= fun devids -> Lwt_list . map_s ( fun devid -> let frontend = { domid = domid ; kind = k ; devid = devid } in Lwt . catch ( fun ( ) -> immediate client ( fun xs -> read xs ( Printf . sprintf " % s /% d / backend " dir devid ) >>= fun x -> match parse_backend_link x with | Some b -> return ( Some { backend = b ; frontend = frontend } ) | None -> return None ) ) ( fun _ -> return None ) ) devids >|= to_list ) kinds >>= fun ll -> return ( List . concat ll ) let backend_path_of_device ( x : device ) client = getdomainpath x . backend . domid client >>= fun dom_path -> return ( Printf . sprintf " % s / backend /% s /% u /% d " dom_path ( string_of_kind x . backend . kind ) x . frontend . domid x . backend . devid ) let backend_error_path_of_device ( x : device ) client = getdomainpath x . backend . domid client >>= fun dom_path -> return ( Printf . sprintf " % s / error / backend /% s /% d " dom_path ( string_of_kind x . backend . kind ) x . frontend . domid ) let frontend_path_of_device ( x : device ) client = getdomainpath x . backend . domid client >>= fun dom_path -> return ( Printf . sprintf " % s / device /% s /% d " dom_path ( string_of_kind x . frontend . kind ) x . frontend . devid ) let frontend_error_path_of_device ( x : device ) client = getdomainpath x . frontend . domid client >>= fun dom_path -> return ( Printf . sprintf " % s / error / device /% s /% d / error " dom_path ( string_of_kind x . frontend . kind ) x . frontend . devid ) let hard_shutdown_request ( x : device ) client = backend_path_of_device x client >>= fun backend_path -> frontend_path_of_device x client >>= fun frontend_path -> let online_path = backend_path ^ " / online " in immediate client ( fun xs -> write xs online_path " 0 " >>= fun ( ) -> rm xs frontend_path ) let private_path = prefix ^ " / xapi " let get_private_path domid = Printf . sprintf " % s /% d " private_path domid let get_private_data_path_of_device ( x : device ) = Printf . sprintf " % s / private /% s /% d " ( get_private_path x . frontend . domid ) ( string_of_kind x . backend . kind ) x . backend . devid let get_hotplug_path ( x : device ) = Printf . sprintf " % s / hotplug /% s /% d " ( get_private_path x . frontend . domid ) ( string_of_kind x . backend . kind ) x . backend . devid let get_private_data_path_of_device ( x : device ) = Printf . sprintf " % s / private /% s /% d " ( get_private_path x . frontend . domid ) ( string_of_kind x . backend . kind ) x . backend . devid let rm_device_state ( x : device ) client = immediate client ( fun xs -> frontend_path_of_device x client >>= fun fe -> backend_path_of_device x client >>= fun be -> backend_error_path_of_device x client >>= fun ber -> frontend_error_path_of_device x client >>= fun fer -> Lwt_list . iter_s ( rm xs ) [ fe ; be ; ber ; Filename . dirname fer ] ) let hard_shutdown device client = hard_shutdown_request device client >>= fun ( ) -> rm_device_state device client let add device client = let backend_list = [ ] and frontend_list = [ ] and private_list = [ ] in frontend_path_of_device device client >>= fun frontend_path -> backend_path_of_device device client >>= fun backend_path -> let hotplug_path = get_hotplug_path device in let private_data_path = get_private_data_path_of_device device in transaction client ( fun xs -> exists ( Printf . sprintf " / local / domain /% d / vm " device . backend . domid ) xs >>= fun _ -> exists frontend_path xs >>= fun _ -> Lwt . catch ( fun ( ) -> rm xs frontend_path ) ( fun _ -> return ( ) ) >>= fun ( ) -> Lwt . catch ( fun ( ) -> rm xs backend_path ) ( fun _ -> return ( ) ) >>= fun ( ) -> mkdir xs frontend_path >>= fun ( ) -> setperms xs frontend_path ( Xs_protocol . ACL . ( { owner = device . frontend . domid ; other = NONE ; acl = [ device . backend . domid , READ ] } ) ) >>= fun ( ) -> mkdir xs backend_path >>= fun ( ) -> setperms xs backend_path ( Xs_protocol . ACL . ( { owner = device . backend . domid ; other = NONE ; acl = [ device . frontend . domid , READ ] } ) ) >>= fun ( ) -> mkdir xs hotplug_path >>= fun ( ) -> setperms xs hotplug_path ( Xs_protocol . ACL . ( { owner = device . backend . domid ; other = NONE ; acl = [ ] } ) ) >>= fun ( ) -> Lwt_list . iter_s ( fun ( x , y ) -> write xs ( frontend_path ^ " " / ^ x ) y ) ( ( " backend " , backend_path ) :: frontend_list ) >>= fun ( ) -> Lwt_list . iter_s ( fun ( x , y ) -> write xs ( backend_path ^ " " / ^ x ) y ) ( ( " frontend " , frontend_path ) :: backend_list ) >>= fun ( ) -> mkdir xs private_data_path >>= fun ( ) -> setperms xs private_data_path ( Xs_protocol . ACL . ( { owner = device . backend . domid ; other = NONE ; acl = [ ] } ) ) >>= fun ( ) -> Lwt_list . iter_s ( fun ( x , y ) -> write xs ( private_data_path ^ " " / ^ x ) y ) ( ( " backend - kind " , string_of_kind device . backend . kind ) :: ( " backend - id " , string_of_int device . backend . domid ) :: private_list ) ) end |
module Domain = struct let make domid client = getdomainpath domid client >>= fun dom_path -> let uuid = Printf . sprintf " uuid -% d " domid in let name = " name " in let vm_path = prefix ^ " / vm " / ^ uuid in let vss_path = prefix ^ " / vss " / ^ uuid in let xsdata = [ " xsdata " , " xsdata " ] in let platformdata = [ " platformdata " , " platformdata " ] in let bios_strings = [ " bios_strings " , " bios_strings " ] in let roperm = Xs_protocol . ACL . ( { owner = 0 ; other = NONE ; acl = [ domid , READ ] } ) in let rwperm = Xs_protocol . ACL . ( { owner = domid ; other = NONE ; acl = [ ] } ) in transaction client ( fun xs -> Lwt . catch ( fun ( ) -> rm xs dom_path ) ( fun _ -> return ( ) ) >>= fun ( ) -> mkdir xs dom_path >>= fun ( ) -> setperms xs dom_path roperm >>= fun ( ) -> immediate client ( exists vm_path ) >>= fun vm_exists -> ( if not vm_exists then begin mkdir xs vm_path >>= fun ( ) -> setperms xs vm_path roperm >>= fun ( ) -> write xs ( vm_path ^ " / uuid " ) uuid >>= fun ( ) -> write xs ( vm_path ^ " / name " ) name end else return ( ) ) >>= fun ( ) -> write xs ( Printf . sprintf " % s / domains /% d " vm_path domid ) dom_path >>= fun ( ) -> mkdir xs vss_path >>= fun ( ) -> setperms xs vss_path rwperm >>= fun ( ) -> write xs ( dom_path ^ " / vm " ) vm_path >>= fun ( ) -> write xs ( dom_path ^ " / vss " ) vss_path >>= fun ( ) -> write xs ( dom_path ^ " / name " ) name >>= fun ( ) -> Lwt_list . iter_s ( fun dir -> let ent = Printf . sprintf " % s /% s " dom_path dir in mkdir xs ent >>= fun ( ) -> setperms xs ent roperm ) [ " cpu " ; " memory " ] >>= fun ( ) -> Lwt_list . iter_s ( fun dir -> let ent = Printf . sprintf " % s /% s " dom_path dir in mkdir xs ent >>= fun ( ) -> setperms xs ent rwperm ) [ " device " ; " error " ; " drivers " ; " control " ; " attr " ; " data " ; " messages " ; " vm - data " ] ) >>= fun ( ) -> immediate client ( fun xs -> Lwt_list . iter_s ( fun ( x , y ) -> write xs ( dom_path ^ " " / ^ x ) y ) xsdata >>= fun ( ) -> Lwt_list . iter_s ( fun ( x , y ) -> write xs ( dom_path ^ " / platform " / ^ x ) y ) platformdata >>= fun ( ) -> Lwt_list . iter_s ( fun ( x , y ) -> write xs ( dom_path ^ " / bios - strings " / ^ x ) y ) bios_strings >>= fun ( ) -> write xs ( dom_path ^ " / action - request " ) " poweroff " >>= fun ( ) -> write xs ( dom_path ^ " / control / platform - feature - multiprocessor - suspend " ) " 1 " >>= fun ( ) -> write xs ( dom_path ^ " / unique - domain - id " ) uuid ) let control_shutdown domid client = getdomainpath domid client >|= ( fun x -> x ^ " / control / shutdown " ) let string_of_shutdown_reason _ = " halt " let get_uuid domid = Printf . sprintf " uuid -% d " domid let shutdown domid req client = let reason = string_of_shutdown_reason req in control_shutdown domid client >>= fun path -> getdomainpath domid client >>= fun dom_path -> transaction client ( fun xs -> immediate client ( exists dom_path ) >>= fun domain_exists -> if domain_exists then begin write xs path reason >>= fun ( ) -> return true end else return false ) let destroy domid client = getdomainpath domid client >>= fun dom_path -> Device . list_frontends domid client >>= fun all_devices -> Lwt_list . iter_s ( fun device -> Device . hard_shutdown device client ) all_devices >>= fun ( ) -> immediate client ( read_opt ( dom_path ^ " / vm " ) ) >>= fun vm_path -> immediate client ( read_opt ( dom_path ^ " / vss " ) ) >>= fun vss_path -> begin match vm_path with | Some vm_path -> immediate client ( fun xs -> rm xs ( vm_path ^ " / domains " / ^ ( string_of_int domid ) ) >>= fun ( ) -> readdir ( vm_path ^ " / domains " ) client >>= fun domains -> if List . filter ( fun x -> x <> " " ) domains = [ ] then begin rm xs vm_path >>= fun ( ) -> begin match vss_path with | Some vss_path -> rm xs vss_path | None -> return ( ) end end else return ( ) ) | None -> return ( ) end >>= fun ( ) -> immediate client ( fun xs -> rm xs dom_path ) >>= fun ( ) -> getdomainpath 0 client >|= ( fun x -> x ^ " / backend " ) >>= fun backend_path -> readdir backend_path client >>= fun all_backend_types -> Lwt_list . iter_s ( fun ty -> immediate client ( fun xs -> rm xs ( Printf . sprintf " % s /% s /% d " backend_path ty domid ) ) ) all_backend_types end |
let vm_shutdown domid client = Domain . shutdown domid ( ) client >>= fun _ -> Domain . destroy domid client |
let vm_start domid client = let vbd devid = { Device . frontend = { Device . domid = domid ; kind = Device . Vbd ; devid = 0 } ; backend = { Device . domid = 0 ; kind = Device . Vbd ; devid = 0 } } in Domain . make domid client >>= fun ( ) -> Lwt_list . iter_s ( fun d -> Device . add d client ) [ vbd 0 ; vbd 1 ; vbd 2 ] |
let vm_cycle domid client = vm_start domid client >>= fun ( ) -> vm_shutdown domid client |
let rec between start finish = if start > finish then [ ] else start :: ( between ( start + 1 ) finish ) |
let sequential n client : unit Lwt . t = Lwt_list . iter_s ( fun domid -> vm_cycle domid client ) ( between 0 n ) |
let parallel n client = Lwt_list . iter_p ( fun domid -> vm_cycle domid client ) ( between 0 n ) |
let query m n client = Lwt_list . iter_s ( fun domid -> vm_start domid client ) ( between 0 n ) >>= fun ( ) -> let rec loop i = if i = m then Lwt . return_unit else begin Lwt_list . iter_p ( fun domid -> immediate client ( fun xs -> read xs ( Printf . sprintf " % s / local / domain /% d / name " prefix domid ) >>= fun _ -> Lwt . return_unit ) ) ( between 0 n ) >>= fun ( ) -> loop ( i + 1 ) end in loop 0 >>= fun ( ) -> Lwt_list . iter_s ( fun domid -> vm_shutdown domid client ) ( between 0 n ) |
let time f = let start = Unix . gettimeofday ( ) in f ( ) >>= fun ( ) -> return ( Unix . gettimeofday ( ) . - start ) |
let usage ( ) = let bin x = Sys . argv . ( 0 ) ^ x in let lines = [ bin " : a xenstore benchmark tool " ; " " ; " Usage " ; : bin " [ - path / var / run / xenstored / socket ] [ - n number of vms ] " ; ] in List . iter ( fun x -> Printf . fprintf stderr " % s \ n " x ) lines |
let main ( ) = let verbose = ref false in let args = Sys . argv |> Array . to_list |> List . tl in if List . mem " - h " args then begin usage ( ) ; return ( ) ; end else begin verbose := List . mem " - v " args ; let args = List . filter ( fun x -> x <> " - v " ) args in let extract args key = let result = ref None in let args = List . fold_left ( fun ( acc , foundit ) x -> if foundit then ( result := Some x ; ( acc , false ) ) else if x = key then ( acc , true ) else ( x :: acc , false ) ) ( [ ] , false ) args |> fst |> List . rev in ! result , args in let path , args = extract args " - path " in begin match path with | Some path -> Xs_transport . xenstored_socket := path | None -> ( ) end ; let n , args = extract args " - n " in let n = match n with | None -> 300 | Some n -> int_of_string n in make ( ) >>= fun client -> time ( fun ( ) -> sequential n client ) >>= fun t -> Lwt_io . write Lwt_io . stdout ( Printf . sprintf " % d sequential starts and shutdowns : . % 02f \ n " n t ) >>= fun ( ) -> time ( fun ( ) -> parallel n client ) >>= fun t -> Lwt_io . write Lwt_io . stdout ( Printf . sprintf " % d parallel starts and shutdowns : . % 02f \ n " n t ) >>= fun ( ) -> time ( fun ( ) -> query 1000 n client ) >>= fun t -> Lwt_io . write Lwt_io . stdout ( Printf . sprintf " % d read queries per % d VMs : . % 02f \ n " 1000 n t ) end |
let _ = Lwt_main . run ( main ( ) ) |
let ( |> ) a b = b a |
type expr = | Val of string | Not of expr | And of expr * expr | Or of expr * expr | Eq of expr * expr |
let rec pretty_print ( ) = let open Format in function | Val x -> sprintf " " \% s " " \ x | Not x -> sprintf " Not ( [ @% a ] ) " @ pretty_print x | And ( x , y ) -> sprintf " And ( [ @% a , @ % a ] ) " @ pretty_print x pretty_print y | Or ( x , y ) -> sprintf " Or ( [ @% a , @ % a ] ) " @ pretty_print x pretty_print y | Eq ( x , y ) -> sprintf " Eq ( [ @% a , @ % a ] ) " @ pretty_print x pretty_print y |
let rec to_conjunction = function | And ( x , y ) -> ( to_conjunction x ) @ ( to_conjunction y ) | Eq ( Val k , Val v ) -> [ k , v ] | _ -> raise Invalid_expression |
let parse_expr s = let open Genlex in let keywords = [ " ( " ; " ) " ; " not " ; " " ; = " and " ; " or " ] in let flatten s = let to_list s = let result = ref [ ] in Stream . iter ( fun x -> result := x :: ! result ) s ; List . rev ! result in let ident is = if is = [ ] then [ ] else [ Ident ( String . concat " " ( List . rev is ) ) ] in let is , tokens = List . fold_left ( fun ( is , tokens ) x -> match is , x with | is , Ident i -> ( i :: is ) , tokens | is , x -> [ ] , ( x :: ( ident is ) @ tokens ) ) ( [ ] , [ ] ) ( to_list s ) in ident is @ tokens |> List . rev |> Stream . of_list in let rec parse_atom ( __strm : _ Stream . t ) = match Stream . peek __strm with | Some ( Int n ) -> ( Stream . junk __strm ; Val ( string_of_int n ) ) | Some ( Ident n ) -> ( Stream . junk __strm ; Val n ) | Some ( Float n ) -> ( Stream . junk __strm ; Val ( string_of_float n ) ) | Some ( String n ) -> ( Stream . junk __strm ; Val n ) | Some ( Kwd " not " ) -> ( Stream . junk __strm ; let e = ( try parse_expr __strm with | Stream . Failure -> raise ( Stream . Error " " ) ) in Not e ) | Some ( Kwd " ( " ) -> ( Stream . junk __strm ; let e = ( try parse_expr __strm with | Stream . Failure -> raise ( Stream . Error " " ) ) in ( match Stream . peek __strm with | Some ( Kwd " ) " ) -> ( Stream . junk __strm ; e ) | _ -> raise ( Stream . Error " " ) ) ) | _ -> raise Stream . Failure and parse_expr ( __strm : _ Stream . t ) = let e1 = parse_atom __strm in let stream = __strm in ( fun ( __strm : _ Stream . t ) -> match Stream . peek __strm with | Some ( Kwd " and " ) -> ( Stream . junk __strm ; let e2 = ( try parse_expr __strm with | Stream . Failure -> raise ( Stream . Error " " ) ) in And ( e1 , e2 ) ) | Some ( Kwd " or " ) -> ( Stream . junk __strm ; let e2 = ( try parse_expr __strm with | Stream . Failure -> raise ( Stream . Error " " ) ) in Or ( e1 , e2 ) ) | Some ( Kwd " " ) = -> ( Stream . junk __strm ; let e2 = ( try parse_expr __strm with | Stream . Failure -> raise ( Stream . Error " " ) ) in Eq ( e1 , e2 ) ) | _ -> e1 ) stream in s |> Stream . of_string |> make_lexer keywords |> flatten |> parse_expr |
let rec eval_expression expr xs = match expr with | Val path -> Lwt . catch ( fun ( ) -> read xs path >>= fun _k -> return true ) ( function Enoent _ -> return false | e -> Lwt . fail e ) | Not a -> eval_expression a xs >>= fun a ' -> return ( not ( a ' ) ) | And ( a , b ) -> eval_expression a xs >>= fun a ' -> eval_expression b xs >>= fun b ' -> return ( a ' && b ' ) | Or ( a , b ) -> eval_expression a xs >>= fun a ' -> eval_expression b xs >>= fun b ' -> return ( a ' || b ' ) | Eq ( Val path , Val v ) -> Lwt . catch ( fun ( ) -> read xs path >>= fun v ' -> return ( v = v ' ) ) ( function | Enoent _ -> return false | e -> Lwt . fail e ) | _ -> fail Invalid_expression |
let usage ( ) = let bin x = Sys . argv . ( 0 ) ^ x in let lines = [ bin " : a xenstore protocol client " ; " " ; " Usage " ; : bin " [ - path / var / run / xenstored / socket ] [ - restrict domid ] < subcommand > [ args ] " ; " " ; " Where < subcommand > can be one of " ; : " " ; bin " read < key " ; > " -- read the value stored at < key , > or fail if it doesn ' t exist " ; bin " write < key = val > [ and keyN = valN ] " ; * " -- write the key value pair ( s ) " ; bin " directory < key " ; > " -- list the direct children of < key " ; > bin " wait < expr " ; > " -- block until the < expr > is true " ; bin " debug < cmd > [ arg ] " ; " -- execute the given debug command " ; " " ; " Example expressions " ; : " " ; bin " wait / foo " ; " -- block until the key " \/ foo " \ exists " ; bin " wait not ( / foo ) " ; " -- block until the key " \/ foo " \ is deleted " ; bin " wait / foo or / bar " ; " -- block until either key " \/ foo " \ or " \/ bar " \ are created " ; bin " wait / foo and ( / bar = hello ) " ; " -- block until either key " \/ foo " \ is created or key " \/ bar " \ has value " \ hello " " ; \ ] in List . iter ( fun x -> Printf . fprintf stderr " % s \ n " x ) lines |
let main ( ) = let verbose = ref false in let args = Sys . argv |> Array . to_list |> List . tl in if List . mem " - h " args then begin usage ( ) ; return ( ) ; end else begin verbose := List . mem " - v " args ; let args = List . filter ( fun x -> x <> " - v " ) args in let extract args key = let result = ref None in let args = List . fold_left ( fun ( acc , foundit ) x -> if foundit then ( result := Some x ; ( acc , false ) ) else if x = key then ( acc , true ) else ( x :: acc , false ) ) ( [ ] , false ) args |> fst |> List . rev in ! result , args in let path , args = extract args " - path " in begin match path with | Some path -> Xs_transport . xenstored_socket := path | None -> ( ) end ; let restrict_domid , args = extract args " - restrict " in let do_restrict xs = match restrict_domid with | Some domid -> restrict xs ( int_of_string domid ) | None -> return ( ) in match args with | [ " read " ; key ] -> make ( ) >>= fun client -> immediate client ( fun xs -> do_restrict xs >>= fun ( ) -> read xs key >>= fun v -> Lwt_io . write Lwt_io . stdout v ) | [ " directory " ; key ] -> make ( ) >>= fun client -> immediate client ( fun xs -> do_restrict xs >>= fun ( ) -> directory xs key >>= fun ls -> Lwt_list . iter_s ( fun x -> Lwt_io . write Lwt_io . stdout ( x ^ " \ n " ) ) ls ) | " write " :: expr -> begin Lwt . catch ( fun ( ) -> let expr = String . concat " " expr |> parse_expr in if ! verbose then Printf . printf " Parsed : % s \ n " %! ( pretty_print ( ) expr ) ; expr |> to_conjunction |> return ) ( function | Invalid_expression as e -> Lwt_io . write Lwt_io . stderr " Invalid expression ; expected < key = val > [ and key = val ] *\ n " >>= fun ( ) -> Lwt . fail e | e -> Lwt . fail e ) >>= fun items -> make ( ) >>= fun client -> immediate client ( fun xs -> do_restrict xs >>= fun ( ) -> Lwt_list . iter_s ( fun ( k , v ) -> write xs k v ) items ) end | " debug " :: cmd_args -> make ( ) >>= fun client -> immediate client ( fun xs -> do_restrict xs >>= fun ( ) -> debug xs cmd_args >>= fun results -> Lwt_list . iter_s ( fun x -> Lwt_io . write Lwt_io . stdout ( x ^ " \ n " ) ) results ) | " wait " :: expr -> Lwt . catch ( fun ( ) -> let expr = String . concat " " expr |> parse_expr in if ! verbose then Printf . printf " Parsed : % s \ n " %! ( pretty_print ( ) expr ) ; make ( ) >>= fun client -> let result = wait client ( fun xs -> do_restrict xs >>= fun ( ) -> eval_expression expr xs >>= fun result -> if not result then fail Eagain else return ( ) ) in Lwt_timeout . create 5 ( fun ( ) -> cancel result ) |> Lwt_timeout . start ; result ) ( function | Invalid_expression as e -> Lwt_io . write Lwt_io . stderr " Invalid expression \ n " >>= fun ( ) -> Lwt . fail e | e -> Lwt . fail e ) | _ -> usage ( ) ; return ( ) end |
let _ = Lwt_main . run ( main ( ) ) |
module type IO = sig type ' a t = ' a Lwt . t val return : ' a -> ' a t val ( >>= ) : ' a t -> ( ' a -> ' b t ) -> ' b t type backend = [ ` xen | ` unix ] val backend : backend type channel val create : unit -> channel t val destroy : channel -> unit t val read : channel -> string -> int -> int -> int t val write : channel -> string -> int -> int -> unit t end |
module type S = sig type client val make : unit -> client Lwt . t val suspend : client -> unit Lwt . t val resume : client -> unit Lwt . t type handle val immediate : client -> ( handle -> ' a Lwt . t ) -> ' a Lwt . t val transaction : client -> ( handle -> ' a Lwt . t ) -> ' a Lwt . t val wait : client -> ( handle -> ' a Lwt . t ) -> ' a Lwt . t val directory : handle -> string -> string list Lwt . t val read : handle -> string -> string Lwt . t val write : handle -> string -> string -> unit Lwt . t val rm : handle -> string -> unit Lwt . t val mkdir : handle -> string -> unit Lwt . t val setperms : handle -> string -> Xs_protocol . ACL . t -> unit Lwt . t val debug : handle -> string list -> string list Lwt . t val restrict : handle -> int -> unit Lwt . t val getdomainpath : handle -> int -> string Lwt . t val watch : handle -> string -> Xs_protocol . Token . t -> unit Lwt . t val unwatch : handle -> string -> Xs_protocol . Token . t -> unit Lwt . t val introduce : handle -> int -> nativeint -> int -> unit Lwt . t val set_target : handle -> int -> int -> unit Lwt . t end |
let ( |> ) a b = b a |
let ( ++ ) f g x = f ( g x ) |
module Watcher = struct type t = { mutable paths : StringSet . t ; mutable cancelling : bool ; c : unit Lwt_condition . t ; m : Lwt_mutex . t ; } let make ( ) = { paths = StringSet . empty ; cancelling = false ; c = Lwt_condition . create ( ) ; m = Lwt_mutex . create ( ) ; } let put ( x : t ) path = Lwt_mutex . with_lock x . m ( fun ( ) -> ) let get ( x : t ) = Lwt_mutex . with_lock x . m ( fun ( ) -> while_lwt x . paths = StringSet . empty && not x . cancelling do Lwt_condition . wait ~ mutex : x . m x . c done >> let results = x . paths in x . paths <- StringSet . empty ; ) let cancel ( x : t ) = let ( _ : unit Lwt . t ) = Lwt_mutex . with_lock x . m ( ) end |
module Client = functor ( IO : IO with type ' a t = ' a Lwt . t ) -> struct module PS = PacketStream ( IO ) type client = { mutable transport : IO . channel ; ps : PS . stream ; rid_to_wakeup : ( int32 , Xs_protocol . t Lwt . u ) Hashtbl . t ; mutable dispatcher_thread : unit Lwt . t ; mutable dispatcher_shutting_down : bool ; watchevents : ( Token . t , Watcher . t ) Hashtbl . t ; mutable suspended : bool ; suspended_m : Lwt_mutex . t ; suspended_c : unit Lwt_condition . t ; } let client_cache = ref None let client_cache_m = Lwt_mutex . create ( ) let recv_one t = match_lwt ( PS . recv t . ps ) with | Ok x -> return x | Exception e -> raise_lwt e let send_one t = PS . send t . ps let handle_exn t e = Printf . fprintf stderr " Caught : % s \ n " %! ( Printexc . to_string e ) ; lwt ( ) = begin match e with | Xs_protocol . Response_parser_failed x -> return ( ) | _ -> return ( ) end in t . dispatcher_shutting_down <- true ; lwt ( ) = Lwt_mutex . with_lock t . suspended_m ( fun ( ) -> Printf . fprintf stderr " Propagating exception to % d threads \ n " %! ( Hashtbl . length t . rid_to_wakeup ) ; Hashtbl . iter ( fun _ u -> Lwt . wakeup_later_exn u e ) t . rid_to_wakeup ; return ( ) ) in raise_lwt e let rec dispatcher t = lwt pkt = try_lwt recv_one t with e -> handle_exn t e in match get_ty pkt with | Op . Watchevent -> lwt ( ) = begin match Unmarshal . list pkt with | Some [ path ; token ] -> let token = Token . of_string token in if Hashtbl . mem t . watchevents token then Watcher . put ( Hashtbl . find t . watchevents token ) path >> dispatcher t else dispatcher t | _ -> handle_exn t Malformed_watch_event end in dispatcher t | _ -> let rid = get_rid pkt in lwt thread = Lwt_mutex . with_lock t . suspended_m ( fun ( ) -> if Hashtbl . mem t . rid_to_wakeup rid then return ( Some ( Hashtbl . find t . rid_to_wakeup rid ) ) else return None ) in match thread with | None -> handle_exn t ( Unexpected_rid rid ) | Some thread -> begin Lwt . wakeup_later thread pkt ; dispatcher t end let make_unsafe ( ) = lwt transport = IO . create ( ) in let t = { transport = transport ; ps = PS . make transport ; rid_to_wakeup = Hashtbl . create 10 ; dispatcher_thread = return ( ) ; dispatcher_shutting_down = false ; watchevents = Hashtbl . create 10 ; suspended = false ; suspended_m = Lwt_mutex . create ( ) ; suspended_c = Lwt_condition . create ( ) ; } in t . dispatcher_thread <- dispatcher t ; return t let make ( ) = match IO . backend with | ` unix -> make_unsafe ( ) | ` xen -> Lwt_mutex . with_lock client_cache_m ( fun ( ) -> match ! client_cache with | Some c -> return c | None -> lwt c = make_unsafe ( ) in client_cache := Some c ; return c ) let suspend t = lwt ( ) = Lwt_mutex . with_lock t . suspended_m ( fun ( ) -> t . suspended <- true ; while_lwt ( Hashtbl . length t . rid_to_wakeup > 0 ) do Lwt_condition . wait ~ mutex : t . suspended_m t . suspended_c done ) in Hashtbl . iter ( fun _ watcher -> Watcher . cancel watcher ) t . watchevents ; Lwt . cancel t . dispatcher_thread ; return ( ) let resume_unsafe t = lwt ( ) = Lwt_mutex . with_lock t . suspended_m ( fun ( ) -> t . suspended <- false ; t . dispatcher_shutting_down <- false ; Lwt_condition . broadcast t . suspended_c ( ) ; return ( ) ) in t . dispatcher_thread <- dispatcher t ; return ( ) let resume t = match IO . backend with | ` unix -> resume_unsafe t | ` xen -> ( match ! client_cache with | None -> Lwt . return ( ) | Some c -> IO . create ( ) >>= fun transport -> c . transport <- transport ; resume_unsafe t ) type handle = client Xs_handle . t let make_rid = let rpc hint h payload unmarshal = let open Xs_handle in let rid = make_rid ( ) in let request = Request . print payload ( get_tid h ) rid in let t , u = wait ( ) in let c = get_client h in if c . dispatcher_shutting_down then raise_lwt Dispatcher_failed else begin lwt ( ) = Lwt_mutex . with_lock c . suspended_m ( fun ( ) -> lwt ( ) = while_lwt c . suspended do Lwt_condition . wait ~ mutex : c . suspended_m c . suspended_c done in Hashtbl . add c . rid_to_wakeup rid u ; lwt ( ) = send_one c request in return ( ) ) in lwt res = t in lwt ( ) = Lwt_mutex . with_lock c . suspended_m ( fun ( ) -> Hashtbl . remove c . rid_to_wakeup rid ; Lwt_condition . broadcast c . suspended_c ( ) ; return ( ) ) in try_lwt return ( response hint request res unmarshal ) end let directory h path = rpc " directory " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Directory ) ) Unmarshal . list let read h path = rpc " read " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Read ) ) Unmarshal . string let write h path data = rpc " write " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Write data ) ) Unmarshal . ok let rm h path = rpc " rm " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Rm ) ) Unmarshal . ok let mkdir h path = rpc " mkdir " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Mkdir ) ) Unmarshal . ok let setperms h path acl = rpc " setperms " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Setperms acl ) ) Unmarshal . ok let debug h cmd_args = rpc " debug " h ( Request . Debug cmd_args ) Unmarshal . list let restrict h domid = rpc " restrict " h ( Request . Restrict domid ) Unmarshal . ok let getdomainpath h domid = rpc " getdomainpath " h ( Request . Getdomainpath domid ) Unmarshal . string let watch h path token = rpc " watch " ( Xs_handle . watch h path ) ( Request . Watch ( path , Token . to_string token ) ) Unmarshal . ok let unwatch h path token = rpc " unwatch " ( Xs_handle . watch h path ) ( Request . Unwatch ( path , Token . to_string token ) ) Unmarshal . ok let introduce h domid store_mfn store_port = rpc " introduce " h ( Request . Introduce ( domid , store_mfn , store_port ) ) Unmarshal . ok let set_target h stubdom_domid domid = rpc " set_target " h ( Request . Set_target ( stubdom_domid , domid ) ) Unmarshal . ok let immediate client f = f ( Xs_handle . no_transaction client ) let counter = ref 0l let wait client f = let open StringSet in counter := Int32 . succ ! counter ; let token = Token . of_string ( Printf . sprintf " % ld : xs_client . wait " ! counter ) in let watcher = Watcher . make ( ) in Hashtbl . add client . watchevents token watcher ; let result , wakener = Lwt . task ( ) in on_cancel result ( fun ( ) -> ) ; let h = Xs_handle . watching client in let adjust_paths ( ) = let current_paths = Xs_handle . get_watched_paths h in let old_paths = diff current_paths ( Xs_handle . get_accessed_paths h ) in lwt ( ) = Lwt_list . iter_s ( fun p -> unwatch h p token ) ( elements old_paths ) in let new_paths = diff ( Xs_handle . get_accessed_paths h ) current_paths in lwt ( ) = Lwt_list . iter_s ( fun p -> watch h p token ) ( elements new_paths ) in if old_paths = empty && ( new_paths = empty ) then begin lwt results = Watcher . get watcher in if results = empty then fail ( Failure " goodnight " ) else return ( ) end else return ( ) in let rec loop ( ) = lwt finished = try_lwt lwt result = f h in wakeup wakener result ; return true with Eagain -> return false in if finished then return ( ) else adjust_paths ( ) >> loop ( ) in let ( _ : unit Lwt . t ) = try_lwt loop ( ) finally let current_paths = Xs_handle . get_watched_paths h in lwt ( ) = Lwt_list . iter_s ( fun p -> unwatch h p token ) ( elements current_paths ) in Hashtbl . remove client . watchevents token ; return ( ) in result let rec transaction client f = lwt tid = rpc " transaction_start " ( Xs_handle . no_transaction client ) Request . Transaction_start Unmarshal . int32 in let h = Xs_handle . transaction client tid in lwt result = f h in try_lwt lwt res ' = rpc " transaction_end " h ( Request . Transaction_end true ) Unmarshal . string in if res ' = " OK " then return result else raise_lwt ( Error ( Printf . sprintf " Unexpected transaction result : % s " res ' ) ) with Eagain -> transaction client f end |
let finally f g = try let result = f ( ) in g ( ) ; result with e -> g ( ) ; raise e |
let with_mutex m f = Mutex . lock m ; finally f ( fun ( ) -> Mutex . unlock m ) |
let find_opt h x = if Hashtbl . mem h x then Some ( Hashtbl . find h x ) else None |
module type IO = sig type ' a t = ' a val return : ' a -> ' a t val ( >>= ) : ' a t -> ( ' a -> ' b t ) -> ' b t type channel val create : unit -> channel t val destroy : channel -> unit t val read : channel -> string -> int -> int -> int t val write : channel -> string -> int -> int -> unit t end |
let ( |> ) a b = b a |
let ( ++ ) f g x = f ( g x ) |
module Watcher = struct type t = { mutable paths : StringSet . t ; mutable cancelling : bool ; c : Condition . t ; m : Mutex . t ; } let make ( ) = { paths = StringSet . empty ; cancelling = false ; c = Condition . create ( ) ; m = Mutex . create ( ) ; } let put ( x : t ) path = with_mutex x . m ( fun ( ) -> x . paths <- StringSet . add path x . paths ; Condition . signal x . c ) let get ( x : t ) = with_mutex x . m ( fun ( ) -> while x . paths = StringSet . empty && not x . cancelling do Condition . wait x . c x . m done ; let results = x . paths in x . paths <- StringSet . empty ; results ) let cancel ( x : t ) = with_mutex x . m ( fun ( ) -> x . cancelling <- true ; Condition . signal x . c ) end |
module Task = struct type ' a u = { mutable thing : ' a option ; mutable cancelling : bool ; mutable on_cancel : unit -> unit ; m : Mutex . t ; c : Condition . t } let make ( ) = { thing = None ; cancelling = false ; on_cancel = ( fun ( ) -> ( ) ) ; m = Mutex . create ( ) ; c = Condition . create ( ) ; } let wakeup u thing = with_mutex u . m ( fun ( ) -> u . thing <- Some thing ; Condition . signal u . c ) let on_cancel u on_cancel = u . on_cancel <- on_cancel let cancel u = with_mutex u . m ( fun ( ) -> u . cancelling <- true ; Condition . signal u . c ) ; u . on_cancel ( ) let wait u = with_mutex u . m ( fun ( ) -> let rec loop ( ) = if u . cancelling then raise Cancelled else match u . thing with | None -> Condition . wait u . c u . m ; loop ( ) | Some thing -> thing in loop ( ) ) end |
type watch_callback = string * string -> unit |
let startswith prefix x = let prefix ' = String . length prefix and x ' = String . length x in x ' >= prefix ' && ( String . sub x 0 prefix ' ) = prefix |
module Client = functor ( IO : IO with type ' a t = ' a ) -> struct module PS = PacketStream ( IO ) let logger = ref ( fun s -> let _ : string = s in ( ) ) let error fmt = Printf . kprintf ! logger fmt let set_logger f = logger := f type client = { transport : IO . channel ; ps : PS . stream ; rid_to_wakeup : ( int32 , Xs_protocol . t Task . u ) Hashtbl . t ; mutable dispatcher_thread : Thread . t option ; mutable dispatcher_shutting_down : bool ; mutable watch_callback_thread : Thread . t option ; watchevents : ( string , Watcher . t ) Hashtbl . t ; incoming_watches : ( string * string ) Queue . t ; queue_overflowed : bool ref ; incoming_watches_m : Mutex . t ; incoming_watches_c : Condition . t ; mutable extra_watch_callback : ( ( string * string ) -> unit ) ; m : Mutex . t ; } type handle = client Xs_handle . t let recv_one t = match ( PS . recv t . ps ) with | Ok x -> x | Exception e -> raise e let send_one t = PS . send t . ps let handle_exn t e = error " Caught : % s \ n " %! ( Printexc . to_string e ) ; begin match e with | Xs_protocol . Response_parser_failed x -> ( ) | _ -> ( ) end ; t . dispatcher_shutting_down <- true ; raise e let enqueue_watch t event = with_mutex t . incoming_watches_m ( fun ( ) -> ) let rec dispatcher t = let pkt = try recv_one t with e -> handle_exn t e in match get_ty pkt with | Op . Watchevent -> begin match Unmarshal . list pkt with | Some [ path ; token ] -> let w = with_mutex t . m ( fun ( ) -> find_opt t . watchevents token ) in begin match w with | Some w -> Watcher . put w path | None -> if not ( startswith auto_watch_prefix token ) then enqueue_watch t ( path , token ) end ; | _ -> handle_exn t Malformed_watch_event end ; dispatcher t | _ -> let rid = get_rid pkt in let u = with_mutex t . m ( fun ( ) -> find_opt t . rid_to_wakeup rid ) in begin match u with | Some u -> Task . wakeup u pkt | None -> error " Unexpected rid : % ld \ n " %! rid end ; dispatcher t let dequeue_watches t = while true do try ( ) with done let make ( ) = let transport = IO . create ( ) in let t = { transport = transport ; ps = PS . make transport ; rid_to_wakeup = Hashtbl . create 10 ; dispatcher_thread = None ; dispatcher_shutting_down = false ; watch_callback_thread = None ; watchevents = Hashtbl . create 10 ; incoming_watches = Queue . create ( ) ; queue_overflowed = ref false ; incoming_watches_m = Mutex . create ( ) ; incoming_watches_c = Condition . create ( ) ; extra_watch_callback = ( fun _ -> ( ) ) ; m = Mutex . create ( ) ; } in t . dispatcher_thread <- Some ( Thread . create dispatcher t ) ; t . watch_callback_thread <- Some ( Thread . create dequeue_watches t ) ; t let set_watch_callback client cb = client . extra_watch_callback <- cb let make_rid = let rpc hint h payload unmarshal = let open Xs_handle in let rid = make_rid ( ) in let request = Request . print payload ( get_tid h ) rid in let t = Task . make ( ) in let c = get_client h in if c . dispatcher_shutting_down then raise Dispatcher_failed else begin with_mutex c . m ( fun ( ) -> Hashtbl . add c . rid_to_wakeup rid t ) ; send_one c request ; let res = Task . wait t in with_mutex c . m ( fun ( ) -> Hashtbl . remove c . rid_to_wakeup rid ) ; response hint request res unmarshal end let directory h path = rpc " directory " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Directory ) ) Unmarshal . list let read h path = rpc " read " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Read ) ) Unmarshal . string let write h path data = rpc " write " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Write data ) ) Unmarshal . ok let rm h path = rpc " rm " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Rm ) ) Unmarshal . ok let mkdir h path = rpc " mkdir " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Mkdir ) ) Unmarshal . ok let setperms h path acl = rpc " setperms " ( Xs_handle . accessed_path h path ) Request . ( PathOp ( path , Setperms acl ) ) Unmarshal . ok let debug h cmd_args = rpc " debug " h ( Request . Debug cmd_args ) Unmarshal . list let restrict h domid = rpc " restrict " h ( Request . Restrict domid ) Unmarshal . ok let getdomainpath h domid = rpc " getdomainpath " h ( Request . Getdomainpath domid ) Unmarshal . string let watch h path token = rpc " watch " ( Xs_handle . watch h path ) ( Request . Watch ( path , token ) ) Unmarshal . ok let unwatch h path token = rpc " unwatch " ( Xs_handle . watch h path ) ( Request . Unwatch ( path , token ) ) Unmarshal . ok let introduce h domid store_mfn store_port = rpc " introduce " h ( Request . Introduce ( domid , store_mfn , store_port ) ) Unmarshal . ok let set_target h stubdom_domid domid = rpc " set_target " h ( Request . Set_target ( stubdom_domid , domid ) ) Unmarshal . ok let immediate client f = f ( Xs_handle . no_transaction client ) let counter = ref 0l let wait client f = let open StringSet in counter := Int32 . succ ! counter ; let token = Printf . sprintf " % s % ld " auto_watch_prefix ! counter in let watcher = Watcher . make ( ) in with_mutex client . m ( fun ( ) -> Hashtbl . add client . watchevents token watcher ) ; let t = Task . make ( ) in Task . on_cancel t ( fun ( ) -> Watcher . cancel watcher ) ; let h = Xs_handle . watching client in let adjust_paths ( ) = let current_paths = Xs_handle . get_watched_paths h in let old_paths = diff current_paths ( Xs_handle . get_accessed_paths h ) in List . iter ( fun p -> unwatch h p token ) ( elements old_paths ) ; let new_paths = diff ( Xs_handle . get_accessed_paths h ) current_paths in List . iter ( fun p -> watch h p token ) ( elements new_paths ) ; if old_paths = empty && ( new_paths = empty ) then begin let results = Watcher . get watcher in if results = empty then raise ( Failure " goodnight " ) end in let rec loop ( ) = let finished = try let result = f h in Task . wakeup t result ; true with Eagain -> false in if not finished then begin adjust_paths ( ) ; loop ( ) end in let ( _ : Thread . t ) = Thread . create ( fun ( ) -> finally loop ( fun ( ) -> let current_paths = Xs_handle . get_watched_paths h in List . iter ( fun p -> unwatch h p token ) ( elements current_paths ) ; with_mutex client . m ( fun ( ) -> Hashtbl . remove client . watchevents token ) ; ) ) ( ) in t let rec transaction client f = let tid = rpc " transaction_start " ( Xs_handle . no_transaction client ) Request . Transaction_start Unmarshal . int32 in let h = Xs_handle . transaction client tid in let result = f h in try let res ' = rpc " transaction_end " h ( Request . Transaction_end true ) Unmarshal . string in if res ' = " OK " then result else raise ( Error ( Printf . sprintf " Unexpected transaction result : % s " res ' ) ) with Eagain -> transaction client f end |
module StringSet = Set . Make ( struct type t = string let compare = compare end ) |
type ' a t = { client : ' a ; tid : int32 ; mutable accessed_paths : StringSet . t option ; mutable watched_paths : StringSet . t ; } |
let make client = { client = client ; tid = 0l ; accessed_paths = None ; watched_paths = StringSet . empty } |
let get_tid h = h . tid |
let get_client h = h . client |
let no_transaction client = make client |
let transaction client tid = { ( make client ) with tid = tid } |
let watching client = { ( make client ) with accessed_paths = Some StringSet . empty } |
let accessed_path h path = match h . accessed_paths with | None -> h | Some ps -> h . accessed_paths <- Some ( StringSet . add path ps ) ; h |
let get_accessed_paths h = match h . accessed_paths with | None -> StringSet . empty | Some xs -> xs |
let watch h path = h . watched_paths <- StringSet . add path h . watched_paths ; h |
let unwatch h path = h . watched_paths <- StringSet . remove path h . watched_paths ; h |
let get_watched_paths h = h . watched_paths |
let ( |> ) f g = g f |
let ( ++ ) f g x = f ( g x ) |
module Op = struct type t = | Debug | Directory | Read | Getperms | Watch | Unwatch | Transaction_start | Transaction_end | Introduce | Release | Getdomainpath | Write | Mkdir | Rm | Setperms | Watchevent | Error | Isintroduced | Resume | Set_target | Restrict |
let on_the_wire = [ | Debug ; Directory ; Read ; Getperms ; Watch ; Unwatch ; Transaction_start ; Transaction_end ; Introduce ; Release ; Getdomainpath ; Write ; Mkdir ; Rm ; Setperms ; Watchevent ; Error ; Isintroduced ; Resume ; Set_target ; Restrict ] | |
let of_int32 i = let i = Int32 . to_int i in if i >= 0 && i < Array . length on_the_wire then Some ( on_the_wire . ( i ) ) else None |
let to_int32 x = match snd ( Array . fold_left ( fun ( idx , result ) v -> if x = v then ( idx + 1 , Some idx ) else ( idx + 1 , result ) ) ( 0 , None ) on_the_wire ) with | None -> assert false | Some i -> Int32 . of_int i |
let to_string = function | Debug -> " debug " | Directory -> " directory " | Read -> " read " | Getperms -> " getperms " | Watch -> " watch " | Unwatch -> " unwatch " | Transaction_start -> " transaction_start " | Transaction_end -> " transaction_end " | Introduce -> " introduce " | Release -> " release " | Getdomainpath -> " getdomainpath " | Write -> " write " | Mkdir -> " mkdir " | Rm -> " rm " | Setperms -> " setperms " | Watchevent -> " watchevent " | Error -> " error " | Isintroduced -> " isintroduced " | Resume -> " resume " | Set_target -> " set_target " | Restrict -> " restrict " end |
let rec split_string ? limit ( : limit ( =- 1 ) ) c s = let i = try String . index s c with Not_found -> - 1 in let nlimit = if limit = - 1 || limit = 0 then limit else limit - 1 in if i = - 1 || nlimit = 0 then [ s ] else let a = String . sub s 0 i and b = String . sub s ( i + 1 ) ( String . length s - i - 1 ) in a :: ( split_string ~ limit : nlimit c b ) |
module ACL = struct type perm = | NONE | READ | WRITE | RDWR let char_of_perm = function | READ -> ' r ' | WRITE -> ' w ' | RDWR -> ' b ' | NONE -> ' n ' let perm_of_char = function | ' r ' -> Some READ | ' w ' -> Some WRITE | ' b ' -> Some RDWR | ' n ' -> Some NONE | _ -> None type domid = int type t = { owner : domid ; other : perm ; acl : ( domid * perm ) list ; } let to_string perms = let string_of_perm ( id , perm ) = Printf . sprintf " % c % u " ( char_of_perm perm ) id in String . concat " \ 000 " ( List . map string_of_perm ( ( perms . owner , perms . other ) :: perms . acl ) ) let of_string s = let perm_of_char_exn x = match ( perm_of_char x ) with Some y -> y | None -> raise Not_found in try let perm_of_string s = if String . length s < 2 then invalid_arg ( Printf . sprintf " Permission string too short : ' % s ' " s ) ; int_of_string ( String . sub s 1 ( String . length s - 1 ) ) , perm_of_char_exn s . [ 0 ] in let l = List . map perm_of_string ( split_string ' \ 000 ' s ) in match l with | ( owner , other ) :: l -> Some { owner = owner ; other = other ; acl = l } | [ ] -> Some { owner = 0 ; other = NONE ; acl = [ ] } with e -> None end |
type t = { tid : int32 ; rid : int32 ; ty : Op . t ; len : int ; data : Buffer . t ; } uint32_t ty ; uint32_t rid ; uint32_t tid ; uint32_t len |
let to_string pkt = let header = Cstruct . create sizeof_header in let len = Int32 . of_int ( Buffer . length pkt . data ) in let ty = Op . to_int32 pkt . ty in set_header_ty header ty ; set_header_rid header pkt . rid ; set_header_tid header pkt . tid ; set_header_len header len ; Cstruct . to_string header ^ ( Buffer . contents pkt . data ) |
let get_tid pkt = pkt . tid |
let get_ty pkt = pkt . ty |
let get_data pkt = if pkt . len > 0 && Buffer . nth pkt . data ( pkt . len - 1 ) = ' \ 000 ' then Buffer . sub pkt . data 0 ( pkt . len - 1 ) else Buffer . contents pkt . data |
let get_rid pkt = pkt . rid |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.