added only_increment param to all action=browse links
[BackupPC.git] / lib / BackupPC / Config / Text.pm
1 package BackupPC::Config::Text;
2
3 use warnings;
4 use strict;
5 use Fcntl qw/:flock/;
6
7 use base 'BackupPC::Config';
8
9 sub BackupInfoRead
10 {
11     my($self, $host) = @_;
12     local(*BK_INFO, *LOCK);
13     my(@Backups);
14
15     flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
16     if ( open(BK_INFO, "$self->{TopDir}/pc/$host/backups") ) {
17         while ( <BK_INFO> ) {
18             s/[\n\r]+//;
19             next if ( !/^(\d+\t(incr|full)[\d\t]*$)/ );
20             $_ = $1;
21             @{$Backups[@Backups]}{@{$self->{BackupFields}}} = split(/\t/);
22         }
23         close(BK_INFO);
24     }
25     close(LOCK);
26     return @Backups;
27 }
28
29 sub BackupInfoWrite
30 {
31     my($self, $host, @Backups) = @_;
32     local(*BK_INFO, *LOCK);
33     my($i);
34
35     flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
36     unlink("$self->{TopDir}/pc/$host/backups.old")
37                 if ( -f "$self->{TopDir}/pc/$host/backups.old" );
38     rename("$self->{TopDir}/pc/$host/backups",
39            "$self->{TopDir}/pc/$host/backups.old")
40                 if ( -f "$self->{TopDir}/pc/$host/backups" );
41     if ( open(BK_INFO, ">$self->{TopDir}/pc/$host/backups") ) {
42         for ( $i = 0 ; $i < @Backups ; $i++ ) {
43             my %b = %{$Backups[$i]};
44             printf(BK_INFO "%s\n", join("\t", @b{@{$self->{BackupFields}}}));
45         }
46         close(BK_INFO);
47     }
48     close(LOCK);
49 }
50
51 sub RestoreInfoRead
52 {
53     my($self, $host) = @_;
54     local(*RESTORE_INFO, *LOCK);
55     my(@Restores);
56
57     flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
58     if ( open(RESTORE_INFO, "$self->{TopDir}/pc/$host/restores") ) {
59         while ( <RESTORE_INFO> ) {
60             s/[\n\r]+//;
61             next if ( !/^(\d+.*)/ );
62             $_ = $1;
63             @{$Restores[@Restores]}{@{$self->{RestoreFields}}} = split(/\t/);
64         }
65         close(RESTORE_INFO);
66     }
67     close(LOCK);
68     return @Restores;
69 }
70
71 sub RestoreInfoWrite
72 {
73     my($self, $host, @Restores) = @_;
74     local(*RESTORE_INFO, *LOCK);
75     my($i);
76
77     flock(LOCK, LOCK_EX) if open(LOCK, "$self->{TopDir}/pc/$host/LOCK");
78     unlink("$self->{TopDir}/pc/$host/restores.old")
79                 if ( -f "$self->{TopDir}/pc/$host/restores.old" );
80     rename("$self->{TopDir}/pc/$host/restores",
81            "$self->{TopDir}/pc/$host/restores.old")
82                 if ( -f "$self->{TopDir}/pc/$host/restores" );
83     if ( open(RESTORE_INFO, ">$self->{TopDir}/pc/$host/restores") ) {
84         for ( $i = 0 ; $i < @Restores ; $i++ ) {
85             my %b = %{$Restores[$i]};
86             printf(RESTORE_INFO "%s\n",
87                         join("\t", @b{@{$self->{RestoreFields}}}));
88         }
89         close(RESTORE_INFO);
90     }
91     close(LOCK);
92 }
93
94 sub ConfigRead
95 {
96     my($self, $host) = @_;
97     my($ret, $mesg, $config, @configs);
98     
99     our %Conf;
100
101     $self->{Conf} = ();
102     push(@configs, "$self->{TopDir}/conf/config.pl");
103     push(@configs, "$self->{TopDir}/pc/$host/config.pl")
104             if ( defined($host) && -f "$self->{TopDir}/pc/$host/config.pl" );
105     foreach $config ( @configs ) {
106         %Conf = ();
107         if ( !defined($ret = do $config) && ($! || $@) ) {
108             $mesg = "Couldn't open $config: $!" if ( $! );
109             $mesg = "Couldn't execute $config: $@" if ( $@ );
110             $mesg =~ s/[\n\r]+//;
111             return $mesg;
112         }
113         %{$self->{Conf}} = ( %{$self->{Conf} || {}}, %Conf );
114     }
115     
116     #$mesg = $self->CheckConfigInfo;
117     #return $mesg if $mesg;
118     
119     return if ( !defined($self->{Conf}{Language}) );
120     
121     my $langFile = "$self->{LibDir}/BackupPC/Lang/$self->{Conf}{Language}.pm";
122     
123     if ( !defined($ret = do $langFile) && ($! || $@) ) {
124         $mesg = "Couldn't open language file $langFile: $!" if ( $! );
125         $mesg = "Couldn't execute language file $langFile: $@" if ( $@ );
126         $mesg =~ s/[\n\r]+//;
127         return $mesg;
128     }
129     
130     our %Lang;
131     $self->{Lang} = \%Lang;
132     
133     return;
134 }
135
136
137 #
138 # Return the mtime of the config file
139 #
140 sub ConfigMTime
141 {
142     my($self) = @_;
143     return (stat("$self->{TopDir}/conf/config.pl"))[9];
144 }
145
146 #
147 # Returns information from the host file in $self->{TopDir}/conf/hosts.
148 # With no argument a ref to a hash of hosts is returned.  Each
149 # hash contains fields as specified in the hosts file.  With an
150 # argument a ref to a single hash is returned with information
151 # for just that host.
152 #
153 sub HostInfoRead
154 {
155     my($self, $host) = @_;
156     my(%hosts, @hdr, @fld);
157     local(*HOST_INFO);
158
159     if ( !open(HOST_INFO, "$self->{TopDir}/conf/hosts") ) {
160         print(STDERR $self->timeStamp,
161                      "Can't open $self->{TopDir}/conf/hosts\n");
162         return {};
163     }
164     while ( <HOST_INFO> ) {
165         s/[\n\r]+//;
166         s/#.*//;
167         s/\s+$//;
168         next if ( /^\s*$/ || !/^([\w\.-]+\s+.*)/ );
169         @fld = split(/\s+/, $1);
170         if ( @hdr ) {
171             if ( defined($host) ) {
172                 next if ( lc($fld[0]) ne $host );
173                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
174                 close(HOST_INFO);
175                 return \%hosts;
176             } else {
177                 @{$hosts{lc($fld[0])}}{@hdr} = @fld;
178             }
179         } else {
180             @hdr = @fld;
181         }
182     }
183     close(HOST_INFO);
184     return \%hosts;
185 }
186
187 #
188 # Return the mtime of the hosts file
189 #
190 sub HostsMTime
191 {
192     my($self) = @_;
193     return (stat("$self->{TopDir}/conf/hosts"))[9];
194 }
195
196
197
198 1;