tar-snapshot-edit 14 KB

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