Debian Codegraph @

examples/vol-upload.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 INPUT-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 open $f: $!";
21
22 eval {
23 $vol->upload($st, 0, 0);
24 while (1) {
25 my $nbytes = 1024;
26 my $data;
27 my $rv = sysread FILE, $data, $nbytes;
28 if ($rv < 0) {
29 die "cannot read $f: $!";
30 }
31 last if $rv == 0;
32 while ($rv > 0) {
33 my $done = $st->send($data, $rv);
34 if ($done) {
35 $data = substr $data, $done;
36 $rv -= $done;
37 }
38 }
39 }
40
41 $st->finish();
42 };
43 if ($@) {
44 close FILE;
45 die $@;
46 }
47 close FILE or die "cannot save $f: $!";
48