From dfde985370180c80ac3a1f944b6e8169c7eb7216 Mon Sep 17 00:00:00 2001 From: Jacob Bachmeyer Date: Wed, 5 Oct 2022 22:50:50 -0500 Subject: [PATCH] Remove sub main in gatekeeper script Unlike C, Perl allows program code to simply appear at the top level of the file. Using this feature is the standard convention in Perl. --- gatekeeper.pl | 124 ++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 65 deletions(-) diff --git a/gatekeeper.pl b/gatekeeper.pl index c824519..666efee 100755 --- a/gatekeeper.pl +++ b/gatekeeper.pl @@ -1450,76 +1450,70 @@ sub cleanup { # - Main execution path # -exit (&main ()); +# Initialize our syslogging +if (IN_TEST_MODE) { + $ENV{TEST_SYSLOG_SOCKET} =~ m/^([[:alnum:]\/]+)$/ + or die "strange test syslog socket"; + -S $1 or die "test syslog socket is not a socket"; + setlogsock(unix => $1); +} +openlog("ftp-upload", 'pid', $facility); +ftp_syslog('info', "($log_style) Beginning upload processing run."); + +# make sure our directories all exist, or it's hopeless. +# Use die instead of fatal - this error should "never" happen. +for my $dir ($package_config_base, $incoming_dir, $incoming_tmp, + $destfinal, $desttmp) { + -d $dir || ftp_die("FATAL: configuration problem, $dir is not a directory"); +} +# the chdir simplifies our filename parsing, so the base names don't +# have any directory. +chdir ($incoming_dir) || ftp_die("FATAL: chdir($incoming_dir) failed: $!"); +my @incoming = &scan_incoming (); -sub main -{ - # Initialize our syslogging - if (IN_TEST_MODE) { - $ENV{TEST_SYSLOG_SOCKET} =~ m/^([[:alnum:]\/]+)$/ - or die "strange test syslog socket"; - -S $1 or die "test syslog socket is not a socket"; - setlogsock(unix => $1); - } - openlog("ftp-upload", 'pid', $facility); - ftp_syslog('info', "($log_style) Beginning upload processing run."); - - # make sure our directories all exist, or it's hopeless. - # Use die instead of fatal - this error should "never" happen. - for my $dir ($package_config_base, $incoming_dir, $incoming_tmp, - $destfinal, $desttmp) { - -d $dir || ftp_die("FATAL: configuration problem, $dir is not a directory"); - } - # the chdir simplifies our filename parsing, so the base names don't - # have any directory. - chdir ($incoming_dir) || ftp_die("FATAL: chdir($incoming_dir) failed: $!"); - my @incoming = &scan_incoming (); - - - # we've moved the files to work on to a new directory. - chdir ($incoming_tmp) || ftp_die("FATAL: chdir($incoming_tmp) failed: $!"); - - for my $files (@incoming) { # each list element is a hash reference. - ftp_syslog('info',"($log_style) found directive: $files->{directive}\n"); - # if we die processing a triplet, the eval allows us to move - # onto the next triplet. - eval { - # set up the %info variable - my $retval = &read_directive_file ($files->{"directive"},$files->{"upload"},$files->{"directive_only"}); - - if ($retval == 0) { - # do the work - &execute_commands($files,%info); - - # report success - if (!$files->{"directive_only"}) { - &success_upload($files->{"sig"}, $files->{"upload"},$files->{"directive"}); - } else { - &success_directive($files->{directive}); - } - } - }; - ftp_warn ("eval failed: $@") if $@; +# we've moved the files to work on to a new directory. +chdir ($incoming_tmp) || ftp_die("FATAL: chdir($incoming_tmp) failed: $!"); - # clean up files if we abort while processing a triplet - cleanup ($files->{"sig"}, $files->{"upload"}, $files->{"directive"}) if ($@); - # clear out the current package that we just finished processing - undef %info; - } - if ((scalar @incoming) == 0) { - ftp_syslog('info', "($log_style) No files found for processing."); - } else { - ftp_syslog('info', "($log_style) Processing complete: " . (scalar @incoming) . " uploads processed."); - system("/usr/local/bin/generate-ftpindex") unless IN_TEST_MODE; - ftp_syslog('info', "($log_style) Updated ftpindex"); - } +for my $files (@incoming) { # each list element is a hash reference. + ftp_syslog('info',"($log_style) found directive: $files->{directive}\n"); + # if we die processing a triplet, the eval allows us to move + # onto the next triplet. + eval { + # set up the %info variable + my $retval = &read_directive_file ($files->{"directive"},$files->{"upload"},$files->{"directive_only"}); - # Clean up the incoming directory and the incoming tmp directory - remove files older than a day - cleanup_dir($incoming_dir); - cleanup_dir($incoming_tmp); + if ($retval == 0) { + # do the work + &execute_commands($files,%info); - return 0; + # report success + if (!$files->{"directive_only"}) { + &success_upload($files->{"sig"}, $files->{"upload"},$files->{"directive"}); + } else { + &success_directive($files->{directive}); + } + } + }; + ftp_warn ("eval failed: $@") if $@; + + # clean up files if we abort while processing a triplet + cleanup ($files->{"sig"}, $files->{"upload"}, $files->{"directive"}) if ($@); + # clear out the current package that we just finished processing + undef %info; +} +if ((scalar @incoming) == 0) { + ftp_syslog('info', "($log_style) No files found for processing."); +} else { + ftp_syslog('info', "($log_style) Processing complete: " . (scalar @incoming) . " uploads processed."); + system("/usr/local/bin/generate-ftpindex") unless IN_TEST_MODE; + ftp_syslog('info', "($log_style) Updated ftpindex"); } +# Clean up the incoming directory and the incoming tmp directory - remove files older than a day +cleanup_dir($incoming_dir); +cleanup_dir($incoming_tmp); + +exit 0; + __END__ -- 2.25.1