0018172754c17a599ddc8bafe22f3b0e3cfb188f
[libguestfs.git] / resize / progress.ml
1 (* virt-resize
2  * Copyright (C) 2010-2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open Printf
20
21 open Utils
22
23 module G = Guestfs
24
25 type progress_bar
26 external progress_bar_init : unit -> progress_bar
27   = "virt_resize_progress_bar_init"
28 external progress_bar_reset : progress_bar -> unit
29   = "virt_resize_progress_bar_reset"
30 external progress_bar_set : progress_bar -> int64 -> int64 -> unit
31   = "virt_resize_progress_bar_set"
32
33 (* Initialize the C mini library. *)
34 let bar = progress_bar_init ()
35
36 (* Reset the progress bar before every libguestfs function. *)
37 let enter_callback g event evh buf array =
38   if event = G.EVENT_ENTER then
39     progress_bar_reset bar
40
41 (* A progress event: move the progress bar. *)
42 let progress_callback g event evh buf array =
43   if event = G.EVENT_PROGRESS && Array.length array >= 4 then (
44     let position = array.(2)
45     and total = array.(3) in
46
47     progress_bar_set bar position total
48   )
49
50 let set_up_progress_bar (g : Guestfs.guestfs) =
51   ignore (g#set_event_callback enter_callback [G.EVENT_ENTER]);
52   ignore (g#set_event_callback progress_callback [G.EVENT_PROGRESS])