chiark / gitweb /
Now actually builds some packages. No idea if they work.
[chiark-utils.git] / backup / backuplib.pl
1 # backuplib.pl
2 # core common routines
3 #
4 # This file is part of chiark backup, a system for backing up GNU/Linux and
5 # other UN*X-compatible machines, as used on chiark.greenend.org.uk.
6 #
7 # chiark backup is:
8 #  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
9 #  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
10 #
11 # This is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2, or (at your option) any later version.
14 #
15 # This is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18 # details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
24 sub printdate () {
25     print scalar(localtime),"\n";
26 }
27
28 # Set status info -- we write the current status to a file 
29 # so if we hang or crash the last thing written to the file
30 # will tell us where we were when things went pear-shaped.
31 sub setstatus ($) {
32     open S, ">this-status.new" or die $!;
33     print S $_[0],"\n" or die $!;
34     close S or die $!;
35     rename "this-status.new","this-status" or die $!;
36 }
37
38 # startprocess, endprocesses, killprocesses are 
39 # used to implement the funky pipeline stuff.
40 sub startprocess ($$$) {
41     my ($i,$o,$c) = @_;
42     print LOG "  $c\n" or die $!;
43     print "  $c\n" or die $!;
44     defined($p= fork) or die $!;
45     if ($p) { $processes{$p}= $c; return; }
46     open STDIN,"$i" or die "$c stdin $i: $!";
47     open STDOUT,"$o" or die "$c stdout $o: $!";
48     &closepipes;
49     exec $c; die "$c: $!";
50 }
51
52 sub endprocesses () {
53     while (keys %processes) {
54         $p= waitpid(-1,0) or die "wait: $!";
55         if (!exists $processes{$p}) { warn "unknown pid exited: $p, code $?\n"; next; }
56         $c= $processes{$p};
57         delete $processes{$p};
58         $? && die "error: command gave code $?: $c\n";
59     }
60     print LOG "  ok\n" or die $!;
61     print "  ok\n" or die $!;
62 }
63
64 sub killprocesses {
65     for $p (keys %processes) {
66         kill 15,$p or warn "kill process $p: $!";
67     }
68     undef %processes;
69 }
70
71 # Read a fsys.foo filesystem group definition file.
72 # Syntax is: empty lines and those beginning with '#' are ignored.
73 # Trailing whitespace is ignored. Lines of the form 'prefix foo bar'
74 # are handled specially, as arex lines 'exclude regexp'; otherwise 
75 # we just shove the line into @fsys and let parsefsys deal with it.
76
77 sub readfsysfile ($) {
78     my ($fn) = @_;
79     my ($fh,$sfn);
80     $fh= new IO::File "$fn", "r" or die "cannot open fsys file $fn ($!).\n";
81     for (;;) {
82         $!=0; $_= <$fh> or die "unexpected EOF in $fn ($!)\n";
83         chomp; s/\s*$//;
84         last if m/^end$/;
85         next unless m/\S/;
86         next if m/^\#/;
87         if (m/^prefix\s+(\w+)\s+(\S.*\S)$/) {
88             $prefix{$1}= $2;
89         } elsif (m/^prefix\-df\s+(\w+)\s+(\S.*\S)$/) {
90             $prefixdf{$1}= $2;
91         } elsif (m/^excludedir\s+(\S.*\S)$/) {
92             push @excldir,$1;
93         } elsif (m/^exclude\s+(\S.*\S)$/) {
94             push @excl,$1;
95         } elsif (m/^include\s+(\S.*\S)$/) {
96             $sfn =~ s/^\./fsys./;
97             $sfn = "$etc/$sfn" if $sfn !~ m,^/,;
98             readfsysfile($sfn);
99         } else {
100             push @fsys,$_;
101         }
102     }
103     close $fn or die $!;
104 }
105
106 sub readfsys ($) {
107     my ($fsnm) = @_;
108     my ($fsf);
109     $fsf= "$etc/fsys.$fsnm";
110     stat $fsf or die "Filesystems $fsnm unknown ($!).\n";
111     readfsysfile($fsf);
112 }
113
114 # Parse a line from a filesystem definition file. We expect the line
115 # to be in $tf.
116 sub parsefsys () {
117     if ($tf =~ m,^(/\S*)\s+(\w+)$,) {
118         # Line of form '/file/system    dumptype'
119         $atf= $1;
120         $tm= $2;
121         $prefix= '<local>';
122         stat $atf or die "stat $atf: $!";
123         -d _ or die "not a dir: $atf";
124         $rstr= '';
125     } elsif ($tf =~ m,^(/\S*)\s+(\w+)\s+(\w+)$,) {
126         # Line of form '/file/system dumptype prefix'
127         # (used for remote backups, I think)
128         $atf= $1;
129         $tm= $2;
130         $prefix= $3;
131         defined($prefix{$prefix}) or die "prefix $prefix in $tf ?\n";
132         $rstr= $prefix{$prefix}.' ';
133     }
134 }
135
136 sub openlog () {
137     unlink 'log';
138     $u= umask(007);
139     open LOG, ">log" or die $!;
140     umask $u;
141     select(LOG); $|=1; select(STDOUT);
142 }
143
144 $SIG{'__DIE__'}= 'killprocesses';
145
146 1;