tar-snapshot-edit 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #! /usr/bin/perl -w
  2. # Display and edit the 'dev' field in tar's snapshots
  3. # Copyright 2007, 2011, 2013 Free Software Foundation, Inc.
  4. # This file is part of GNU tar.
  5. # GNU tar 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 of the License, or
  8. # (at your option) any later version.
  9. # GNU tar is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. # tar-snapshot-edit
  16. #
  17. # This script is capable of replacing values in the 'dev' field of an
  18. # incremental backup 'snapshot' file. This is useful when the device
  19. # used to store files in a tar archive changes, without the files
  20. # themselves changing. This may happen when, for example, a device
  21. # driver changes major or minor numbers.
  22. #
  23. # It can also run a check on all the field values found in the
  24. # snapshot file, printing out a detailed message when it finds values
  25. # that would cause an "Unexpected field value in snapshot file" error
  26. # if tar were run using that snapshot file as input. (See the
  27. # comments included in the definition of the check_field_values
  28. # routine for more detailed information regarding these checks.)
  29. #
  30. #
  31. #
  32. # Author: Dustin J. Mitchell <dustin@zmanda.com>
  33. #
  34. # Modified Aug 25, 2011 by Nathan Stratton Treadway <nathanst AT ontko.com>:
  35. # * update Perl syntax to work correctly with more recent versions of
  36. # Perl. (The original code worked with in the v5.8 timeframe but
  37. # not with Perl v5.10.1 and later.)
  38. # * added a "-c" option to check the snapshot file for invalid field values.
  39. # * handle NFS indicator character ("+") in version 0 and 1 files
  40. # * preserve the original header/version line when editing version 1
  41. # or 2 files.
  42. # * tweak output formatting
  43. #
  44. #
  45. use Getopt::Std;
  46. ## reading
  47. sub read_incr_db ($) {
  48. my $filename = shift;
  49. open(my $file, "<$filename") || die "Could not open '$filename' for reading";
  50. my $header_str = <$file>;
  51. my $file_version;
  52. if ($header_str =~ /^GNU tar-[^-]*-([0-9]+)\n$/) {
  53. $file_version = $1+0;
  54. } else {
  55. $file_version = 0;
  56. }
  57. print "\nFile: $filename\n";
  58. print " Detected snapshot file version: $file_version\n\n";
  59. if ($file_version == 0) {
  60. return read_incr_db_0($file, $header_str);
  61. } elsif ($file_version == 1) {
  62. return read_incr_db_1($file, $header_str);
  63. } elsif ($file_version == 2) {
  64. return read_incr_db_2($file, $header_str);
  65. } else {
  66. die "Unrecognized snapshot version in header '$header_str'";
  67. }
  68. }
  69. sub read_incr_db_0 ($$) {
  70. my $file = shift;
  71. my $header_str = shift;
  72. my $hdr_timestamp_sec = $header_str;
  73. chop $hdr_timestamp_sec;
  74. my $hdr_timestamp_nsec = ''; # not present in file format 0
  75. my $nfs;
  76. my @dirs;
  77. while (<$file>) {
  78. /^(\+?)([0-9]*) ([0-9]*) (.*)\n$/ || die("Bad snapshot line $_");
  79. if ( $1 eq "+" ) {
  80. $nfs="1";
  81. } else {
  82. $nfs="0";
  83. }
  84. push @dirs, { nfs=>$nfs,
  85. dev=>$2,
  86. ino=>$3,
  87. name=>$4 };
  88. }
  89. close($file);
  90. # file version, timestamp, timestamp, dir list, file header line
  91. return [ 0, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs, ""];
  92. }
  93. sub read_incr_db_1 ($$) {
  94. my $file = shift;
  95. my $header_str = shift;
  96. my $timestamp = <$file>; # "sec nsec"
  97. my ($hdr_timestamp_sec, $hdr_timestamp_nsec) = ($timestamp =~ /([0-9]*) ([0-9]*)/);
  98. my $nfs;
  99. my @dirs;
  100. while (<$file>) {
  101. /^(\+?)([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*) (.*)\n$/ || die("Bad snapshot line $_");
  102. if ( $1 eq "+" ) {
  103. $nfs="1";
  104. } else {
  105. $nfs="0";
  106. }
  107. push @dirs, { nfs=>$nfs,
  108. timestamp_sec=>$2,
  109. timestamp_nsec=>$3,
  110. dev=>$4,
  111. ino=>$5,
  112. name=>$6 };
  113. }
  114. close($file);
  115. # file version, timestamp, timestamp, dir list, file header line
  116. return [ 1, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs, $header_str ];
  117. }
  118. sub read_incr_db_2 ($$) {
  119. my $file = shift;
  120. my $header_str = shift;
  121. $/="\0"; # $INPUT_RECORD_SEPARATOR
  122. my $hdr_timestamp_sec = <$file>;
  123. chop $hdr_timestamp_sec;
  124. my $hdr_timestamp_nsec = <$file>;
  125. chop $hdr_timestamp_nsec;
  126. my @dirs;
  127. while (1) {
  128. last if eof($file);
  129. my $nfs = <$file>;
  130. my $timestamp_sec = <$file>;
  131. my $timestamp_nsec = <$file>;
  132. my $dev = <$file>;
  133. my $ino = <$file>;
  134. my $name = <$file>;
  135. # get rid of trailing NULs
  136. chop $nfs;
  137. chop $timestamp_sec;
  138. chop $timestamp_nsec;
  139. chop $dev;
  140. chop $ino;
  141. chop $name;
  142. my @dirents;
  143. while (my $dirent = <$file>) {
  144. chop $dirent;
  145. push @dirents, $dirent;
  146. last if ($dirent eq "");
  147. }
  148. die "missing terminator" unless (<$file> eq "\0");
  149. push @dirs, { nfs=>$nfs,
  150. timestamp_sec=>$timestamp_sec,
  151. timestamp_nsec=>$timestamp_nsec,
  152. dev=>$dev,
  153. ino=>$ino,
  154. name=>$name,
  155. dirents=>\@dirents };
  156. }
  157. close($file);
  158. $/ = "\n"; # reset to normal
  159. # file version, timestamp, timestamp, dir list, file header line
  160. return [ 2, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs, $header_str];
  161. }
  162. ## display
  163. sub show_device_counts ($) {
  164. my $info = shift;
  165. my %devices;
  166. foreach my $dir (@{$info->[3]}) {
  167. my $dev = $dir->{'dev'};
  168. $devices{$dev}++;
  169. }
  170. foreach $dev (sort {$a <=> $b} keys %devices) {
  171. printf " Device 0x%04x occurs $devices{$dev} times.\n", $dev;
  172. }
  173. }
  174. ## check field values
  175. # returns a warning message if $field isn't a valid string representation
  176. # of an integer, or if the resulting integer is out of the specified range
  177. sub validate_integer_field ($$$$) {
  178. my $field = shift;
  179. my $field_name = shift;
  180. my $min = shift;
  181. my $max = shift;
  182. my $msg = "";
  183. if ( not $field =~ /^-?\d+$/ ) {
  184. $msg = " $field_name value contains invalid characters: \"$field\"\n";
  185. } else {
  186. if ( $field < $min ) {
  187. $msg = " $field_name value too low: \"$field\" < $min \n";
  188. } elsif ( $field > $max ) {
  189. $msg = " $field_name value too high: \"$field\" > $max \n";
  190. }
  191. }
  192. return $msg;
  193. }
  194. # This routine loops through each directory entry in the $info data
  195. # structure and prints a warning message if tar would abort with an
  196. # "Unexpected field value in snapshot file" error upon reading this
  197. # snapshot file.
  198. #
  199. # (Note that this specific error message was introduced along with the
  200. # change to snapshot file format "2", starting with tar v1.16 [or,
  201. # more precisely, v1.15.91].)
  202. #
  203. # The checks here are intended to match those found in the incremen.c
  204. # source file (as of tar v1.16.1).
  205. #
  206. # In that code, the checks are done against pre-processor expressions,
  207. # as defined in the C header files at compile time. In the routine
  208. # below, a Perl variable is created for each expression used as part of
  209. # one of these checks, assigned the value of the related pre-processor
  210. # expression as found on a Linux 2.6.8/i386 system.
  211. #
  212. # It seems likely that these settings will catch most invalid
  213. # field values found in actual snapshot files on all systems. However,
  214. # if "tar" is erroring out on a snapshot file that this check routine
  215. # does not complain about, that probably indicates that the values
  216. # below need to be adjusted to match those used by "tar" in that
  217. # particular environment.
  218. #
  219. # (Note: the checks here are taken from the code that processes
  220. # version 2 snapshot files, but to keep things simple we apply those
  221. # same checks to files having earlier versions -- but only for
  222. # the fields that actually exist in those input files.)
  223. sub check_field_values ($) {
  224. my $info = shift;
  225. # set up a variable with the value of each pre-processor
  226. # expression used for field-value checks in incremen.c
  227. # (these values here are from a Linux 2.6.8/i386 system)
  228. my $BILLION = 1000000000; # BILLION
  229. my $MIN_TIME_T = -2147483648; # TYPE_MINIMUM(time_t)
  230. my $MAX_TIME_T = 2147483647; # TYPE_MAXIUMUM(time_t)
  231. my $MAX_DEV_T = 4294967295; # TYPE_MAXIUMUM(dev_t)
  232. my $MAX_INO_T = 4294967295; # TYPE_MAXIUMUM(ino_t)
  233. my $msg;
  234. my $error_found = 0;
  235. print " Checking field values in snapshot file...\n";
  236. $snapver = $info->[0];
  237. $msg = "";
  238. $msg .= validate_integer_field($info->[1],
  239. 'timestamp_sec', $MIN_TIME_T, $MAX_TIME_T);
  240. if ($snapver >= 1) {
  241. $msg .= validate_integer_field($info->[2],
  242. 'timestamp_nsec', 0, $BILLION-1);
  243. }
  244. if ( $msg ne "" ) {
  245. $error_found = 1;
  246. print "\n shapshot file header:\n";
  247. print $msg;
  248. }
  249. foreach my $dir (@{$info->[3]}) {
  250. $msg = "";
  251. $msg .= validate_integer_field($dir->{'nfs'}, 'nfs', 0, 1);
  252. if ($snapver >= 1) {
  253. $msg .= validate_integer_field($dir->{'timestamp_sec'},
  254. 'timestamp_sec', $MIN_TIME_T, $MAX_TIME_T);
  255. $msg .= validate_integer_field($dir->{'timestamp_nsec'},
  256. 'timestamp_nsec', 0, $BILLION-1);
  257. }
  258. $msg .= validate_integer_field($dir->{'dev'}, 'dev', 0, $MAX_DEV_T);
  259. $msg .= validate_integer_field($dir->{'ino'}, 'ino', 0, $MAX_INO_T);
  260. if ( $msg ne "" ) {
  261. $error_found = 1;
  262. print "\n directory: $dir->{'name'}\n";
  263. print $msg;
  264. }
  265. }
  266. print "\n Snapshot field value check complete" ,
  267. $error_found ? "" : ", no errors found" ,
  268. ".\n";
  269. }
  270. ## editing
  271. sub replace_device_number ($@) {
  272. my $info = shift(@_);
  273. my @repl = @_;
  274. my $count = 0;
  275. foreach my $dir (@{$info->[3]}) {
  276. foreach $x (@repl) {
  277. if ($dir->{'dev'} eq $$x[0]) {
  278. $dir->{'dev'} = $$x[1];
  279. $count++;
  280. last;
  281. }
  282. }
  283. }
  284. print " Updated $count records.\n"
  285. }
  286. ## writing
  287. sub write_incr_db ($$) {
  288. my $info = shift;
  289. my $filename = shift;
  290. my $file_version = $$info[0];
  291. open($file, ">$filename") || die "Could not open '$filename' for writing";
  292. if ($file_version == 0) {
  293. write_incr_db_0($info, $file);
  294. } elsif ($file_version == 1) {
  295. write_incr_db_1($info, $file);
  296. } elsif ($file_version == 2) {
  297. write_incr_db_2($info, $file);
  298. } else {
  299. die "Unknown file version $file_version.";
  300. }
  301. close($file);
  302. }
  303. sub write_incr_db_0 ($$) {
  304. my $info = shift;
  305. my $file = shift;
  306. my $timestamp_sec = $info->[1];
  307. print $file "$timestamp_sec\n";
  308. foreach my $dir (@{$info->[3]}) {
  309. if ($dir->{'nfs'}) {
  310. print $file '+'
  311. }
  312. print $file "$dir->{'dev'} ";
  313. print $file "$dir->{'ino'} ";
  314. print $file "$dir->{'name'}\n";
  315. }
  316. }
  317. sub write_incr_db_1 ($$) {
  318. my $info = shift;
  319. my $file = shift;
  320. print $file $info->[4];
  321. my $timestamp_sec = $info->[1];
  322. my $timestamp_nsec = $info->[2];
  323. print $file "$timestamp_sec $timestamp_nsec\n";
  324. foreach my $dir (@{$info->[3]}) {
  325. if ($dir->{'nfs'}) {
  326. print $file '+'
  327. }
  328. print $file "$dir->{'timestamp_sec'} ";
  329. print $file "$dir->{'timestamp_nsec'} ";
  330. print $file "$dir->{'dev'} ";
  331. print $file "$dir->{'ino'} ";
  332. print $file "$dir->{'name'}\n";
  333. }
  334. }
  335. sub write_incr_db_2 ($$) {
  336. my $info = shift;
  337. my $file = shift;
  338. print $file $info->[4];
  339. my $timestamp_sec = $info->[1];
  340. my $timestamp_nsec = $info->[2];
  341. print $file $timestamp_sec . "\0";
  342. print $file $timestamp_nsec . "\0";
  343. foreach my $dir (@{$info->[3]}) {
  344. print $file $dir->{'nfs'} . "\0";
  345. print $file $dir->{'timestamp_sec'} . "\0";
  346. print $file $dir->{'timestamp_nsec'} . "\0";
  347. print $file $dir->{'dev'} . "\0";
  348. print $file $dir->{'ino'} . "\0";
  349. print $file $dir->{'name'} . "\0";
  350. foreach my $dirent (@{$dir->{'dirents'}}) {
  351. print $file $dirent . "\0";
  352. }
  353. print $file "\0";
  354. }
  355. }
  356. ## main
  357. sub main {
  358. our ($opt_b, $opt_r, $opt_h, $opt_c);
  359. getopts('br:hc');
  360. HELP_MESSAGE() if ($opt_h || $#ARGV == -1 || ($opt_b && !$opt_r) ||
  361. ($opt_r && $opt_c) );
  362. my @repl;
  363. if ($opt_r) {
  364. foreach my $spec (split(/,/, $opt_r)) {
  365. ($spec =~ /^([^-]+)-([^-]+)/) || die "Invalid replacement specification '$opt_r'";
  366. push @repl, [interpret_dev($1), interpret_dev($2)];
  367. }
  368. }
  369. foreach my $snapfile (@ARGV) {
  370. my $info = read_incr_db($snapfile);
  371. if ($opt_r ) {
  372. if ($opt_b) {
  373. rename($snapfile, $snapfile . "~") || die "Could not rename '$snapfile' to backup";
  374. }
  375. replace_device_number($info, @repl);
  376. write_incr_db($info, $snapfile);
  377. } elsif ($opt_c) {
  378. check_field_values($info);
  379. } else {
  380. show_device_counts($info);
  381. }
  382. }
  383. }
  384. sub HELP_MESSAGE {
  385. print <<EOF;
  386. Usage:
  387. tar-snapshot-edit SNAPFILE [SNAPFILE [...]]
  388. tar-snapshot-edit -r 'DEV1-DEV2[,DEV3-DEV4...]' [-b] SNAPFILE [SNAPFILE [...]]
  389. tar-snapshot-edit -c SNAPFILE [SNAPFILE [...]]
  390. With no options specified: print a summary of the 'device' values
  391. found in each SNAPFILE.
  392. With -r: replace occurrences of DEV1 with DEV2 in each SNAPFILE.
  393. DEV1 and DEV2 may be specified in hex (e.g., 0xfe01), decimal (e.g.,
  394. 65025), or MAJ:MIN (e.g., 254:1). To replace multiple occurrences,
  395. separate them with commas. If -b is also specified, backup files
  396. (ending with '~') will be created.
  397. With -c: Check the field values in each SNAPFILE and print warning
  398. messages if any invalid values are found. (An invalid value is one
  399. that would cause \"tar\" to generate an
  400. Unexpected field value in snapshot file
  401. error message as it processed the snapshot file.)
  402. EOF
  403. exit 1;
  404. }
  405. sub interpret_dev ($) {
  406. my $dev = shift;
  407. if ($dev =~ /^([0-9]+):([0-9]+)$/) {
  408. return $1 * 256 + $2;
  409. } elsif ($dev =~ /^0x[0-9a-fA-F]+$/) {
  410. return oct $dev;
  411. } elsif ($dev =~ /^[0-9]+$/) {
  412. return $dev+0;
  413. } else {
  414. die "Invalid device specification '$dev'";
  415. }
  416. }
  417. main