tar-snapshot-edit 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. #! /usr/bin/perl -w
  2. # Display and edit the 'dev' field in tar's snapshots
  3. # Copyright 2007, 2011, 2013-2014, 2016-2017 Free Software Foundation,
  4. # Inc.
  5. # This file is part of GNU tar.
  6. # GNU tar is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3 of the License, or
  9. # (at your option) any later version.
  10. # GNU tar 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. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. # tar-snapshot-edit
  17. #
  18. # This script is capable of replacing values in the 'dev' field of an
  19. # incremental backup 'snapshot' file. This is useful when the device
  20. # used to store files in a tar archive changes, without the files
  21. # themselves changing. This may happen when, for example, a device
  22. # driver changes major or minor numbers.
  23. #
  24. # It can also run a check on all the field values found in the
  25. # snapshot file, printing out a detailed message when it finds values
  26. # that would cause an "Unexpected field value in snapshot file",
  27. # "Numerical result out of range", or "Invalid argument" error
  28. # if tar were run using that snapshot file as input. (See the
  29. # comments included in the definition of the check_field_values
  30. # routine for more detailed information regarding these checks.)
  31. #
  32. #
  33. #
  34. # Author: Dustin J. Mitchell <[email protected]>
  35. #
  36. # Modified Aug 25, 2011 by Nathan Stratton Treadway <nathanst AT ontko.com>:
  37. # * update Perl syntax to work correctly with more recent versions of
  38. # Perl. (The original code worked with in the v5.8 timeframe but
  39. # not with Perl v5.10.1 and later.)
  40. # * added a "-c" option to check the snapshot file for invalid field values.
  41. # * handle NFS indicator character ("+") in version 0 and 1 files
  42. # * preserve the original header/version line when editing version 1
  43. # or 2 files.
  44. # * tweak output formatting
  45. #
  46. # Modified March 13, 2013 by Nathan Stratton Treadway <nathanst AT ontko.com>:
  47. # * configure field ranges used for -c option based on the system
  48. # architecture (in response to the December 2012 update to GNU tar
  49. # enabling support for systems with signed dev_t values).
  50. # * when printing the list of device ids found in the snapshot file
  51. # (when run in the default mode), print the raw device id values
  52. # instead of the hex-string version in those cases where they
  53. # can't be converted successfully.
  54. use Getopt::Std;
  55. use Config;
  56. my %snapshot_field_ranges; # used in check_field_values function
  57. ## reading
  58. sub read_incr_db ($) {
  59. my $filename = shift;
  60. open(my $file, "<$filename") || die "Could not open '$filename' for reading";
  61. my $header_str = <$file>;
  62. my $file_version;
  63. if ($header_str =~ /^GNU tar-[^-]*-([0-9]+)\n$/) {
  64. $file_version = $1+0;
  65. } else {
  66. $file_version = 0;
  67. }
  68. print "\nFile: $filename\n";
  69. print " Detected snapshot file version: $file_version\n\n";
  70. if ($file_version == 0) {
  71. return read_incr_db_0($file, $header_str);
  72. } elsif ($file_version == 1) {
  73. return read_incr_db_1($file, $header_str);
  74. } elsif ($file_version == 2) {
  75. return read_incr_db_2($file, $header_str);
  76. } else {
  77. die "Unrecognized snapshot version in header '$header_str'";
  78. }
  79. }
  80. sub read_incr_db_0 ($$) {
  81. my $file = shift;
  82. my $header_str = shift;
  83. my $hdr_timestamp_sec = $header_str;
  84. chop $hdr_timestamp_sec;
  85. my $hdr_timestamp_nsec = ''; # not present in file format 0
  86. my $nfs;
  87. my @dirs;
  88. while (<$file>) {
  89. /^(\+?)([0-9]*) ([0-9]*) (.*)\n$/ || die("Bad snapshot line $_");
  90. if ( $1 eq "+" ) {
  91. $nfs="1";
  92. } else {
  93. $nfs="0";
  94. }
  95. push @dirs, { nfs=>$nfs,
  96. dev=>$2,
  97. ino=>$3,
  98. name=>$4 };
  99. }
  100. close($file);
  101. # file version, timestamp, timestamp, dir list, file header line
  102. return [ 0, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs, ""];
  103. }
  104. sub read_incr_db_1 ($$) {
  105. my $file = shift;
  106. my $header_str = shift;
  107. my $timestamp = <$file>; # "sec nsec"
  108. my ($hdr_timestamp_sec, $hdr_timestamp_nsec) = ($timestamp =~ /([0-9]*) ([0-9]*)/);
  109. my $nfs;
  110. my @dirs;
  111. while (<$file>) {
  112. /^(\+?)([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*) (.*)\n$/ || die("Bad snapshot line $_");
  113. if ( $1 eq "+" ) {
  114. $nfs="1";
  115. } else {
  116. $nfs="0";
  117. }
  118. push @dirs, { nfs=>$nfs,
  119. timestamp_sec=>$2,
  120. timestamp_nsec=>$3,
  121. dev=>$4,
  122. ino=>$5,
  123. name=>$6 };
  124. }
  125. close($file);
  126. # file version, timestamp, timestamp, dir list, file header line
  127. return [ 1, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs, $header_str ];
  128. }
  129. sub read_incr_db_2 ($$) {
  130. my $file = shift;
  131. my $header_str = shift;
  132. $/="\0"; # $INPUT_RECORD_SEPARATOR
  133. my $hdr_timestamp_sec = <$file>;
  134. chop $hdr_timestamp_sec;
  135. my $hdr_timestamp_nsec = <$file>;
  136. chop $hdr_timestamp_nsec;
  137. my @dirs;
  138. while (1) {
  139. last if eof($file);
  140. my $nfs = <$file>;
  141. my $timestamp_sec = <$file>;
  142. my $timestamp_nsec = <$file>;
  143. my $dev = <$file>;
  144. my $ino = <$file>;
  145. my $name = <$file>;
  146. # get rid of trailing NULs
  147. chop $nfs;
  148. chop $timestamp_sec;
  149. chop $timestamp_nsec;
  150. chop $dev;
  151. chop $ino;
  152. chop $name;
  153. my @dirents;
  154. while (my $dirent = <$file>) {
  155. chop $dirent;
  156. push @dirents, $dirent;
  157. last if ($dirent eq "");
  158. }
  159. die "missing terminator" unless (<$file> eq "\0");
  160. push @dirs, { nfs=>$nfs,
  161. timestamp_sec=>$timestamp_sec,
  162. timestamp_nsec=>$timestamp_nsec,
  163. dev=>$dev,
  164. ino=>$ino,
  165. name=>$name,
  166. dirents=>\@dirents };
  167. }
  168. close($file);
  169. $/ = "\n"; # reset to normal
  170. # file version, timestamp, timestamp, dir list, file header line
  171. return [ 2, $hdr_timestamp_sec, $hdr_timestamp_nsec, \@dirs, $header_str];
  172. }
  173. ## display
  174. sub show_device_counts ($) {
  175. my $info = shift;
  176. my %devices;
  177. foreach my $dir (@{$info->[3]}) {
  178. my $dev = $dir->{'dev'};
  179. $devices{$dev}++;
  180. }
  181. my $devstr;
  182. foreach $dev (sort {$a <=> $b} keys %devices) {
  183. $devstr = sprintf ("0x%04x", $dev);
  184. if ( $dev > 0xffffffff or $dev < 0 or hex($devstr) != $dev ) {
  185. # sprintf "%x" will not return a useful value for device ids
  186. # that are negative or which overflow the integer size on this
  187. # instance of Perl, so we convert the hex string back to a
  188. # number, and if it doesn't (numerically) equal the original
  189. # device id value, we know the hex conversion hasn't worked.
  190. #
  191. # Unfortunately, since we're running in "-w" mode, Perl will
  192. # also print a warning message if the hex() routine is called
  193. # on anything larger than "0xffffffff", even in 64-bit Perl
  194. # where such values are actually supported... so we have to
  195. # avoid calling hex() at all if the device id is too large or
  196. # negative. (If it's negative, the conversion to an unsigned
  197. # integer for the "%x" specifier will mean the result will
  198. # always trigger hex()'s warning on a 64-bit machine.)
  199. #
  200. # These situations don't seem to occur very often, so for now
  201. # when they do occur, we simply print the original text value
  202. # that was read from the snapshot file; it will look a bit
  203. # funny next to the values that do print in hex, but that's
  204. # preferable to printing values that aren't actually correct.
  205. $devstr = $dev;
  206. }
  207. printf " Device %s occurs $devices{$dev} times.\n", $devstr;
  208. }
  209. }
  210. ## check field values
  211. # initializes the global %snapshot_field_ranges hash, based on the "-a"
  212. # command-line option if given, otherwise based on the "archname" of
  213. # the current system.
  214. #
  215. # Each value in the hash is a two-element array containing the minimum
  216. # and maximum allowed values, respectively, for that field in the snapshot
  217. # file. GNU tar's allowed values for each architecture are determined
  218. # in the incremen.c source file, where the TYPE_MIN and TYPE_MAX
  219. # pre-processor expressions are used to determine the range that can be
  220. # expressed by the C data type used for each field; the values in the
  221. # array defined below should match those calculations. (For tar v1.27
  222. # and later, the valid ranges for a particular tar binary can easily
  223. # be determined using the "tar --show-snapshot-field-ranges" command.)
  224. sub choose_architecture ($) {
  225. my $opt_a = shift;
  226. my $arch = $opt_a ? $opt_a : $Config{'archname'};
  227. # These ranges apply to Linux 2.4/2.6 on iX86 systems, but are used
  228. # by default on unrecognized/unsupported systems, too.
  229. %iX86_linux_field_ranges = (
  230. timestamp_sec => [ -2147483648, 2147483647 ], # min/max of time_t
  231. timestamp_nsec => [ 0, 999999999 ], # 0 to BILLION-1
  232. nfs => [ 0, 1 ],
  233. dev => [ 0, 18446744073709551615 ], # min/max of dev_t
  234. ino => [ 0, 4294967295 ], # min/max of ino_t
  235. );
  236. if ( $arch =~ m/^i[\dxX]86-linux/i ) {
  237. %snapshot_field_ranges = %iX86_linux_field_ranges;
  238. print "Checking snapshot field values using \"iX86-linux\" ranges.\n\n";
  239. } elsif ( $arch =~ m/^x86_64-linux/i ) {
  240. %snapshot_field_ranges = (
  241. timestamp_sec => [ -9223372036854775808, 9223372036854775807 ],
  242. timestamp_nsec => [ 0, 999999999 ],
  243. nfs => [ 0, 1 ],
  244. dev => [ 0, 18446744073709551615 ],
  245. ino => [ 0, 18446744073709551615 ],
  246. );
  247. print "Checking snapshot field values using \"x86_64-linux\" ranges.\n\n";
  248. } elsif ( $arch =~ m/^IA64.ARCHREV_0/i ) {
  249. # HP/UX running on Itanium/ia64 architecture
  250. %snapshot_field_ranges = (
  251. timestamp_sec => [ -2147483648, 2147483647 ],
  252. timestamp_nsec => [ 0, 999999999 ],
  253. nfs => [ 0, 1 ],
  254. dev => [ -2147483648, 2147483647 ],
  255. ino => [ 0, 4294967295 ],
  256. );
  257. print "Checking snapshot field values using \"IA64.ARCHREV_0\" (HP/UX) ranges.\n\n";
  258. } else {
  259. %snapshot_field_ranges = %iX86_linux_field_ranges;
  260. print "Unrecognized architecture \"$arch\"; defaulting to \"iX86-linux\".\n";
  261. print "(Use -a option to override.)\n" unless $opt_a;
  262. print "\n";
  263. }
  264. if ( ref(1) ne "" ) {
  265. print "(\"bignum\" mode is in effect; skipping 64-bit-integer check.)\n\n"
  266. } else {
  267. # find the largest max value in the current set of ranges
  268. my $maxmax = 0;
  269. for $v (values %snapshot_field_ranges ) {
  270. $maxmax = $v->[1] if ($v->[1] > $maxmax);
  271. }
  272. # "~0" translates into a platform-native integer with all bits turned
  273. # on -- that is, the largest value that can be represented as
  274. # an integer. We print a warning if our $maxmax value is greater
  275. # than that largest integer, since in that case Perl will switch
  276. # to using floats for those large max values. The wording of
  277. # the message assumes that the only way this situation can exist
  278. # is that the platform uses 32-bit integers but some of the
  279. # snapshot-file fields have 64-bit values.
  280. if ( ~0 < $maxmax ) {
  281. print <<EOF
  282. Note: this version of Perl uses 32-bit integers, which means that it
  283. will switch to using floating-point numbers when checking the ranges
  284. for 64-bit snapshot-file fields. This normally will work fine, but
  285. might fail to detect cases where the value in the input field value is
  286. only slightly out of range. (For example, a "9223372036854775808"
  287. might not be recognized as being larger than 9223372036854775807.)
  288. If you suspect you are experiencing this problem, you can try running
  289. the program using the "-Mbignum" option, as in
  290. \$ perl $0 -Mbignum -c [FILES]
  291. (but doing so will make the program run *much* slower).
  292. EOF
  293. }
  294. }
  295. }
  296. # returns a warning message if $field_value isn't a valid string
  297. # representation of an integer, or if the resulting integer is out of range
  298. # defined by the two-element array retrieved using up the $field_name key in
  299. # the global %snapshot_field_ranges hash.
  300. sub validate_integer_field ($$) {
  301. my $field_value = shift;
  302. my $field_name = shift;
  303. my ($min, $max) = @{$snapshot_field_ranges{$field_name}};
  304. my $msg = "";
  305. if ( not $field_value =~ /^-?\d+$/ ) {
  306. $msg = " $field_name value contains invalid characters: \"$field_value\"\n";
  307. } else {
  308. if ( $field_value < $min ) {
  309. $msg = " $field_name value too low: \"$field_value\" < $min \n";
  310. } elsif ( $field_value > $max ) {
  311. $msg = " $field_name value too high: \"$field_value\" > $max \n";
  312. }
  313. }
  314. return $msg;
  315. }
  316. # This routine loops through each directory entry in the $info data
  317. # structure and prints a warning message if tar would abort with an
  318. # "Unexpected field value in snapshot file", "Numerical result out of
  319. # range", or "Invalid argument" error upon reading this snapshot file.
  320. #
  321. # (Note that the "Unexpected field value in snapshot file" error message
  322. # was introduced along with the change to snapshot file format "2",
  323. # starting with tar v1.16 [or, more precisely, v1.15.91], while the
  324. # other two were introduced in v1.27.)
  325. #
  326. # The checks here are intended to match those found in the incremen.c
  327. # source file. See the choose_architecture() function (above) for more
  328. # information on how to configure the range of values considered valid
  329. # by this script.
  330. #
  331. # (Note: the checks here are taken from the code that processes
  332. # version 2 snapshot files, but to keep things simple we apply those
  333. # same checks to files having earlier versions -- but only for
  334. # the fields that actually exist in those input files.)
  335. sub check_field_values ($) {
  336. my $info = shift;
  337. my $msg;
  338. my $error_found = 0;
  339. print " Checking field values in snapshot file...\n";
  340. $snapver = $info->[0];
  341. $msg = "";
  342. $msg .= validate_integer_field($info->[1], 'timestamp_sec');
  343. if ($snapver >= 1) {
  344. $msg .= validate_integer_field($info->[2], 'timestamp_nsec');
  345. }
  346. if ( $msg ne "" ) {
  347. $error_found = 1;
  348. print "\n shapshot file header:\n";
  349. print $msg;
  350. }
  351. foreach my $dir (@{$info->[3]}) {
  352. $msg = "";
  353. $msg .= validate_integer_field($dir->{'nfs'}, 'nfs');
  354. if ($snapver >= 1) {
  355. $msg .= validate_integer_field($dir->{'timestamp_sec'}, 'timestamp_sec');
  356. $msg .= validate_integer_field($dir->{'timestamp_nsec'}, 'timestamp_nsec');
  357. }
  358. $msg .= validate_integer_field($dir->{'dev'}, 'dev');
  359. $msg .= validate_integer_field($dir->{'ino'}, 'ino');
  360. if ( $msg ne "" ) {
  361. $error_found = 1;
  362. print "\n directory: $dir->{'name'}\n";
  363. print $msg;
  364. }
  365. }
  366. print "\n Snapshot field value check complete" ,
  367. $error_found ? "" : ", no errors found" ,
  368. ".\n";
  369. }
  370. ## editing
  371. sub replace_device_number ($@) {
  372. my $info = shift(@_);
  373. my @repl = @_;
  374. my $count = 0;
  375. foreach my $dir (@{$info->[3]}) {
  376. foreach $x (@repl) {
  377. if ($dir->{'dev'} eq $$x[0]) {
  378. $dir->{'dev'} = $$x[1];
  379. $count++;
  380. last;
  381. }
  382. }
  383. }
  384. print " Updated $count records.\n"
  385. }
  386. ## writing
  387. sub write_incr_db ($$) {
  388. my $info = shift;
  389. my $filename = shift;
  390. my $file_version = $$info[0];
  391. open($file, ">$filename") || die "Could not open '$filename' for writing";
  392. if ($file_version == 0) {
  393. write_incr_db_0($info, $file);
  394. } elsif ($file_version == 1) {
  395. write_incr_db_1($info, $file);
  396. } elsif ($file_version == 2) {
  397. write_incr_db_2($info, $file);
  398. } else {
  399. die "Unknown file version $file_version.";
  400. }
  401. close($file);
  402. }
  403. sub write_incr_db_0 ($$) {
  404. my $info = shift;
  405. my $file = shift;
  406. my $timestamp_sec = $info->[1];
  407. print $file "$timestamp_sec\n";
  408. foreach my $dir (@{$info->[3]}) {
  409. if ($dir->{'nfs'}) {
  410. print $file '+'
  411. }
  412. print $file "$dir->{'dev'} ";
  413. print $file "$dir->{'ino'} ";
  414. print $file "$dir->{'name'}\n";
  415. }
  416. }
  417. sub write_incr_db_1 ($$) {
  418. my $info = shift;
  419. my $file = shift;
  420. print $file $info->[4];
  421. my $timestamp_sec = $info->[1];
  422. my $timestamp_nsec = $info->[2];
  423. print $file "$timestamp_sec $timestamp_nsec\n";
  424. foreach my $dir (@{$info->[3]}) {
  425. if ($dir->{'nfs'}) {
  426. print $file '+'
  427. }
  428. print $file "$dir->{'timestamp_sec'} ";
  429. print $file "$dir->{'timestamp_nsec'} ";
  430. print $file "$dir->{'dev'} ";
  431. print $file "$dir->{'ino'} ";
  432. print $file "$dir->{'name'}\n";
  433. }
  434. }
  435. sub write_incr_db_2 ($$) {
  436. my $info = shift;
  437. my $file = shift;
  438. print $file $info->[4];
  439. my $timestamp_sec = $info->[1];
  440. my $timestamp_nsec = $info->[2];
  441. print $file $timestamp_sec . "\0";
  442. print $file $timestamp_nsec . "\0";
  443. foreach my $dir (@{$info->[3]}) {
  444. print $file $dir->{'nfs'} . "\0";
  445. print $file $dir->{'timestamp_sec'} . "\0";
  446. print $file $dir->{'timestamp_nsec'} . "\0";
  447. print $file $dir->{'dev'} . "\0";
  448. print $file $dir->{'ino'} . "\0";
  449. print $file $dir->{'name'} . "\0";
  450. foreach my $dirent (@{$dir->{'dirents'}}) {
  451. print $file $dirent . "\0";
  452. }
  453. print $file "\0";
  454. }
  455. }
  456. ## main
  457. sub main {
  458. our ($opt_b, $opt_r, $opt_h, $opt_c, $opt_a);
  459. getopts('br:hca:');
  460. HELP_MESSAGE() if ($opt_h || $#ARGV == -1 || ($opt_b && !$opt_r) ||
  461. ($opt_a && !$opt_c) || ($opt_r && $opt_c) );
  462. my @repl;
  463. if ($opt_r) {
  464. foreach my $spec (split(/,/, $opt_r)) {
  465. ($spec =~ /^([^-]+)-([^-]+)/) || die "Invalid replacement specification '$opt_r'";
  466. push @repl, [interpret_dev($1), interpret_dev($2)];
  467. }
  468. }
  469. choose_architecture($opt_a) if ($opt_c);
  470. foreach my $snapfile (@ARGV) {
  471. my $info = read_incr_db($snapfile);
  472. if ($opt_r) {
  473. if ($opt_b) {
  474. rename($snapfile, $snapfile . "~") || die "Could not rename '$snapfile' to backup";
  475. }
  476. replace_device_number($info, @repl);
  477. write_incr_db($info, $snapfile);
  478. } elsif ($opt_c) {
  479. check_field_values($info);
  480. } else {
  481. show_device_counts($info);
  482. }
  483. }
  484. }
  485. sub HELP_MESSAGE {
  486. print <<EOF;
  487. Usage:
  488. tar-snapshot-edit SNAPFILE [SNAPFILE [...]]
  489. tar-snapshot-edit -r 'DEV1-DEV2[,DEV3-DEV4...]' [-b] SNAPFILE [SNAPFILE [...]]
  490. tar-snapshot-edit -c [-aARCH] SNAPFILE [SNAPFILE [...]]
  491. With no options specified: print a summary of the 'device' values
  492. found in each SNAPFILE.
  493. With -r: replace occurrences of DEV1 with DEV2 in each SNAPFILE.
  494. DEV1 and DEV2 may be specified in hex (e.g., 0xfe01), decimal (e.g.,
  495. 65025), or MAJ:MIN (e.g., 254:1). To replace multiple occurrences,
  496. separate them with commas. If -b is also specified, backup files
  497. (ending with '~') will be created.
  498. With -c: Check the field values in each SNAPFILE and print warning
  499. messages if any invalid values are found. (An invalid value is one
  500. that would cause \"tar\" to abort with an error message such as
  501. Unexpected field value in snapshot file
  502. Numerical result out of range
  503. or
  504. Invalid argument
  505. as it processed the snapshot file.)
  506. Normally the program automatically chooses the valid ranges for
  507. the fields based on the current system's architecture, but the
  508. -a option can be used to override the selection, e.g. in order
  509. to validate a snapshot file generated on a some other system.
  510. (Currently only three architectures are supported, "iX86-linux",
  511. "x86_64-linux", and "IA64.ARCHREV_0" [HP/UX running on Itanium/ia64],
  512. and if the current system isn't recognized, then the iX86-linux
  513. values are used by default.)
  514. EOF
  515. exit 1;
  516. }
  517. sub interpret_dev ($) {
  518. my $dev = shift;
  519. if ($dev =~ /^([0-9]+):([0-9]+)$/) {
  520. return $1 * 256 + $2;
  521. } elsif ($dev =~ /^0x[0-9a-fA-F]+$/) {
  522. return oct $dev;
  523. } elsif ($dev =~ /^[0-9]+$/) {
  524. return $dev+0;
  525. } else {
  526. die "Invalid device specification '$dev'";
  527. }
  528. }
  529. main