Debian Codegraph @

examples/vol-download.pl

raw
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Sys::Virt;
7
8
9 my $c = Sys::Virt->new(uri => "qemu:///session");
10
11 my $st = $c->new_stream();
12
13 die "syntax: $0 VOL-PATH OUTPUT-FILE" unless int(@ARGV) == 2;
14
15 my $volpath = shift @ARGV;
16 my $f = shift @ARGV;
17
18 my $vol = $c->get_storage_volume_by_path($volpath);
19
20 open FILE, ">$f" or die "cannot create $f: $!";
21
22 eval {
23 $vol->download($st, 0, 0);
24 while (1) {
25 my $nbytes = 1024;
26 my $data;
27 my $rv = $st->recv($data, $nbytes);
28 last if $rv == 0;
29 while ($rv > 0) {
30 my $done = syswrite FILE, $data, $rv;
31 if ($done) {
32 $data = substr $data, $done;
33 $rv -= $done;
34 }
35 }
36 }
37
38 $st->finish();
39 };
40 if ($@) {
41 unlink $f if $@;
42 close FILE;
43 die $@;
44 }
45 close FILE or die "cannot save $f: $!";
46