blib/lib/Ls.pm untested

filter: Statementstmt 0.0% 0 / 96 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 146 CC 14 SCAR 33.9
CC
14
Cov
0%
stat_attr
56.0
format_mode
42.0
new
6.0
CRAP 29.6
1 package File::Stat::Ls;
2  
3 # Perl standard modules
4 use strict;
5 use warnings;
6 use Carp;
7 use POSIX qw(strftime);
8  
9  
10 require 5.003;
11 my $VERSION = 0.11;
12  
13 require Exporter;
14 our @ISA = qw(Exporter);
15 our @EXPORT = qw(ls_stat format_mode);
16 our @EXPORT_OK = qw(ls_stat format_mode stat_attr
17 );
18 our %EXPORT_TAGS = (
19 all => [@EXPORT_OK]
20 );
21 our @IMPORT_OK = qw(
22 );
23  
24 =head1 NAME
25  
26 File::Stat::Ls - Perl class for converting stat to ls -l format
27  
28 =head1 SYNOPSIS
29  
30 use File::Stat::Ls;
31  
32 my $obj = File::Stat::Ls->new;
33 my $ls = $obj->ls_stat('/my/file/name.txt');
34  
35 =head1 DESCRIPTION
36  
37 This class contains methods to convert stat elements into ls format.
38 It exports two methods: I<format_mode> and I<ls_stat>.
39 The I<format_mode> is borrowed from I<Stat::lsMode> class by
40 Mark_Jason Dominus. The I<ls_stat> will build a string formated as
41 the output of 'ls -l'.
42  
43 =cut
44  
45 =head2 new ()
46  
47 Input variables:
48  
49 None
50  
51 Variables used or routines called:
52  
53 None
54  
55 How to use:
56  
57 my $obj = new File::Stat::Ls; # or
58 my $obj = File::Stat::Ls->new; # or
59  
60 Return: new empty or initialized File::Stat::Ls object.
61  
62 =cut
63  
64 sub new {
65 my $caller = shift;
66 my $caller_is_obj = ref($caller);
67 my $class = $caller_is_obj || $caller;
68 my $self = bless {}, $class;
69 my %arg = @_; # convert rest of inputs into hash array
70 foreach my $k ( keys %arg ) {
71 if ($caller_is_obj) {
72 $self->{$k} = $caller->{$k};
73 } else {
74 $self->{$k} = $arg{$k};
75 }
76 }
77 return $self;
78 }
79  
80 =head1 METHODS
81  
82 This class defines the following common methods, routines, and
83 functions.
84  
85 =head2 Exported Tag: All
86  
87 The I<:all> tag includes all the methods or sub-rountines
88 defined in this class.
89  
90 use File::Stat::Ls qw(:all);
91  
92 It includes the following sub-routines:
93  
94 =cut
95  
96 # ------ partial inline of Stat::lsMode v0.50 code
97 # (see http://www.plover.com/~mjd/perl/lsMode/
98 # for the complete module)
99 #
100 #
101 # Stat::lsMode
102 #
103 # Copyright 1998 M-J. Dominus
104 # (mjd-perl-lsmode@plover.com)
105 #
106 # You may distribute this module under the same terms as Perl itself.
107 #
108 # $Revision: 1.2 $ $Date: 2004/08/05 14:17:43 $
109  
110 =head2 format_mode ($mode)
111  
112 Input variables:
113  
114 $mode - the third element from stat
115  
116 Variables used or routines called:
117  
118 None
119  
120 How to use:
121  
122 my $md = $self->format_mode((stat $fn)[2]);
123  
124 Return: string with permission bits such as -r-xr-xr-x.
125  
126 =cut
127  
128 sub format_mode {
129 my $s = ref($_[0]) ? shift : (File::Stat::Ls->new);
130 my $mode = shift;
131 my %opts = @_;
132  
133 my @perms = qw(--- --x -w- -wx r-- r-x rw- rwx);
134 my @ftype = qw(. p c ? d ? b ? - ? l ? s ? ? ?);
135 $ftype[0] = '';
136 my $setids = ($mode & 07000)>>9;
137 my @permstrs = @perms[($mode&0700)>>6, ($mode&0070)>>3, $mode&0007];
138 my $ftype = $ftype[($mode & 0170000)>>12];
139
140 if ($setids) {
141 if ($setids & 01) { # Sticky bit
142 $permstrs[2] =~ s/([-x])$/$1 eq 'x' ? 't' : 'T'/e;
143 }
144 if ($setids & 04) { # Setuid bit
145 $permstrs[0] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e;
146 }
147 if ($setids & 02) { # Setgid bit
148 $permstrs[1] =~ s/([-x])$/$1 eq 'x' ? 's' : 'S'/e;
149 }
150 }
151
152 join '', $ftype, @permstrs;
153 }
154  
155 =head2 ls_stat ($fn)
156  
157 Input variables:
158  
159 $fn - file name
160 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
161 $atime,$mtime,$ctime,$blksize,$blocks) = stat($fn);
162  
163  
164 Variables used or routines called:
165  
166 None
167  
168 How to use:
169  
170 my $ls = $self->ls_stat($fn);
171  
172 Return: the ls string such as one of the followings:
173  
174 -r-xr-xr-x 1 root other 4523 Jul 12 09:49 uniq
175 drwxr-xr-x 2 root other 2048 Jul 12 09:50 bin
176 lrwxrwxrwx 1 oracle7 dba 40 Jun 12 2002 linked.pl
177 -> /opt/bin/linked2.pl
178  
179 =cut
180  
181 sub ls_stat {
182 my $s = ref($_[0]) ? shift : (File::Stat::Ls->new);
183 my $fn = shift;
184 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
185 $atime,$mtime,$ctime,$blksize,$blocks) = lstat $fn;
186 my @a = lstat $fn;
187 my $dft = "%b %d %Y";
188 my $ud = getpwuid($uid);
189 my $gd = getgrgid($gid);
190 my $fm = format_mode($mode);
191 my $mt = strftime $dft,localtime $mtime;
192 my $fmt = "%10s %3d %7s %4s %12d %12s %-26s\n";
193 return sprintf $fmt, $fm,$nlink,$ud,$gd,$size,$mt,$fn;
194 }
195  
196 =head2 stat_attr ($fn, $typ)
197  
198 Input variables:
199  
200 $fn - file name for getting stat attributes
201 ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
202 $atime,$mtime,$ctime,$blksize,$blocks) = stat($fn);
203 $typ - what type of object that you want it to return.
204 The default is to return a hash containing filename, longname,
205 and a hash ref with all the element from stat.
206 SFTP - to return a Net::SFTP::Attributes object
207
208  
209  
210 Variables used or routines called:
211  
212 ls_stat
213  
214 How to use:
215  
216 my $hr = $self->stat_attr($fn); # get hash ref
217 my %h = $self->stat_attr($fn); # get hash
218  
219 Return: $hr or %h where the hash elements are depended on the type.
220 The default is to get a hash array with the following elements:
221  
222 filename - file name
223 longname - the ls_stat string for the file
224 a - the attributes of the file with the following elements:
225 dev,ino,mode,nlink,uid,gid,rdev,size,atime,mtime,
226 ctime,blksize,blocks
227  
228 If the type is SFTP, then it will only return a
229 I<Net::SFTP::Attributes> object with the following elements:
230  
231 flags,perm,uid,gid,size,atime,mtime
232  
233 =cut
234  
235 sub stat_attr {
236 my $s = ref($_[0]) ? shift : (File::Stat::Ls->new);
237 my ($fn,$typ) = @_;
238 croak "ERR: no file name for stat_attr.\n" if ! $fn;
239 return undef if ! $fn;
240 my $vs = 'dev,ino,mode,nlink,uid,gid,rdev,size,atime,mtime,';
241 $vs .= 'ctime,blksize,blocks';
242 my $v1 = 'flags,perm,uid,gid,size,atime,mtime';
243 my $ls = ls_stat $fn; chomp $ls;
244 my @a = (); my @v = ();
245 my $attr = {};
246 if ($typ && $typ =~ /SFTP/i) {
247 @v = split /,/, $v1;
248 @a = (stat($fn))[1,2,4,5,7,8,9];
249 %$attr = map { $v[$_] => $a[$_] } 0..$#a ;
250 # 'SSH2_FILEXFER_ATTR_SIZE' => 0x01,
251 # 'SSH2_FILEXFER_ATTR_UIDGID' => 0x02,
252 # 'SSH2_FILEXFER_ATTR_PERMISSIONS' => 0x04,
253 # 'SSH2_FILEXFER_ATTR_ACMODTIME' => 0x08,
254 $attr->{flags} = 0;
255 $attr->{flags} |= 0x01;
256 $attr->{flags} |= 0x02;
257 $attr->{flags} |= 0x04;
258 $attr->{flags} |= 0x08;
259 return wantarray ? %{$attr} : $attr;
260 } else {
261 @v = split /,/, $vs;
262 @a = stat($fn);
263 %$attr = map { $v[$_] => $a[$_] } 0..$#a ;
264 }
265 my %r = (filename=>$fn, longname=>$ls, a=>$attr);
266 # foreach my $k (keys %r) { print "$k=$r{$k}\n"; }
267 # foreach my $k (keys %a) { print "$k=$a{$k}\n"; }
268 # print "X: " . (wantarray ? %r : \%r) . "\n";
269 return wantarray ? %r : \%r;
270 }
271  
272 1;
273  
274 =head1 HISTORY
275  
276 =over 4
277  
278 =item * Version 0.10
279  
280 This version includes two methods: format_mode and ls_stat.
281 It is uploaded to CPAN on 7/11/2005.
282  
283 =item * Version 0.11
284  
285 07/12/2005 (htu) - added stat_attr method.
286  
287 =cut
288  
289 =head1 SEE ALSO (some of docs that I check often)
290  
291 Data::Describe, Oracle::Loader, CGI::Getopt, File::Xcopy,
292 Oracle::Trigger, Debug::EchoMessage, CGI::Getopt, etc.
293  
294 =head1 AUTHOR
295  
296 Copyright (c) 2005 Hanming Tu. All rights reserved.
297  
298 This package is free software and is provided "as is" without express
299 or implied warranty. It may be used, redistributed and/or modified
300 under the terms of the Perl Artistic License (see
301 http://www.perl.com/perl/misc/Artistic.html)
302  
303 =cut
304  
305