correctly handle empty description in pending section

So that we can delete descriptions with

 qm set <vmid> --descr ''
This commit is contained in:
Dietmar Maurer 2015-08-11 11:24:41 +02:00
parent ef00f33b11
commit b0ec896e43

View File

@ -1876,7 +1876,7 @@ sub parse_vm_config {
my $vmid = $1; my $vmid = $1;
my $conf = $res; my $conf = $res;
my $descr = ''; my $descr;
my $section = ''; my $section = '';
my @lines = split(/\n/, $raw); my @lines = split(/\n/, $raw);
@ -1885,25 +1885,33 @@ sub parse_vm_config {
if ($line =~ m/^\[PENDING\]\s*$/i) { if ($line =~ m/^\[PENDING\]\s*$/i) {
$section = 'pending'; $section = 'pending';
$conf->{description} = $descr if $descr; if (defined($descr)) {
$descr = ''; $descr =~ s/\s+$//;
$conf->{description} = $descr;
}
$descr = undef;
$conf = $res->{$section} = {}; $conf = $res->{$section} = {};
next; next;
} elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) { } elsif ($line =~ m/^\[([a-z][a-z0-9_\-]+)\]\s*$/i) {
$section = $1; $section = $1;
$conf->{description} = $descr if $descr; if (defined($descr)) {
$descr = ''; $descr =~ s/\s+$//;
$conf->{description} = $descr;
}
$descr = undef;
$conf = $res->{snapshots}->{$section} = {}; $conf = $res->{snapshots}->{$section} = {};
next; next;
} }
if ($line =~ m/^\#(.*)\s*$/) { if ($line =~ m/^\#(.*)\s*$/) {
$descr = '' if !defined($descr);
$descr .= PVE::Tools::decode_text($1) . "\n"; $descr .= PVE::Tools::decode_text($1) . "\n";
next; next;
} }
if ($line =~ m/^(description):\s*(.*\S)\s*$/) { if ($line =~ m/^(description):\s*(.*\S)\s*$/) {
$descr = '' if !defined($descr);
$descr .= PVE::Tools::decode_text($2); $descr .= PVE::Tools::decode_text($2);
} elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) { } elsif ($line =~ m/snapstate:\s*(prepare|delete)\s*$/) {
$conf->{snapstate} = $1; $conf->{snapstate} = $1;
@ -1946,8 +1954,10 @@ sub parse_vm_config {
} }
} }
$conf->{description} = $descr if $descr; if (defined($descr)) {
$descr =~ s/\s+$//;
$conf->{description} = $descr;
}
delete $res->{snapstate}; # just to be sure delete $res->{snapstate}; # just to be sure
return $res; return $res;
@ -2018,15 +2028,20 @@ sub write_vm_config {
} }
my $generate_raw_config = sub { my $generate_raw_config = sub {
my ($conf) = @_; my ($conf, $pending) = @_;
my $raw = ''; my $raw = '';
# add description as comment to top of file # add description as comment to top of file
my $descr = $conf->{description} || ''; if (defined(my $descr = $conf->{description})) {
if ($descr) {
foreach my $cl (split(/\n/, $descr)) { foreach my $cl (split(/\n/, $descr)) {
$raw .= '#' . PVE::Tools::encode_text($cl) . "\n"; $raw .= '#' . PVE::Tools::encode_text($cl) . "\n";
} }
} else {
$raw .= "#\n" if $pending;
}
}
foreach my $key (sort keys %$conf) { foreach my $key (sort keys %$conf) {
next if $key eq 'digest' || $key eq 'description' || $key eq 'pending' || $key eq 'snapshots'; next if $key eq 'digest' || $key eq 'description' || $key eq 'pending' || $key eq 'snapshots';
@ -2039,7 +2054,7 @@ sub write_vm_config {
if (scalar(keys %{$conf->{pending}})){ if (scalar(keys %{$conf->{pending}})){
$raw .= "\n[PENDING]\n"; $raw .= "\n[PENDING]\n";
$raw .= &$generate_raw_config($conf->{pending}); $raw .= &$generate_raw_config($conf->{pending}, 1);
} }
foreach my $snapname (sort keys %{$conf->{snapshots}}) { foreach my $snapname (sort keys %{$conf->{snapshots}}) {
@ -3856,6 +3871,7 @@ my $fast_plug_option = {
'onboot' => 1, 'onboot' => 1,
'shares' => 1, 'shares' => 1,
'startup' => 1, 'startup' => 1,
'description' => 1,
}; };
# hotplug changes in [PENDING] # hotplug changes in [PENDING]