New tool: virt-sparsify to make disk images sparse.
[libguestfs.git] / sparsify / progress.ml
1 (* virt-sparsify
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 : machine_readable:bool -> progress_bar
27   = "virt_sparsify_progress_bar_init"
28 external progress_bar_reset : progress_bar -> unit
29   = "virt_sparsify_progress_bar_reset"
30 external progress_bar_set : progress_bar -> int64 -> int64 -> unit
31   = "virt_sparsify_progress_bar_set"
32
33 let set_up_progress_bar ?(machine_readable = false) (g : Guestfs.guestfs) =
34   (* Initialize the C mini library. *)
35   let bar = progress_bar_init ~machine_readable in
36
37   (* Reset the progress bar before every libguestfs function. *)
38   let enter_callback g event evh buf array =
39     if event = G.EVENT_ENTER then
40       progress_bar_reset bar
41   in
42
43   (* A progress event: move the progress bar. *)
44   let progress_callback g event evh buf array =
45     if event = G.EVENT_PROGRESS && Array.length array >= 4 then (
46       let position = array.(2)
47       and total = array.(3) in
48
49       progress_bar_set bar position total
50     )
51   in
52
53   ignore (g#set_event_callback enter_callback [G.EVENT_ENTER]);
54   ignore (g#set_event_callback progress_callback [G.EVENT_PROGRESS])