Matthew Booth
2010-Apr-07 11:05 UTC
[Libguestfs] [PATCH] Target::LibVirt: Don't truncate a volume when opening it
perl's '>' open mode truncates by default, and will create a new file if necessary. We don't want to truncate an existing volume when writing to it, and it's an error if the volume doesn't already exist. Use sysopen to be explicit about this. --- lib/Sys/VirtV2V/Target/LibVirt.pm | 6 +++++- 1 files changed, 5 insertions(+), 1 deletions(-) diff --git a/lib/Sys/VirtV2V/Target/LibVirt.pm b/lib/Sys/VirtV2V/Target/LibVirt.pm index c8802ac..99a1ad2 100644 --- a/lib/Sys/VirtV2V/Target/LibVirt.pm +++ b/lib/Sys/VirtV2V/Target/LibVirt.pm @@ -20,6 +20,8 @@ use warnings; package Sys::VirtV2V::Target::LibVirt::Vol; +use POSIX; + use Sys::VirtV2V::UserMessage qw(user_message); sub _new @@ -106,7 +108,9 @@ sub open my $self = shift; my $path = $self->get_path(); - open(my $fd, '>', $path) + + # We want to open the existing volume without truncating it + sysopen(my $fd, $path, O_WRONLY) or die(user_message(__x("Error opening storage volume {path} ". "for writing: {error}", error => $!))); -- 1.6.6.1
Daniel P. Berrange
2010-Apr-07 11:07 UTC
[Libguestfs] [PATCH] Target::LibVirt: Don't truncate a volume when opening it
On Wed, Apr 07, 2010 at 12:05:37PM +0100, Matthew Booth wrote:> perl's '>' open mode truncates by default, and will create a new file if > necessary. We don't want to truncate an existing volume when writing to it, and > it's an error if the volume doesn't already exist. Use sysopen to be explicit > about this. > --- > lib/Sys/VirtV2V/Target/LibVirt.pm | 6 +++++- > 1 files changed, 5 insertions(+), 1 deletions(-) > > diff --git a/lib/Sys/VirtV2V/Target/LibVirt.pm b/lib/Sys/VirtV2V/Target/LibVirt.pm > index c8802ac..99a1ad2 100644 > --- a/lib/Sys/VirtV2V/Target/LibVirt.pm > +++ b/lib/Sys/VirtV2V/Target/LibVirt.pm > @@ -20,6 +20,8 @@ use warnings; > > package Sys::VirtV2V::Target::LibVirt::Vol; > > +use POSIX; > + > use Sys::VirtV2V::UserMessage qw(user_message); > > sub _new > @@ -106,7 +108,9 @@ sub open > my $self = shift; > > my $path = $self->get_path(); > - open(my $fd, '>', $path) > + > + # We want to open the existing volume without truncating it > + sysopen(my $fd, $path, O_WRONLY)NB There's no need to switch to sysopen. You can use '>>' instead of '>' if you want to append instead of truncate Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://deltacloud.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
Richard W.M. Jones
2010-Apr-08 09:39 UTC
[Libguestfs] [PATCH] Target::LibVirt: Don't truncate a volume when opening it
On Wed, Apr 07, 2010 at 12:05:37PM +0100, Matthew Booth wrote:> perl's '>' open mode truncates by default, and will create a new file if > necessary. We don't want to truncate an existing volume when writing to it, and > it's an error if the volume doesn't already exist. Use sysopen to be explicit > about this.Yes, looks fine. ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://et.redhat.com/~rjones/libguestfs/ See what it can do: http://et.redhat.com/~rjones/libguestfs/recipes.html
Maybe Matching Threads
- [PATCH 1/2] Refactor guest and volume creation into Sys::VirtV2V::Target::LibVirt
- [PATCH] Add LocalCopy transfer method to transfer local files to a target
- [PATCH] Remove v2v-snapshot
- Create new Sys::VirtV2V::Util
- [PATCH 1/6] Convert config file to XML, and translate networks/bridge for all connections