1 # Copyright (c) 2010-2012 Zmanda, Inc. All Rights Reserved.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20 package Amanda::Curinfo;
26 use File::Path qw( mkpath );
28 use Amanda::Debug qw( :logging );
29 use Amanda::Util qw( sanitise_filename );
31 use Amanda::Curinfo::Info;
35 Amanda::Curinfo - Perl extension for representing the curinfo database
40 use Amanda::Curinfo::Info;
44 my $ci = Amanda::Curinfo->new($somedir);
45 my $info = $ci->get_info($host, $disk);
49 $ci->put_info($host, $disk, $newinfo);
53 $ci->del_info($oldhost, $olddisk);
57 C<Amanda::Curinfo> is a pure perl implementation of the older infofile
60 This package manages a directory of files, referred to in the code as
61 an C<$infodir>, that contain dump data. Each of these files is stored
62 in a nested directory structure by its host and disk name. These
63 files can be accessed and modified using the provided functions with
64 the help of the L<Amanda::Curinfo::Info> class.
66 Note that this terminology is slightly different from the older
67 infofile.h implementation. Users with no experience with infofile.h
68 can skip to the interface section.
70 In the API for infofile.h, the term C<infofile> actually refers a
71 directory. This directory is called an C<infodir> within the
72 infofile.c code. This directory held text files, which are referred
73 to as both C<infofile> and C<txinfofile> internally to infofile.c.
75 This rewrite simplifies the terminology by referring to the storage
76 directory as an C<$infodir> and an individual data-storing file as a
82 C<Amanda::Curinfo> is an interface to retrieve and store info files
83 regarding the backup history of DLEs.
85 C<Amanda::Curinfo> provides three major routines for handling info
88 The C<Amanda::Curinfo> constructor is pretty straightforward:
90 my $ci = Amanda::Curinfo->new($infodir);
92 Where C<$infodir> is a directory. In order to retrieve a previously
93 stored info file if the host and disk are known, one can use
95 my $info = $ci->get_info($host, $disk);
97 Once the structure has been updated, it may be re-written to the
98 database in a similar fashion:
100 $ci->put_info($host, $disk, $info);
102 If one would like to erase an existing info entry in an infodir, the
103 usage is the same as retrieving an info object.
105 $ci->del_info($host, $disk);
107 To create a new info object, please see the documentation for
108 L<Amanda::Curinfo::Info>.
112 This module is meant to replicate the behavior of the library
113 described in server-src/infofile.h. If anyone notices any major
114 problems, please report them.
118 Paul C. Mantz E<lt>pcmantz@zmanda.comE<gt>
125 my ($class, $infodir) = @_;
128 || croak("error: infodir not provided to Amanda::Curinfo");
130 my $self = { infodir => $infodir };
138 my ($self, $host, $disk) = @_;
140 my $infodir = $self->{infodir};
141 my $host_q = sanitise_filename($host);
142 my $disk_q = sanitise_filename($disk);
143 my $infofile = "$infodir/$host_q/$disk_q/info";
145 return Amanda::Curinfo::Info->new($infofile);
150 my ($self, $host, $disk, $info) = @_;
152 my $infodir = $self->{infodir};
153 my $host_q = sanitise_filename($host);
154 my $disk_q = sanitise_filename($disk);
155 my $infofiledir = "$infodir/$host_q/$disk_q";
156 my $infofile = "$infofiledir/info";
157 my $infofile_tmp = "$infofile.tmp";
160 copy($infofile, $infofile_tmp)
161 || croak "error: couldn't back up $infofile";
162 } elsif (!-d $infofiledir) {
164 || croak "error: couldn't make path $infofiledir";
168 if (-e $infofile_tmp) {
169 copy($infofile_tmp, $infofile)
171 "error: couldn't restore infofile from backup $infofile_tmp";
172 unlink $infofile_tmp;
174 croak "error encountered when writing info to $infofile";
177 $info->write_to_file($infofile) || $restore->();
178 unlink $infofile_tmp if -e $infofile_tmp;
184 my ($self, $host, $disk) = @_;
186 my $infodir = $self->{infodir};
187 my $host_q = sanitise_filename($host);
188 my $disk_q = sanitise_filename($disk);
189 my $infofile = "$infodir/$host_q/$disk_q/info";
191 return unlink $infofile;