File Coverage

blib/lib/BoxBackup/Config/Accounts.pm
Criterion Covered Total %
statement 24 30 80.0
branch 4 8 50.0
condition 2 5 40.0
subroutine 6 7 85.7
pod 3 5 60.0
total 39 55 70.9


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             package BoxBackup::Config::Accounts;
4 2     2   49177 use strict;
  2         4  
  2         61  
5 2     2   8 use Carp;
  2         4  
  2         1547  
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)
63            
64             =cut
65            
66             our $VERSION = v0.03;
67              
68             sub new
69             {
70 1     1 1 15 my ($self, @args) = @_;
71 1   50     5 my $accountFile = $args[0] || "/etc/box/bbstored/accounts.txt";
72              
73 1 50       72 open (ACCOUNTS, "<$accountFile") or croak("Can't open $accountFile: $!\n");
74              
75 1         3 my %accounts;
76 1         40 while ()
77             {
78 11         86 chomp;
79 11 50       23 next if(m/^\#/);
80             # Format of Accounts file: :
81 11         34 my ($acct,$disk) = split /:/;
82 11 50 33     47 if(($acct eq "") || !defined($disk))
83             {
84 0         0 carp("Bad line in $accountFile: [$_] skipping");
85 0         0 next;
86             }
87 11         28 my $acctF = sprintf("%lx", hex($acct));
88             # print $acctF , " - ", $disk, "\n";
89 11         49 $accounts{$acctF} = $disk;
90             }
91 1         7 bless \%accounts, $self;
92             }
93              
94 27     27 0 40 sub numerically { $a <=> $b }
95              
96             sub getDisk
97             {
98 1     1 1 1888 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         7 my $accountIDF = sprintf("%lx", hex($accountID));
104 1 50       9 return $self->{$accountIDF} if exists($self->{$accountIDF});
105 0         0 return -1;
106             }
107              
108             sub setDisk
109             {
110 0     0 0 0 my ($self, $accountID, $diskSet) = @_;
111 0         0 my $accountIDF = sprintf("%lx", hex($accountID));
112            
113 0         0 $self->{$accountIDF} = $diskSet;
114            
115             }
116              
117              
118             sub getAccountIDs
119             {
120 1     1 1 7 my ($self) = @_;
121              
122 1         19 return sort numerically (keys %$self);
123              
124             }
125              
126              
127              
128             1;
129              
130              
131