chiark / gitweb /
From Peter Maydell (/u2/pmaydell/iwjbackup/) as per <E156bwm-0005xq-00@watchdragon...
[chiark-utils.git] / backup / whatsthis
1 #!/usr/bin/perl
2
3 # whatsthis : just read an ID off the tape and display it to the user.
4 # Peter Maydell <pmaydell@chiark.greenend.org.uk>
5 # First rough hack; mostly just code nabbed from full. 
6 # --list assumes the dump type was 'zafio', which is a bit bogus.
7
8 # whatsthis   : no args => just print tapeid
9 # whatsthis --list [n] : print tapeid then list archive n (if n omitted,
10 # 0 is assumed.) Note that archives are numbered from zero!
11
12 sub rewind();
13 sub stopandsay(@);
14
15 $etc='/etc/backup';
16 require "$etc/settings.pl";
17 require 'backuplib.pl';
18
19 $| = 1;
20
21 # This isn't intended to be run automatically, so don't bother 
22 # with setting status.
23
24 # If we are run with the argument --list then list the backup to 
25 # stdout. Otherwise just print the tape ID.
26 $listing = 0;  # default : don't list
27 $listing = 1 if ($ARGV[0] eq '--list');
28 $listarchive = 0;
29 $listarchive = $ARGV[1] if defined $ARGV[1];
30
31 print "Trying to read tape ID from currently inserted tape...\n";
32
33 unlink 'TAPEID';
34 system "mt -f $ntape setblk $blocksizebytes"; $? and die $?;
35 system "dd if=$ntape bs=${blocksize}b count=10 | tar -b$blocksize -vvxf - TAPEID";
36 $? and stopandsay "Failed to read TAPEID.\n";
37
38 if (!open(T, "TAPEID"))
39 {
40   stopandsay "Tape has no ID label.\n";
41 }
42
43 # OK, there's a TAPEID file, read the ID and check for sanity.
44 chomp($tapeid= <T>);
45 if ($tapeid =~ m/[^0-9a-zA-Z]/)
46 {
47    stopandsay "Tape has a bad (non-alphanumeric) TAPEID ($&).\n";
48 }
49 elsif (! $tapeid =~ m/[0-9a-zA-Z]/)
50 {
51    stopandsay "Empty TAPEID.\n";
52 }
53
54 print "TAPEID is $tapeid.\n";
55 close T;
56
57 # If we aren't listing the tape contents, we can just rewind the tape
58 # and exit.
59 if (!$listing)
60 {
61    rewind();
62    exit;
63 }
64
65 # List the contents of archive $listarchive on the tape.
66 # We are already at the right place for the first archive
67 # (after the TAPEID). 
68 # For any other archive, we skip forwards to the start of that archive.
69 if ($listarchive)
70 {
71    system "mt -f $ntape fsf $listarchive";
72    $? and stopandsay "Couldn't skip forward to archive $listarchive.";
73 }
74
75 # Use file to figure out what the archive type is
76 # This doesn't seem to work too well, so I'm disabling it -- PMM 
77 #$ftype = `dd if=$ntape ibs=$blocksizebytes | file -`;
78 #$? and stopandsay "couldn't determine file type: $?";
79 $ftype = 'POSIX tar';
80
81 # What we want to do here is roughly:
82 # dd if=$ntape ibs=$blocksizebytes | readbuffer | afio ???
83 #
84 # where the afio options are such as to list an archive created with
85 # afio -b $softblocksizebytes -Zvo
86
87 if ($ftype =~ /POSIX tar/) {
88    # POSIX tar archive; we read it with cpio
89    $reader = "cpio -it -C$softblocksizebytes -Hustar";
90 } elsif ($ftype =~ /ASCII cpio/) {
91    $reader = "afio -b $softblocksizebytes -Zt -";
92 } elsif ($ftype =~ /dump file/) {
93    stopandsay "sorry: can't list dump files yet";
94 } else {
95    stopandsay "listing failed: unknown archive type";
96 }
97
98 # Now back up so we can read the file again properly
99 #system "mt -f $ntape bsf 1"; $? and stopandsay "couldn't backspace tape: $?";
100
101 system "dd if=$ntape ibs=$blocksizebytes | /usr/local/lib/backup/readbuffer | $reader";
102 $? and stopandsay "listing failed: $?";
103
104 # All's well, stop here.
105 print "Listing complete.\n";
106 rewind();
107 exit;
108
109
110 # Rewind the tape.
111 sub rewind ()
112 {
113    system "mt -f $tape rewind"; $? and die $?;
114 }
115
116 # Print the given message, rewind the tape and exit failure.
117 sub stopandsay(@)
118 {
119    print @_;
120    rewind();
121    exit(1);
122 }