chiark / gitweb /
do not sort since summer does
[chiark-utils.git] / backup / whatsthis
1 #!/usr/bin/perl
2 # whatsthis
3 # read an id off the tape and display it to the user
4
5 # This file is part of chiark backup, a system for backing up GNU/Linux and
6 # other UN*X-compatible machines, as used on chiark.greenend.org.uk.
7 #
8 # chiark backup is:
9 #  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
10 #  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
11 #
12 # This is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2, or (at your option) any later version.
15 #
16 # This is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19 # details.
20 #
21 # You should have received a copy of the GNU General Public License along
22 # with this program; if not, write to the Free Software Foundation, Inc.,
23 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 # First rough hack; mostly just code nabbed from full. 
26 # --list assumes the dump type was 'zafio', which is a bit bogus.
27
28 # whatsthis   : no args => just print tapeid
29 # whatsthis --list [n] : print tapeid then list archive n (if n omitted,
30 # 0 is assumed.) Note that archives are numbered from zero!
31
32 sub rewind();
33 sub stopandsay(@);
34
35 $etc='/etc/chiark-backup';
36 require "$etc/settings.pl";
37 require 'backuplib.pl';
38
39 $| = 1;
40
41 # This isn't intended to be run automatically, so don't bother 
42 # with setting status.
43
44 # If we are run with the argument --list then list the backup to 
45 # stdout. Otherwise just print the tape ID.
46 $listing = 0;  # default : don't list
47 $listing = 1 if ($ARGV[0] eq '--list');
48 $listarchive = 0;
49 $listarchive = $ARGV[1] if defined $ARGV[1];
50
51 print "Trying to read tape ID from currently inserted tape...\n";
52
53 unlink 'TAPEID';
54 system "mt -f $ntape setblk $blocksizebytes"; $? and die $?;
55 system "dd if=$ntape bs=${blocksize}b count=10 | tar -b$blocksize -vvxf - TAPEID";
56 $? and stopandsay "Failed to read TAPEID.\n";
57
58 if (!open(T, "TAPEID"))
59 {
60   stopandsay "Tape has no ID label.\n";
61 }
62
63 # OK, there's a TAPEID file, read the ID and check for sanity.
64 chomp($tapeid= <T>);
65 if ($tapeid =~ m/[^0-9a-zA-Z]/)
66 {
67    stopandsay "Tape has a bad (non-alphanumeric) TAPEID ($&).\n";
68 }
69 elsif (! $tapeid =~ m/[0-9a-zA-Z]/)
70 {
71    stopandsay "Empty TAPEID.\n";
72 }
73
74 print "TAPEID is $tapeid.\n";
75 close T;
76
77 # If we aren't listing the tape contents, we can just rewind the tape
78 # and exit.
79 if (!$listing)
80 {
81    rewind();
82    exit;
83 }
84
85 # List the contents of archive $listarchive on the tape.
86 # We are already at the right place for the first archive
87 # (after the TAPEID). 
88 # For any other archive, we skip forwards to the start of that archive.
89 if ($listarchive)
90 {
91    system "mt -f $ntape fsf $listarchive";
92    $? and stopandsay "Couldn't skip forward to archive $listarchive.";
93 }
94
95 # Use file to figure out what the archive type is
96 # This doesn't seem to work too well, so I'm disabling it -- PMM 
97 #$ftype = `dd if=$ntape ibs=$blocksizebytes | file -`;
98 #$? and stopandsay "couldn't determine file type: $?";
99 $ftype = 'POSIX tar';
100
101 # What we want to do here is roughly:
102 # dd if=$ntape ibs=$blocksizebytes | readbuffer | afio ???
103 #
104 # where the afio options are such as to list an archive created with
105 # afio -b $softblocksizebytes -Zvo
106
107 if ($ftype =~ /POSIX tar/) {
108    # POSIX tar archive; we read it with cpio
109    $reader = "cpio -it -C$softblocksizebytes -Hustar";
110 } elsif ($ftype =~ /ASCII cpio/) {
111    $reader = "afio -b $softblocksizebytes -Zt -";
112 } elsif ($ftype =~ /dump file/) {
113    stopandsay "sorry: can't list dump files yet";
114 } else {
115    stopandsay "listing failed: unknown archive type";
116 }
117
118 # Now back up so we can read the file again properly
119 #system "mt -f $ntape bsf 1"; $? and stopandsay "couldn't backspace tape: $?";
120
121 system "dd if=$ntape ibs=$blocksizebytes | readbuffer | $reader";
122 $? and stopandsay "listing failed: $?";
123
124 # All's well, stop here.
125 print "Listing complete.\n";
126 rewind();
127 exit;
128
129
130 # Rewind the tape.
131 sub rewind ()
132 {
133    system "mt -f $tape rewind"; $? and die $?;
134 }
135
136 # Print the given message, rewind the tape and exit failure.
137 sub stopandsay(@)
138 {
139    print @_;
140    rewind();
141    exit(1);
142 }