(* mclu: Mini Cloud * Copyright (C) 2014-2015 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *) (* Miscellaneous utility functions. *) open Scanf open Printf module D = Libvirt.Domain let (//) = Filename.concat let quote = Filename.quote let ( +^ ) = Int64.add let ( -^ ) = Int64.sub let ( *^ ) = Int64.mul let ( /^ ) = Int64.div let ( &^ ) = Int64.logand let ( ~^ ) = Int64.lognot let human_size i = let sign, i = if i < 0L then "-", Int64.neg i else "", i in if i < 1024L then sprintf "%s%Ld" sign i else ( let f = Int64.to_float i /. 1024. in let i = i /^ 1024L in if i < 1024L then sprintf "%s%.1fK" sign f else ( let f = Int64.to_float i /. 1024. in let i = i /^ 1024L in if i < 1024L then sprintf "%s%.1fM" sign f else ( let f = Int64.to_float i /. 1024. in (*let i = i /^ 1024L in*) sprintf "%s%.1fG" sign f ) ) ) let bytes_of_human_size s = try sscanf s "%Ld%[Gg]" (fun b _ -> b *^ 1024L *^ 1024L *^ 1024L) with Scan_failure _ -> try sscanf s "%Ld%[Mm]" (fun b _ -> b *^ 1024L *^ 1024L) with Scan_failure _ -> try sscanf s "%Ld%[Kk]" (fun b _ -> b *^ 1024L) with Scan_failure _ -> try sscanf s "%Ld%[Bb]" (fun b _ -> b) with Scan_failure _ -> raise Not_found let string_of_dom_state = function | D.InfoNoState -> "unknown" | D.InfoRunning -> "running" | D.InfoBlocked -> "blocked" | D.InfoPaused -> "paused" | D.InfoShutdown -> "shutdown" | D.InfoShutoff -> "shutoff" | D.InfoCrashed -> "crashed" let regexp_of_glob s = let len = String.length s in let buf = Buffer.create len in Buffer.add_char buf '^'; for i = 0 to len-1 do match String.unsafe_get s i with (* Wildcard characters converted to regular expressions. *) | '?' -> Buffer.add_char buf '.' | '*' -> Buffer.add_string buf ".*" (* Must escape any character which is special for PCRE - see * pcrepattern(3). However ignore [..] because they are * (approximately) the same for globs and regexps. *) | ('\\' | '^' | '$' | '.' | '|' | '(' | ')' | '+' | '{') as c -> Buffer.add_char buf '\\'; Buffer.add_char buf c | c -> Buffer.add_char buf c done; Buffer.add_char buf '$'; Buffer.contents buf let string_random8 = let chars = "abcdefghijklmnopqrstuvwxyz0123456789" in fun () -> String.concat "" ( List.map ( fun _ -> let c = Random.int 36 in let c = chars.[c] in String.make 1 c ) [1;2;3;4;5;6;7;8] ) let name_parse name = let i = try Some (String.index name ':') with Not_found -> None in match i with | None -> None, name | Some i -> Some (String.sub name 0 i), String.sub name (i+1) (String.length name - i - 1)