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