Accounts.pm

filter: Statementstmt 81.2% 26 / 32 Branchbran 60.0% 6 / 10 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.7% 6 / 7 Podpod 60.0% 3 / 5 totaltotal 69.3% 43 / 62 CC 8 SCAR 15.8
CC
8
Cov
72%
new
8.3
getDisk
2.1
setDisk
2.0
CRAP 4.9
« BBackupd.pm ↑ Summary DiskSets.pm »
1 #!/usr/bin/perl -w
2  
3 package BoxBackup::Config::Accounts;
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 BBConfig::Accounts - Access to Box Backup account config files
10  
11 =head1 SYNOPSIS
12  
13 use BoxBackup::Config::Accounts;
14 $bbconfig = BoxBackup::Config::Accounts->new();
15  
16 or
17  
18 use BoxBackup::Config::Accounts;
19 $file = "/etc/bbox/accounts.txt";
20 $bbconfig = BoxBackup::Config::Accounts->new($file);
21  
22 @accounts = $bbconfig->getAccountIDs();
23 foreach $i (@accounts)
24 {
25 # Find out what account is on what diskset.
26 $disk = $bbconfig->getDisk($i);
27 }
28
29 =head1 ABSTRACT
30  
31 BoxBackup::Config::Accounts is a rather simple package that lets the user
32 have access to the data from the accounts configuration file for
33 Box Backup. It provides methods to retrieve the data only. No creation
34 or editing is supported.
35  
36 =head1 DESCRIPTION
37  
38 Allows for programmatic access to the information stored in the Box
39 Backup 'accounts' config file, which simply holds the mapping between
40 accounts and disk sets.
41  
42 =head2 Methods
43  
44 =over
45  
46 =item *
47 new(). The new() method parses the accounts file given as the first (and only)
48 parameter, or, if none are given, parses /etc/box/bbstored/accounts.txt, and
49 creates the object.
50  
51 =item *
52 getAccountIDs(). The getAccountIDs() method returns a sorted array of all the account IDs
53 found in the config file. This will often be used for processing all the
54 accounts in some way.
55  
56 =item *
57 getDisk(). The getDisk() method returns a diskset ID when given an account ID.
58  
59 =back
60  
61 =head1 AUTHOR
62 Per Reedtz Thomsen (L<mailto:pthomsen@reedtz.com>)
63
64 =cut
65
66 2 our $VERSION = v0.03;
67  
68 sub new
69 1 ▶{
Subroutine new: called CC=7 SCAR=21.1
Pod documented
70 1 my ($self, @args) = @_;
71 1 ▶ my $accountFile = $args[0] || "/etc/box/bbstored/accounts.txt";
Condition50%1/2
$args[0] || '/etc/box/bbstored/accounts.txt'
l!l
10
MC/DC0%0/1
$args[0] || '/etc/box/bbstored/accounts.txt'
$args[0]
Truth table50%1/2
A
A= $args[0]
Aresult
11
00
72  
73 1 ▶ open (ACCOUNTS, "<$accountFile") or croak("Can't open $accountFile: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open ACCOUNTS, "<$accountFile" or croak("Can't open ${accountFile}: $!\n") 1 0 1
74  
75 1 my %accounts;
76 1 ▶ while (<ACCOUNTS>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline ACCOUNTS)) 11 1 12
77 {
78 11 chomp;
79 11 ▶ next if(m/^\#/);
Branch: 1 branch
BranchTrueFalseTotal
if /^\#/ 0 11 11
80 # Format of Accounts file: <account number>:<disk set>
81 11 my ($acct,$disk) = split /:/;
82 11 ▶ if(($acct eq "") || !defined($disk))
Branch: 1 branch
BranchTrueFalseTotal
if ($acct eq "" or not defined $disk) 0 11 11
Condition33%1/3
$acct eq "" or not defined $disk
l!l&&r!l&&!r
0011
MC/DC0%0/2
$acct eq "" or not defined $disk
$acct eq "" not defined $disk
Truth table33%1/3
A or not(B)
A= $acct eq ""B= not defined $disk
ABresult
1X1
011
000
83 {
84 0 carp("Bad line in $accountFile: [$_] skipping");
85 0 next;
86 }
87 11 my $acctF = sprintf("%lx", hex($acct));
88 # print $acctF , " - ", $disk, "\n";
89 11 $accounts{$acctF} = $disk;
90 }
91 1 bless \%accounts, $self;
92 }
93  
94 27 ▶sub numerically { $a <=> $b }
Subroutine numerically: called CC=1 SCAR=0.0
Pod undocumented
95  
96 sub getDisk
97 1 ▶{
Subroutine getDisk: called CC=2 SCAR=7.6
Pod documented
98 1 my ($self, $accountID) = @_;
99  
100 # Return the disk set number for the given account.
101 # The $accountID string is reformatted to a non-padded
102 # hex number, to conform to the object storage format.
103 1 my $accountIDF = sprintf("%lx", hex($accountID));
104 1 ▶ return $self->{$accountIDF} if exists($self->{$accountIDF});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$accountIDF} 1 0 1
105 0 return -1;
106 }
107  
108 sub setDisk
109 0 ▶{
Subroutine setDisk: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my ($self, $accountID, $diskSet) = @_;
111 0 my $accountIDF = sprintf("%lx", hex($accountID));
112
113 0 $self->{$accountIDF} = $diskSet;
114
115 }
116  
117  
118 sub getAccountIDs
119 1 ▶{
Subroutine getAccountIDs: called CC=1 SCAR=0.0
Pod documented
120 1 my ($self) = @_;
121  
122 1 return sort numerically (keys %$self);
123  
124 }
125  
126  
127  
128 2 1;
129  
130  
131
« BBackupd.pm ↑ Summary DiskSets.pm »