mirror of
https://git.proxmox.com/git/pve-guest-common
synced 2025-08-15 10:32:16 +00:00
abstractconfig: add pending changes related helpers
use a better naming scheme for methods from qemuserver: split_flagged_list -> parse_pending_delete join_flagged_list -> print_pending_delete vmconfig_delete_pending_option -> add_to_pending_delete vmconfig_undelete_pending_option -> remove_from_pending_delete vmconfig_cleanup_pending -> cleanup_pending parse_pending_delete now has a better representation of the force value, which is encoded in a perl hash i.e. $conf->{$opt}->{force} = 1 or 0 depending on if the string in config has '!' or not. Signed-off-by: Oguz Bektas <o.bektas@proxmox.com>
This commit is contained in:
parent
1aa76f2f0b
commit
810ee08809
@ -68,6 +68,75 @@ sub write_config {
|
|||||||
PVE::Cluster::cfs_write_file($cfspath, $conf);
|
PVE::Cluster::cfs_write_file($cfspath, $conf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Pending changes related
|
||||||
|
|
||||||
|
sub parse_pending_delete {
|
||||||
|
my ($class, $data) = @_;
|
||||||
|
$data ||= '';
|
||||||
|
$data =~ s/[,;]/ /g;
|
||||||
|
$data =~ s/^\s+//;
|
||||||
|
return { map { /^(!?)(.*)$/ && ($2, { 'force' => $1 eq '!' ? 1 : 0 } ) } ($data =~ /\S+/g) };
|
||||||
|
}
|
||||||
|
|
||||||
|
sub print_pending_delete {
|
||||||
|
my ($class, $delete_hash) = @_;
|
||||||
|
join ",", map { ( $delete_hash->{$_}->{force} ? '!' : '' ) . $_ } keys %$delete_hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub add_to_pending_delete {
|
||||||
|
my ($class, $conf, $key, $force) = @_;
|
||||||
|
|
||||||
|
delete $conf->{pending}->{$key};
|
||||||
|
my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
|
||||||
|
$pending_delete_hash->{$key}->{force} = $force;
|
||||||
|
$conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub remove_from_pending_delete {
|
||||||
|
my ($class, $conf, $key) = @_;
|
||||||
|
|
||||||
|
my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
|
||||||
|
delete $pending_delete_hash->{$key};
|
||||||
|
|
||||||
|
if (%$pending_delete_hash) {
|
||||||
|
$conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
|
||||||
|
} else {
|
||||||
|
delete $conf->{pending}->{delete};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sub cleanup_pending {
|
||||||
|
my ($class, $conf) = @_;
|
||||||
|
|
||||||
|
# remove pending changes when nothing changed
|
||||||
|
my $changes;
|
||||||
|
foreach my $opt (keys %{$conf->{pending}}) {
|
||||||
|
next if $opt eq 'delete'; # just to be sure
|
||||||
|
if (defined($conf->{$opt}) && ($conf->{pending}->{$opt} eq $conf->{$opt})) {
|
||||||
|
$changes = 1;
|
||||||
|
delete $conf->{pending}->{$opt};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
my $current_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
|
||||||
|
my $pending_delete_hash = {};
|
||||||
|
while (my ($opt, $force) = each %$current_delete_hash) {
|
||||||
|
if (defined($conf->{$opt})) {
|
||||||
|
$pending_delete_hash->{$opt} = $force;
|
||||||
|
} else {
|
||||||
|
$changes = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (%$pending_delete_hash) {
|
||||||
|
$conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
|
||||||
|
} else {
|
||||||
|
delete $conf->{pending}->{delete};
|
||||||
|
}
|
||||||
|
|
||||||
|
return $changes;
|
||||||
|
}
|
||||||
|
|
||||||
# Lock config file using flock, run $code with @param, unlock config file.
|
# Lock config file using flock, run $code with @param, unlock config file.
|
||||||
# $timeout is the maximum time to aquire the flock
|
# $timeout is the maximum time to aquire the flock
|
||||||
sub lock_config_full {
|
sub lock_config_full {
|
||||||
|
Loading…
Reference in New Issue
Block a user