blib/lib/FreeBSD/Pkgs.pm

filter: Statementstmt 3.8% 11 / 286 Branchbran 0.0% 0 / 146 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 100% 4 / 4 totaltotal 4.0% 18 / 443 CC 74 SCAR 76.7
CC
74
Cov
3%
parseContents
3080.0
parseInstalled
90.0
parseInstalledPkg
90.0
CRAP 2138.8
↑ Summary
1 package FreeBSD::Pkgs;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 FreeBSD::Pkgs - Reads the FreeBSD installed packaged DB.
10  
11 =head1 VERSION
12  
13 Version 0.2.0
14  
15 =cut
16  
17 1 our $VERSION = '0.2.0';
18  
19  
20 =head1 SYNOPSIS
21  
22 The following example prints out the package information.
23  
24  
25 use FreeBSD::Pkgs;
26
27 $pkgdb=FreeBSD::Pkgs->new;
28
29 $pkgdb->parseInstalled;
30
31 while(my ($name, $pkg) = each %{$pkgdb->{packages}}){
32 print $name."\nComment=".$pkg->{comment}."\n";
33
34 #prints the packages that require it
35 if (defined($pkg->{requiredby})){
36 my $requiredbyInt=0;
37 while (defined($pkg->{requiredby}[$requiredbyInt])){
38 print $name." required by ".$pkg->{requiredby}[$requiredbyInt]."\n";
39 $requiredbyInt++;
40 }
41 }
42
43 #if the extract-in-place option is set, print it
44 if (defined($pkg->{contents}{extract-in-place})){
45 print $name." is set to extract in place\n";
46 }
47
48 #if the extract-in-place option is set, print it
49 if (defined($pkg->{contents}{preserve})){
50 print $name." is set to preserve the old file\n";
51 }
52
53 #print the mtree
54 if (defined($pkg->{contents}{mtree})){
55 print $name." the mtree for this package is '".$pkg->{contents}{mtree}."'\n";
56 }
57
58 #print installed files and associated info
59 if (defined($pkg->{contents}{files})){
60 my @files=keys(%{$pkg->{contents}{files}});
61 my $filesInt=0;
62 while (defined($files[$filesInt])){
63 print $name." installs ".$files[$filesInt]." md5='".
64 $pkg->{contents}{files}{$files[$filesInt]}{MD5}."' ";
65 #prints the the group if there is a specific one for the file
66 if (defined($pkg->{contents}{files}{$files[$filesInt]}{group})) {
67 print " group='".$pkg->{contents}{files}{$files[$filesInt]}{group}."'";
68 }
69 #prints the the user if there is a specific one for the file
70 if (defined($pkg->{contents}{files}{$files[$filesInt]}{user})) {
71 print " user='".$pkg->{contents}{files}{$files[$filesInt]}{user}."'";
72 }
73 #prints the the mode if there is a specific one for the file
74 if (defined($pkg->{contents}{files}{$mfiles[$filesInt]}{mode})) {
75 print " mode='".$pkg->{contents}{files}{$files[$filesInt]}{mode}."'";
76 }
77 #prints the directory if it is not relative to the base
78 if (defined($pkg->{contents}{files}{$mfiles[$filesInt]}{cwd})) {
79 print " cwd='".$pkg->{contents}{files}{$files[$filesInt]}{cwd}."'";
80 }
81 #print if the file is set to be ignored
82 if (defined($pkg->{contents}{files}{$mfiles[$filesInt]}{ignore})) {
83 print " ignore='".$pkg->{contents}{files}{$files[$filesInt]}{ignore}."'";
84 }
85 #print if the file is set to be ignored
86 if (defined($pkg->{contents}{files}{$mfiles[$filesInt]}{ignore_inst})) {
87 print " ignore_inst='".$pkg->{contents}{files}{$files[$filesInt]}{ignore_inst}."'";
88 }
89 print "\n";
90 $filesInt++;
91 }
92 }
93
94 #prints the conflict information
95 my $conflictInt=0;
96 while (defined($pkg->{contents}{conflict}[$conflictInt])){
97 print $name." conflicts with ".$pkg->{contents}{conflict}[$conflictInt]."\n";
98 $conflictInt++;
99 }
100
101 #print dirrm stuff
102 my $dirrmInt=0;
103 while (defined($pkg->{contents}{dirrm}[$dirrmInt])){
104 print $name." conflicts with ".$pkg->{contents}{dirrm}[$dirrmInt]."\n";
105 $dirrmInt++;
106 }
107
108 print "\n";
109 }
110  
111 =head1 FUNCTIONS
112  
113 =head2 new
114  
115 #creates a new object with the default settings
116 my $pkgdb=FreeBSD::Pkgs->new;
117  
118 #creates a new package with the a specified pkgdb dir
119 my $pkgdb=FreeBSD::Pkgs->new({pkgdb=>'/var/db/pkg'});
120  
121 #if this is true there is a error
122 if($pkgdb->error){
123 warn('Error:'.$pkgdb->error.': '.$pkgdb->errorString);
124 }
125  
126 =head3 arguement keys
127  
128 =over
129  
130 =item pkgdb
131  
132 If this key is defined, it uses this dir to use for the package db.
133  
134 The same environmental variables as used by the package tools are respected.
135  
136 =back
137  
138 =cut
139  
140 0 ▶sub new{
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
141 0 my %args;
142 0 ▶ if (defined($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
143 0 %args=%{$_[1]};
144 }
145  
146 #blesses it and inits the hash
147 0 my $self={
148 packages=>{},
149 error=>undef,
150 errorString=>'',
151 };
152 0 bless $self;
153  
154 #figures out what to use for pkgdb
155 0 ▶ if (!defined($args{pkgdb})){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{'pkgdb'}) { } 0 0 0
156 0 ▶ if (!defined($ENV{PKG_DBDIR})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ENV{'PKG_DBDIR'}) { } 0 0 0
157 0 $self->{pkgdb}='/var/db/pkg/';
158 }else{
159 0 $self->{pkgdb}=$ENV{PKG_DBDIR};
160 }
161 }else{
162 0 $self->{pkgdb}=$args{pkgdb};
163 }
164  
165 # #makes sure it exists
166 # if (! -e $args{pkgdb}){
167 # warn("FreeBSD-Pkgs:1: PKG_DBDIR, '".$args{pkgdb}."' does not exist");
168 # $self->{error}='1';
169 # return $self;
170 # }
171  
172 # #reads the packages
173 # if (opendir(PKGDBDIR, $args{pkgdb})){
174 # warn("FreeBSD-Pkgs:2: Could not open PKG_DBDIR, '".$args{pkgdb}."',");
175 # exit 2;
176 # }
177 # my @packages=readdir(PKGDBDIR);
178 # closedir(PKGDBDIR);
179  
180 # #removes directories that start with '.'
181 # @packages=grep(/^\./, @packages);
182  
183 # #processes them all
184 # my @packagesInt=0;
185 # while (defined($packages[$packagesInt])){
186 # #only process it if it is a directory
187 # if (-d $args{pkgdb}.'/'.$packages[$packagesInt]){
188 # my %returned=$self->paseInstalledPkg($packages[$packagesInt]);
189 # #only add it if it is not an error
190 # if (!defined($self->{error})) {
191 # $self->{packages}{$packages[$packagesInt]}=%returned;
192 # }else{
193 # warn('FreeBSD-Pkgs:3: Parsing "'.$packages[$packagesInt].'" as failed.');
194 # }
195 # }else{
196 # #non-fatal
197 # warn('FreeBSD-Pkgs:3: Skipping "'.$packages[$packagesInt].'" as it is not a directory');
198 # }
199 # }
200  
201 0 return $self;
202 }
203  
204 =head2 parseContents
205  
206 This parses the '+CONTENTS' file for a package. The only required is a
207 string containing the contents of the file to be parsed.
208  
209 =head3 args hash
210  
211 =head4 files
212  
213 A boolean controlling if file information is parsed or not.
214  
215 my %contents=$pkgdb->parseContents($contentString, %args);
216 if($pkgdb->error){
217 warn('Error:'.$pkgdb->error.': '.$pkgdb->errorString);
218 }
219  
220 =cut
221  
222 0 ▶sub parseContents{
Subroutine parseContents: not called CC=55 SCAR=80.3
Pod documented
223 0 my $self=$_[0];
224 0 my $contents=$_[1];
225 0 my %args;
226 0 ▶ if (defined($_[2])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) 0 0 0
227 0 %args=%{$_[2]};
228 }
229  
230 0 ▶ if ( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
231 0 return undef;
232 }
233  
234 #process the file info unless told to do otherwise
235 0 ▶ if (!defined($args{file})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"file"}) 0 0 0
236 0 $args{file}=1;
237 }
238  
239 #splits the contents at every new line
240 0 my @contentsA=split(/\n/, $contents);
241  
242 0 my %hash;
243  
244 0 my $contentsAint=0;
245  
246 #holds the the last line matching /^\@pkgdep/
247 0 my $pkgdep=undef;
248  
249 #holds the last file
250 0 my $file=undef;
251
252 #defined if a file has a specific mode
253 0 my $mode=undef;
254  
255 #defined if a file has a specific group
256 0 my $group=undef;
257  
258 #defined if a file has a specific user
259 0 my $user=undef;
260  
261 #holds any second cwds that pop up
262 0 my $cwd=undef;
263  
264 #if this is set to true, the ignore flag is set on a file
265 0 my $ignore=undef;
266  
267 #if this is set to true, the ignore_inst flag is set on a file
268 0 my $ignore_inst=undef;
269  
270 #process it
271 0 ▶ while (defined($contentsA[$contentsAint])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $contentsA[$contentsAint]) 0 0 0
272 0 my $line=$contentsA[$contentsAint];
273 0 chomp($line);
274  
275 #set to true if matched... used for checking at the end of this loop
276 0 my $matched=undef;
277  
278 #handles it if the PKG_FORMAT_REVISION
279 0 ▶ if ($line =~ /^\@comment PKG_FORMAT_REVISION:/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@comment PKG_FORMAT_REVISION:/) 0 0 0
280 0 $line =~ s/^\@comment PKG_FORMAT_REVISION://;
281 0 $hash{PKG_FORMAT_REVISION}=$line;
282  
283 0 goto contentsParseLoopEnd;
284 }
285  
286 #handles it if it is the name line
287 0 ▶ if ($line =~ /^\@name / ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@name /) 0 0 0
288 0 $line =~ s/^\@name //;
289 0 $hash{name}=$line;
290  
291 0 goto contentsParseLoopEnd;
292 }
293
294 #handles ignore lines
295 0 ▶ if ($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'file'}) { } 0 0 0
296 0 ▶ if ($line =~ /^\@ignore/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@ignore/) 0 0 0
297 0 $line =~ s/^\@ignore//;
298 0 $ignore=1;
299
300 0 goto contentsParseLoopEnd;
301 }
302 }else {
303 0 goto contentsParseLoopEnd;
304 }
305  
306 #handles ignore lines
307 0 ▶ if ($line =~ /^\@srcdir / ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@srcdir /) 0 0 0
308 0 $line =~ s/^\@srcdir //;
309 0 $hash{srcdir}=$line;
310  
311 0 goto contentsParseLoopEnd;
312 }
313  
314 #handles ignore_inst lines
315 0 ▶ if ($line =~ /^\@ignore_inst/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@ignore_inst/) 0 0 0
316 0 $line =~ s/^\@ignore_inst//;
317 0 $ignore_inst=1;
318  
319 0 goto contentsParseLoopEnd;
320 }
321  
322 #handles it if origin line
323 0 ▶ if ($line =~ /^\@comment ORIGIN:/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@comment ORIGIN:/) 0 0 0
324 0 $line =~ s/^\@comment ORIGIN://;
325 0 $hash{origin}=$line;
326  
327 0 goto contentsParseLoopEnd;
328 }
329  
330 #extract-in-place options
331 0 ▶ if ($line =~ /^\@option extract-in-place/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@option extract-in-place/) 0 0 0
332 0 $line =~ s/^\@option extract-in-place//;
333 0 $hash{'extract-in-place'}=1;
334  
335 0 goto contentsParseLoopEnd;
336 }
337  
338 #extract-in-place option
339 0 ▶ if ($line =~ /^\@option preserve/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@option preserve/) 0 0 0
340 0 $line =~ s/^\@option preserve//;
341 0 $hash{preserve}=1;
342  
343 0 goto contentsParseLoopEnd;
344 }
345  
346 #handles it if base dir line
347 0 ▶ if ($line =~ /^\@cwd /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@cwd /) 0 0 0
348 0 $line =~ s/^\@cwd //;
349 0 ▶ if (!defined($hash{cwd})){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $hash{'cwd'}) { } 0 0 0
350 0 $hash{cwd}=$line;
351 }else {
352 0 ▶ if ($hash{cwd} eq $line){
Branch: 1 branch
BranchTrueFalseTotal
if ($hash{'cwd'} eq $line) { } 0 0 0
353 0 $cwd=undef;
354 }else{
355 0 $cwd=$line;
356 }
357 }
358  
359 0 goto contentsParseLoopEnd;
360 }
361  
362 0 ▶ if ($args{file}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"file"}) 0 0 0
363 #handles it group lines
364 0 ▶ if ($line =~ /^\@group/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@group/) 0 0 0
365 0 $line =~ s/^\@group//;
366
367 #remove any spaces at the beginning
368 0 $line =~ s/^\ *//;
369
370 0 ▶ if ($line eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq '') { } 0 0 0
371 0 $group=undef;
372 }else{
373 0 $group=$line;
374 }
375
376 0 goto contentsParseLoopEnd;
377 }
378  
379 #handles it mode lines
380 0 ▶ if ($line =~ /^\@mode/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@mode/) 0 0 0
381 0 $line =~ s/^\@mode//;
382
383 #remove any spaces at the beginning
384 0 $line =~ s/^\ *//;
385
386 0 ▶ if ($line eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq '') { } 0 0 0
387 0 $mode=undef;
388 }else{
389 0 $mode=$line;
390 }
391
392 0 goto contentsParseLoopEnd;
393 }
394 }
395 #handles it if metree line
396 0 ▶ if ($line =~ /^\@mtree /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@mtree /) 0 0 0
397 0 $line =~ s/^\@mtree //;
398 #it should only be defined once
399 0 ▶ if (!defined($hash{mtree})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"mtree"}) 0 0 0
400 0 $hash{mtree}=$line;
401 }
402  
403 0 goto contentsParseLoopEnd;
404 }
405  
406 #handles it if dependency line
407 0 ▶ if ($line =~ /^\@pkgdep /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@pkgdep /) 0 0 0
408 0 $line =~ s/^\@pkgdep //;
409  
410 0 $pkgdep=$line;
411  
412 #creates the dep hash if it already exists
413 0 ▶ if (!defined($hash{deps})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"deps"}) 0 0 0
414 0 $hash{deps}={};
415 }
416  
417 #sets up the depdency hash
418 0 $hash{deps}{$line}={};
419  
420 0 goto contentsParseLoopEnd;
421 }
422  
423 #handles it if line describing the origin of a dependency
424 0 ▶ if ($line =~ /^\@comment DEPORIGIN:/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@comment DEPORIGIN:/) 0 0 0
425 0 ▶ if (!defined($pkgdep)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pkgdep) 0 0 0
426 0 ▶ if (defined($hash{name})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{'name'}) { } 0 0 0
427 $self->warnString('A line matching /^\@comment DEPORIGIN:/'.
428 ' was found, but no previous package dependencies were found. Line="'.
429 0 $line.'" name="'.$hash{name}.'"');
430 }else {
431 0 $self->warnString('A line matching /^\@comment DEPORIGIN:/'.
432 ' was found, but no previous package dependencies were found. Line="'.
433 $line.'"');
434 }
435 }
436  
437 0 $line =~ s/^\@comment DEPORIGIN://;
438  
439 0 $hash{deps}{$pkgdep}{origin}=$line;
440  
441 0 goto contentsParseLoopEnd;
442 }
443  
444 #handles it if it is a file
445 0 ▶ if ($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'file'}) { } 0 0 0
446 0 ▶ if ($line !~ /^\@/){
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\@/) 0 0 0
447 0 ▶ if (!defined($hash{files})){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"files"}) 0 0 0
448 0 $hash{files}={};
449 }
450
451 0 $file=$line;
452
453 0 $hash{files}{$line}={};
454
455 #if a specific group is set, set it for the group
456 0 ▶ if (defined($group)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $group) 0 0 0
457 0 $hash{files}{$line}{group}=$group;
458 }
459  
460 #if a specific user is set, set it for the group
461 0 ▶ if (defined($user)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user) 0 0 0
462 0 $hash{files}{$line}{user}=$user;
463 }
464  
465 #if a specific mode is set, set it for the file
466 0 ▶ if (defined($mode)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) 0 0 0
467 0 $hash{files}{$line}{mode}=$mode;
468 }
469
470 #sets the ignore flag on a file if it is defined
471 0 ▶ if ($ignore){
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore) 0 0 0
472 0 $hash{files}{$line}{ignore}=1;
473 0 $ignore=undef;
474 }
475  
476 #sets the ignore_inst flag on a file if it is defined
477 0 ▶ if ($ignore){
Branch: 1 branch
BranchTrueFalseTotal
if ($ignore) 0 0 0
478 0 $hash{files}{$line}{ignore_inst}=1;
479 0 $ignore_inst=undef;
480 }
481  
482 #adds the directory for the file if it is not in the base
483 0 ▶ if (defined($cwd)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cwd) 0 0 0
484 0 $hash{files}{$line}{cwd}=$cwd;
485 }
486
487 0 goto contentsParseLoopEnd;
488 }
489 }else {
490 0 goto contentsParseLoopEnd;
491 }
492  
493 #handles a line if it is describing a MD5 of a file
494 0 ▶ if ($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'file'}) { } 0 0 0
495 0 ▶ if ($line =~ /^\@comment MD5:/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@comment MD5:/) 0 0 0
496 0 ▶ if (!defined($file)){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
497 0 ▶ if (defined($hash{name})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{'name'}) { } 0 0 0
498 $self->warnString('A line matching /^\@comment MD5:/'.
499 ' was found, but no previous files were found. Line="'.
500 0 $line.'" name="'.$hash{name}.'"');
501 }else {
502 0 $self->warnString('A line matching /^\@comment MD5:/'.
503 ' was found, but no previous files were found. Line="'.$line.'"');
504 }
505 }
506  
507 0 $line =~ s/^\@comment DEPORIGIN://;
508
509 0 $hash{files}{$file}{MD5}=$line;
510
511 0 goto contentsParseLoopEnd;
512 }
513 }else {
514 0 goto contentsParseLoopEnd;
515 }
516  
517 #handles unexec lines
518 0 ▶ if ($line =~ /^\@unexec /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@unexec /) 0 0 0
519 0 $line =~ s/^\@unexec //;
520  
521 0 ▶ if (!defined($hash{unexec})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"unexec"}) 0 0 0
522 0 $hash{unexec}=[];
523 }
524
525 0 push(@{$hash{unexec}}, $line);
526
527 0 goto contentsParseLoopEnd;
528 }
529  
530 #handles exec lines
531 0 ▶ if ($line =~ /^\@exec /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@exec /) 0 0 0
532 0 $line =~ s/^\@exec //;
533  
534 0 ▶ if (!defined($hash{exec})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"exec"}) 0 0 0
535 0 $hash{exec}=[];
536 }
537
538 0 push(@{$hash{exexec}}, $line);
539
540 0 goto contentsParseLoopEnd;
541 }
542  
543 #handles unexec lines
544 0 ▶ if ($line =~ /^\@dirrm /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@dirrm /) 0 0 0
545 0 $line =~ s/^\@dirrm //;
546  
547 0 ▶ if (!defined($hash{dirrm})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"dirrm"}) 0 0 0
548 0 $hash{dirrm}=[];
549 }
550
551 0 push(@{$hash{dirrm}}, $line);
552
553 0 goto contentsParseLoopEnd;
554 }
555  
556 #handles display lines
557 0 ▶ if ($line =~ /^\@display /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@display /) 0 0 0
558 0 $line =~ s/^\@display //;
559  
560 0 ▶ if (!defined($hash{display})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"display"}) 0 0 0
561 0 $hash{display}=[];
562 }
563
564 0 push(@{$hash{display}}, $line);
565
566 0 goto contentsParseLoopEnd;
567 }
568  
569 #handles conflict lines
570 0 ▶ if ($line =~ /^\@conflicts /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@conflicts /) 0 0 0
571 0 $line =~ s/^\@conflicts //;
572  
573 0 ▶ if (!defined($hash{conflict})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"conflict"}) 0 0 0
574 0 $hash{conflict}=[];
575 }
576
577 0 push(@{$hash{conflict}}, $line);
578
579 0 goto contentsParseLoopEnd;
580 }
581  
582 #handles generic comments
583 0 ▶ if ($line =~ /^\@comment /){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\@comment /) 0 0 0
584 0 $line =~ s/^\@comment //;
585  
586 0 ▶ if (!defined($hash{comment})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $hash{"comment"}) 0 0 0
587 0 $hash{comment}=[];
588 }
589
590 0 push(@{$hash{comment}}, $line);
591
592 0 goto contentsParseLoopEnd;
593 }
594  
595 #if we reach this it means the line was not matched
596 0 ▶ if (defined($hash{name})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash{'name'}) { } 0 0 0
597 warn('FreeBSD-Pkgs parseContents:10: Unmatched line. line="'.$line.
598 0 '" name="'.$hash{name}.'"');
599 }else{
600 0 warn('FreeBSD-Pkgs parseContents:10: Unmatched line. line="'.$line.
601 '"');
602 }
603  
604 #where using a goto here to simplify checking and speed it up a bit
605 0 contentsParseLoopEnd:
606  
607 $contentsAint++;
608 }
609  
610 0 return %hash;
611 }
612  
613 =head2 parseInstalled
614  
615 This reads all installed packages. The returned value is a boolean.
616  
617 =head3 args hash
618  
619 =head4 files
620  
621 A boolean controlling if file information is parsed or not.
622  
623 This will be passed to parseInstalledPkg and then parseContents.
624  
625 $pkgdb->parseInstalled(%args);
626 #checks to see if it completed successfully
627 if($pkgdb->{error}){
628 print 'Error: '.$pkgdb->{error}."\n";
629 }
630  
631 #checks to make sure it completed successfully using a if statement
632 if($pkgdb->parseInstalled(%args)){
633 print 'Error: '.$pkgdb->{error}."\n";
634 }
635  
636 =cut
637  
638 0 ▶sub parseInstalled{
Subroutine parseInstalled: not called CC=9 SCAR=45.0
Pod documented
639 0 my $self=$_[0];
640 0 my %args;
641 0 ▶ if (defined($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
642 0 %args=%{$_[1]};
643 }
644  
645 0 ▶ if ( ! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
646 0 return undef;
647 }
648  
649 #makes sure it exists
650 0 ▶ if (! -e $self->{pkgdb}){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->{"pkgdb"}) 0 0 0
651 0 $self->{errorString}="PKG_DBDIR, '".$self->{pkgdb}."' does not exist";
652 0 $self->{error}='1';
653 0 $self->warn;
654 0 return undef;
655 }
656  
657 #reads the packages
658 0 ▶ if (!opendir(PKGDBDIR, $self->{pkgdb})){
Branch: 1 branch
BranchTrueFalseTotal
unless (opendir PKGDBDIR, $self->{"pkgdb"}) 0 0 0
659 0 $self->{errorString}="Could not open PKG_DBDIR, '".$self->{pkgdb}."',";
660 0 $self->{error}=2;
661 0 $self->warn;
662 0 return undef;
663 }
664 0 my @packages=readdir(PKGDBDIR);
665 0 closedir(PKGDBDIR);
666  
667 #removes directories that start with '.'
668 0 @packages=grep(!/^\./, @packages);
669  
670 #processes them all
671 0 my $packagesInt=0;
672 0 ▶ while (defined($packages[$packagesInt])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $packages[$packagesInt]) 0 0 0
673 #only process it if it is a directory
674 0 ▶ if (-d $self->{pkgdb}.'/'.$packages[$packagesInt]){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'pkgdb'} . '/' . $packages[$packagesInt]) { } 0 0 0
675 0 my %returned=$self->parseInstalledPkg($packages[$packagesInt], \%args);
676 #only add it if it is not an error
677 0 ▶ if (!defined($self->{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'error'}) { } 0 0 0
678 0 $self->{packages}{$packages[$packagesInt]}={%returned};
679  
680 }else{
681 0 $self->warnString('Parsing "'.$packages[$packagesInt].'" as failed.');
682 }
683 }else{
684 #non-fatal
685 #exception for the portupgrade DB file
686 0 ▶ if ($packages[$packagesInt] ne 'pkgdb.db') {
Branch: 1 branch
BranchTrueFalseTotal
if ($packages[$packagesInt] ne "pkgdb.db") 0 0 0
687 0 $self->warnString('Skipping "'.$packages[$packagesInt].'" as it is not a directory');
688 }
689 }
690  
691 0 $packagesInt++;
692 }
693  
694 0 return 1;
695 }
696  
697 =head2 parseInstalledPkg
698  
699 This parses the specified installed package.
700  
701 =head3 args hash
702  
703 =head4 files
704  
705 A boolean controlling if file information is parsed or not.
706  
707 This will be passed to parseContents.
708  
709 my %pkg=$pkgdb->parseInstalledPkg($pkgname, %args);
710 if($error){
711 print "Error!\n";
712 }
713  
714 =cut
715  
716 0 ▶sub parseInstalledPkg{
Subroutine parseInstalledPkg: not called CC=9 SCAR=45.0
Pod documented
717 0 my $self=$_[0];
718 0 my $pkg=$_[1];
719 0 my %args;
720 0 ▶ if (defined($_[2])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[2]) 0 0 0
721 0 %args=%{$_[2]};
722 }
723  
724 0 ▶ if (! $self->errorblank ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->errorblank) 0 0 0
725 0 return undef;
726 }
727  
728 #this is the directory that holds the package information
729 0 my $pkgdir=$self->{pkgdb}."/".$pkg;
730  
731 0 my %hash;
732  
733 #the require by file
734 0 my $requiredby=$pkgdir.'/+REQUIRED_BY';
735  
736 #if the required by file exists, process it
737 0 ▶ if (-f $requiredby){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $requiredby) 0 0 0
738 #adds the required by array to the hash
739 0 $hash{requiredby}=[];
740  
741
742 0 ▶ if (!open(REQUIREDBY, $requiredby)){
Branch: 1 branch
BranchTrueFalseTotal
unless (open REQUIREDBY, $requiredby) 0 0 0
743 0 $self->{error}='4';
744 0 $self->{errorString}='Could not open the required by file, "'.$requiredby.'",';
745 0 $self->warn;
746 0 return undef;
747 }
748 0 my @reqs=<REQUIREDBY>;
749 0 close(REQUIREDBY);
750  
751 #processes the required by entries
752 0 my $reqsInt=0;
753 0 ▶ while (defined($reqs[$reqsInt])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reqs[$reqsInt]) 0 0 0
754 0 chomp($reqs[$reqsInt]);
755 0 push(@{$hash{requiredby}}, $reqs[$reqsInt]);
756 0 $reqsInt++;
757 }
758 }
759  
760 #reads the comment
761 0 my $comment=$pkgdir.'/+COMMENT';
762  
763 #opens the comment
764 0 ▶ if (!open(COMMENT, $comment)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open COMMENT, $comment) 0 0 0
765 0 warn('FreeBSD-Pkgs:5: Could not open the comment file, "'.$comment.'"');
766 0 $self->{error}=5;
767 0 return undef;
768 }
769 #reads the comment
770 0 read(COMMENT, $hash{comment}, 32768);
771 0 close(COMMENT);
772 0 chomp($hash{comment});
773  
774 #reads the comment
775 0 my $desc=$pkgdir.'/+DESC';
776  
777 #opens the description
778 0 ▶ if (!open(DESCRIPTION, $comment)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open DESCRIPTION, $comment) 0 0 0
779 0 warn('FreeBSD-Pkgs:6: Could not open the description file, "'.$desc.'"');
780 0 $self->{error}=6;
781 0 return undef;
782 }
783 #reads the comment
784 0 read(DESCRIPTION, $hash{desc}, 32768);
785 0 close(DESCRIPTION);
786  
787  
788 #reads the comment
789 0 my $contents=$pkgdir.'/+CONTENTS';
790  
791 #opens the description
792 0 ▶ if (!open(CONTENTS, $contents)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open CONTENTS, $contents) 0 0 0
793 0 warn('FreeBSD-Pkgs:7: Could not open the comment file, "'.$desc.'"');
794 0 $self->{error}=6;
795 0 return undef;
796 }
797 #reads the comment
798 0 my $contentsString;
799 0 read(CONTENTS, $contentsString, 30000000);
800 0 close(CONTENTS);
801  
802 #we don't do any error checking here as if there is a error, it will
803 #already by defined
804 0 $hash{contents}={$self->parseContents($contentsString,\%args)};
805  
806 0 return %hash;
807 }
808  
809 =head1 Package Hash
810  
811 This hash is contained in $pkgdb->{packages} or returned by $pkgdb-parseInstalledPkg.
812 Each of it's key is name of a installed package and a hash. See the information
813 below for key values of that hash.
814  
815 =over
816  
817 =item comment
818  
819 This holds the package comment.
820  
821 =item contents
822  
823 This contains a hash that contains the parsed contents of the '+CONTENTS' file.
824 For additional information on this hash see the section '+CONTENTS Hash'.
825  
826 =item desc
827  
828 This holds the description of the package.
829  
830 =item requiredby
831  
832 This contains a array holding the a list of packages that require this package.
833  
834 =back
835  
836 =head1 +CONTENTS Hash
837  
838 This has is either contained in $pkgdb->{packages}{<packagename>}{contents} or returned
839 by $pkgdb->parseContents.
840  
841 =over
842  
843 =item cwd
844  
845 This is a string that contains the prefix for the package.
846  
847 =item mtree
848  
849 This is a string that contains the mtree file to use for the package.
850  
851 =item requiredby
852  
853 This is a array that contains a list of packages that requires this package.
854  
855 =item srcdir
856  
857 See pkg_create(1) for @srcdir, as I can't think of a decent description.
858  
859 =item unexec
860  
861 This is a array that contains the unexec lines.
862  
863 =item exec
864  
865 This is a array that contains the exec lines.
866  
867 =item dirrm
868  
869 This is a array of directories to remove.
870  
871 =item conflicts
872  
873 This is a array that holds a list of conflicting packages.
874  
875 =item deps
876  
877 This contains another hash. See the section 'deps Hash' for more information.
878  
879 =item comment
880  
881 This is a array that contains comments that this module was not sure what to do with.
882  
883 =item files
884  
885 This is a hash that contains information on the installed files. See the section 'files
886 Hash' for more information.
887  
888 =back
889  
890 =head1 deps Hash
891  
892 This is a hash contained in $pkgdb->{<package>}{contents}{deps}{<pkgdep>}.
893  
894 =over
895  
896 =item origin
897  
898 This is the ports origin location, if one is given.
899  
900 =back
901  
902 =head1 files Hash
903  
904 This is a hash that contains information on the installed files. See the section
905 'files Hash' for more information. This is a hash contained in
906 $pkgdb->{<package>}{contents}{files}{<file>}. The keys of the hash are the name
907 of the files.
908  
909 =over
910  
911 =item MD5
912  
913 This is the MD5 sum of the file.
914  
915 =item cwd
916  
917 This is the cwd for the file if it is not set to the base one.
918  
919 =item ignore_inst
920  
921 This is defined if the ignore_inst flag is set to.
922  
923 =item ignore
924  
925 This is defined if the ignore flag is set to.
926  
927 =item mode
928  
929 This is defined if a specific mode is set for a file.
930  
931 =item group
932  
933 This is defined if a specific group is set for a file.
934  
935 =item owner
936  
937 This is defined if a specific owner is set for a file.
938  
939 =back
940  
941 =head1 NOTES
942  
943 =over
944  
945 =item @noinst
946  
947 These lines in the '+CONTENTS' file are not currently handled.
948  
949 =item @exec
950  
951 The handling of these lines are going to be changed in the near future.
952  
953 =item @unexec
954  
955 The handling of these lines are going to be changed in the near future.
956  
957 =item @display
958  
959 Currently not handled in this version of.
960  
961 =item memory
962  
963 It is generally uses atleast twice the amount of ram as the size of the pkgdb.
964  
965 =back
966  
967 =head1 ERROR CODES/HANDLING
968  
969 Error handling is provided by L<Error::Helper>.
970  
971 =head2 1
972  
973 PKG_DBDIR does not exist.
974  
975 =head2 2
976  
977 Could not open PKG_DBDIR.
978  
979 =head2 3
980  
981 The named dir entry in PKG_DBDIR is not a directory.
982  
983 =head2 4
984  
985 Could not open the specified required by file.
986  
987 =head2 5
988  
989 Could not open the specified comment file.
990  
991 =head2 6
992  
993 Could not open the specified description file.
994  
995 =head2 7
996  
997 Could not open the specified contents file.
998  
999 =head2 8
1000  
1001 A line matching /^\@comment DEPORIGIN:/ was found, no previous depedencies have
1002 been found.
1003  
1004 =head2 9
1005  
1006 A line matching /^\@comment MD5:/ was found, no previous files have
1007 been found.
1008  
1009 =head2 10
1010  
1011 Unable to make sense of the specified '+CONTENTS' line.
1012  
1013 =head2 11
1014  
1015 /^\@cwd/ matched twice in a '+CONTENTS' file.
1016  
1017 =head1 AUTHOR
1018  
1019 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
1020  
1021 =head1 BUGS
1022  
1023 Please report any bugs or feature requests to C<bug-freebsd-pkgs at rt.cpan.org>, or through
1024 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FreeBSD-Pkgs>. I will be notified, and then you'll
1025 automatically be notified of progress on your bug as I make changes.
1026  
1027  
1028  
1029  
1030 =head1 SUPPORT
1031  
1032 You can find documentation for this module with the perldoc command.
1033  
1034 perldoc FreeBSD::Pkgs
1035  
1036  
1037 You can also look for information at:
1038  
1039 =over 4
1040  
1041 =item * RT: CPAN's request tracker
1042  
1043 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FreeBSD-Pkgs>
1044  
1045 =item * AnnoCPAN: Annotated CPAN documentation
1046  
1047 L<http://annocpan.org/dist/FreeBSD-Pkgs>
1048  
1049 =item * CPAN Ratings
1050  
1051 L<http://cpanratings.perl.org/d/FreeBSD-Pkgs>
1052  
1053 =item * Search CPAN
1054  
1055 L<http://search.cpan.org/dist/FreeBSD-Pkgs>
1056  
1057 =back
1058  
1059  
1060 =head1 ACKNOWLEDGEMENTS
1061  
1062 Peter V. Vereshagin, #69658, notified me about a pointless warning for the portupgrade DB file
1063  
1064 =head1 COPYRIGHT & LICENSE
1065  
1066 Copyright 2012 Zane C. Bowers-Hadley, all rights reserved.
1067  
1068 This program is free software; you can redistribute it and/or modify it
1069 under the same terms as Perl itself.
1070  
1071  
1072 =cut
1073  
1074 1 1; # End of FreeBSD::Pkgs
↑ Summary