[pmg-devel] [PATCH pmg-api v2 2/4] api: spamassassin: read local channels

Stoiko Ivanov s.ivanov at proxmox.com
Mon Jan 18 20:47:56 CET 2021


On Fri, 15 Jan 2021 11:02:42 +0100
Thomas Lamprecht <t.lamprecht at proxmox.com> wrote:

> On 30.12.20 18:15, Stoiko Ivanov wrote:
> > Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
> > ---
> > v1->v2:
> > * changed the transliteration (tr///) to an equivalent more common s///
> > 
> >  src/PMG/API2/SpamAssassin.pm | 83 +++++++++++++++++++++---------------
> >  1 file changed, 49 insertions(+), 34 deletions(-)
> > 
> > diff --git a/src/PMG/API2/SpamAssassin.pm b/src/PMG/API2/SpamAssassin.pm
> > index eab02d9..6b9f8f9 100644
> > --- a/src/PMG/API2/SpamAssassin.pm
> > +++ b/src/PMG/API2/SpamAssassin.pm
> > @@ -80,50 +80,65 @@ __PACKAGE__->register_method({
> >  	my ($param) = @_;
> >  
> >  	my $saversion = $Mail::SpamAssassin::VERSION;
> > -	my $channelfile = "/var/lib/spamassassin/$saversion/updates_spamassassin_org.cf";
> > +	my $sa_update_dir = "/var/lib/spamassassin/$saversion/";
> > +
> > +	my $check_channel = sub {
> > +	    my ($channel) = @_;
> > +
> > +	    # see sa-update source:
> > +	    my $channel_file_base = $channel;
> > +	    $channel_file_base =~ s/[^A-Za-z0-9-]+/_/g;
> > +	    my $channelfile = "${sa_update_dir}${channel_file_base}.cf";
> > +
> > +	    my $mtime = -1;
> > +	    my $version = -1;
$version gets initialized to -1 here...
> > +	    my $newversion = -1;
> > +
> > +	    if (-f $channelfile) {
> > +		# stat metadata cf file
> > +		$mtime = (stat($channelfile))[9]; # 9 is mtime
> > +
> > +		# parse version from metadata cf file
> > +		my $metadata = PVE::Tools::file_read_firstline($channelfile);
> > +		if ($metadata =~ m/\s([0-9]+)$/) {
> > +		    $version = $1;
> > +		} else {
> > +		    warn "invalid metadata in '$channelfile'\n";  
> 
> I know it was already there, but this  mayresults in a undef comparison
> when checking `if ($newversion > $version) {` below.
> I'd do a separate followup with that being fixed, e.g., by either prepending the
> if check with a `!defined($version) || ...` check or setting it to -1000 or so.
so it should not be undefined if the metadata is not matching?
(I quickly tested in on my system - and if I drop the -1 I get a undef
comparison in the logs - but it works as is)
or am I missing something?

> 
> > +		}
> > +	    }
> > +	    # call sa-update to see if updates are available
> >  
> > -	my $mtime = -1;
> > -	my $version = -1;
> > -	my $newversion = -1;
> > +	    my $cmd = "$SAUPDATE -v --checkonly --channel $channel";
> > +	    PVE::Tools::run_command($cmd, noerr => 1, logfunc => sub {
> > +		my ($line) = @_;
> >  
> > -	if (-f $channelfile) {
> > -	    # stat metadata cf file
> > -	    $mtime = (stat($channelfile))[9]; # 9 is mtime
> > +		if ($line =~ m/Update available for channel \S+: -?[0-9]+ -> ([0-9]+)/) {
> > +		    $newversion = $1;
> > +		}
> > +	    });
> >  
> > -	    # parse version from metadata cf file
> > -	    my $metadata = PVE::Tools::file_read_firstline($channelfile);
> > -	    if ($metadata =~ m/\s([0-9]+)$/) {
> > -		$version = $1;
> > -	    } else {
> > -		warn "invalid metadata in '$channelfile'\n";
> > -	    }
> > -	}
> > -	# call sa-update to see if updates are available
> > +	    my $result = {
> > +		channel => $channel,
> > +	    };
> >  
> > -	my $cmd = "$SAUPDATE -v --checkonly";
> > -	PVE::Tools::run_command($cmd, noerr => 1, logfunc => sub {
> > -	    my ($line) = @_;
> > +	    $result->{version} = $version if $version > -1;
> > +	    $result->{update_version} = $newversion if $newversion > -1;
> > +	    $result->{last_updated} = $mtime if $mtime > -1;
> >  
> > -	    if ($line =~ m/Update available for channel \S+: -?[0-9]+ -> ([0-9]+)/) {
> > -		$newversion = $1;
> > +	    if ($newversion > $version) {
> > +		$result->{update_avail} = 1;
> > +	    } else {
> > +		$result->{update_avail} = 0;
> >  	    }
> > -	});
> > -
> > -	my $result = {
> > -	    channel => 'updates.spamassassin.org',
> > +	    return $result;
> >  	};
> >  
> > -	$result->{version} = $version if $version > -1;
> > -	$result->{update_version} = $newversion if $newversion > -1;
> > -	$result->{last_updated} = $mtime if $mtime > -1;
> > +	my @channels = ('updates.spamassassin.org');
> >  
> > -	if ($newversion > $version) {
> > -	    $result->{update_avail} = 1;
> > -	} else {
> > -	    $result->{update_avail} = 0;
> > -	}
> > +	my $localchannels = PMG::Utils::local_spamassassin_channels();
> > +	push(@channels, map { $_->{channelurl} } @$localchannels);
> >  
> > -	return [$result];
> > +	return [ map { $check_channel->($_) } @channels];  
> 
> this reads a bit weird, but can be OK as $check_channel does seem to catch
> all actual possible errors.
> 
> >      }});
> >  
> >  __PACKAGE__->register_method({
> >   
> 
> 





More information about the pmg-devel mailing list