Bug 42992: FTP file transport: fix list_files() corrupting filenames
Koha::File::Transport::FTP::_list_files parsed the LIST / "ls -l" output
with a non-greedy date capture that only consumed the first of the three
date tokens (the month), leaving the day and time glued to the front of
the filename. Every filename returned for an FTP transport was therefore
prefixed with the listing date, e.g.
-rw-r--r-- 1 user group 1234 Jan 01 12:00 QUOTES_413514.CEQ
=> filename "01 12:00 QUOTES_413514.CEQ"
Callers (Koha::Edifact::Transport, marc_ordering_process.pl) then asked
the server for a file that does not exist, so no files were downloaded,
renamed or deleted over FTP. SFTP and Local transports were unaffected.
This regressed when the EDI transport was moved onto the file_transports
system (bug 38489). The previous Koha::Edifact::Transport::ftp_download
listed files with Net::FTP->ls (NLST, bare names); the refactor switched
to Net::FTP->dir (LIST) so it could return the richer hashref shape used
by the SFTP and Local transports, but the parse was faulty.
The date field in "ls -l" output is a fixed run of three whitespace
separated tokens (Mon DD HH:MM or Mon DD YYYY). Consume it explicitly so
the filename is captured cleanly, preserving spaces and still skipping
directories. The LIST-based approach and the filename/longname/size/perms
return shape are kept so the structure stays consistent across transport
classes (relied on by consumers, including out-of-tree ones).
Test plan:
1) prove t/db_dependent/Koha/File/Transport/FTP.t
2) git checkout Koha/File/Transport/FTP.pm and re-run: the list_files()
subtest fails on the filename/size/perms assertions
3) prove t/db_dependent/Koha/Edifact/Transport.t
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
parent
27a4dd8daa
commit
fad48b3123
|
|
@ -158,34 +158,63 @@ Internal method that performs the FTP-specific file listing operation.
|
|||
Returns an array reference of hashrefs with file information.
|
||||
Each hashref contains: filename, longname, size, perms.
|
||||
|
||||
The listing is obtained with MLSD (RFC 3659), whose machine-readable output
|
||||
is locale- and platform-independent and gives us the filename unambiguously
|
||||
(the pathname is the entire text after the "facts" and a single space, so
|
||||
filenames containing spaces are handled correctly). Servers that do not
|
||||
support MLSD fall back to NLST, which returns bare filenames only.
|
||||
|
||||
This mirrors what Net::FTP does internally for rmdir(). Net::FTP 3.x exposes
|
||||
no public MLSD method, so we drive it through its _list_cmd() helper - the
|
||||
same private method that backs the public ls()/dir() and that Net::FTP's own
|
||||
rmdir() uses for MLSD. That interface has been stable for many releases.
|
||||
|
||||
The hashref shape is kept consistent with the SFTP and Local transports so
|
||||
that consumers of list_files() (including out-of-tree ones) can rely on the
|
||||
same structure regardless of transport type. Note that over MLSD the perms
|
||||
value is the RFC 3659 "perm" fact (e.g. "adfr"), not a Unix mode string, and
|
||||
size is only present when the server reports it.
|
||||
|
||||
=cut
|
||||
|
||||
sub _list_files {
|
||||
my ($self) = @_;
|
||||
my $operation = "list";
|
||||
|
||||
# Get detailed listing using dir() for consistency with SFTP format
|
||||
my $detailed_list = $self->{connection}->dir or return $self->_abort_operation($operation);
|
||||
|
||||
# Convert to hash format consistent with SFTP
|
||||
my @file_list;
|
||||
foreach my $line ( @{$detailed_list} ) {
|
||||
|
||||
# Parse FTP dir output (similar to ls -l format)
|
||||
# Example: "-rw-r--r-- 1 user group 1234 Jan 01 12:00 filename.txt"
|
||||
if ( $line =~ /^([d\-rwx]+)\s+\S+\s+\S+\s+\S+\s+(\d+)\s+(.+?)\s+(.+)$/ ) {
|
||||
my ( $perms, $size, $date_part, $filename ) = ( $1, $2, $3, $4 );
|
||||
# Prefer MLSD: machine-readable, no locale/format guessing. Net::FTP has
|
||||
# no public MLSD method, so use its internal _list_cmd() (as its own
|
||||
# rmdir() does). Returns an arrayref of "facts filename" lines, or undef.
|
||||
my $mlsd = $self->{connection}->_list_cmd("MLSD");
|
||||
|
||||
# Skip directories (start with 'd')
|
||||
next if $perms =~ /^d/;
|
||||
if ( $mlsd && @{$mlsd} ) {
|
||||
foreach my $line ( @{$mlsd} ) {
|
||||
|
||||
# Each line is "fact1=val;fact2=val;...; pathname"; the pathname is
|
||||
# everything after the facts and the single separating space.
|
||||
next unless $line =~ /^(.*?;)\s(.+)$/;
|
||||
my ( $facts_str, $filename ) = ( $1, $2 );
|
||||
|
||||
my %fact =
|
||||
map { my ( $k, $v ) = split /=/, $_, 2; ( lc($k) => $v ) }
|
||||
grep { length } split /;/, $facts_str;
|
||||
|
||||
# Skip the directory itself, its parent and any subdirectories
|
||||
next if ( $fact{type} // q{} ) =~ /^(?:dir|cdir|pdir)$/i;
|
||||
|
||||
push @file_list, {
|
||||
filename => $filename,
|
||||
longname => $line,
|
||||
size => $size,
|
||||
perms => $perms
|
||||
size => $fact{size},
|
||||
perms => $fact{perm}
|
||||
};
|
||||
}
|
||||
} else {
|
||||
|
||||
# Fallback for servers without MLSD: NLST returns bare filenames only.
|
||||
my $names = $self->{connection}->ls or return $self->_abort_operation($operation);
|
||||
@file_list = map { { filename => $_ } } grep { !/^[.]{1,2}$/ } @{$names};
|
||||
}
|
||||
|
||||
$self->add_message(
|
||||
|
|
@ -258,6 +287,16 @@ sub _is_connected {
|
|||
return $self->{connection} && $self->{connection}->pwd();
|
||||
}
|
||||
|
||||
=head3 _abort_operation
|
||||
|
||||
return $self->_abort_operation($operation);
|
||||
|
||||
Records an error message for the named operation, aborts any in-progress
|
||||
transfer on the current connection and returns nothing, so callers can
|
||||
C<return $self-E<gt>_abort_operation($operation)> on failure.
|
||||
|
||||
=cut
|
||||
|
||||
sub _abort_operation {
|
||||
my ( $self, $operation ) = @_;
|
||||
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@
|
|||
|
||||
use Modern::Perl;
|
||||
|
||||
use Test::More tests => 7;
|
||||
use Test::More tests => 8;
|
||||
use Test::Exception;
|
||||
use Test::NoWarnings;
|
||||
use Test::Warn;
|
||||
|
|
@ -135,8 +135,11 @@ subtest 'change_directory() tests' => sub {
|
|||
can_ok( $transport, 'change_directory' );
|
||||
};
|
||||
|
||||
subtest 'list_files() tests' => sub {
|
||||
plan tests => 1;
|
||||
subtest 'list_files() MLSD tests' => sub {
|
||||
plan tests => 6;
|
||||
|
||||
$schema->storage->txn_begin;
|
||||
|
||||
my $transport = $builder->build_object(
|
||||
{
|
||||
class => 'Koha::File::Transports',
|
||||
|
|
@ -145,6 +148,89 @@ subtest 'list_files() tests' => sub {
|
|||
);
|
||||
|
||||
can_ok( $transport, 'list_files' );
|
||||
|
||||
# MLSD output: "facts filename" per RFC 3659. Facts are locale-independent,
|
||||
# the filename is everything after the facts and a single space (so spaces
|
||||
# in filenames are handled), and cdir/pdir/dir entries must be skipped.
|
||||
my @mlsd_output = (
|
||||
'type=file;size=1234;modify=20250101120000;perm=adfr; QUOTES_413514.CEQ',
|
||||
'type=file;size=4096;modify=20250214093000; INVOICE_99.CEI',
|
||||
'type=dir;modify=20250303080000; subdir',
|
||||
'type=cdir;modify=20250303080000; .',
|
||||
'type=pdir;modify=20250303080000; ..',
|
||||
'type=file;size=55;modify=20250405091000; name with spaces.CEA',
|
||||
);
|
||||
|
||||
my $mock_ftp = Test::MockModule->new('Net::FTP');
|
||||
$mock_ftp->mock( '_list_cmd', sub { my ( $s, $cmd ) = @_; return $cmd eq 'MLSD' ? [@mlsd_output] : undef; } );
|
||||
$mock_ftp->mock( 'ls', sub { die 'MLSD succeeded, fallback ls() must not be called'; } );
|
||||
$mock_ftp->mock( 'pwd', sub { return '/incoming/'; } );
|
||||
|
||||
# Pretend we are already connected so list_files() skips (re)connection
|
||||
$transport->{connection} = bless {}, 'Net::FTP';
|
||||
$transport->{_user_set_directory} = 1;
|
||||
|
||||
my $files = $transport->list_files();
|
||||
|
||||
is( ref($files), 'ARRAY', 'list_files() returns an arrayref' );
|
||||
|
||||
my @names = sort map { $_->{filename} } @{$files};
|
||||
is( scalar @names, 3, 'directory, cdir and pdir entries are skipped' );
|
||||
|
||||
is_deeply(
|
||||
\@names,
|
||||
[ 'INVOICE_99.CEI', 'QUOTES_413514.CEQ', 'name with spaces.CEA' ],
|
||||
'filenames parsed from MLSD, spaces preserved'
|
||||
);
|
||||
|
||||
my ($quote) = grep { $_->{filename} eq 'QUOTES_413514.CEQ' } @{$files};
|
||||
is( $quote->{size}, 1234, 'size fact parsed correctly' );
|
||||
is( $quote->{perms}, 'adfr', 'perm fact captured' );
|
||||
|
||||
$transport->{connection} = undef;
|
||||
|
||||
$schema->storage->txn_rollback;
|
||||
};
|
||||
|
||||
subtest 'list_files() NLST fallback tests' => sub {
|
||||
plan tests => 4;
|
||||
|
||||
$schema->storage->txn_begin;
|
||||
|
||||
my $transport = $builder->build_object(
|
||||
{
|
||||
class => 'Koha::File::Transports',
|
||||
value => { transport => 'ftp', password => 'testpass' }
|
||||
}
|
||||
);
|
||||
|
||||
# Server without MLSD support: _list_cmd('MLSD') returns nothing, so
|
||||
# list_files() must fall back to NLST (ls) and return bare filenames.
|
||||
my $ls_called = 0;
|
||||
my $mock_ftp = Test::MockModule->new('Net::FTP');
|
||||
$mock_ftp->mock( '_list_cmd', sub { return; } );
|
||||
$mock_ftp->mock( 'ls', sub { $ls_called = 1; return [ 'QUOTES_1.CEQ', '.', '..', 'INVOICE_2.CEI' ]; } );
|
||||
$mock_ftp->mock( 'pwd', sub { return '/incoming/'; } );
|
||||
|
||||
$transport->{connection} = bless {}, 'Net::FTP';
|
||||
$transport->{_user_set_directory} = 1;
|
||||
|
||||
my $files = $transport->list_files();
|
||||
|
||||
is( $ls_called, 1, 'falls back to ls() when MLSD is unsupported' );
|
||||
|
||||
my @names = sort map { $_->{filename} } @{$files};
|
||||
is( scalar @names, 2, 'current/parent dir entries filtered from NLST output' );
|
||||
is_deeply(
|
||||
\@names,
|
||||
[ 'INVOICE_2.CEI', 'QUOTES_1.CEQ' ],
|
||||
'bare filenames returned from NLST fallback'
|
||||
);
|
||||
ok( ( exists $files->[0]{filename} ), 'fallback entries expose a filename key' );
|
||||
|
||||
$transport->{connection} = undef;
|
||||
|
||||
$schema->storage->txn_rollback;
|
||||
};
|
||||
|
||||
1;
|
||||
|
|
|
|||
Loading…
Reference in New Issue