Bugfix (tiny change)
[tar/ericb.git] / scripts / tar-snapshot-edit
blob1fae773eb6afa01bb304792399e26a686f1ee879
1 #! /usr/bin/perl -w
2 # Display and edit the 'dev' field in tar's snapshots
3 # Copyright (C) 2007 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3, or (at your option)
8 # any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
20 # Author: Dustin J. Mitchell <dustin@zmanda.com>
22 # This script is capable of replacing values in the 'dev' field of an
23 # incremental backup 'snapshot' file. This is useful when the device
24 # used to store files in a tar archive changes, without the files
25 # themselves changing. This may happen when, for example, a device
26 # driver changes major or minor numbers.
28 use Getopt::Std;
30 ## reading
32 sub read_incr_db ($) {
33 my $filename = shift;
34 open(my $file, "<$filename") || die "Could not open '$filename' for reading";
36 my $header_str = <$file>;
37 my $file_version;
38 if ($header_str =~ /^GNU tar-[^-]*-([0-9]+)\n$/) {
39 $file_version = $1+0;
40 } else {
41 $file_version = 0;
44 print "file version $file_version\n";
46 if ($file_version == 0) {
47 return read_incr_db_0($file, $header_str);
48 } elsif ($file_version == 1) {
49 return read_incr_db_1($file);
50 } elsif ($file_version == 2) {
51 return read_incr_db_2($file);
52 } else {
53 die "Unrecognized snapshot version in header '$header_str'";
57 sub read_incr_db_0 ($$) {
58 my $file = shift;
59 my $header_str = shift;
61 my $hdr_timestamp_sec = $header_str;
62 chop $hdr_timestamp_sec;
63 my $hdr_timestamp_nsec = ''; # not present in file format 0
65 my @dirs;
67 while (<$file>) {
68 /^([0-9]*) ([0-9]*) (.*)\n$/ || die("Bad snapshot line $_");
70 push @dirs, { dev=>$1,
71 ino=>$2,
72 name=>$3 };
75 close($file);
77 # file version, timestamp, timestamp, dir list
78 return [ 0, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs ];
81 sub read_incr_db_1 ($) {
82 my $file = shift;
84 my $timestamp = <$file>; # "sec nsec"
85 my ($hdr_timestamp_sec, $hdr_timestamp_nsec) = ($timestamp =~ /([0-9]*) ([0-9]*)/);
87 my @dirs;
89 while (<$file>) {
90 /^([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*) (.*)\n$/ || die("Bad snapshot line $_");
92 push @dirs, { timestamp_sec=>$1,
93 timestamp_nsec=>$2,
94 dev=>$3,
95 ino=>$4,
96 name=>$5 };
99 close($file);
101 # file version, timestamp, timestamp, dir list
102 return [ 1, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs ];
105 sub read_incr_db_2 ($) {
106 my $file = shift;
108 $/="\0"; # $INPUT_RECORD_SEPARATOR
109 my $hdr_timestamp_sec = <$file>;
110 chop $hdr_timestamp_sec;
111 my $hdr_timestamp_nsec = <$file>;
112 chop $hdr_timestamp_nsec;
113 my @dirs;
115 while (1) {
116 last if eof($file);
118 my $nfs = <$file>;
119 my $timestamp_sec = <$file>;
120 my $timestamp_nsec = <$file>;
121 my $dev = <$file>;
122 my $ino = <$file>;
123 my $name = <$file>;
125 # get rid of trailing NULs
126 chop $nfs;
127 chop $timestamp_sec;
128 chop $timestamp_nsec;
129 chop $dev;
130 chop $ino;
131 chop $name;
133 my @dirents;
134 while (my $dirent = <$file>) {
135 chop $dirent;
136 push @dirents, $dirent;
137 last if ($dirent eq "");
139 die "missing terminator" unless (<$file> eq "\0");
141 push @dirs, { nfs=>$nfs,
142 timestamp_sec=>$timestamp_sec,
143 timestamp_nsec=>$timestamp_nsec,
144 dev=>$dev,
145 ino=>$ino,
146 name=>$name,
147 dirents=>\@dirents };
150 close($file);
151 $/ = "\n"; # reset to normal
153 # file version, timestamp, timestamp, dir list
154 return [ 2, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs ];
157 ## display
159 sub show_device_counts ($$) {
160 my $info = shift;
161 my $filename = shift;
162 my %devices;
163 foreach my $dir (@{${@$info}[3]}) {
164 my $dev = ${%$dir}{'dev'};
165 $devices{$dev}++;
168 foreach $dev (sort keys %devices) {
169 printf "$filename: Device 0x%04x occurs $devices{$dev} times.\n", $dev;
173 ## editing
175 sub replace_device_number ($@) {
176 my $info = shift(@_);
177 my @repl = @_;
179 foreach my $dir (@{${@$info}[3]}) {
180 foreach $x (@repl) {
181 if (${%$dir}{'dev'} eq $$x[0]) {
182 ${%$dir}{'dev'} = $$x[1];
183 last;
189 ## writing
191 sub write_incr_db ($$) {
192 my $info = shift;
193 my $filename = shift;
194 my $file_version = $$info[0];
196 open($file, ">$filename") || die "Could not open '$filename' for writing";
198 if ($file_version == 0) {
199 write_incr_db_0($info, $file);
200 } elsif ($file_version == 1) {
201 write_incr_db_1($info, $file);
202 } elsif ($file_version == 2) {
203 write_incr_db_2($info, $file);
204 } else {
205 die "Unknown file version $file_version.";
208 close($file);
211 sub write_incr_db_0 ($$) {
212 my $info = shift;
213 my $file = shift;
215 my $timestamp_sec = $info->[1];
216 print $file "$timestamp_sec\n";
218 foreach my $dir (@{${@$info}[3]}) {
219 print $file "${%$dir}{'dev'} ";
220 print $file "${%$dir}{'ino'} ";
221 print $file "${%$dir}{'name'}\n";
226 sub write_incr_db_1 ($$) {
227 my $info = shift;
228 my $file = shift;
230 print $file "GNU tar-1.15-1\n";
232 my $timestamp_sec = $info->[1];
233 my $timestamp_nsec = $info->[2];
234 print $file "$timestamp_sec $timestamp_nsec\n";
236 foreach my $dir (@{${@$info}[3]}) {
237 print $file "${%$dir}{'timestamp_sec'} ";
238 print $file "${%$dir}{'timestamp_nsec'} ";
239 print $file "${%$dir}{'dev'} ";
240 print $file "${%$dir}{'ino'} ";
241 print $file "${%$dir}{'name'}\n";
246 sub write_incr_db_2 ($$) {
247 my $info = shift;
248 my $file = shift;
250 print $file "GNU tar-1.16-2\n";
252 my $timestamp_sec = $info->[1];
253 my $timestamp_nsec = $info->[2];
254 print $file $timestamp_sec . "\0";
255 print $file $timestamp_nsec . "\0";
257 foreach my $dir (@{${@$info}[3]}) {
258 print $file ${%$dir}{'nfs'} . "\0";
259 print $file ${%$dir}{'timestamp_sec'} . "\0";
260 print $file ${%$dir}{'timestamp_nsec'} . "\0";
261 print $file ${%$dir}{'dev'} . "\0";
262 print $file ${%$dir}{'ino'} . "\0";
263 print $file ${%$dir}{'name'} . "\0";
264 foreach my $dirent (@{${%$dir}{'dirents'}}) {
265 print $file $dirent . "\0";
267 print $file "\0";
271 ## main
273 sub main {
274 our ($opt_b, $opt_r, $opt_h);
275 getopts('br:h');
276 HELP_MESSAGE() if ($opt_h || $#ARGV == -1 || ($opt_b && !$opt_r));
278 my @repl;
279 if ($opt_r) {
280 foreach my $spec (split(/,/, $opt_r)) {
281 ($spec =~ /^([^-]+)-([^-]+)/) || die "Invalid replacement specification '$opt_r'";
282 push @repl, [interpret_dev($1), interpret_dev($2)];
286 foreach my $snapfile (@ARGV) {
287 my $info = read_incr_db($snapfile);
288 if ($opt_r ) {
289 if ($opt_b) {
290 rename($snapfile, $snapfile . "~") || die "Could not rename '$snapfile' to backup";
293 replace_device_number($info, @repl);
294 write_incr_db($info, $snapfile);
295 } else {
296 show_device_counts($info, $snapfile);
301 sub HELP_MESSAGE {
302 print "Usage: tar-snapshot-edit.pl [-r 'DEV1-DEV2[,DEV3-DEV4...]' [-b]] SNAPFILE [SNAPFILE [..]]\n";
303 print "\n";
304 print " Without -r, summarize the 'device' values in each SNAPFILE.\n";
305 print "\n";
306 print " With -r, replace occurrences of DEV1 with DEV2 in each SNAPFILE.\n";
307 print " DEV1 and DEV2 may be specified in hex (e.g., 0xfe01), decimal (e.g.,\n";
308 print " 65025), or MAJ:MIN (e.g., 254:1). To replace multiple occurrences,\n";
309 print " separate them with commas. If -b is also specified, backup\n";
310 print " files (ending with '~') will be created.\n";
311 exit 1;
314 sub interpret_dev ($) {
315 my $dev = shift;
317 if ($dev =~ /^([0-9]+):([0-9]+)$/) {
318 return $1 * 256 + $2;
319 } elsif ($dev =~ /^0x[0-9a-fA-F]+$/) {
320 return oct $dev;
321 } elsif ($dev =~ /^[0-9]+$/) {
322 return $dev+0;
323 } else {
324 die "Invalid device specification '$dev'";
328 main