lib/NBU/Image.pm

filter: Statementstmt 13.3% 29 / 217 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 21.2% 7 / 33 Podpod 0.0% 0 / 26 totaltotal 9.7% 36 / 370 CC 47 SCAR 36.1
CC
47
Cov
9%
loadImages
156.0
loadFileList
56.0
loadDetail
30.0
CRAP 36.9
« lib/NBU/Drive.pm ↑ Summary lib/NBU.pm »
1 #
2 # Copyright (c) 2002 Paul Winkeler. All Rights Reserved.
3 # This program is free software; you may redistribute it and/or modify it under
4 # the same terms as Perl itself.
5 #
6 package NBU::Image;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use NBU::Media;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my %imageList;
14  
15 1 my $fileRecursionDepth = 1;
16 1 my $showEmptyFragments = 0;
17  
18 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 $VERSION = do { my @r=(q$Revision: 1.39 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
23 1 @ISA = qw();
24 1 @EXPORT = qw();
25 1 @EXPORT_OK = qw();
26 1 %EXPORT_TAGS = qw();
27 }
28  
29 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
30 0 my $proto = shift;
31 0 my $image;
32  
33 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
34 0 my $backupID = shift;
35 0 ▶ if (exists($imageList{$backupID})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $imageList{$backupID}) { } 0 0 0
36 0 $image = $imageList{$backupID};
37 }
38 else {
39 0 $image = { };
40 0 bless $image, $proto;
41 0 $imageList{$image->id($backupID)} = $image;
42 }
43 }
44 0 return $image;
45 }
46  
47 0 ▶sub populate {
Subroutine populate: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my $proto = shift;
49  
50 0 $proto->loadImages(NBU->cmd("bpimmedia -l |"));
51 }
52  
53 0 ▶sub byID {
Subroutine byID: not called CC=2 SCAR=17.9
Pod undocumented
54 0 my $proto = shift;
55 0 my $backupID = shift;
56  
57 0 ▶ if (exists($imageList{$backupID})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $imageList{$backupID}) 0 0 0
58 0 return $imageList{$backupID};
59 }
60  
61 0 return undef;
62 }
63  
64 0 ▶sub list {
Subroutine list: not called CC=1 SCAR=6.9
Pod undocumented
65 0 my $proto = shift;
66  
67 0 return (values %imageList);
68 }
69  
70 0 ▶sub id {
Subroutine id: not called CC=2 SCAR=17.9
Pod undocumented
71 0 my $self = shift;
72  
73 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
74 0 $self->{BACKUPID} = shift;
75 # The backup ID of an image in part encodes the time the image was
76 # created
77 0 $self->{CTIME} = substr($self->{BACKUPID}, -10);
78 }
79 0 return $self->{BACKUPID};
80 }
81  
82 0 ▶sub ctime {
Subroutine ctime: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my $self = shift;
84  
85 0 return $self->{CTIME};
86 }
87  
88 0 ▶sub client {
Subroutine client: not called CC=4 SCAR=30.0
Pod undocumented
89 0 my $self = shift;
90  
91 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
92 0 my $host = shift;
93 0 ▶ if (defined($self->{CLIENT})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"CLIENT"}) 0 0 0
94 0 ▶ if ((my $oldHost = $self->{CLIENT}) == $host) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $oldHost = $self->{'CLIENT'}) == $host) { } 0 0 0
95 0 return $host;
96 }
97 else {
98 # $oldHost->removeImage($self);
99 }
100 }
101 0 $host->addImage($self);
102 0 $self->{CLIENT} = $host;
103 }
104 0 return $self->{CLIENT};
105 }
106  
107 0 ▶sub class {
Subroutine class: not called CC=1 SCAR=6.9
Pod undocumented
108 0 my $self = shift;
109  
110 0 return $self->policy(@_);
111 }
112  
113 0 ▶sub policy {
Subroutine policy: not called CC=2 SCAR=17.9
Pod undocumented
114 0 my $self = shift;
115  
116 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
117 0 $self->{POLICY} = shift;
118 }
119 0 return $self->{POLICY};
120 }
121  
122 0 ▶sub schedule {
Subroutine schedule: not called CC=2 SCAR=17.9
Pod undocumented
123 0 my $self = shift;
124  
125 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
126 0 $self->{SCHEDULE} = shift;
127 }
128 0 return $self->{SCHEDULE};
129 }
130  
131 0 ▶sub expires {
Subroutine expires: not called CC=2 SCAR=17.9
Pod undocumented
132 0 my $self = shift;
133  
134 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
135 0 $self->{EXPIRES} = shift;
136 }
137 0 return $self->{EXPIRES};
138 }
139  
140 0 ▶sub loadDetail {
Subroutine loadDetail: not called CC=5 SCAR=34.0
Pod undocumented
141 0 my $self = shift;
142  
143 #
144 # Layout of this output divined from
145 # http://mailman.eng.auburn.edu/pipermail/veritas-bu/2001-May/003861.html
146 0 my $pipe = NBU->cmd("bpimagelist -l -backupid ".$self->id." |");
147 0 ▶ while (<$pipe>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pipe)) 0 0 0
148 0 ▶ next if (/^HIST/);
Branch: 1 branch
BranchTrueFalseTotal
if /^HIST/ 0 0 0
149 0 ▶ next if (/^FRAG/);
Branch: 1 branch
BranchTrueFalseTotal
if /^FRAG/ 0 0 0
150 0 ▶ if (/^IMAGE/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^IMAGE/) 0 0 0
151 0 my ($tag, $clientName,
152 $u3, $u4, $u5,
153 $id, $className,
154 $u8, $u9, $u10,
155 $scheduleName,
156 $u12, $u13, $u14,
157 $elapsed,
158 $u16, $u17, $u18,
159 $kbWritten,
160 $fileCount, $copyCount, $fragmentCount,
161 $compressed, $u24,
162 $softwareVersion, $u26, $u27,
163 $primary,
164 $imageType, $TIRInfo, $TIRExpiration,
165 $keywords
166 ) = split;
167 0 $self->{ELAPSED} = $elapsed;
168 }
169 }
170 0 $self->{DETAILED} = 1;
171 0 close($pipe);
172 }
173  
174 0 ▶sub elapsed {
Subroutine elapsed: not called CC=2 SCAR=17.9
Pod undocumented
175 0 my $self = shift;
176  
177 0 ▶ $self->loadDetail if (!defined($self->{DETAILED}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"DETAILED"} 0 0 0
178  
179 0 return $self->{ELAPSED};
180 }
181  
182 0 ▶sub retention {
Subroutine retention: not called CC=2 SCAR=17.9
Pod undocumented
183 0 my $self = shift;
184  
185 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
186 0 $self->{RETENTION} = shift;
187 }
188 0 return $self->{RETENTION};
189 }
190  
191  
192 0 ▶sub size {
Subroutine size: not called CC=3 SCAR=24.8
Pod undocumented
193 0 my $self = shift;
194 0 my $size;
195  
196 0 for my $f ($self->fragments) {
197 0 ▶ next unless defined($f);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 0 0
198 0 $size += $f->size;
199 }
200  
201 0 return $size;
202 }
203  
204 #
205 # Add another fragment to the referenced copy of this image
206 0 ▶sub insertFragment {
Subroutine insertFragment: not called CC=2 SCAR=17.9
Pod undocumented
207 0 my $self = shift;
208 0 my $copy = shift;
209 0 my $fragment = shift;
210
211 0 my $fragmentListR = $self->{FRAGMENTLISTS};
212 0 ▶ if (!defined(@$fragmentListR[$copy])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined @$fragmentListR[$copy]) 0 0 0
213 0 @$fragmentListR[$copy] = [];
214 }
215 0 my $toc = @$fragmentListR[$copy];;
216  
217 0 return $$toc[$fragment->number - 1] = $fragment;
218 }
219  
220 0 ▶sub fragments {
Subroutine fragments: not called CC=5 SCAR=34.0
Pod undocumented
221 0 my $self = shift;
222  
223 0 my $fragmentListR = $self->{FRAGMENTLISTS};
224 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
225 0 my $copy = shift;
226 0 ▶ if (!defined(@$fragmentListR[$copy])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined @$fragmentListR[$copy]) 0 0 0
227 0 return ();
228 }
229 0 my $toc = @$fragmentListR[$copy];;
230 0 return @$toc;
231 }
232  
233 0 my @toc;
234 0 foreach my $copy (1..$self->copies) {
235 0 ▶ if (defined(@$fragmentListR[$copy])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined @$fragmentListR[$copy]) 0 0 0
236 0 my $copytoc = @$fragmentListR[$copy];
237 0 @toc = (@toc, @$copytoc);
238 }
239 }
240 0 return @toc;
241 }
242  
243 0 ▶sub loadImages {
Subroutine loadImages: not called CC=12 SCAR=50.5
Pod undocumented
244 0 my $proto = shift;
245 0 my $pipe = shift;
246  
247 0 my $image;
248 0 ▶ while (<$pipe>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pipe)) 0 0 0
249 0 ▶ if (/^IMAGE/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^IMAGE/) { } 0 0 0
elsif (/^FRAG/) { } 0 0 0
250 0 my ($tag, $clientName, $clientVersion, $backupID, $className,
251 $classType,
252 $scheduleName,
253 $scheduleType,
254 $retentionLevel, $fileCount, $expires,
255 $compressed, $encrypted
256 ) = split;
257  
258 0 $image = undef;
259 0 ▶ next if ($expires < time);
Branch: 1 branch
BranchTrueFalseTotal
if $expires < time 0 0 0
260  
261 0 $image = NBU::Image->new($backupID);
262 0 $image->{ENCRYPTED} = $encrypted;
263 0 $image->{COMPRESSED} = $compressed;
264 0 $image->{FILECOUNT} = $fileCount;
265 0 $image->{EXPIRES} = $expires;
266 0 $image->{RETENTION} = NBU::Retention->byLevel($retentionLevel);
267 0 $image->{COPYCOUNT} = 0;
268 0 $image->{FRAGMENTLISTS} = [];
269  
270 0 my $class = $image->class(NBU::Class->new($className, $classType));
271 0 $image->schedule(NBU::Schedule->new($class, $scheduleName, $scheduleType));
272  
273 0 my $host;
274 0 $image->client($host = NBU::Host->new($clientName));
275 }
276 elsif (/^FRAG/) {
277 0 ▶ next if (!defined($image));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $image 0 0 0
278  
279 0 my ($tag, $copy, $number, $size, $removable,
280 $mediaType, $density, $fileNumber,
281 $rest) = split(/[\s]+/, $_, 9);
282  
283 #
284 # The fragment list contains failed fragments which are left-overs from
285 # failed backups. Normally we skip these, but they can be made visible
286 # when running diagnostics
287 # Such fragments appear to be identified by having a negative fragment number
288 0 ▶ next if (!$showEmptyFragments && (($number < 1)));
Branch: 1 branch
BranchTrueFalseTotal
if not $showEmptyFragments and $number < 1 0 0 0
Condition0%0/3
not $showEmptyFragments and $number < 1
!ll&&!rl&&r
000
MC/DC0%0/2
not $showEmptyFragments and $number < 1
not $showEmptyFragments $number < 1
Truth table0%0/3
not(A) and B
A= not $showEmptyFragmentsB= $number < 1
ABresult
0X0
100
111
289  
290 #
291 # Is this the start of a new copy of this image?
292 0 ▶ if ($copy > $image->{COPYCOUNT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copy > $image->{"COPYCOUNT"}) 0 0 0
293 0 $image->{COPYCOUNT} = $copy;
294 }
295  
296 0 my ($mediaID, $volume);
297 0 ▶ if (($removable == 0) && ($mediaType == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($removable == 0 and $mediaType == 0) { } 0 0 0
Condition0%0/3
$removable == 0 and $mediaType == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$removable == 0 and $mediaType == 0
$removable == 0 $mediaType == 0
Truth table0%0/3
A and B
A= $removable == 0B= $mediaType == 0
ABresult
0X0
100
111
298 #
299 # Non-removable media (aka disk files) occasionally have spaces in their
300 # names and those will then be surrounded by double quotes...
301 # Additionally we tag these "media" as removable when creating them.
302 0 ▶ if ($rest =~ s/\"(.*)\"[\s]//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ s/\"(.*)\"[\s]//) { } 0 0 0
303 0 $mediaID = $1;
304 }
305 else {
306 0 $rest =~ s/([\S]+)//;
307 0 $mediaID = $1;
308 }
309 0 $volume = NBU::Media->new($mediaID, undef, 0);
310 }
311 else {
312 0 $rest =~ s/([\S]+)//;
313 0 $mediaID = $1;
314 0 $volume = NBU::Media->new($mediaID, undef, 1);
315 0 $volume->density($density);
316 }
317 0 $rest =~ s/^[\s]*//;
318 0 my ($mmdbHostName,
319 $blockSize, $offset, $allocated, $dwo,
320 $u6, $u7,
321 $expires, $mpx
322 ) = split(/[\s]+/, $rest);
323 0 $volume->mmdbHost(NBU::Host->new($mmdbHostName));
324  
325 0 my $fragment = NBU::Fragment->new($number, $copy, $image, $volume, $offset, $size, $dwo, $fileNumber, $blockSize);
326  
327 0 $volume->insertFragment($fileNumber - 1, $fragment);
328 0 $image->insertFragment($copy, $fragment);
329  
330 0 $image->{REMOVABLE} += $volume->removable;
331  
332 0 $image->volume($volume);
333 }
334 }
335 0 close($pipe);
336 }
337  
338 0 ▶sub loadFileList {
Subroutine loadFileList: not called CC=7 SCAR=40.3
Pod undocumented
339 0 my $self = shift;
340 0 my $func = shift;
341 0 my $depth = shift;
342 0 my @fl;
343
344 0 my ($s, $m, $h, $dd, $mon, $year, $wday, $yday, $isdst) = localtime($self->ctime);
345 0 my $mm = $mon + 1;
346 0 my $yy = sprintf("%02d", ($year + 1900) % 100);
347  
348 0 ▶ $depth = $fileRecursionDepth if (!defined($depth));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 0 0 0
349 0 my $pipe = NBU->cmd("bpflist -t ANY"
350 ." -option GET_ALL_FILES"
351 ." -client ".$self->client->name
352 ." -backupid ".$self->id
353 ." -d ${mm}/${dd}/${yy}"
354 ." -rl $depth"
355 ." |");
356  
357 0 ▶ while (<$pipe>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pipe)) 0 0 0
358 0 ▶ next if (/^FILES/);
Branch: 1 branch
BranchTrueFalseTotal
if /^FILES/ 0 0 0
359 0 chop;
360 #
361 # Since file names can contain spaces and some bright soul decided to place the file
362 # name in the middle of this line, we need to pick it apart in three pieces:
363 # before, filename, after
364 # More details at https://forums.symantec.com/syment/board/message?board.id=21&thread.id=5475
365 0 my ($i, $compressedFileSize, $pathLength, $u3, $offset,
366 $imageMode, $rawPartitionSize,
367 $largeFileSize, # only set if file over 2GB; units are in GB
368 $physicalDeviceNumber,
369 $rest) = split(/[\s]+/, $_, 10);
370 0 ▶ if (!($rest =~ /^(.*)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rest =~ /^(.*)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)[\s]([\S]+)/) 0 0 0
371 0 print STDERR "IMAGE filename match failed on $_\n";
372 0 exit;
373 }
374 0 my ($name, $mode, $user, $group, $size,
375 $lastAccessed, $lastModified, $lastInodeModified
376 ) = ($1, $2, $3, $4, $5, $6. $7, $8);
377 # next if ($name =~ /(\/|\\)$/);
378 0 ▶ if (defined($func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $func) { } 0 0 0
379 0 &$func($name);
380 }
381 else {
382 0 push @fl, $name;
383 }
384 }
385 0 close($pipe);
386  
387 0 ▶ if (!defined($func)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $func) 0 0 0
388 0 $self->{FLIST} = \@fl;
389 }
390 }
391  
392 0 ▶sub fileList {
Subroutine fileList: not called CC=3 SCAR=24.8
Pod undocumented
393 0 my $self = shift;
394  
395 0 ▶ if (!$self->{FLIST}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"FLIST"}) 0 0 0
396 0 $self->loadFileList(undef, undef);
397 }
398  
399 0 ▶ if (my $flR = $self->{FLIST}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flR = $self->{"FLIST"}) 0 0 0
400 0 return @$flR;
401 }
402 0 return undef;
403 }
404  
405 0 ▶sub copies {
Subroutine copies: not called CC=1 SCAR=6.9
Pod undocumented
406 0 my $self = shift;
407  
408 0 return $self->{COPYCOUNT};
409 }
410  
411 #
412 # An image is considered removable if any of its fragments is
413 # is stored on removable media. This attribute is computed during
414 # the loading if the image
415 0 ▶sub removable {
Subroutine removable: not called CC=1 SCAR=6.9
Pod undocumented
416 0 my $self = shift;
417  
418 0 return $self->{REMOVABLE};
419 }
420  
421 0 ▶sub density {
Subroutine density: not called CC=2 SCAR=17.9
Pod undocumented
422 0 my $self = shift;
423  
424 0 ▶ return $self->volume->density
Branch: 1 branch
BranchTrueFalseTotal
if $self->volume 0 0 0
425 if ($self->volume);
426 0 return undef;
427 }
428  
429 0 ▶sub showEmptyFragments {
Subroutine showEmptyFragments: not called CC=2 SCAR=17.9
Pod undocumented
430 0 my $proto = shift;
431  
432 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
433 0 $showEmptyFragments = shift;
434 }
435  
436 0 return $showEmptyFragments;
437 }
438  
439 0 ▶sub fileRecursionDepth {
Subroutine fileRecursionDepth: not called CC=2 SCAR=17.9
Pod undocumented
440 0 my $proto = shift;
441  
442 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
443 0 $fileRecursionDepth = shift;
444 }
445  
446 0 return $fileRecursionDepth;
447 }
448  
449 0 ▶sub volume {
Subroutine volume: not called CC=2 SCAR=17.9
Pod undocumented
450 0 my $self = shift;
451  
452 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
453 0 my $volume = shift;
454  
455 0 $self->{VOLUME} = $volume;
456 }
457  
458 0 return $self->{VOLUME};
459 }
460  
461 1 1;
462  
463 __END__
« lib/NBU/Drive.pm ↑ Summary lib/NBU.pm »