home *** CD-ROM | disk | FTP | other *** search
/ Netrunner 2004 October / NETRUNNER0410.ISO / regular / ActivePerl-5.8.4.810-MSWin32-x86.msi / _8aa80c60ee4e78fea3f415b17dae4ec0 < prev    next >
Encoding:
Text File  |  2004-06-01  |  10.0 KB  |  412 lines

  1. package File::Listing;
  2.  
  3. # $Id: Listing.pm,v 1.15 2003/10/26 14:24:22 gisle Exp $
  4.  
  5. sub Version { $VERSION; }
  6. $VERSION = sprintf("%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/);
  7.  
  8. require Exporter;
  9. @ISA = qw(Exporter);
  10. @EXPORT = qw(parse_dir);
  11.  
  12. use strict;
  13.  
  14. use Carp ();
  15. use HTTP::Date qw(str2time);
  16.  
  17.  
  18.  
  19. sub parse_dir ($;$$$)
  20. {
  21.    my($dir, $tz, $fstype, $error) = @_;
  22.  
  23.    $fstype ||= 'unix';
  24.    $fstype = "File::Listing::" . lc $fstype;
  25.  
  26.    my @args = $_[0];
  27.    push(@args, $tz) if(@_ >= 2);
  28.    push(@args, $error) if(@_ >= 4);
  29.  
  30.    $fstype->parse(@args);
  31. }
  32.  
  33.  
  34. sub line { Carp::croak("Not implemented yet"); }
  35. sub init { } # Dummy sub
  36.  
  37.  
  38. sub file_mode ($)
  39. {
  40.     # This routine was originally borrowed from Graham Barr's
  41.     # Net::FTP package.
  42.  
  43.     local $_ = shift;
  44.     my $mode = 0;
  45.     my($type,$ch);
  46.  
  47.     s/^(.)// and $type = $1;
  48.  
  49.     while (/(.)/g) {
  50.     $mode <<= 1;
  51.     $mode |= 1 if $1 ne "-" &&
  52.               $1 ne 'S' &&
  53.               $1 ne 't' &&
  54.               $1 ne 'T';
  55.     }
  56.  
  57.     $type eq "d" and $mode |= 0040000 or    # Directory
  58.       $type eq "l" and $mode |= 0120000 or    # Symbolic Link
  59.     $mode |= 0100000;            # Regular File
  60.  
  61.     $mode |= 0004000 if /^...s....../i;
  62.     $mode |= 0002000 if /^......s.../i;
  63.     $mode |= 0001000 if /^.........t/i;
  64.  
  65.     $mode;
  66. }
  67.  
  68.  
  69. sub parse
  70. {
  71.    my($pkg, $dir, $tz, $error) = @_;
  72.  
  73.    # First let's try to determine what kind of dir parameter we have
  74.    # received.  We allow both listings, reference to arrays and
  75.    # file handles to read from.
  76.  
  77.    if (ref($dir) eq 'ARRAY') {
  78.        # Already splitted up
  79.    }
  80.    elsif (ref($dir) eq 'GLOB') {
  81.        # A file handle
  82.    }
  83.    elsif (ref($dir)) {
  84.       Carp::croak("Illegal argument to parse_dir()");
  85.    }
  86.    elsif ($dir =~ /^\*\w+(::\w+)+$/) {
  87.       # This scalar looks like a file handle, so we assume it is
  88.    }
  89.    else {
  90.       # A normal scalar listing
  91.       $dir = [ split(/\n/, $dir) ];
  92.    }
  93.  
  94.    $pkg->init();
  95.  
  96.    my @files = ();
  97.    if (ref($dir) eq 'ARRAY') {
  98.        for (@$dir) {
  99.        push(@files, $pkg->line($_, $tz, $error));
  100.        }
  101.    }
  102.    else {
  103.        local($_);
  104.        while (<$dir>) {
  105.        chomp;
  106.        push(@files, $pkg->line($_, $tz, $error));
  107.        }
  108.    }
  109.    wantarray ? @files : \@files;
  110. }
  111.  
  112.  
  113.  
  114. package File::Listing::unix;
  115.  
  116. use HTTP::Date qw(str2time);
  117.  
  118. # A place to remember current directory from last line parsed.
  119. use vars qw($curdir);
  120. no strict qw(vars);
  121.  
  122. @ISA = qw(File::Listing);
  123.  
  124.  
  125.  
  126. sub init
  127. {
  128.     $curdir = '';
  129. }
  130.  
  131.  
  132. sub line
  133. {
  134.     shift; # package name
  135.     local($_) = shift;
  136.     my($tz, $error) = @_;
  137.  
  138.     s/\015//g;
  139.     #study;
  140.  
  141.     my ($kind, $size, $date, $name);
  142.     if (($kind, $size, $date, $name) =
  143.     /^([\-FlrwxsStTdD]{10})                   # Type and permission bits
  144.      .*                                       # Graps
  145.      \D(\d+)                                  # File size
  146.      \s+                                      # Some space
  147.      (\w{3}\s+\d+\s+(?:\d{1,2}:\d{2}|\d{4}))  # Date
  148.      \s+                                      # Some more space
  149.      (.*)$                                    # File name
  150.     /x )
  151.  
  152.     {
  153.     return if $name eq '.' || $name eq '..';
  154.     $name = "$curdir/$name" if length $curdir;
  155.     my $type = '?';
  156.     if ($kind =~ /^l/ && $name =~ /(.*) -> (.*)/ ) {
  157.         $name = $1;
  158.         $type = "l $2";
  159.     }
  160.     elsif ($kind =~ /^[\-F]/) { # (hopefully) a regular file
  161.         $type = 'f';
  162.     }
  163.     elsif ($kind =~ /^[dD]/) {
  164.         $type = 'd';
  165.         $size = undef;  # Don't believe the reported size
  166.     }
  167.     return [$name, $type, $size, str2time($date, $tz), 
  168.               File::Listing::file_mode($kind)];
  169.  
  170.     }
  171.     elsif (/^(.+):$/ && !/^[dcbsp].*\s.*\s.*:$/ ) {
  172.     my $dir = $1;
  173.     return () if $dir eq '.';
  174.     $curdir = $dir;
  175.     return ();
  176.     }
  177.     elsif (/^[Tt]otal\s+(\d+)$/ || /^\s*$/) {
  178.     return ();
  179.     }
  180.     elsif (/not found/    || # OSF1, HPUX, and SunOS return
  181.              # "$file not found"
  182.              /No such file/ || # IRIX returns
  183.              # "UX:ls: ERROR: Cannot access $file: No such file or directory"
  184.                                # Solaris returns
  185.              # "$file: No such file or directory"
  186.              /cannot find/     # Windows NT returns
  187.              # "The system cannot find the path specified."
  188.              ) {
  189.     return () unless defined $error;
  190.     &$error($_) if ref($error) eq 'CODE';
  191.     warn "Error: $_\n" if $error eq 'warn';
  192.     return ();
  193.     }
  194.     elsif ($_ eq '') {       # AIX, and Linux return nothing
  195.     return () unless defined $error;
  196.     &$error("No such file or directory") if ref($error) eq 'CODE';
  197.     warn "Warning: No such file or directory\n" if $error eq 'warn';
  198.     return ();
  199.     }
  200.     else {
  201.         # parse failed, check if the dosftp parse understands it
  202.         return(File::Listing::dosftp->line($_,$tz,$error));
  203.     }
  204.  
  205. }
  206.  
  207.  
  208.  
  209. package File::Listing::dosftp;
  210.  
  211. use HTTP::Date qw(str2time);
  212.  
  213. # A place to remember current directory from last line parsed.
  214. use vars qw($curdir);
  215. no strict qw(vars);
  216.  
  217. @ISA = qw(File::Listing);
  218.  
  219.  
  220.  
  221. sub init
  222. {
  223.     $curdir = '';
  224. }
  225.  
  226.  
  227. sub line
  228. {
  229.     shift; # package name
  230.     local($_) = shift;
  231.     my($tz, $error) = @_;
  232.  
  233.     s/\015//g;
  234.  
  235.     my ($kind, $size, $date, $name);
  236.  
  237.     # 02-05-96  10:48AM                 1415 src.slf
  238.     # 09-10-96  09:18AM       <DIR>          sl_util
  239.     if (($date,$size_or_dir,$name) =
  240.         /^(\d\d-\d\d-\d\d\s+\d\d:\d\d\wM)         # Date and time info
  241.          \s+                                      # Some space
  242.          (<\w{3}>|\d+)                            # Dir or Size
  243.          \s+                                      # Some more space
  244.          (.+)$                                    # File name
  245.         /x )
  246.     {
  247.     return if $name eq '.' || $name eq '..';
  248.     $name = "$curdir/$name" if length $curdir;
  249.     my $type = '?';
  250.     if ($size_or_dir eq '<DIR>') {
  251.         $type = "d";
  252.             $size = ""; # directories have no size in the pc listing
  253.         }
  254.         else {
  255.         $type = 'f';
  256.             $size = $size_or_dir;
  257.     }
  258.     return [$name, $type, $size, str2time($date, $tz),
  259.               File::Listing::file_mode($kind)];
  260.  
  261.     }
  262.     else {
  263.     return () unless defined $error;
  264.     &$error($_) if ref($error) eq 'CODE';
  265.     warn "Can't parse: $_\n" if $error eq 'warn';
  266.     return ();
  267.     }
  268.  
  269. }
  270.  
  271.  
  272.  
  273. package File::Listing::vms;
  274. @File::Listing::vms::ISA = qw(File::Listing);
  275.  
  276. package File::Listing::netware;
  277. @File::Listing::netware::ISA = qw(File::Listing);
  278.  
  279.  
  280.  
  281. package File::Listing::apache;
  282.  
  283. @ISA = qw(File::Listing);
  284.  
  285.  
  286. sub init { }
  287.  
  288.  
  289. sub line {
  290.     shift; # package name
  291.     local($_) = shift;
  292.     my($tz, $error) = @_; # ignored for now...
  293.  
  294.     if (m!<A\s+HREF=\"([^\"]+)\">.*</A>.*?(\d+)-([a-zA-Z]+)-(\d+)\s+(\d+):(\d+)\s+(?:([\d\.]+[kM]?|-))!i) {
  295.     my($filename, $filesize) = ($1, $7);
  296.     my($d,$m,$y, $H,$M) = ($2,$3,$4,$5,$6);
  297.  
  298.     $filesize = 0 if $filesize eq '-';
  299.     if ($filesize =~ s/k$//i) {
  300.         $filesize *= 1024;
  301.     }
  302.     elsif ($filesize =~ s/M$//) {
  303.         $filesize *= 1024*1024;
  304.     }
  305.     elsif ($filesize =~ s/G$//) {
  306.         $filesize *= 1024*1024*1024;
  307.     }
  308.     $filesize = int $filesize;
  309.  
  310.     require Time::Local;
  311.     my $filetime = Time::Local::timelocal(0,$M,$H,$d,_monthabbrev_number($m)-1,_guess_year($y)-1900);
  312.     my $filetype = ($filename =~ s|/$|| ? "d" : "f");
  313.     return [$filename, $filetype, $filesize, $filetime, undef];
  314.     }
  315.  
  316.     return ();
  317. }
  318.  
  319.  
  320. sub _guess_year {
  321.     my $y = shift;
  322.     if ($y >= 90) {
  323.     $y = 1900+$y;
  324.     }
  325.     elsif ($y < 100) {
  326.     $y = 2000+$y;
  327.     }
  328.     $y;
  329. }
  330.  
  331.  
  332. sub _monthabbrev_number {
  333.     my $mon = shift;
  334.     +{'Jan' => 1,
  335.       'Feb' => 2,
  336.       'Mar' => 3,
  337.       'Apr' => 4,
  338.       'May' => 5,
  339.       'Jun' => 6,
  340.       'Jul' => 7,
  341.       'Aug' => 8,
  342.       'Sep' => 9,
  343.       'Oct' => 10,
  344.       'Nov' => 11,
  345.       'Dec' => 12,
  346.      }->{$mon};
  347. }
  348.  
  349.  
  350. 1;
  351.  
  352. __END__
  353.  
  354. =head1 NAME
  355.  
  356. File::Listing - parse directory listing
  357.  
  358. =head1 SYNOPSIS
  359.  
  360.  use File::Listing qw(parse_dir);
  361.  for (parse_dir(`ls -l`)) {
  362.      ($name, $type, $size, $mtime, $mode) = @$_;
  363.      next if $type ne 'f'; # plain file
  364.      #...
  365.  }
  366.  
  367.  # directory listing can also be read from a file
  368.  open(LISTING, "zcat ls-lR.gz|");
  369.  $dir = parse_dir(\*LISTING, '+0000');
  370.  
  371. =head1 DESCRIPTION
  372.  
  373. This module exports a single function called parse_dir(), which can be
  374. used to parse directory listings. Currently it only understand Unix
  375. C<'ls -l'> and C<'ls -lR'> format.  It should eventually be able to
  376. most things you might get back from a ftp server file listing (LIST
  377. command), i.e. VMS listings, NT listings, DOS listings,...
  378.  
  379. The first parameter to parse_dir() is the directory listing to parse.
  380. It can be a scalar, a reference to an array of directory lines or a
  381. glob representing a filehandle to read the directory listing from.
  382.  
  383. The second parameter is the time zone to use when parsing time stamps
  384. in the listing. If this value is undefined, then the local time zone is
  385. assumed.
  386.  
  387. The third parameter is the type of listing to assume.  The values will
  388. be strings like 'unix', 'vms', 'dos'.  Currently only 'unix' is
  389. implemented and this is also the default value.  Ideally, the listing
  390. type should be determined automatically.
  391.  
  392. The fourth parameter specifies how unparseable lines should be treated.
  393. Values can be 'ignore', 'warn' or a code reference.  Warn means that
  394. the perl warn() function will be called.  If a code reference is
  395. passed, then this routine will be called and the return value from it
  396. will be incorporated in the listing.  The default is 'ignore'.
  397.  
  398. Only the first parameter is mandatory.
  399.  
  400. The return value from parse_dir() is a list of directory entries.  In
  401. a scalar context the return value is a reference to the list.  The
  402. directory entries are represented by an array consisting of [
  403. $filename, $filetype, $filesize, $filetime, $filemode ].  The
  404. $filetype value is one of the letters 'f', 'd', 'l' or '?'.  The
  405. $filetime value is the seconds since Jan 1, 1970.  The
  406. $filemode is a bitmask like the mode returned by stat().
  407.  
  408. =head1 CREDITS
  409.  
  410. Based on lsparse.pl (from Lee McLoughlin's ftp mirror package) and
  411. Net::FTP's parse_dir (Graham Barr).
  412.