2007-07-12 02:10:48 -04:00
|
|
|
package WgetTest;
|
2005-12-05 08:35:07 -05:00
|
|
|
|
|
|
|
use strict;
|
2008-11-25 13:59:36 -05:00
|
|
|
use warnings;
|
2005-12-05 08:35:07 -05:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
our $VERSION = 0.01;
|
|
|
|
|
|
|
|
use Carp;
|
2006-01-24 07:15:58 -05:00
|
|
|
use Cwd;
|
2014-11-01 05:06:26 -04:00
|
|
|
use English qw(-no_match_vars);
|
2006-01-24 07:15:58 -05:00
|
|
|
use File::Path;
|
2014-11-01 05:06:26 -04:00
|
|
|
use IO::Handle;
|
2014-09-24 10:40:03 -04:00
|
|
|
use POSIX qw(locale_h);
|
|
|
|
use locale;
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
our $WGETPATH = '../src/wget';
|
2015-04-06 06:33:37 -04:00
|
|
|
our $VALGRIND_SUPP_FILE = Cwd::getcwd();
|
|
|
|
if (defined $ENV{'srcdir'}) {
|
|
|
|
$VALGRIND_SUPP_FILE = $VALGRIND_SUPP_FILE
|
|
|
|
. "/" . $ENV{'srcdir'};
|
|
|
|
}
|
|
|
|
$VALGRIND_SUPP_FILE = $VALGRIND_SUPP_FILE . '/valgrind-suppressions';
|
2007-07-12 02:10:48 -04:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
my @unexpected_downloads = ();
|
2005-12-05 08:35:07 -05:00
|
|
|
|
|
|
|
{
|
2014-11-01 05:06:26 -04:00
|
|
|
my %_attr_data = ( # DEFAULT
|
|
|
|
_cmdline => q{},
|
|
|
|
_workdir => Cwd::getcwd(),
|
|
|
|
_errcode => 0,
|
|
|
|
_existing => {},
|
|
|
|
_input => {},
|
|
|
|
_name => $PROGRAM_NAME,
|
|
|
|
_output => {},
|
|
|
|
_server_behavior => {},
|
|
|
|
);
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-12-27 04:00:12 -05:00
|
|
|
sub _default_for
|
|
|
|
{
|
|
|
|
my ($self, $attr) = @_;
|
2014-11-01 05:06:26 -04:00
|
|
|
return $_attr_data{$attr};
|
2006-12-27 04:00:12 -05:00
|
|
|
}
|
|
|
|
|
2008-11-13 06:29:59 -05:00
|
|
|
sub _standard_keys
|
2006-12-27 04:00:12 -05:00
|
|
|
{
|
2014-11-01 05:06:26 -04:00
|
|
|
return keys %_attr_data;
|
2006-12-27 04:00:12 -05:00
|
|
|
}
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
sub new
|
|
|
|
{
|
2005-12-05 08:35:07 -05:00
|
|
|
my ($caller, %args) = @_;
|
2014-11-01 05:06:26 -04:00
|
|
|
my $caller_is_obj = ref $caller;
|
2005-12-05 08:35:07 -05:00
|
|
|
my $class = $caller_is_obj || $caller;
|
2014-11-01 05:06:26 -04:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR "class = ", $class, "\n";
|
2007-07-12 02:10:48 -04:00
|
|
|
#print STDERR "_attr_data {workdir} = ", $WgetTest::_attr_data{_workdir}, "\n";
|
2005-12-05 08:35:07 -05:00
|
|
|
my $self = bless {}, $class;
|
2014-11-01 05:06:26 -04:00
|
|
|
for my $attrname ($self->_standard_keys())
|
|
|
|
{
|
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR "attrname = ", $attrname, " value = ";
|
2014-11-01 05:06:26 -04:00
|
|
|
my ($argname) = ($attrname =~ m/^_(.*)/msx);
|
|
|
|
if (exists $args{$argname})
|
|
|
|
{
|
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
#printf STDERR "Setting up $attrname\n";
|
|
|
|
$self->{$attrname} = $args{$argname};
|
2014-11-01 05:06:26 -04:00
|
|
|
}
|
|
|
|
elsif ($caller_is_obj)
|
|
|
|
{
|
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
#printf STDERR "Copying $attrname\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->{$attrname} = $caller->{$attrname};
|
2014-11-01 05:06:26 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-05 08:35:07 -05:00
|
|
|
#printf STDERR "Using default for $attrname\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->{$attrname} = $self->_default_for($attrname);
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
2014-11-01 05:06:26 -04:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
#print STDERR $attrname, '=', $self->{$attrname}, "\n";
|
2005-12-05 08:35:07 -05:00
|
|
|
}
|
2014-11-01 05:06:26 -04:00
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
#printf STDERR "_workdir default = ", $self->_default_for("_workdir");
|
2005-12-05 08:35:07 -05:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
sub run
|
|
|
|
{
|
|
|
|
my $self = shift;
|
2006-01-24 07:15:58 -05:00
|
|
|
my $result_message = "Test successful.\n";
|
2009-09-04 21:32:52 -04:00
|
|
|
my $errcode;
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
$self->{_name} =~ s{.*/}{}msx; # remove path
|
|
|
|
$self->{_name} =~ s{[.][^.]+$}{}msx; # remove extension
|
2006-01-24 07:15:58 -05:00
|
|
|
printf "Running test $self->{_name}\n";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
|
|
|
# Setup
|
2009-09-04 21:32:52 -04:00
|
|
|
my $new_result = $self->_setup();
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir "$self->{_workdir}/$self->{_name}/input"
|
|
|
|
or carp "Could not chdir to input directory: $ERRNO";
|
|
|
|
if (defined $new_result)
|
|
|
|
{
|
2009-09-04 21:32:52 -04:00
|
|
|
$result_message = $new_result;
|
2014-11-01 05:06:26 -04:00
|
|
|
$errcode = 1;
|
2009-09-04 21:32:52 -04:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
# Launch server
|
2006-05-29 05:15:06 -04:00
|
|
|
my $pid = $self->_fork_and_launch_server();
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
# Call wget
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir "$self->{_workdir}/$self->{_name}/output"
|
|
|
|
or carp "Could not chdir to output directory: $ERRNO";
|
2014-10-08 05:03:45 -04:00
|
|
|
|
2008-06-12 05:18:35 -04:00
|
|
|
my $cmdline = $self->{_cmdline};
|
|
|
|
$cmdline = $self->_substitute_port($cmdline);
|
2014-11-01 05:06:26 -04:00
|
|
|
$cmdline =
|
|
|
|
($cmdline =~ m{^/.*}msx) ? $cmdline : "$self->{_workdir}/$cmdline";
|
2014-10-08 05:03:45 -04:00
|
|
|
|
|
|
|
my $valgrind = $ENV{VALGRIND_TESTS};
|
2015-04-11 11:14:56 -04:00
|
|
|
if (!defined $valgrind)
|
2014-11-01 05:06:26 -04:00
|
|
|
{
|
2015-04-11 11:14:56 -04:00
|
|
|
$valgrind = 0;
|
|
|
|
}
|
2014-11-01 05:06:26 -04:00
|
|
|
|
2015-04-11 11:14:56 -04:00
|
|
|
my $gdb = $ENV{GDB_TESTS};
|
|
|
|
if (!defined $gdb)
|
|
|
|
{
|
|
|
|
$gdb = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($gdb == 1)
|
|
|
|
{
|
|
|
|
$cmdline = 'gdb --args ' . $cmdline;
|
2014-11-01 05:06:26 -04:00
|
|
|
}
|
2015-04-14 06:14:21 -04:00
|
|
|
elsif ($valgrind eq "1")
|
2014-11-01 05:06:26 -04:00
|
|
|
{
|
|
|
|
$cmdline =
|
2015-04-06 06:33:37 -04:00
|
|
|
'valgrind --suppressions=' . $VALGRIND_SUPP_FILE
|
|
|
|
. ' --error-exitcode=301 --leak-check=yes --track-origins=yes '
|
2014-11-01 05:06:26 -04:00
|
|
|
. $cmdline;
|
|
|
|
}
|
2015-04-14 06:14:21 -04:00
|
|
|
elsif ($valgrind ne q{} && $valgrind ne "0")
|
2014-11-01 05:06:26 -04:00
|
|
|
{
|
|
|
|
$cmdline = "$valgrind $cmdline";
|
2014-10-08 05:03:45 -04:00
|
|
|
}
|
|
|
|
|
2008-06-12 05:18:35 -04:00
|
|
|
print "Calling $cmdline\n";
|
2014-11-01 05:06:26 -04:00
|
|
|
$errcode = system $cmdline;
|
|
|
|
$errcode >>= 8; # XXX: should handle abnormal error codes.
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Shutdown server
|
2008-11-13 06:29:59 -05:00
|
|
|
# if we didn't explicitely kill the server, we would have to call
|
|
|
|
# waitpid ($pid, 0) here in order to wait for the child process to
|
2006-05-29 05:15:06 -04:00
|
|
|
# terminate
|
2014-11-01 05:06:26 -04:00
|
|
|
kill 'TERM', $pid;
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Verify download
|
2014-11-01 05:06:26 -04:00
|
|
|
if ($errcode != $self->{_errcode})
|
|
|
|
{
|
|
|
|
$result_message =
|
|
|
|
"Test failed: wrong code returned (was: $errcode, expected: $self->{_errcode})\n";
|
|
|
|
goto CLEANUP;
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
2007-07-12 02:10:48 -04:00
|
|
|
my $error_str;
|
2014-11-01 05:06:26 -04:00
|
|
|
if ($error_str = $self->_verify_download())
|
|
|
|
{
|
2006-01-24 07:15:58 -05:00
|
|
|
$result_message = $error_str;
|
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
CLEANUP:
|
2006-01-24 07:15:58 -05:00
|
|
|
$self->_cleanup();
|
|
|
|
|
|
|
|
print $result_message;
|
2007-07-12 02:10:48 -04:00
|
|
|
return $errcode != $self->{_errcode} || ($error_str ? 1 : 0);
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
sub _setup
|
|
|
|
{
|
2006-01-24 07:15:58 -05:00
|
|
|
my $self = shift;
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir $self->{_workdir}
|
|
|
|
or carp "Could not chdir into $self->{_workdir}: $ERRNO";
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# Create temporary directory
|
2014-11-01 05:06:26 -04:00
|
|
|
mkdir $self->{_name} or carp "Could not mkdir '$self->{_name}': $ERRNO";
|
|
|
|
chdir $self->{_name}
|
|
|
|
or carp "Could not chdir into '$self->{_name}': $ERRNO";
|
|
|
|
mkdir 'input' or carp "Could not mkdir 'input' $ERRNO";
|
|
|
|
mkdir 'output' or carp "Could not mkdir 'output': $ERRNO";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-04-27 09:27:17 -04:00
|
|
|
# Setup existing files
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir 'output' or carp "Could not chdir into 'output': $ERRNO";
|
|
|
|
for my $filename (keys %{$self->{_existing}})
|
|
|
|
{
|
|
|
|
open my $fh, '>', $filename
|
|
|
|
or return "Test failed: cannot open pre-existing file $filename\n";
|
2007-08-22 10:05:18 -04:00
|
|
|
|
|
|
|
my $file = $self->{_existing}->{$filename};
|
2014-11-01 05:06:26 -04:00
|
|
|
print {$fh} $file->{content}
|
|
|
|
or return "Test failed: cannot write pre-existing file $filename\n";
|
2007-08-22 10:05:18 -04:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
close $fh or carp $ERRNO;
|
2007-08-22 10:05:18 -04:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
if (exists($file->{timestamp}))
|
|
|
|
{
|
2007-08-22 10:05:18 -04:00
|
|
|
utime $file->{timestamp}, $file->{timestamp}, $filename
|
2014-11-01 05:06:26 -04:00
|
|
|
or return
|
|
|
|
"Test failed: cannot set timestamp on pre-existing file $filename\n";
|
2007-08-22 10:05:18 -04:00
|
|
|
}
|
2008-11-13 06:29:59 -05:00
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir '../input' or carp "Cannot chdir into '../input': $ERRNO";
|
2009-09-06 16:04:22 -04:00
|
|
|
$self->_setup_server();
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir $self->{_workdir}
|
|
|
|
or carp "Cannot chdir into '$self->{_workdir}': $ERRNO";
|
2009-09-06 16:04:22 -04:00
|
|
|
return;
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
sub _cleanup
|
|
|
|
{
|
2006-01-24 07:15:58 -05:00
|
|
|
my $self = shift;
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir $self->{_workdir}
|
|
|
|
or carp "Could not chdir into '$self->{_workdir}': $ERRNO";
|
|
|
|
if (!$ENV{WGET_TEST_NO_CLEANUP})
|
|
|
|
{
|
|
|
|
File::Path::rmtree($self->{_name});
|
|
|
|
}
|
|
|
|
return 1;
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
2009-09-06 16:04:22 -04:00
|
|
|
# not a method
|
2014-11-01 05:06:26 -04:00
|
|
|
sub quotechar
|
|
|
|
{
|
|
|
|
my $c = ord shift;
|
|
|
|
if ($c >= 0x7 && $c <= 0xD)
|
|
|
|
{
|
|
|
|
return q{\\} . qw(a b t n v f r) [$c - 0x7];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return sprintf '\\x%02x', $c;
|
2009-09-06 16:04:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# not a method
|
2014-11-01 05:06:26 -04:00
|
|
|
sub _show_diff
|
|
|
|
{
|
2009-09-06 16:04:22 -04:00
|
|
|
my ($expected, $actual) = @_;
|
2014-11-01 05:06:26 -04:00
|
|
|
my $SNIPPET_SIZE = 10;
|
2009-09-06 16:04:22 -04:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
my $str = q{};
|
2009-09-06 16:04:22 -04:00
|
|
|
my $explen = length $expected;
|
|
|
|
my $actlen = length $actual;
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
if ($explen != $actlen)
|
|
|
|
{
|
2009-09-06 16:04:22 -04:00
|
|
|
$str .= "Sizes don't match: expected = $explen, actual = $actlen\n";
|
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
my $min = $explen <= $actlen ? $explen : $actlen;
|
2009-09-06 16:04:22 -04:00
|
|
|
my $line = 1;
|
2014-11-01 05:06:26 -04:00
|
|
|
my $col = 1;
|
2015-05-19 16:20:17 -04:00
|
|
|
my $i = 0;
|
2014-11-01 05:06:26 -04:00
|
|
|
|
2015-05-19 16:20:17 -04:00
|
|
|
while ( $i < $min )
|
2014-11-01 05:06:26 -04:00
|
|
|
{
|
|
|
|
last if substr($expected, $i, 1) ne substr $actual, $i, 1;
|
2015-05-19 16:20:17 -04:00
|
|
|
if (substr($expected, $i, 1) eq "\n")
|
2014-11-01 05:06:26 -04:00
|
|
|
{
|
2009-09-06 16:04:22 -04:00
|
|
|
$line++;
|
|
|
|
$col = 0;
|
2014-11-01 05:06:26 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-06 16:04:22 -04:00
|
|
|
$col++;
|
|
|
|
}
|
2015-05-19 16:20:17 -04:00
|
|
|
$i++;
|
2009-09-06 16:04:22 -04:00
|
|
|
}
|
|
|
|
my $snip_start = $i - ($SNIPPET_SIZE / 2);
|
2014-11-01 05:06:26 -04:00
|
|
|
if ($snip_start < 0)
|
|
|
|
{
|
|
|
|
$SNIPPET_SIZE += $snip_start; # Take it from the end.
|
2009-09-06 16:04:22 -04:00
|
|
|
$snip_start = 0;
|
|
|
|
}
|
2014-11-01 05:06:26 -04:00
|
|
|
my $exp_snip = substr $expected, $snip_start, $SNIPPET_SIZE;
|
|
|
|
my $act_snip = substr $actual, $snip_start, $SNIPPET_SIZE;
|
|
|
|
$exp_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
|
|
|
|
$act_snip =~ s/[^[:print:]]/ quotechar($&) /gemsx;
|
2009-09-06 16:04:22 -04:00
|
|
|
$str .= "Mismatch at line $line, col $col:\n";
|
|
|
|
$str .= " $exp_snip\n";
|
|
|
|
$str .= " $act_snip\n";
|
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
sub _verify_download
|
|
|
|
{
|
2006-01-24 07:15:58 -05:00
|
|
|
my $self = shift;
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir "$self->{_workdir}/$self->{_name}/output"
|
|
|
|
or carp "Could not chdir into output directory: $ERRNO";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2006-01-24 07:15:58 -05:00
|
|
|
# use slurp mode to read file content
|
2014-11-01 05:06:26 -04:00
|
|
|
my $old_input_record_separator = $INPUT_RECORD_SEPARATOR;
|
|
|
|
local $INPUT_RECORD_SEPARATOR = undef;
|
|
|
|
|
|
|
|
while (my ($filename, $filedata) = each %{$self->{_output}})
|
|
|
|
{
|
|
|
|
open my $fh, '<', $filename
|
|
|
|
or return "Test failed: file $filename not downloaded\n";
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
my $content = <$fh>;
|
|
|
|
|
|
|
|
close $fh or carp $ERRNO;
|
2008-11-13 06:29:59 -05:00
|
|
|
|
2008-06-12 05:18:35 -04:00
|
|
|
my $expected_content = $filedata->{'content'};
|
|
|
|
$expected_content = $self->_substitute_port($expected_content);
|
2014-11-01 05:06:26 -04:00
|
|
|
if ($content ne $expected_content)
|
|
|
|
{
|
2009-09-06 16:04:22 -04:00
|
|
|
return "Test failed: wrong content for file $filename\n"
|
2014-11-01 05:06:26 -04:00
|
|
|
. _show_diff($expected_content, $content);
|
2009-09-06 16:04:22 -04:00
|
|
|
}
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
if (exists($filedata->{'timestamp'}))
|
|
|
|
{
|
|
|
|
my (
|
|
|
|
$dev, $ino, $mode, $nlink, $uid,
|
|
|
|
$gid, $rdev, $size, $atime, $mtime,
|
|
|
|
$ctime, $blksize, $blocks
|
|
|
|
)
|
|
|
|
= stat $filename;
|
2006-01-24 07:15:58 -05:00
|
|
|
|
2008-11-13 06:29:59 -05:00
|
|
|
$mtime == $filedata->{'timestamp'}
|
2014-12-20 06:07:42 -05:00
|
|
|
or return "Test failed: wrong timestamp for file $filename: expected = $filedata->{'timestamp'}, actual = $mtime\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
2008-11-13 06:29:59 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
local $INPUT_RECORD_SEPARATOR = $old_input_record_separator;
|
2006-01-24 07:15:58 -05:00
|
|
|
|
|
|
|
# make sure no unexpected files were downloaded
|
2014-11-01 05:06:26 -04:00
|
|
|
chdir "$self->{_workdir}/$self->{_name}/output"
|
|
|
|
or carp "Could not change into output directory: $ERRNO";
|
|
|
|
|
|
|
|
__dir_walk(
|
|
|
|
q{.},
|
|
|
|
sub {
|
|
|
|
if (!(exists $self->{_output}{$_[0]} || $self->{_existing}{$_[0]}))
|
|
|
|
{
|
|
|
|
push @unexpected_downloads, $_[0];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
sub { shift; return @_ }
|
|
|
|
);
|
|
|
|
if (@unexpected_downloads)
|
|
|
|
{
|
|
|
|
return 'Test failed: unexpected downloaded files [' . join ', ',
|
|
|
|
@unexpected_downloads . "]\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
return q{};
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
sub __dir_walk
|
|
|
|
{
|
2006-01-24 07:15:58 -05:00
|
|
|
my ($top, $filefunc, $dirfunc) = @_;
|
|
|
|
|
|
|
|
my $DIR;
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
if (-d $top)
|
|
|
|
{
|
2006-01-24 07:15:58 -05:00
|
|
|
my $file;
|
2014-11-01 05:06:26 -04:00
|
|
|
if (!opendir $DIR, $top)
|
|
|
|
{
|
|
|
|
warn "Couldn't open directory $DIR: $ERRNO; skipping.\n";
|
2006-01-24 07:15:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @results;
|
2014-11-01 05:06:26 -04:00
|
|
|
while ($file = readdir $DIR)
|
|
|
|
{
|
|
|
|
next if $file eq q{.} || $file eq q{..};
|
|
|
|
my $nextdir = $top eq q{.} ? $file : "$top/$file";
|
2006-01-24 07:15:58 -05:00
|
|
|
push @results, __dir_walk($nextdir, $filefunc, $dirfunc);
|
|
|
|
}
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
return $dirfunc ? $dirfunc->($top, @results) : ();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $filefunc ? $filefunc->($top) : ();
|
2006-01-24 07:15:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-13 06:29:59 -05:00
|
|
|
sub _fork_and_launch_server
|
2006-05-29 05:15:06 -04:00
|
|
|
{
|
|
|
|
my $self = shift;
|
|
|
|
|
2014-11-01 05:06:26 -04:00
|
|
|
pipe FROM_CHILD, TO_PARENT or croak 'Cannot create pipe!';
|
|
|
|
TO_PARENT->autoflush();
|
|
|
|
|
|
|
|
my $pid = fork;
|
|
|
|
if ($pid < 0)
|
|
|
|
{
|
|
|
|
carp 'Cannot fork';
|
|
|
|
}
|
|
|
|
elsif ($pid == 0)
|
|
|
|
{
|
2006-05-29 05:15:06 -04:00
|
|
|
|
2008-11-13 06:29:59 -05:00
|
|
|
# child
|
2014-11-01 05:06:26 -04:00
|
|
|
close FROM_CHILD or carp $ERRNO;
|
|
|
|
|
2014-09-24 10:40:03 -04:00
|
|
|
# FTP Server has to start with english locale due to use of strftime month names in LIST command
|
2014-11-01 05:06:26 -04:00
|
|
|
setlocale(LC_ALL, 'C');
|
|
|
|
$self->_launch_server(
|
|
|
|
sub {
|
|
|
|
print {*TO_PARENT} "SYNC\n";
|
|
|
|
close TO_PARENT or carp $ERRNO;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-05-29 05:15:06 -04:00
|
|
|
# father
|
2014-11-01 05:06:26 -04:00
|
|
|
close TO_PARENT or carp $ERRNO;
|
2006-05-29 05:15:06 -04:00
|
|
|
chomp(my $line = <FROM_CHILD>);
|
2014-11-01 05:06:26 -04:00
|
|
|
close FROM_CHILD or carp $ERRNO;
|
2006-05-29 05:15:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $pid;
|
|
|
|
}
|
|
|
|
|
2005-12-05 08:35:07 -05:00
|
|
|
1;
|
|
|
|
|
|
|
|
# vim: et ts=4 sw=4
|