From aa7e89e90d43cb6ac2067e6bc7551040916861cf Mon Sep 17 00:00:00 2001 From: Jash Date: Sun, 22 Nov 2026 00:36:38 +0530 Subject: [PATCH] fix: zmfilter skips AutoMove and AutoCopy for events already in the target storage refs #7236 --- scripts/zmfilter.pl.in | 9 ++- tests/perl/test_zmfilter_storage_actions.pl | 62 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 tests/perl/test_zmfilter_storage_actions.pl diff --git a/scripts/zmfilter.pl.in b/scripts/zmfilter.pl.in index 51631445e6..00855ab4b1 100644 --- a/scripts/zmfilter.pl.in +++ b/scripts/zmfilter.pl.in @@ -379,13 +379,18 @@ sub checkFilterEvent { } # end if AutoDelete - if ( $filter->{AutoMove} ) { + if ( $filter->{AutoMove} and ($Event->{StorageId}//-1) == $filter->{AutoMoveTo} ) { + Debug("Event $Event->{Id} is already in storage area $filter->{AutoMoveTo}, not moving it"); + } elsif ( $filter->{AutoMove} ) { my $NewStorage = new ZoneMinder::Storage($filter->{AutoMoveTo}); Info("Moving event $Event->{Id} from ".$Event->Storage(undef)->Path().' to '.$NewStorage->Path()); $_ = $Event->MoveTo($NewStorage); Error($_) if $_; } - if ( $filter->{AutoCopy} ) { + if ( $filter->{AutoCopy} and (($Event->{StorageId}//-1) == $filter->{AutoCopyTo} + or ($Event->{SecondaryStorageId}//-1) == $filter->{AutoCopyTo}) ) { + Debug("Event $Event->{Id} is already in storage area $filter->{AutoCopyTo}, not copying it"); + } elsif ( $filter->{AutoCopy} ) { # Copy To is different from MoveTo in that it JUST copies the files # So we still need to update the Event object with the new SecondaryStorageId my $NewStorage = ZoneMinder::Storage->find_one(Id=>$filter->{AutoCopyTo}); diff --git a/tests/perl/test_zmfilter_storage_actions.pl b/tests/perl/test_zmfilter_storage_actions.pl new file mode 100644 index 0000000000..8cd3a26e10 --- /dev/null +++ b/tests/perl/test_zmfilter_storage_actions.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# +# AutoMove and AutoCopy must not redo work for an event that is already in the +# target storage area, or the daemon copies every video again on each pass. +# Set ZMFILTER_IN to another zmfilter.pl.in to test it. +# +# Run as: perl tests/perl/test_zmfilter_storage_actions.pl +# +use strict; +use warnings; +use FindBin; + +my ($failures, $moves, $copies, $saves) = (0, 0, 0, 0); +sub check { + my ($name, $got, $want) = @_; + if ($got == $want) { print "ok - $name\n"; } + else { $failures++; print "FAIL - $name (got $got, want $want)\n"; } +} + +package ZoneMinder::Storage; +sub new { my ($c, $id) = @_; return bless { Id => $id }, $c; } +sub find_one { my ($c, %a) = @_; return bless { Id => $a{Id} }, $c; } +sub Path { return '/nonexistent/'.$_[0]{Id}; } +package FakeEvent; +sub save { $saves++; return ''; } +sub Storage { return ZoneMinder::Storage->new($_[0]{StorageId}); } +sub MoveTo { $moves++; return ''; } +sub CopyTo { $copies++; return ''; } +package main; + +our %Config; +sub Debug {} sub Info {} sub Warning {} sub Error {} + +my $file = $ENV{ZMFILTER_IN} || "$FindBin::Bin/../../scripts/zmfilter.pl.in"; +open(my $fh, '<', $file) or die "Can't read $file: $!"; +my $src = do { local $/; <$fh> }; +close($fh); +my ($sub) = $src =~ /^(sub checkFilterEvent \{.*?^\}[^\n]*\n)/ms or die "checkFilterEvent not found\n"; +eval "$sub; 1" or die "Can't compile: $@"; + +sub run { + my ($event, %filter) = @_; + ($moves, $copies, $saves) = (0, 0, 0); + checkFilterEvent(\%filter, bless { Id => 1, %$event }, 'FakeEvent'); +} + +run({StorageId => 1}, AutoMove => 1, AutoMoveTo => 2); +check('event elsewhere is moved', $moves, 1); +run({StorageId => 2}, AutoMove => 1, AutoMoveTo => 2); +check('event already in the target is not moved again', $moves, 0); + +run({StorageId => 1}, AutoCopy => 1, AutoCopyTo => 2); +check('event with no copy is copied', $copies, 1); +run({StorageId => 1, SecondaryStorageId => 3}, AutoCopy => 1, AutoCopyTo => 2); +check('event with its copy elsewhere is copied', $copies, 1); +run({StorageId => 1, SecondaryStorageId => 2}, AutoCopy => 1, AutoCopyTo => 2); +check('event already copied to the target is not copied again', $copies, 0); +run({StorageId => 2}, AutoCopy => 1, AutoCopyTo => 2); +check('event whose primary storage is the target is not copied', $copies, 0); + +print $failures ? "$failures failed\n" : "all passed\n"; +exit($failures ? 1 : 0);