blib/lib/App/GitFind/FileStatLs.pm untested

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