From: <bva...@us...> - 2011-06-22 19:18:45
|
Revision: 3606 http://scst.svn.sourceforge.net/scst/?rev=3606&view=rev Author: bvassche Date: 2011-06-22 19:18:39 +0000 (Wed, 22 Jun 2011) Log Message: ----------- 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(). Modified Paths: -------------- trunk/scstadmin/scstadmin.sysfs/scstadmin Modified: trunk/scstadmin/scstadmin.sysfs/scstadmin =================================================================== --- trunk/scstadmin/scstadmin.sysfs/scstadmin 2011-06-20 20:52:08 UTC (rev 3605) +++ trunk/scstadmin/scstadmin.sysfs/scstadmin 2011-06-22 19:18:39 UTC (rev 3606) @@ -6,6 +6,7 @@ # Author: Mark R. Buechler # License: GPLv2 # Copyright (c) 2005-2010 Mark R. Buechler +# Copyright (C) 2011 Bart Van Assche <bva...@ac...> sub usage { @@ -992,6 +993,75 @@ 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,134 +1085,47 @@ 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'})); + $handler_buff = serializeKeyAttr("\t", $attributes); + $handler_buff_nk = serializeNkAttr("\t", $attributes) if ($nonkey); - 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)); - } - } - } - } - my $devices = $CURRENT{'handler'}->{$handler}; my $device_buff; 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 @@ 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 @@ $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,39 +1203,16 @@ $t_lun_buff .= "\t\tLUN $lun $lun_dev"; - $attributes = $SCST->lunAttributes($driver, $target, $lun); + 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); - 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)); - } - } - } - } - if ($l_attribute_buff_nk) { $l_attribute_buff .= "\t\t\t\t# Non-key attributes\n"; $l_attribute_buff .= $l_attribute_buff_nk; @@ -1331,39 +1247,18 @@ $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'})); + 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 (!$$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)); - } - } - } - } - if ($l_attribute_buff_nk) { $l_attribute_buff .= "\t\t\t\t# Non-key attributes\n"; $l_attribute_buff .= $l_attribute_buff_nk; @@ -1384,36 +1279,18 @@ 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'})); + 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 (!$$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)); - } - } - } - } - if ($i_attribute_buff_nk) { $i_attribute_buff .= "\t\t\t\t# Non-key attributes\n"; $i_attribute_buff .= $i_attribute_buff_nk; @@ -1421,7 +1298,7 @@ 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,34 +1312,14 @@ } my $grp_attributes = $SCST->groupAttributes($driver, $target, $group); + my $g_attribute_buff + = serializeKeyAttr("\t\t\t", + $grp_attributes); + my $g_attribute_buff_nk + = serializeNkAttr("\t\t\t", + $grp_attributes) + if ($nonkey); - 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)); - } - } - } - } - if ($g_attribute_buff_nk) { $g_attribute_buff .= "\n" if ($g_attribute_buff); $g_attribute_buff .= "\t\t\t# Non-key attributes\n"; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |