scstadmin: Multiple -write_config bug fixes.

Bug fixes:
- Do not write out the line "# Non-key attributes" for the top level if -nonkey
  has not been specified.
- If -nonkey has been specified, write out the device handler creation time
  attributes too (blocksize, o_direct, read_only, removable, ...).
- If -nonkey has been specified, write out the LUN creation time attributes
  too (read_only).

Also, simplify the -write_config implementation by introducing the Perl
subroutines serializeKeyAttr() and serializeNkAttr().


git-svn-id: http://svn.code.sf.net/p/scst/svn/trunk@3606 d57e44dd-8a1f-0410-8b47-8ef2f437770f
This commit is contained in:
Bart Van Assche
2011-06-22 19:18:39 +00:00
parent 6a6dd2bf08
commit 0c0cf3215b

View File

@@ -6,6 +6,7 @@ $Version = 'SCST Configurator v2.0.0';
# Author: Mark R. Buechler
# License: GPLv2
# Copyright (c) 2005-2010 Mark R. Buechler
# Copyright (C) 2011 Bart Van Assche <bvanassche@acm.org>
sub usage
{
@@ -992,6 +993,75 @@ sub readWorkingConfig {
return FALSE;
}
# Serialize key attributes.
# $prefix: output prefix, e.g. "\t\t".
# $attributes: reference to a hash with attributes and their values.
# $attr_filter: if specified, reference to a hash with the names of which
# (static) attributes to serialize.
sub serializeKeyAttr {
my $prefix = shift;
my $attributes = shift;
my $attr_filter = shift;
my $result;
foreach my $attribute (sort keys %{$attributes}) {
next if defined($$attributes{$attribute}->{'set'});
next if ($$attributes{$attribute}->{'static'} &&
!(defined($attr_filter)
&& defined($$attr_filter{$attribute})));
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (sort keys %{$$attributes{$attribute}->{'keys'}}) {
my $value =
$$attributes{$attribute}->{'keys'}->{$key}->{'value'};
if ($value ne '') {
if ($value =~ / /) {
$value = "\"$value\"";
}
$result .= "$prefix$attribute $value\n";
}
}
} elsif ($attribute eq 'enabled' || $attribute eq 'hw_target') {
my $value = $$attributes{$attribute}->{'value'};
$result .= "$prefix$attribute $value\n";
}
}
return $result;
}
# Serialize non-key attributes.
# $prefix: output prefix, e.g. "\t\t".
# $attributes: reference to a hash with attributes and their values.
# $attr_filter: if specified, reference to a hash with the names of which
# (static) attributes to serialize.
sub serializeNkAttr {
my $prefix = shift;
my $attributes = shift;
my $attr_filter = shift;
my $result;
foreach my $attribute (sort keys %{$attributes}) {
if (!defined($$attributes{$attribute}->{'set'})
&& (!$$attributes{$attribute}->{'static'}
|| defined($attr_filter)
&& defined($$attr_filter{$attribute}))
&& !defined($$attributes{$attribute}->{'keys'})
&& $attribute ne 'enabled'
&& $attribute ne 'hw_target') {
my $value = $$attributes{$attribute}->{'value'};
if ($value ne '') {
if ($value =~ / /) {
$value = "\"$value\"";
}
$result .= "$prefix$attribute $value\n";
}
}
}
return $result;
}
# Returns 0 upon success and 1 upon error.
sub writeConfiguration {
my $nonkey = shift;
@@ -1015,69 +1085,30 @@ sub writeConfiguration {
print $io "# Automatically generated by $Version.\n\n";
my $attributes = $SCST->scstAttributes();
immediateExit($SCST->errorString());
{
my $attributes = $SCST->scstAttributes();
immediateExit($SCST->errorString());
my %nattrs;
foreach my $attribute (sort keys %{$attributes}) {
next if (defined($$attributes{$attribute}->{'set'}));
if (!$$attributes{$attribute}->{'static'}) {
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (%{$$attributes{$attribute}->{'keys'}}) {
my $value = $$attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
print $io "$attribute $value\n"
if (defined($value) && ($value ne ''));
}
} elsif ($nonkey) {
my $value = $$attributes{$attribute}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$nattrs{$attribute} = $value
if (defined($value) && ($value ne ''));
}
print $io serializeKeyAttr("", $attributes);
if ($nonkey) {
my $nk = serializeNkAttr("", $attributes);
if ($nk) {
print $io "# Non-key attributes\n";
print $io $nk;
}
}
print $io "\n";
}
if ($nonkey && (scalar keys %nattrs)) {
print $io "# Non-key attributes\n";
foreach my $attr (keys %nattrs) {
my $value = $nattrs{$attr};
print $io "$attr $value\n";
}
}
print $io "\n";
foreach my $handler (sort keys %{$CURRENT{'handler'}}) {
my $handler_buff;
my $handler_buff_nk;
my $handler_attrs = $SCST->deviceCreateAttributes($handler);
$attributes = $SCST->handlerAttributes($handler);
my $attributes = $SCST->handlerAttributes($handler);
foreach my $attribute (sort keys %{$attributes}) {
next if (defined($$attributes{$attribute}->{'set'}));
if (!$$attributes{$attribute}->{'static'}) {
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (@{$$attributes{$attribute}->{'keys'}}) {
my $value = $$attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$handler_buff .= "\t$attribute $value\n" if (defined($value));
}
} elsif (($attribute eq 'enabled') || $nonkey) {
my $value = $$attributes{$attribute}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
if ($attribute eq 'enabled') {
$handler_buff .= "\t$attribute $value\n" if (defined($value));
} else {
$handler_buff_nk .= "\t$attribute $value\n" if (defined($value));
}
}
}
}
$handler_buff = serializeKeyAttr("\t", $attributes);
$handler_buff_nk = serializeNkAttr("\t", $attributes) if ($nonkey);
my $devices = $CURRENT{'handler'}->{$handler};
@@ -1085,64 +1116,16 @@ sub writeConfiguration {
foreach my $device (sort @{$devices}) {
$device_buff .= "\tDEVICE $device";
$attributes = $SCST->deviceAttributes($device);
my $attributes = $SCST->deviceAttributes($device);
my $attribute_buff;
my $attribute_buff_nk;
foreach my $attribute (sort keys %{$attributes}) {
next if (defined($$attributes{$attribute}->{'set'}));
if (!defined($$handler_attrs{$attribute})) {
if (!$$attributes{$attribute}->{'static'}) {
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (keys %{$$attributes{$attribute}->{'keys'}}) {
my $value = $$attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$attribute_buff .= "\t\t$attribute $value\n"
if (defined($value));
}
} elsif (($attribute eq 'enabled') || $nonkey) {
my $value = $$attributes{$attribute}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
if ($attribute eq 'enabled') {
$attribute_buff .= "\t\t$attribute $value\n"
if (defined($value));
} else {
$attribute_buff_nk .= "\t\t$attribute $value\n"
if (defined($value));
}
}
}
}
}
$attribute_buff = serializeKeyAttr("\t\t", $attributes, $handler_attrs);
$attribute_buff_nk = serializeNkAttr("\t\t", $attributes, $handler_attrs) if ($nonkey);
$attribute_buff .= "\n" if ($attribute_buff);
$attribute_buff_nk .= "\n" if ($attribute_buff_nk);
if (!defined($$attributes{'scsi_device'})) {
my $create_buff;
foreach my $attribute (sort keys %{$attributes}) {
next if (defined($$attributes{$attribute}->{'set'}));
if (defined($$handler_attrs{$attribute})) {
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (keys %{$$attributes{$attribute}->{'keys'}}) {
my $value = $$attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$create_buff .= "\t\t$attribute $value\n"
if (defined($value));
}
# Shouldn't be any non-key create attributes
}
}
}
if ($create_buff) {
$attribute_buff .= $create_buff;
}
}
if ($attribute_buff_nk) {
$attribute_buff .= "\n" if ($attribute_buff);
$attribute_buff .= "\t\t# Non-key attributes\n";
@@ -1183,30 +1166,9 @@ sub writeConfiguration {
my $drv_attr_buff;
my $drv_attr_buff_nk;
foreach my $attr (keys %{$drv_attrs}) {
next if ($$drv_attrs{$attr}->{'static'});
next if ($$drv_attrs{$attr}->{'set'});
if (defined($$drv_attrs{$attr}->{'keys'})) {
foreach my $key (keys %{$$drv_attrs{$attr}->{'keys'}}) {
my $value = $$drv_attrs{$attr}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$drv_attr_buff .= "\t$attr $value\n"
if (defined($value));
}
} elsif (($attr eq 'enabled') || $nonkey) {
my $value = $$drv_attrs{$attr}->{'value'};
$value = "\"$value\"" if ($value =~ /\s/);
if ($attr eq 'enabled') {
$drv_attr_buff .= "\t$attr $value\n"
if (defined($value));
} else {
$drv_attr_buff_nk .= "\t$attr $value\n"
if (defined($value));
}
}
}
$drv_attr_buff = serializeKeyAttr("\t", $drv_attrs);
$drv_attr_buff_nk = serializeNkAttr("\t", $drv_attrs) if ($nonkey);
$drv_attr_buff .= "\n" if ($drv_attr_buff);
$drv_attr_buff_nk .= "\n" if ($drv_attr_buff_nk);
@@ -1227,31 +1189,8 @@ sub writeConfiguration {
$attribute_buff = "\t\tHW_TARGET\n\n";
}
foreach my $attr (keys %{$attributes}) {
next if ($$attributes{$attr}->{'static'} &&
!defined($$tgt_attrs{$attr}));
next if ($$attributes{$attr}->{'set'});
if (defined($$attributes{$attr}->{'keys'})) {
foreach my $key (keys %{$$attributes{$attr}->{'keys'}}) {
my $value = $$attributes{$attr}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$attribute_buff .= "\t\t$attr $value\n"
if (defined($value));
}
} elsif (($attr eq 'hw_target') || ($attr eq 'enabled') || $nonkey) {
my $value = $$attributes{$attr}->{'value'};
$value = "\"$value\"" if ($value =~ /\s/);
if ($attr eq 'enabled') {
$attribute_buff .= "\t\t$attr $value\n"
if (defined($value));
} else {
$attribute_buff_nk .= "\t\t$attr $value\n"
if (defined($value));
}
}
}
$attribute_buff .= serializeKeyAttr("\t\t", $attributes);
$attribute_buff_nk .= serializeNkAttr("\t\t", $attributes) if ($nonkey);
$attribute_buff .= "\n" if ($attribute_buff);
$attribute_buff_nk .= "\n" if ($attribute_buff_nk);
@@ -1264,38 +1203,15 @@ sub writeConfiguration {
$t_lun_buff .= "\t\tLUN $lun $lun_dev";
$attributes = $SCST->lunAttributes($driver, $target, $lun);
my $l_attribute_buff;
my $l_attribute_buff_nk;
foreach my $attribute (sort keys %{$attributes}) {
next if (defined($$attributes{$attribute}->{'set'}));
if (!$$attributes{$attribute}->{'static'} ||
defined($$lun_attrs{$attribute})) {
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (keys %{$$attributes{$attribute}->{'keys'}}) {
my $value = $$attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$l_attribute_buff .= "\t\t\t$attribute $value\n"
if (defined($value));
}
} elsif (($attribute eq 'enabled') || $nonkey) {
my $value = $$attributes{$attribute}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
if (defined($$lun_attrs{$attribute})) {
$l_attribute_buff .= "\t\t\t$attribute $value\n"
if (defined($value));
} elsif ($attribute eq 'enabled') {
$l_attribute_buff .= "\t\t\t$attribute $value\n"
if (defined($value));
} else {
$l_attribute_buff_nk .= "\t\t\t$attribute $value\n"
if (defined($value));
}
}
}
}
my $attributes = $SCST->lunAttributes($driver, $target, $lun);
my $l_attribute_buff =
serializeKeyAttr("\t\t\t\t",
$attributes,
$lun_attrs);
my $l_attribute_buff_nk =
serializeNkAttr("\t\t\t\t",
$attributes,
$lun_attrs) if ($nonkey);
if ($l_attribute_buff_nk) {
$l_attribute_buff .= "\t\t\t\t# Non-key attributes\n";
@@ -1331,38 +1247,17 @@ sub writeConfiguration {
$lun_buff .= "\t\t\tLUN $lun $lun_dev";
$attributes = $SCST->lunAttributes($driver, $target, $lun, $group);
my $attributes = $SCST->lunAttributes($driver, $target, $lun, $group);
my $l_attribute_buff;
my $l_attribute_buff_nk;
foreach my $attribute (sort keys %{$attributes}) {
next if (defined($$attributes{$attribute}->{'set'}));
if (!$$attributes{$attribute}->{'static'} ||
defined($$lun_attrs{$attribute})) {
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (keys %{$$attributes{$attribute}->{'keys'}}) {
my $value = $$attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$l_attribute_buff .= "\t\t\t\t$attribute $value\n"
if (defined($value));
}
} elsif (($attribute eq 'enabled') || $nonkey) {
my $value = $$attributes{$attribute}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
if (defined($$lun_attrs{$attribute})) {
$l_attribute_buff .= "\t\t\t\t$attribute $value\n"
if (defined($value));
} elsif ($attribute eq 'enabled') {
$l_attribute_buff .= "\t\t\t\t$attribute $value\n"
if (defined($value));
} else {
$l_attribute_buff_nk .= "\t\t\t\t$attribute $value\n"
if (defined($value));
}
}
}
}
my $l_attribute_buff
= serializeKeyAttr("\t\t\t\t",
$attributes,
$lun_attrs);
my $l_attribute_buff_nk
= serializeNkAttr("\t\t\t\t",
$attributes,
$lun_attrs)
if ($nonkey);
if ($l_attribute_buff_nk) {
$l_attribute_buff .= "\t\t\t\t# Non-key attributes\n";
@@ -1384,35 +1279,17 @@ sub writeConfiguration {
foreach my $init (@{$inits}) {
$init_buff .= "\n\t\t\tINITIATOR $init";
$attributes = $SCST->initiatorAttributes($driver, $target, $group, $init);
my $attributes = $SCST->initiatorAttributes($driver, $target, $group, $init);
my $i_attribute_buff;
my $i_attribute_buff_nk;
foreach my $attribute (sort keys %{$attributes}) {
next if (defined($$attributes{$attribute}->{'set'}));
if (!$$attributes{$attribute}->{'static'} ||
defined($$ini_attrs{$attribute})) {
if (defined($$attributes{$attribute}->{'keys'})) {
foreach my $key (keys %{$$attributes{$attribute}->{'keys'}}) {
my $value = $$attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$i_attribute_buff .= "\t\t\t\t$attribute $value\n"
if (defined($value));
}
} elsif (($attribute eq 'enabled') || $nonkey) {
my $value = $$attributes{$attribute}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
if ($attribute eq 'enabled') {
$i_attribute_buff .= "\t\t\t\t$attribute $value\n"
if (defined($value));
} else {
$i_attribute_buff_nk .= "\t\t\t\t$attribute $value\n"
if (defined($value));
}
}
}
}
my $i_attribute_buff
= serializeKeyAttr("\t\t\t\t",
$attributes,
$ini_attrs);
my $i_attribute_buff_nk
= serializeNkAttr("\t\t\t\t",
$attributes,
$ini_attrs)
if ($nonkey);
if ($i_attribute_buff_nk) {
$i_attribute_buff .= "\t\t\t\t# Non-key attributes\n";
@@ -1421,7 +1298,7 @@ sub writeConfiguration {
if ($i_attribute_buff) {
$init_buff .= " {\n";
$init_buff .= $attribute_buff;
$init_buff .= $i_attribute_buff;
$init_buff .= "\t\t\t}\n";
} else {
$init_buff .= "\n";
@@ -1435,33 +1312,13 @@ sub writeConfiguration {
}
my $grp_attributes = $SCST->groupAttributes($driver, $target, $group);
my $g_attribute_buff;
my $g_attribute_buff_nk;
foreach my $attribute (sort keys %{$grp_attributes}) {
next if (defined($$grp_attributes{$attribute}->{'set'}));
if (!$$grp_attributes{$attribute}->{'static'}) {
if (defined($$grp_attributes{$attribute}->{'keys'})) {
foreach my $key (keys %{$$grp_attributes{$attribute}->{'keys'}}) {
my $value = $$grp_attributes{$attribute}->{'keys'}->{$key}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
$g_attribute_buff .= "\t\t\t$attribute $value\n"
if (defined($value));
}
} elsif (($attribute eq 'enabled') || $nonkey) {
my $value = $$grp_attributes{$attribute}->{'value'};
$value = "\"$value\"" if ($value =~ / /);
if ($attribute eq 'enabled') {
$g_attribute_buff .= "\t\t\t$attribute $value\n"
if (defined($value));
} else {
$g_attribute_buff_nk .= "\t\t\t$attribute $value\n"
if (defined($value));
}
}
}
}
my $g_attribute_buff
= serializeKeyAttr("\t\t\t",
$grp_attributes);
my $g_attribute_buff_nk
= serializeNkAttr("\t\t\t",
$grp_attributes)
if ($nonkey);
if ($g_attribute_buff_nk) {
$g_attribute_buff .= "\n" if ($g_attribute_buff);