]> sjero.net Git - wget/blob - tests/FTPServer.pm
NEWS: cite --start-pos
[wget] / tests / FTPServer.pm
1 # Part of this code was borrowed from Richard Jones's Net::FTPServer
2 # http://www.annexia.org/freeware/netftpserver
3
4 package FTPServer;
5
6 use strict;
7 use warnings;
8
9 use Cwd;
10 use Socket;
11 use IO::Socket::INET;
12 use IO::Seekable;
13 use POSIX qw(strftime);
14
15 my $log        = undef;
16 my $GOT_SIGURG = 0;
17
18 # CONSTANTS
19
20 # connection states
21 my %_connection_states = (
22     'NEWCONN'  => 0x01,
23     'WAIT4PWD' => 0x02,
24     'LOGGEDIN' => 0x04,
25     'TWOSOCKS' => 0x08,
26 );
27
28 # subset of FTP commands supported by these server and the respective
29 # connection states in which they are allowed
30 my %_commands = (
31     # Standard commands from RFC 959.
32     'CWD'  => $_connection_states{LOGGEDIN} |
33               $_connection_states{TWOSOCKS},
34 #   'EPRT' => $_connection_states{LOGGEDIN},
35 #   'EPSV' => $_connection_states{LOGGEDIN},
36     'LIST' => $_connection_states{TWOSOCKS},
37 #   'LPRT' => $_connection_states{LOGGEDIN},
38 #   'LPSV' => $_connection_states{LOGGEDIN},
39     'PASS' => $_connection_states{WAIT4PWD},
40     'PASV' => $_connection_states{LOGGEDIN},
41     'PORT' => $_connection_states{LOGGEDIN},
42     'PWD'  => $_connection_states{LOGGEDIN} |
43               $_connection_states{TWOSOCKS},
44     'QUIT' => $_connection_states{LOGGEDIN} |
45               $_connection_states{TWOSOCKS},
46     'REST' => $_connection_states{TWOSOCKS},
47     'RETR' => $_connection_states{TWOSOCKS},
48     'SYST' => $_connection_states{LOGGEDIN},
49     'TYPE' => $_connection_states{LOGGEDIN} |
50               $_connection_states{TWOSOCKS},
51     'USER' => $_connection_states{NEWCONN},
52     # From ftpexts Internet Draft.
53     'SIZE' => $_connection_states{LOGGEDIN} |
54               $_connection_states{TWOSOCKS},
55 );
56
57
58
59 # COMMAND-HANDLING ROUTINES
60
61 sub _CWD_command
62 {
63     my ($conn, $cmd, $path) = @_;
64     my $paths = $conn->{'paths'};
65
66     local $_;
67     my $new_path = FTPPaths::path_merge($conn->{'dir'}, $path);
68
69     # Split the path into its component parts and process each separately.
70     if (! $paths->dir_exists($new_path)) {
71         print {$conn->{socket}} "550 Directory not found.\r\n";
72         return;
73     }
74
75     $conn->{'dir'} = $new_path;
76     print {$conn->{socket}} "200 directory changed to $new_path.\r\n";
77 }
78
79 sub _LIST_command
80 {
81     my ($conn, $cmd, $path) = @_;
82     my $paths = $conn->{'paths'};
83
84     my $ReturnEmptyList = ( $paths->GetBehavior('list_empty_if_list_a') &&
85                             $path eq '-a');
86     my $SkipHiddenFiles = ( $paths->GetBehavior('list_no_hidden_if_list') &&
87                             ( ! $path ) );
88
89     if ($paths->GetBehavior('list_fails_if_list_a') && $path eq '-a')
90       {
91             print {$conn->{socket}} "500 Unknown command\r\n";
92             return;
93       }
94
95
96     if (!$paths->GetBehavior('list_dont_clean_path'))
97       {
98         # This is something of a hack. Some clients expect a Unix server
99         # to respond to flags on the 'ls command line'. Remove these flags
100         # and ignore them. This is particularly an issue with ncftp 2.4.3.
101         $path =~ s/^-[a-zA-Z0-9]+\s?//;
102       }
103
104     my $dir = $conn->{'dir'};
105
106     print STDERR "_LIST_command - dir is: $dir\n";
107
108     # Parse the first elements of the path until we find the appropriate
109     # working directory.
110     local $_;
111
112     my $listing;
113     if (!$ReturnEmptyList)
114       {
115         $dir = FTPPaths::path_merge($dir, $path);
116         $listing = $paths->get_list($dir,$SkipHiddenFiles);
117         unless ($listing) {
118             print {$conn->{socket}} "550 File or directory not found.\r\n";
119             return;
120         }
121       }
122
123     print STDERR "_LIST_command - dir is: $dir\n" if $log;
124
125     print {$conn->{socket}} "150 Opening data connection for file listing.\r\n";
126
127     # Open a path back to the client.
128     my $sock = __open_data_connection ($conn);
129     unless ($sock) {
130         print {$conn->{socket}} "425 Can't open data connection.\r\n";
131         return;
132     }
133
134     if (!$ReturnEmptyList)
135       {
136         for my $item (@$listing) {
137             print $sock "$item\r\n";
138         }
139       }
140
141     unless ($sock->close) {
142         print {$conn->{socket}} "550 Error closing data connection: $!\r\n";
143         return;
144     }
145
146     print {$conn->{socket}} "226 Listing complete. Data connection has been closed.\r\n";
147 }
148
149 sub _PASS_command
150 {
151     my ($conn, $cmd, $pass) = @_;
152
153     # TODO: implement authentication?
154
155     print STDERR "switching to LOGGEDIN state\n" if $log;
156     $conn->{state} = $_connection_states{LOGGEDIN};
157
158     if ($conn->{username} eq "anonymous") {
159         print {$conn->{socket}} "202 Anonymous user access is always granted.\r\n";
160     } else {
161         print {$conn->{socket}} "230 Authentication not implemented yet, access is always granted.\r\n";
162     }
163 }
164
165 sub _PASV_command
166 {
167     my ($conn, $cmd, $rest) = @_;
168
169     # Open a listening socket - but don't actually accept on it yet.
170     "0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
171     my $sock = IO::Socket::INET->new (LocalHost => '127.0.0.1',
172                                       LocalPort => '0',
173                                       Listen => 1,
174                                       Reuse => 1,
175                                       Proto => 'tcp',
176                                       Type => SOCK_STREAM);
177
178     unless ($sock) {
179         # Return a code 550 here, even though this is not in the RFC. XXX
180         print {$conn->{socket}} "550 Can't open a listening socket.\r\n";
181         return;
182     }
183
184     $conn->{passive} = 1;
185     $conn->{passive_socket} = $sock;
186
187     # Get our port number.
188     my $sockport = $sock->sockport;
189
190     # Split the port number into high and low components.
191     my $p1 = int ($sockport / 256);
192     my $p2 = $sockport % 256;
193
194     $conn->{state} = $_connection_states{TWOSOCKS};
195
196     # We only accept connections from localhost.
197     print {$conn->{socket}} "227 Entering Passive Mode (127,0,0,1,$p1,$p2)\r\n";
198 }
199
200 sub _PORT_command
201 {
202     my ($conn, $cmd, $rest) = @_;
203
204     # The arguments to PORT are a1,a2,a3,a4,p1,p2 where a1 is the
205     # most significant part of the address (eg. 127,0,0,1) and
206     # p1 is the most significant part of the port.
207     unless ($rest =~ /^\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})/) {
208         print {$conn->{socket}} "501 Syntax error in PORT command.\r\n";
209         return;
210     }
211
212     # Check host address.
213     unless ($1  > 0 && $1 < 224 &&
214             $2 >= 0 && $2 < 256 &&
215             $3 >= 0 && $3 < 256 &&
216             $4 >= 0 && $4 < 256) {
217         print {$conn->{socket}} "501 Invalid host address.\r\n";
218         return;
219     }
220
221     # Construct host address and port number.
222     my $peeraddrstring = "$1.$2.$3.$4";
223     my $peerport = $5 * 256 + $6;
224
225     # Check port number.
226     unless ($peerport > 0 && $peerport < 65536) {
227         print {$conn->{socket}} "501 Invalid port number.\r\n";
228     }
229
230     $conn->{peeraddrstring} = $peeraddrstring;
231     $conn->{peeraddr} = inet_aton ($peeraddrstring);
232     $conn->{peerport} = $peerport;
233     $conn->{passive} = 0;
234
235     $conn->{state} = $_connection_states{TWOSOCKS};
236
237     print {$conn->{socket}} "200 PORT command OK.\r\n";
238 }
239
240 sub _PWD_command
241 {
242     my ($conn, $cmd, $rest) = @_;
243
244     # See RFC 959 Appendix II and draft-ietf-ftpext-mlst-11.txt section 6.2.1.
245     my $pathname = $conn->{dir};
246     $pathname =~ s,/+$,, unless $pathname eq "/";
247     $pathname =~ tr,/,/,s;
248
249     print {$conn->{socket}} "257 \"$pathname\"\r\n";
250 }
251
252 sub _REST_command
253 {
254     my ($conn, $cmd, $restart_from) = @_;
255
256     unless ($restart_from =~ /^([1-9][0-9]*|0)$/) {
257         print {$conn->{socket}} "501 REST command needs a numeric argument.\r\n";
258         return;
259     }
260
261     $conn->{restart} = $1;
262
263     print {$conn->{socket}} "350 Restarting next transfer at $1.\r\n";
264 }
265
266 sub _RETR_command
267 {
268     my ($conn, $cmd, $path) = @_;
269
270     $path = FTPPaths::path_merge($conn->{dir}, $path);
271     my $info = $conn->{'paths'}->get_info($path);
272
273     unless ($info->{'_type'} eq 'f') {
274         print {$conn->{socket}} "550 File not found.\r\n";
275         return;
276     }
277
278     print {$conn->{socket}} "150 Opening " .
279         ($conn->{type} eq 'A' ? "ASCII mode" : "BINARY mode") .
280         " data connection.\r\n";
281
282     # Open a path back to the client.
283     my $sock = __open_data_connection ($conn);
284
285     unless ($sock) {
286         print {$conn->{socket}} "425 Can't open data connection.\r\n";
287         return;
288     }
289
290     my $content = $info->{'content'};
291
292     # Restart the connection from previous point?
293     if ($conn->{restart}) {
294         $content = substr($content, $conn->{restart});
295         $conn->{restart} = 0;
296     }
297
298     # What mode are we sending this file in?
299     unless ($conn->{type} eq 'A') # Binary type.
300     {
301         my ($r, $buffer, $n, $w, $sent);
302
303         # Copy data.
304         $sent = 0;
305         while ($sent < length($content))
306         {
307             $buffer = substr($content, $sent, 65536);
308             $r = length $buffer;
309
310             # Restart alarm clock timer.
311             alarm $conn->{idle_timeout};
312
313             for ($n = 0; $n < $r; )
314             {
315                 $w = syswrite ($sock, $buffer, $r - $n, $n);
316
317                 # Cleanup and exit if there was an error.
318                 unless (defined $w) {
319                     close $sock;
320                     print {$conn->{socket}} "426 File retrieval error: $!. Data connection has been closed.\r\n";
321                     return;
322                 }
323
324                 $n += $w;
325             }
326
327             # Transfer aborted by client?
328             if ($GOT_SIGURG) {
329                 $GOT_SIGURG = 0;
330                 close $sock;
331                 print {$conn->{socket}} "426 Transfer aborted. Data connection closed.\r\n";
332                 return;
333             }
334             $sent += $r;
335         }
336
337         # Cleanup and exit if there was an error.
338         unless (defined $r) {
339             close $sock;
340             print {$conn->{socket}} "426 File retrieval error: $!. Data connection has been closed.\r\n";
341             return;
342         }
343     } else { # ASCII type.
344         # Copy data.
345         my @lines = split /\r\n?|\n/, $content;
346         for (@lines) {
347             # Remove any native line endings.
348             s/[\n\r]+$//;
349
350             # Restart alarm clock timer.
351             alarm $conn->{idle_timeout};
352
353             # Write the line with telnet-format line endings.
354             print $sock "$_\r\n";
355
356             # Transfer aborted by client?
357             if ($GOT_SIGURG) {
358                 $GOT_SIGURG = 0;
359                 close $sock;
360                 print {$conn->{socket}} "426 Transfer aborted. Data connection closed.\r\n";
361                 return;
362             }
363         }
364     }
365
366     unless (close ($sock)) {
367         print {$conn->{socket}} "550 File retrieval error: $!.\r\n";
368         return;
369     }
370
371     print {$conn->{socket}} "226 File retrieval complete. Data connection has been closed.\r\n";
372 }
373
374 sub _SIZE_command
375 {
376     my ($conn, $cmd, $path) = @_;
377
378     $path = FTPPaths::path_merge($conn->{dir}, $path);
379     my $info = $conn->{'paths'}->get_info($path);
380     unless ($info) {
381         print {$conn->{socket}} "550 File or directory not found.\r\n";
382         return;
383     }
384
385     if ($info->{'_type'} eq 'd') {
386         print {$conn->{socket}} "550 SIZE command is not supported on directories.\r\n";
387         return;
388     }
389
390     my $size = length $info->{'content'};
391
392     print {$conn->{socket}} "213 $size\r\n";
393 }
394
395 sub _SYST_command
396 {
397     my ($conn, $cmd, $dummy) = @_;
398
399     if ($conn->{'paths'}->GetBehavior('syst_response'))
400       {
401         print {$conn->{socket}} $conn->{'paths'}->GetBehavior('syst_response') . "\r\n";
402       }
403     else
404       {
405         print {$conn->{socket}} "215 UNIX Type: L8\r\n";
406       }
407 }
408
409 sub _TYPE_command
410 {
411     my ($conn, $cmd, $type) = @_;
412
413     # See RFC 959 section 5.3.2.
414     if ($type =~ /^([AI])$/i) {
415         $conn->{type} = $1;
416     } elsif ($type =~ /^([AI])\sN$/i) {
417         $conn->{type} = $1;
418     } elsif ($type =~ /^L\s8$/i) {
419         $conn->{type} = 'L8';
420     } else {
421         print {$conn->{socket}} "504 This server does not support TYPE $type.\r\n";
422         return;
423     }
424
425     print {$conn->{socket}} "200 TYPE changed to $type.\r\n";
426 }
427
428 sub _USER_command
429 {
430     my ($conn, $cmd, $username) = @_;
431
432     print STDERR "username: $username\n" if $log;
433     $conn->{username} = $username;
434
435     print STDERR "switching to WAIT4PWD state\n" if $log;
436     $conn->{state} = $_connection_states{WAIT4PWD};
437
438     if ($conn->{username} eq "anonymous") {
439         print {$conn->{socket}} "230 Anonymous user access granted.\r\n";
440     } else {
441         print {$conn->{socket}} "331 Password required.\r\n";
442     }
443 }
444
445
446 # HELPER ROUTINES
447
448 sub __open_data_connection
449 {
450     my $conn = shift;
451
452     my $sock;
453
454     if ($conn->{passive}) {
455         # Passive mode - wait for a connection from the client.
456         accept ($sock, $conn->{passive_socket}) or return undef;
457     } else {
458         # Active mode - connect back to the client.
459         "0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
460         $sock = IO::Socket::INET->new (LocalAddr => '127.0.0.1',
461                                        PeerAddr => $conn->{peeraddrstring},
462                                        PeerPort => $conn->{peerport},
463                                        Proto => 'tcp',
464                                        Type => SOCK_STREAM) or return undef;
465     }
466
467     return $sock;
468 }
469
470
471 ###########################################################################
472 # FTPSERVER CLASS
473 ###########################################################################
474
475 {
476     my %_attr_data = ( # DEFAULT
477         _input           => undef,
478         _localAddr       => 'localhost',
479         _localPort       => undef,
480         _reuseAddr       => 1,
481         _rootDir         => Cwd::getcwd(),
482         _server_behavior => {},
483     );
484
485     sub _default_for
486     {
487         my ($self, $attr) = @_;
488         $_attr_data{$attr};
489     }
490
491     sub _standard_keys
492     {
493         keys %_attr_data;
494     }
495 }
496
497
498 sub new {
499     my ($caller, %args) = @_;
500     my $caller_is_obj = ref($caller);
501     my $class = $caller_is_obj || $caller;
502     my $self = bless {}, $class;
503     foreach my $attrname ($self->_standard_keys()) {
504         my ($argname) = ($attrname =~ /^_(.*)/);
505         if (exists $args{$argname}) {
506             $self->{$attrname} = $args{$argname};
507         } elsif ($caller_is_obj) {
508             $self->{$attrname} = $caller->{$attrname};
509         } else {
510             $self->{$attrname} = $self->_default_for($attrname);
511         }
512     }
513     # create server socket
514     "0" =~ /(0)/; # Perl 5.7 / IO::Socket::INET bug workaround.
515     $self->{_server_sock}
516                     = IO::Socket::INET->new (LocalHost => $self->{_localAddr},
517                                              LocalPort => $self->{_localPort},
518                                              Listen => 1,
519                                              Reuse => $self->{_reuseAddr},
520                                              Proto => 'tcp',
521                                              Type => SOCK_STREAM)
522                                         or die "bind: $!";
523
524     foreach my $file (keys %{$self->{_input}}) {
525         my $ref = \$self->{_input}{$file}{content};
526         $$ref =~ s/{{port}}/$self->sockport/eg;
527     }
528
529     return $self;
530 }
531
532
533 sub run
534 {
535     my ($self, $synch_callback) = @_;
536     my $initialized = 0;
537
538     # turn buffering off on STDERR
539     select((select(STDERR), $|=1)[0]);
540
541     # initialize command table
542     my $command_table = {};
543     foreach (keys %_commands) {
544         my $subname = "_${_}_command";
545         $command_table->{$_} = \&$subname;
546     }
547
548     my $old_ils = $/;
549     $/ = "\r\n";
550
551     if (!$initialized) {
552         $synch_callback->();
553         $initialized = 1;
554     }
555
556     $SIG{CHLD} = sub { wait };
557     my $server_sock = $self->{_server_sock};
558
559     # the accept loop
560     while (my $client_addr = accept (my $socket, $server_sock))
561     {
562         # turn buffering off on $socket
563         select((select($socket), $|=1)[0]);
564
565         # find out who connected
566         my ($client_port, $client_ip) = sockaddr_in ($client_addr);
567         my $client_ipnum = inet_ntoa ($client_ip);
568
569         # print who connected
570         print STDERR "got a connection from: $client_ipnum\n" if $log;
571
572         # fork off a process to handle this connection.
573         # my $pid = fork();
574         # unless (defined $pid) {
575         #     warn "fork: $!";
576         #     sleep 5; # Back off in case system is overloaded.
577         #     next;
578         # }
579
580         if (1) { # Child process.
581
582             # install signals
583             $SIG{URG}  = sub {
584                 $GOT_SIGURG  = 1;
585             };
586
587             $SIG{PIPE} = sub {
588                 print STDERR "Client closed connection abruptly.\n";
589                 exit;
590             };
591
592             $SIG{ALRM} = sub {
593                 print STDERR "Connection idle timeout expired. Closing server.\n";
594                 exit;
595             };
596
597             #$SIG{CHLD} = 'IGNORE';
598
599
600             print STDERR "in child\n" if $log;
601
602             my $conn = {
603                 'paths'           => FTPPaths->new($self->{'_input'},
604                                         $self->{'_server_behavior'}),
605                 'socket'          => $socket,
606                 'state'           => $_connection_states{NEWCONN},
607                 'dir'             => '/',
608                 'restart'         => 0,
609                 'idle_timeout'    => 60, # 1 minute timeout
610                 'rootdir'         => $self->{_rootDir},
611             };
612
613             print {$conn->{socket}} "220 GNU Wget Testing FTP Server ready.\r\n";
614
615             # command handling loop
616             for (;;) {
617                 print STDERR "waiting for request\n" if $log;
618
619                 last unless defined (my $req = <$socket>);
620
621                 # Remove trailing CRLF.
622                 $req =~ s/[\n\r]+$//;
623
624                 print STDERR "received request $req\n" if $log;
625
626                 # Get the command.
627                 # See also RFC 2640 section 3.1.
628                 unless ($req =~ m/^([A-Z]{3,4})\s?(.*)/i) {
629                     # badly formed command
630                     exit 0;
631                 }
632
633                 # The following strange 'eval' is necessary to work around a
634                 # very odd bug in Perl 5.6.0. The following assignment to
635                 # $cmd will fail in some cases unless you use $1 in some sort
636                 # of an expression beforehand.
637                 # - RWMJ 2002-07-05.
638                 eval '$1 eq $1';
639
640                 my ($cmd, $rest) = (uc $1, $2);
641
642                 # Got a command which matches in the table?
643                 unless (exists $command_table->{$cmd}) {
644                     print {$conn->{socket}} "500 Unrecognized command.\r\n";
645                     next;
646                 }
647
648                 # Command requires user to be authenticated?
649                 unless ($_commands{$cmd} | $conn->{state}) {
650                     print {$conn->{socket}} "530 Not logged in.\r\n";
651                     next;
652                 }
653
654                 # Handle the QUIT command specially.
655                 if ($cmd eq "QUIT") {
656                     print {$conn->{socket}} "221 Goodbye. Service closing connection.\r\n";
657                     last;
658                 }
659
660                 if (defined ($self->{_server_behavior}{fail_on_pasv})
661                         && $cmd eq 'PASV') {
662                     undef $self->{_server_behavior}{fail_on_pasv};
663                     close $socket;
664                     last;
665                 }
666
667                 # Run the command.
668                 &{$command_table->{$cmd}} ($conn, $cmd, $rest);
669             }
670         } else { # Father
671             close $socket;
672         }
673     }
674
675     $/ = $old_ils;
676 }
677
678 sub sockport {
679     my $self = shift;
680     return $self->{_server_sock}->sockport;
681 }
682
683
684 package FTPPaths;
685
686 use POSIX qw(strftime);
687
688 # not a method
689 sub final_component {
690     my $path = shift;
691
692     $path =~ s|.*/||;
693     return $path;
694 }
695
696 # not a method
697 sub path_merge {
698     my ($a, $b) = @_;
699
700     return $a unless $b;
701
702     if ($b =~ m.^/.) {
703         $a = '';
704         $b =~ s.^/..;
705     }
706     $a =~ s./$..;
707
708     my @components = split('/', $b);
709
710     foreach my $c (@components) {
711         if ($c =~ /^\.?$/) {
712             next;
713         } elsif ($c eq '..') {
714             next if $a eq '';
715             $a =~ s|/[^/]*$||;
716         } else {
717             $a .= "/$c";
718         }
719     }
720
721     return $a;
722 }
723
724 sub new {
725     my ($this, @args) = @_;
726     my $class = ref($this) || $this;
727     my $self = {};
728     bless $self, $class;
729     $self->initialize(@args);
730     return $self;
731 }
732
733 sub initialize {
734     my ($self, $urls, $behavior) = @_;
735     my $paths = {_type => 'd'};
736
737     # From a path like '/foo/bar/baz.txt', construct $paths such that
738     # $paths->{'foo'}->{'bar'}->{'baz.txt'} is
739     # $urls->{'/foo/bar/baz.txt'}.
740     for my $path (keys %$urls) {
741         my @components = split('/', $path);
742         shift @components;
743         my $x = $paths;
744         for my $c (@components) {
745             unless (exists $x->{$c}) {
746                 $x->{$c} = {_type => 'd'};
747             }
748             $x = $x->{$c};
749         }
750         %$x = %{$urls->{$path}};
751         $x->{_type} = 'f';
752     }
753
754     $self->{'_paths'} = $paths;
755     $self->{'_behavior'} = $behavior;
756 }
757
758 sub get_info {
759     my ($self, $path, $node) = @_;
760     $node = $self->{'_paths'} unless $node;
761     my @components = split('/', $path);
762     shift @components if @components && $components[0] eq '';
763
764     for my $c (@components) {
765         if ($node->{'_type'} eq 'd') {
766             $node = $node->{$c};
767         } else {
768             return undef;
769         }
770     }
771     return $node;
772 }
773
774 sub dir_exists {
775     my ($self, $path) = @_;
776     return $self->exists($path, 'd');
777 }
778
779 sub exists {
780     # type is optional, in which case we don't check it.
781     my ($self, $path, $type) = @_;
782     my $paths = $self->{'_paths'};
783
784     die "Invalid path $path (not absolute).\n" unless $path =~ m.^/.;
785     my $info = $self->get_info($path);
786     return 0 unless defined($info);
787     return $info->{'_type'} eq $type if defined($type);
788     return 1;
789 }
790
791 sub _format_for_list {
792     my ($self, $name, $info) = @_;
793
794     # XXX: mode should be specifyable as part of the node info.
795     my $mode_str;
796     if ($info->{'_type'} eq 'd') {
797         $mode_str = 'dr-xr-xr-x';
798     } else {
799         $mode_str = '-r--r--r--';
800     }
801
802     my $size = 0;
803     if ($info->{'_type'} eq 'f') {
804         $size = length  $info->{'content'};
805         if ($self->{'_behavior'}{'bad_list'}) {
806             $size = 0;
807         }
808     }
809     my $date = strftime ("%b %e %H:%M", localtime);
810     return "$mode_str 1  0  0  $size $date $name";
811 }
812
813 sub get_list {
814     my ($self, $path, $no_hidden) = @_;
815     my $info = $self->get_info($path);
816     return undef unless defined $info;
817     my $list = [];
818
819     if ($info->{'_type'} eq 'd') {
820         for my $item (keys %$info) {
821             next if $item =~ /^_/;
822             # 2013-10-17 Andrea Urbani (matfanjol)
823             #            I skip the hidden files if requested
824             if (($no_hidden) &&
825                 (defined($info->{$item}->{'attr'})) &&
826                 (index($info->{$item}->{'attr'}, "H")>=0))
827               {
828                 # This is an hidden file and I don't want to see it!
829                 print STDERR "get_list: Skipped hidden file [$item]\n";
830               }
831             else
832               {
833                 push @$list, $self->_format_for_list($item, $info->{$item});
834               }
835         }
836     } else {
837         push @$list, $self->_format_for_list(final_component($path), $info);
838     }
839
840     return $list;
841 }
842
843 # 2013-10-17 Andrea Urbani (matfanjol)
844 # It returns the behavior of the given name.
845 # In this file I handle also the following behaviors:
846 #  list_dont_clean_path  : if defined, the command
847 #                           $path =~ s/^-[a-zA-Z0-9]+\s?//;
848 #                          is not runt and the given path
849 #                          remains the original one
850 #  list_empty_if_list_a  : if defined, "LIST -a" returns an
851 #                          empty content
852 #  list_fails_if_list_a  : if defined, "LIST -a" returns an
853 #                          error
854 #  list_no_hidden_if_list: if defined, "LIST" doesn't return
855 #                          hidden files.
856 #                          To define an hidden file add
857 #                            attr => "H"
858 #                          to the url files
859 #  syst_response         : if defined, its content is printed
860 #                          out as SYST response
861 sub GetBehavior {
862   my ($self, $name) = @_;
863   return $self->{'_behavior'}{$name};
864 }
865
866 1;
867
868 # vim: et ts=4 sw=4