From 53ee5e795d539fab05387837a9a02b703710275d Mon Sep 17 00:00:00 2001 From: Jacob Bachmeyer Date: Fri, 14 Oct 2022 21:54:32 -0500 Subject: [PATCH] Use arguments instead of globals in scan_incoming Also move the declaration of the return array. --- gatekeeper.pl | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/gatekeeper.pl b/gatekeeper.pl index fab79a2..9e9dc50 100755 --- a/gatekeeper.pl +++ b/gatekeeper.pl @@ -854,12 +854,14 @@ sub read_directive_from_file { # ftp.gnu.org does not allow overwrites or deletes. # sub scan_incoming { - my @ret; + my $directory = shift; + my $scratchpad = shift; + my %possible; # Get list of all possible files from incoming dir. # - opendir (INCOMING, $incoming_dir) - or ftp_die("FATAL opendir($incoming_dir) failed: $!"); + opendir (INCOMING, $directory) + or ftp_die("FATAL opendir($directory) failed: $!"); while (my $tainted_ent = readdir (INCOMING)) { # don't look at files with a leading dot or dash, but allow those chars # subsequently. Omit files containing any other weird characters. @@ -873,7 +875,7 @@ sub scan_incoming { $possible{$ent} = 1; } closedir (INCOMING) - or ftp_die("FATAL: closedir($incoming_dir) failed: $!"); + or ftp_die("FATAL: closedir($directory) failed: $!"); # No possible files found, so return before we call lsof return () unless %possible; @@ -893,7 +895,7 @@ sub scan_incoming { # the open files because they are owned by another user. # On modern (Debian) systems, condition a) is not met. my @lsof_args = (LSOF_BIN, "-Fn", - map { "$incoming_dir/$_" } keys %possible); + map { "$directory/$_" } keys %possible); ftp_syslog('debug', "DEBUG: " ."lsof command line: " . join(' ',@lsof_args)) if DEBUG; @@ -904,7 +906,7 @@ sub scan_incoming { ftp_syslog('debug', "DEBUG: " . "lsof output: $line") if DEBUG; # only look at the name lines. - next unless $line =~ /^n${incoming_dir}\/(.+)$/; + next unless $line =~ /^n${directory}\/(.+)$/; ftp_syslog('debug', "DEBUG: " ."upload in progress for $1, ignoring during this run") if DEBUG; @@ -933,6 +935,8 @@ sub scan_incoming { } } + my @ret; + # For each remaining possibility, do some more checks for my $ent (keys %possible) { my $base = $ent; @@ -949,8 +953,8 @@ sub scan_incoming { # consume lots of memory reading it. if (exists($possible{$base}) && exists($possible{$sig}) && exists($possible{$directive}) - && (-s "$incoming_dir/$directive" < 50*1024) - && (-s "$incoming_dir/$sig" < 50*1024)) { + && (-s "$directory/$directive" < 50*1024) + && (-s "$directory/$sig" < 50*1024)) { push (@ret, { "directive" => $directive, "sig" => $sig, "upload" => $base, "directive_only" => 0 }); ftp_syslog('info', "processing [$directive:$sig:$base]"); @@ -959,8 +963,8 @@ sub scan_incoming { # anything, for safety. # for my $f (($directive, $sig, $base)) { - rename ($f, "$incoming_tmp/$f") - or fatal("rename $incoming_dir/$f to $incoming_tmp/$f failed: $!",0); + rename ($f, "$scratchpad/$f") + or fatal("rename $directory/$f to $scratchpad/$f failed: $!",0); } # don't bother to try any part of this triple again. @@ -973,7 +977,7 @@ sub scan_incoming { # Here we have a potential problem. It's possible that we are seeing a # directive file that belongs to a triplet the rest of which has not been # uploaded yet. If so, we should ignore this file and not move it to - # $incoming_dir. This means we need to read the file and see if there is a + # $directory. This means we need to read the file and see if there is a # 'filename:' directive. my $racecondition = 0; @@ -1001,18 +1005,18 @@ sub scan_incoming { "upload" => '', "directive_only" => 1 }); # Do atomic rename to temp incoming directory before reading # anything, for safety. - rename ($base, "$incoming_tmp/$base") - or fatal("rename $incoming_dir/$base " - ."to $incoming_tmp/$base failed: $!",0); + rename ($base, "$scratchpad/$base") + or fatal("rename $directory/$base " + ."to $scratchpad/$base failed: $!",0); } delete $possible{$base}; } elsif ((-f $directive) && ((-s $directive) >= MAX_DIRECTIVE_SIZE)) { - rename ("$incoming_dir/$directive", "$incoming_dir/.$directive"); + rename ("$directory/$directive", "$directory/.$directive"); ftp_syslog('info', "directive file ($directive) larger than 50KB"); fatal("The directive file $directive is larger than 50KB. " ."This can not be correct, ignoring upload.",0); } elsif ((-f $sig) && ((-s $sig) >= MAX_SIGNATURE_SIZE)) { - rename ("$incoming_dir/$sig", "$incoming_dir/.$sig"); + rename ("$directory/$sig", "$directory/.$sig"); ftp_syslog('info', "signature file ($sig) larger than 50KB"); fatal("The signature file $sig is larger than 50KB. " ."This can not be correct, ignoring upload.",0); @@ -1834,7 +1838,7 @@ sub cleanup { # have any directory. chdir ($incoming_dir) or ftp_die("FATAL: chdir($incoming_dir) failed: $!"); -my @incoming = scan_incoming (); +my @incoming = scan_incoming ($incoming_dir, $incoming_tmp); # we've moved the files to work on to a new directory. -- 2.25.1