lib/SMB/File.pm

filter: Statementstmt 82.5% 128 / 155 Branchbran 72.1% 75 / 104 Conditioncond 62.8% 22 / 35 MC/DCmcdc 42.1% 8 / 19 Subroutinesub 72.2% 26 / 36 Podpod 4.1% 1 / 24 totaltotal 69.7% 260 / 373 SCAR 48.5
CC
68
Cov
77%
new
26.0
update
20.0
find_files
10.0
CRAP 127.8
« lib/SMB/v2/Command/Close.pm ↑ Summary lib/SMB/Command.pm »
1 # SMB-Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 package SMB::File;
17  
18 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 ▶use parent 'SMB';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use Time::HiRes qw(stat);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use File::Glob qw(bsd_glob GLOB_NOCASE GLOB_BRACE);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Fcntl qw(:mode O_DIRECTORY O_RDONLY O_RDWR O_CREAT O_EXCL O_TRUNC);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 3 ▶use SMB::Time qw(from_nttime to_nttime);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 use constant {
32 3 ATTR_READONLY => 0x00000001,
33 ATTR_HIDDEN => 0x00000002,
34 ATTR_SYSTEM => 0x00000004,
35 ATTR_DIRECTORY => 0x00000010,
36 ATTR_ARCHIVE => 0x00000020,
37 ATTR_DEVICE => 0x00000040,
38 ATTR_NORMAL => 0x00000080,
39 ATTR_TEMPORARY => 0x00000100,
40 ATTR_SPARSE_FILE => 0x00000200,
41 ATTR_REPARSE_POINT => 0x00000400,
42 ATTR_COMPRESSED => 0x00000800,
43 ATTR_OFFLINE => 0x00001000,
44 ATTR_NOT_CONTENT_INDEXED => 0x00002000,
45 ATTR_ENCRYPTED => 0x00004000,
46  
47 DISPOSITION_SUPERSEDE => 0, # exists ? supersede : create
48 DISPOSITION_OPEN => 1, # exists ? open : fail
49 DISPOSITION_CREATE => 2, # exists ? fail : create
50 DISPOSITION_OPEN_IF => 3, # exists ? open : create
51 DISPOSITION_OVERWRITE => 4, # exists ? overwrite : fail
52 DISPOSITION_OVERWRITE_IF => 5, # exists ? overwrite : create
53  
54 ACTION_NONE => -1,
55 ACTION_SUPERSEDED => 0, # existing file was deleted and new file was created in its place
56 ACTION_OPENED => 1, # existing file was opened
57 ACTION_CREATED => 2, # new file was created
58 ACTION_OVERWRITTEN => 3, # new file was overwritten
59 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61 3 ▶use SMB::OpenFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 sub from_ntattr ($) {
64 21 ▶ my $attr = shift || 0;
Subroutine from_ntattr: called CC=4 SCAR=15.8
Pod undocumented
Condition50%1/2
shift() || 0
l!l
021
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
65  
66 return
67 21 ▶ $attr & ATTR_DIRECTORY ? O_DIRECTORY :
Branch: 2 branches
BranchTrueFalseTotal
$attr & 16 ? : 0 21 21
$attr & 1 ? : 0 21 21
68 $attr & ATTR_READONLY ? O_RDONLY : O_RDWR;
69 }
70  
71 sub to_ntattr ($) {
72 8 ▶ my $mode = shift || 0;
Subroutine to_ntattr: called CC=6 SCAR=18.8
Pod undocumented
Condition50%1/2
shift() || 0
l!l
80
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
73  
74 8 ▶ return 0
Branch: 4 branches
BranchTrueFalseTotal
S_ISREG($mode) ? : 4 4 8
S_ISDIR($mode) ? : 4 4 8
S_ISCHR($mode) ? : 0 8 8
$mode & 128 ? : 8 0 8
75 | (S_ISREG($mode) ? ATTR_NORMAL : 0)
76 | (S_ISDIR($mode) ? ATTR_DIRECTORY : 0)
77 # | (S_ISBLK($mode) ? ATTR_DEVICE : 0)
78 | (S_ISCHR($mode) ? ATTR_DEVICE : 0)
79 | ($mode & S_IWUSR ? 0 : ATTR_READONLY)
80 ;
81 }
82  
83 sub new ($%) {
84 9 ▶ my $class = shift;
Subroutine new: called CC=23 SCAR=32.6
Pod documented
85 9 my %options = @_;
86  
87 9 ▶ my $name = delete $options{name} // die "No name in constructor";
Condition50%1/2
delete $options{"name"} // die("No name in constructor")
l!l
90
MC/DC0%0/1
delete $options{"name"} // die("No name in constructor")
delete $options{"name"}
Truth table50%1/2
A
A= delete $options{"name"}
Aresult
11
00
88 9 $name =~ s!\\!\/!g;
89 9 $name =~ s!/{2,}!/!g;
90 9 $name =~ s!/$!!;
91 9 ▶ my $root = $options{share_root} //= undef;
Condition50%1/2
$options{"share_root"} //= undef
l!l
90
MC/DC0%0/1
$options{"share_root"} //= undef
$options{"share_root"}
Truth table50%1/2
A
A= $options{"share_root"}
Aresult
11
00
92 9 my $filename = undef;
93 9 ▶ if ($root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root) 9 0 9
94 9 ▶ die "No share_root directory ($root)" unless -d $root;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $root 0 9 9
95 9 ▶ while ($root =~ s=(^|/)(?!\.\./)[^/]+/\.\./=$1=) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($root =~ s[(^|/)(?!\.\./)[^/]+/\.\./][$1]) 9 9 18
96 9 ▶ $filename = $name eq '' ? $root : "$root/$name";
Branch: 1 branch
BranchTrueFalseTotal
$name eq '' ? : 1 8 9
97 9 $filename =~ s!/{2,}!/!g;
98 9 ▶ $filename = '.' if $filename eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $filename eq "" 0 9 9
99 }
100 9 ▶ my $is_ipc = $options{is_ipc} ||= 0;
Condition50%1/2
$options{"is_ipc"} ||= 0
l!l
09
MC/DC0%0/1
$options{"is_ipc"} ||= 0
$options{"is_ipc"}
Truth table50%1/2
A
A= $options{"is_ipc"}
Aresult
11
00
101 9 ▶ my @stat = !$is_ipc && $filename && -e $filename ? stat($filename) : ();
Branch: 1 branch
BranchTrueFalseTotal
!$is_ipc && $filename && -e $filename ? : 8 1 9
Condition50%3/6
not $is_ipc and $filename and -e $filename
!ll&&!rl&&r
018
not $is_ipc and $filename
!ll&&!rl&&r
009
MC/DC33%1/3
not $is_ipc and $filename and -e $filename
not $is_ipc $filename -e $filename
Truth table50%2/4
not(A) and B and C
A= not $is_ipcB= $filenameC= -e $filename
ABCresult
0XX0
10X0
1100
1111
102 9 ▶ my $is_srv = $is_ipc && $name =~ /^(?:srvsvc|wkssvc)$/;
Condition33%1/3
$is_ipc && $name =~ /^(?:srvsvc|wkssvc)$/
!ll&&!rl&&r
900
MC/DC0%0/2
$is_ipc && $name =~ /^(?:srvsvc|wkssvc)$/
$is_ipc $name =~ /^(?:srvsvc|wkssvc)$/
Truth table33%1/3
A && B
A= $is_ipcB= $name =~ /^(?:srvsvc|wkssvc)$/
ABresult
0X0
100
111
103  
104 9 ▶ my $self = $class->SUPER::new(
Branch: 8 branches
BranchTrueFalseTotal
@stat ? : 8 1 9
@stat ? : 8 1 9
@stat ? : 8 1 9
@stat ? : 8 1 9
@stat ? : 8 1 9
@stat ? : 8 1 9
@stat ? : 8 1 9
@stat || $is_srv ? : 8 1 9
Condition80%4/5
$stat[12] || 0
l!l
26
@stat or $is_srv
l!l&&r!l&&!r
801
MC/DC100%1/1
$stat[12] || 0
$stat[12]
MC/DC50%1/2
@stat or $is_srv
@stat $is_srv
Truth table100%2/2
A
A= $stat[12]
Aresult
11
00
Truth table66%2/3
A or B
A= @statB= $is_srv
ABresult
1X1
011
000
105 name => $name,
106 filename => $filename, # server-side file only
107 creation_time => @stat ? to_nttime($stat[10]) : 0,
108 last_access_time => @stat ? to_nttime($stat[ 8]) : 0,
109 last_write_time => @stat ? to_nttime($stat[ 9]) : 0,
110 change_time => @stat ? to_nttime($stat[ 9]) : 0,
111 allocation_size => @stat ? ($stat[12] || 0) * 512: 0,
112 end_of_file => @stat ? $stat[ 7] : 0,
113 attributes => @stat ? to_ntattr($stat[ 2]) : 0,
114 exists => @stat || $is_srv ? 1 : 0,
115 opens => 0,
116 %options,
117 );
118  
119 9 return $self;
120 }
121  
122 sub update ($$$$$$$$;$) {
123 0 ▶ my $self = shift;
Subroutine update: not called CC=4 SCAR=30.0
Pod undocumented
124  
125 0 $self->creation_time(shift);
126 0 $self->last_access_time(shift);
127 0 $self->last_write_time(shift);
128 0 $self->change_time(shift);
129 0 my $size1 = shift;
130 0 my $size2 = shift;
131 0 $self->attributes(shift);
132 0 my $is_eof_first = shift;
133 0 ▶ $self->allocation_size($is_eof_first ? $size2 : $size1);
Branch: 1 branch
BranchTrueFalseTotal
$is_eof_first ? : 0 0 0
134 0 ▶ $self->end_of_file ($is_eof_first ? $size1 : $size2);
Branch: 1 branch
BranchTrueFalseTotal
$is_eof_first ? : 0 0 0
135  
136 0 ▶ $self->exists($self->creation_time ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->creation_time ? : 0 0 0
137 }
138  
139 sub is_directory ($) {
140 4 ▶ my $self = shift;
Subroutine is_directory: called CC=3 SCAR=12.0
Pod undocumented
141  
142 4 ▶ return $self->is_ipc ? 0 : $self->attributes & ATTR_DIRECTORY ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
$self->is_ipc ? : 0 4 4
$self->attributes & 16 ? : 4 0 4
143 }
144  
145 sub to_string ($;$) {
146 0 ▶ my $time = shift;
Subroutine to_string: not called CC=2 SCAR=17.9
Pod undocumented
147 0 ▶ my $format = shift || "%4Y-%2m-%2d %2H:%2M";
Condition0%0/2
shift() || '%4Y-%2m-%2d %2H:%2M'
l!l
00
MC/DC0%0/1
shift() || '%4Y-%2m-%2d %2H:%2M'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
148  
149 0 return strftime($format, localtime $time);
150 }
151  
152 0 ▶sub ctime { from_nttime($_[0]->creation_time) }
Subroutine ctime: not called CC=1 SCAR=6.9
Pod undocumented
153 0 ▶sub atime { from_nttime($_[0]->last_access_time) }
Subroutine atime: not called CC=1 SCAR=6.9
Pod undocumented
154 0 ▶sub wtime { from_nttime($_[0]->last_write_time) }
Subroutine wtime: not called CC=1 SCAR=6.9
Pod undocumented
155 0 ▶sub mtime { from_nttime($_[0]->change_time) }
Subroutine mtime: not called CC=1 SCAR=6.9
Pod undocumented
156 0 ▶sub ctime_string { to_string($_[0]->ctime, $_[1]) }
Subroutine ctime_string: not called CC=1 SCAR=6.9
Pod undocumented
157 0 ▶sub atime_string { to_string($_[0]->atime, $_[1]) }
Subroutine atime_string: not called CC=1 SCAR=6.9
Pod undocumented
158 0 ▶sub wtime_string { to_string($_[0]->wtime, $_[1]) }
Subroutine wtime_string: not called CC=1 SCAR=6.9
Pod undocumented
159 0 ▶sub mtime_string { to_string($_[0]->mtime, $_[1]) }
Subroutine mtime_string: not called CC=1 SCAR=6.9
Pod undocumented
160  
161 sub add_openfile ($$$) {
162 17 ▶ my $self = shift;
Subroutine add_openfile: called CC=1 SCAR=0.0
Pod undocumented
163 17 my $action = shift;
164 17 my $handle = shift;
165  
166 17 my $openfile = SMB::OpenFile->new($self, $action, $handle);
167  
168 17 $self->{opens}++;
169 17 $self->exists(1);
170  
171 17 return $openfile;
172 }
173  
174 sub delete_openfile ($$) {
175 17 ▶ my $self = shift;
Subroutine delete_openfile: called CC=2 SCAR=7.0
Pod undocumented
176 17 my $openfile = shift;
177  
178 17 ▶ close($openfile->handle)
Branch: 1 branch
BranchTrueFalseTotal
if $openfile->handle 17 0 17
179 if $openfile->handle;
180  
181 17 --$self->{opens};
182 }
183  
184 sub _fail_exists ($$) {
185 4 ▶ my $self = shift;
Subroutine _fail_exists: called CC=2 SCAR=6.9
186 4 my $exists = shift;
187  
188 4 $self->err("Can't open file [$self->{filename}]: $!");
189 4 ▶ $self->exists($exists || 0);
Condition100%2/2
$exists || 0
l!l
13
MC/DC100%1/1
$exists || 0
$exists
Truth table100%2/2
A
A= $exists
Aresult
11
00
190  
191 4 return undef;
192 }
193  
194 sub supersede ($) {
195 3 ▶ my $self = shift;
Subroutine supersede: called CC=6 SCAR=19.7
Pod undocumented
196  
197 3 ▶ return $self->create unless -e $self->filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->filename 1 2 3
198  
199 2 my $filename = $self->filename;
200 2 my $tmp_filename = sprintf "%s.%06d", $self->filename, rand(1000000);
201  
202 2 ▶ rename($filename, $tmp_filename)
Branch: 1 branch
BranchTrueFalseTotal
rename $filename, $tmp_filename or return $self->_fail_exists(1) 0 2 2
203 or return $self->_fail_exists(1);
204 2 my $openfile = $self->create;
205 2 ▶ unless ($openfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($openfile) 0 2 2
206 0 ▶ rename($tmp_filename, $filename)
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp_filename, $filename or $self->err("Can't rename tmp file ($tmp_filename) to orig file ($filename)") 0 0 0
207 or $self->err("Can't rename tmp file ($tmp_filename) to orig file ($filename)");
208 0 return $self->_fail_exists(0);
209 }
210 2 ▶ unlink($tmp_filename)
Branch: 1 branch
BranchTrueFalseTotal
unlink $tmp_filename or $self->err("Can't remove tmp file ($tmp_filename)") 0 2 2
211 or $self->err("Can't remove tmp file ($tmp_filename)");
212  
213 2 $openfile->action(ACTION_SUPERSEDED);
214  
215 2 return $openfile;
216 }
217  
218 sub open ($) {
219 8 ▶ my $self = shift;
Subroutine open: called CC=2 SCAR=6.9
Pod undocumented
220  
221 8 ▶ sysopen(my $fh, $self->filename, from_ntattr($self->attributes))
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $self->filename, &from_ntattr(scalar $self->attributes) or return $self->_fail_exists(0) 2 6 8
222 or return $self->_fail_exists(0);
223  
224 6 $self->add_openfile($fh, ACTION_OPENED);
225 }
226  
227 sub create ($) {
228 8 ▶ my $self = shift;
Subroutine create: called CC=3 SCAR=11.1
Pod undocumented
229  
230 8 ▶ sysopen(my $fh, $self->filename, from_ntattr($self->attributes) | O_CREAT | O_EXCL)
Branch: 2 branches
BranchTrueFalseTotal
sysopen my $fh, $self->filename, &from_ntattr(scalar $self->attributes) | 64 | 128 or return $self->_fail_exists(-e $self->filename ? 1 : 0) 1 7 8
-e $self->filename ? : 1 0 1
231 or return $self->_fail_exists(-e $self->filename ? 1 : 0);
232  
233 7 $self->add_openfile($fh, ACTION_CREATED);
234 }
235  
236 sub overwrite ($) {
237 5 ▶ my $self = shift;
Subroutine overwrite: called CC=4 SCAR=14.1
Pod undocumented
238  
239 # no idea why O_TRUNC fails on Windows
240 5 ▶ my $mode = $^O eq 'MSWin32' ? 0 : O_TRUNC;
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 5 5
241  
242 5 ▶ sysopen(my $fh, $self->filename, from_ntattr($self->attributes) | $mode)
Branch: 1 branch
BranchTrueFalseTotal
sysopen my $fh, $self->filename, &from_ntattr(scalar $self->attributes) | $mode or return $self->_fail_exists(0) 1 4 5
243 or return $self->_fail_exists(0);
244  
245 4 ▶ truncate($fh, 0) if $^O eq 'MSWin32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 4 4
246  
247 4 $self->add_openfile($fh, ACTION_OVERWRITTEN);
248 }
249  
250 sub open_if ($) {
251 4 ▶ my $self = shift;
Subroutine open_if: called CC=2 SCAR=6.9
Pod undocumented
252  
253 4 ▶ return -e $self->filename ? $self->open : $self->create;
Branch: 1 branch
BranchTrueFalseTotal
-e $self->filename ? : 3 1 4
254 }
255  
256 sub overwrite_if ($) {
257 3 ▶ my $self = shift;
Subroutine overwrite_if: called CC=2 SCAR=6.9
Pod undocumented
258  
259 3 ▶ return -e $self->filename ? $self->overwrite : $self->create;
Branch: 1 branch
BranchTrueFalseTotal
-e $self->filename ? : 2 1 3
260 }
261  
262 sub open_by_disposition ($$) {
263 6 ▶ my $self = shift;
Subroutine open_by_disposition: called CC=8 SCAR=21.1
Pod undocumented
264 6 my $disposition = shift;
265  
266 6 ▶ return $self->add_openfile(ACTION_OPENED, undef)
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_ipc 0 6 6
267 if $self->is_ipc;
268  
269 6 ▶ return $self->supersede if $disposition == DISPOSITION_SUPERSEDE;
Branch: 1 branch
BranchTrueFalseTotal
if $disposition == 0 1 5 6
270 5 ▶ return $self->open if $disposition == DISPOSITION_OPEN;
Branch: 1 branch
BranchTrueFalseTotal
if $disposition == 1 1 4 5
271 4 ▶ return $self->create if $disposition == DISPOSITION_CREATE;
Branch: 1 branch
BranchTrueFalseTotal
if $disposition == 2 1 3 4
272 3 ▶ return $self->open_if if $disposition == DISPOSITION_OPEN_IF;
Branch: 1 branch
BranchTrueFalseTotal
if $disposition == 3 1 2 3
273 2 ▶ return $self->overwrite if $disposition == DISPOSITION_OVERWRITE;
Branch: 1 branch
BranchTrueFalseTotal
if $disposition == 4 1 1 2
274 1 ▶ return $self->overwrite_if if $disposition == DISPOSITION_OVERWRITE_IF;
Branch: 1 branch
BranchTrueFalseTotal
if $disposition == 5 1 0 1
275  
276 0 warn "Invalid disposition $disposition, can not open file\n";
277 0 return;
278 }
279  
280 sub find_files ($%) {
281 4 ▶ my $self = shift;
Subroutine find_files: called CC=10 SCAR=23.1
Pod undocumented
282 4 my %params = @_;
283  
284 4 ▶ return unless $self->is_directory;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_directory 0 4 4
285  
286 4 ▶ my $pattern = $params{pattern} || '*';
Condition100%2/2
$params{'pattern'} || '*'
l!l
22
MC/DC100%1/1
$params{'pattern'} || '*'
$params{'pattern'}
Truth table100%2/2
A
A= $params{'pattern'}
Aresult
11
00
287 4 my $want_all = $pattern eq '*';
288 4 ▶ my $start_idx = $params{start_idx} || 0;
Condition100%2/2
$params{'start_idx'} || 0
l!l
13
MC/DC100%1/1
$params{'start_idx'} || 0
$params{'start_idx'}
Truth table100%2/2
A
A= $params{'start_idx'}
Aresult
11
00
289 4 my $files = $self->{files}; # cached
290 4 my $name = $self->name;
291  
292 # fix pattern if needed
293 4 my $pattern0 = $pattern;
294 4 $pattern0 =~ s/^\*/{.*,*}/;
295  
296 4 ▶ unless ($want_all && $files) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($want_all and $files) 3 1 4
Condition100%3/3
$want_all and $files
!ll&&!rl&&r
211
MC/DC100%2/2
$want_all and $files
$want_all $files
Truth table100%3/3
A and B
A= $want_allB= $files
ABresult
0X0
100
111
297 3 ▶ my @filenames = map { -e $_ && basename($_) } bsd_glob($self->filename . "/$pattern0", GLOB_NOCASE | GLOB_BRACE);
Branch: 1 branch
BranchTrueFalseTotal
-e $_ and basename($_) 7 0 7
298 3 $self->msg("Find [$self->{filename}/$pattern] - " . scalar(@filenames) . " files");
299 3 ▶ $files = [ map { SMB::File->new(
Branch: 1 branch
BranchTrueFalseTotal
$name eq '' ? : 7 0 7
300 name => $name eq '' ? $_ : "$name/$_",
301 share_root => $self->share_root,
302 ) } @filenames ];
303 3 ▶ $self->{files} = $files if $want_all;
Branch: 1 branch
BranchTrueFalseTotal
if $want_all 1 2 3
304 }
305  
306 4 ▶ return $start_idx ? [ @{$files}[$start_idx .. (@$files - 1)] ] : $files;
Branch: 1 branch
BranchTrueFalseTotal
$start_idx ? : 1 3 4
307 }
308  
309 3 1;
« lib/SMB/v2/Command/Close.pm ↑ Summary lib/SMB/Command.pm »