4
0

tar-snapshot-edit 19 KB

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