update-alternatives.pl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. #!/usr/bin/perl --
  2. BEGIN { # Work-around for bug #479711 in perl
  3. $ENV{PERL_DL_NONLAZY} = 1;
  4. }
  5. use strict;
  6. use warnings;
  7. use POSIX qw(:errno_h);
  8. use Dpkg;
  9. use Dpkg::Gettext;
  10. textdomain("dpkg");
  11. # Global variables:
  12. my $altdir = '/etc/alternatives';
  13. # FIXME: this should not override the previous assignment.
  14. $admindir = $admindir . '/alternatives';
  15. my $verbosemode = 0;
  16. my $action = ''; # Action to perform (display / query / install / remove / auto / config)
  17. my $mode = 'auto'; # Update mode for alternative (manual / auto)
  18. my $skip = ''; # Skip alternatives properly configured in auto mode (for --config)
  19. my $state; # State of alternative:
  20. # expected: alternative with highest priority is the active alternative
  21. # expected-inprogress: busy selecting alternative with highest priority
  22. # unexpected: alternative another alternative is active / error during readlink
  23. # nonexistent: alternative-symlink does not exist
  24. my %versionnum; # Map from currently available versions into @versions and @priorities
  25. my @versions; # List of available versions for alternative
  26. my @priorities; # Map from @version-index to priority
  27. my $best;
  28. my $bestpri;
  29. my $bestnum;
  30. my $link; # Link we are working with
  31. my $linkname;
  32. my $alink; # Alternative we are managing (ie the symlink we're making/removing) (install only)
  33. my $name; # Name of the alternative (the symlink) we are processing
  34. my $apath; # Path of alternative we are offering
  35. my $apriority; # Priority of link (only when we are installing an alternative)
  36. my %aslavelink;
  37. my %aslavepath;
  38. my %aslavelinkcount;
  39. my $slink;
  40. my $sname;
  41. my $spath;
  42. my @slavenames; # List with names of slavelinks
  43. my %slavenum; # Map from name of slavelink to slave-index (into @slavelinks)
  44. my @slavelinks; # List of slavelinks (indexed by slave-index)
  45. my %slavepath; # Map from (@version-index,slavename) to slave-path
  46. my %slavelinkcount;
  47. sub version {
  48. printf _g("Debian %s version %s.\n"), $progname, $version;
  49. printf _g("
  50. Copyright (C) 1995 Ian Jackson.
  51. Copyright (C) 2000-2002 Wichert Akkerman.");
  52. printf _g("
  53. This is free software; see the GNU General Public Licence version 2 or
  54. later for copying conditions. There is NO warranty.
  55. ");
  56. }
  57. sub usage {
  58. printf _g(
  59. "Usage: %s [<option> ...] <command>
  60. Commands:
  61. --install <link> <name> <path> <priority>
  62. [--slave <link> <name> <path>] ...
  63. add a group of alternatives to the system.
  64. --remove <name> <path> remove <path> from the <name> group alternative.
  65. --remove-all <name> remove <name> group from the alternatives system.
  66. --auto <name> switch the master link <name> to automatic mode.
  67. --display <name> display information about the <name> group.
  68. --query <name> machine parseable version of --display <name>.
  69. --list <name> display all targets of the <name> group.
  70. --config <name> show alternatives for the <name> group and ask the
  71. user to select which one to use.
  72. --set <name> <path> set <path> as alternative for <name>.
  73. --all call --config on all alternatives.
  74. <link> is the symlink pointing to %s/<name>.
  75. (e.g. /usr/bin/pager)
  76. <name> is the master name for this link group.
  77. (e.g. pager)
  78. <path> is the location of one of the alternative target files.
  79. (e.g. /usr/bin/less)
  80. <priority> is an integer; options with higher numbers have higher priority in
  81. automatic mode.
  82. Options:
  83. --altdir <directory> change the alternatives directory.
  84. --admindir <directory> change the administrative directory.
  85. --skip-auto skip prompt for alternatives correctly configured
  86. in automatic mode (relevant for --config only)
  87. --verbose verbose operation, more output.
  88. --quiet quiet operation, minimal output.
  89. --help show this help message.
  90. --version show the version.
  91. "), $progname, $altdir;
  92. }
  93. sub quit
  94. {
  95. printf STDERR "%s: %s\n", $progname, "@_";
  96. exit(2);
  97. }
  98. sub badusage
  99. {
  100. printf STDERR "%s: %s\n\n", $progname, "@_";
  101. usage();
  102. exit(2);
  103. }
  104. sub read_link_group
  105. {
  106. if (open(AF, "$admindir/$name")) {
  107. $mode = gl("update_mode");
  108. $mode eq 'auto' || $mode eq 'manual' || badfmt(_g("invalid update mode"));
  109. $link = gl("link");
  110. while (($sname = gl("sname")) ne '') {
  111. push(@slavenames, $sname);
  112. defined($slavenum{$sname}) && badfmt(sprintf(_g("duplicate slave %s"), $sname));
  113. $slavenum{$sname} = $#slavenames;
  114. $slink = gl("slink");
  115. $slink eq $link && badfmt(sprintf(_g("slave link same as main link %s"), $link));
  116. $slavelinkcount{$slink}++ && badfmt(sprintf(_g("duplicate slave link %s"), $slink));
  117. push(@slavelinks, $slink);
  118. }
  119. while (($version = gl("version")) ne '') {
  120. defined($versionnum{$version}) && badfmt(sprintf(_g("duplicate path %s"), $version));
  121. if (-r $version) {
  122. push(@versions, $version);
  123. my $i;
  124. $versionnum{$version} = $i = $#versions;
  125. my $priority = gl("priority");
  126. $priority =~ m/^[-+]?\d+$/ || badfmt(sprintf(_g("priority %s %s"), $version, $priority));
  127. $priorities[$i] = $priority;
  128. for (my $j = 0; $j <= $#slavenames; $j++) {
  129. $slavepath{$i,$j} = gl("spath");
  130. }
  131. } else {
  132. # File not found - remove
  133. pr(sprintf(_g("Alternative for %s points to %s - which wasn't found. Removing from list of alternatives."), $name, $version))
  134. if $verbosemode > 0;
  135. gl("priority");
  136. for (my $j = 0; $j <= $#slavenames; $j++) {
  137. gl("spath");
  138. }
  139. }
  140. }
  141. close(AF);
  142. return 0;
  143. } elsif ($! != ENOENT) {
  144. quit(sprintf(_g("unable to open %s: %s"), "$admindir/$name", $!));
  145. } elsif ($! == ENOENT) {
  146. return 1;
  147. }
  148. }
  149. sub fill_missing_slavepaths()
  150. {
  151. for (my $j = 0; $j <= $#slavenames; $j++) {
  152. for (my $i = 0; $i <= $#versions; $i++) {
  153. $slavepath{$i,$j} ||= '';
  154. }
  155. }
  156. }
  157. sub find_best_version
  158. {
  159. $best = '';
  160. for (my $i = 0; $i <= $#versions; $i++) {
  161. if ($best eq '' || $priorities[$i] > $bestpri) {
  162. $best = $versions[$i];
  163. $bestpri = $priorities[$i];
  164. $bestnum = $i;
  165. }
  166. }
  167. }
  168. sub display_link_group
  169. {
  170. pr(sprintf("%s - %s", $name,
  171. ($mode eq "auto") ? _g("auto mode") : _g("manual mode")));
  172. $linkname = readlink("$altdir/$name");
  173. if (defined($linkname)) {
  174. pr(sprintf(_g(" link currently points to %s"), $linkname));
  175. } elsif ($! == ENOENT) {
  176. pr(_g(" link currently absent"));
  177. } else {
  178. pr(sprintf(_g(" link unreadable - %s"), $!));
  179. }
  180. for (my $i = 0; $i <= $#versions; $i++) {
  181. pr(sprintf(_g("%s - priority %s"), $versions[$i], $priorities[$i]));
  182. for (my $j = 0; $j <= $#slavenames; $j++) {
  183. my $tspath = $slavepath{$i, $j};
  184. next unless length($tspath);
  185. pr(sprintf(_g(" slave %s: %s"), $slavenames[$j], $tspath));
  186. }
  187. }
  188. if ($best eq '') {
  189. pr(_g("No versions available."));
  190. } else {
  191. pr(sprintf(_g("Current \`best' version is %s."), $best));
  192. }
  193. }
  194. sub query_link_group
  195. {
  196. pr(sprintf("Link: %s", $name));
  197. pr(sprintf("Status: %s", $mode));
  198. if ($best ne '') {
  199. pr(sprintf("Best: %s", $best));
  200. }
  201. $linkname = readlink("$altdir/$name");
  202. if (defined($linkname)) {
  203. pr(sprintf("Value: %s", $linkname));
  204. } elsif ($! == ENOENT) {
  205. pr("Value: none");
  206. } else {
  207. pr("Value: error");
  208. pr(sprintf("Error: %s", $!));
  209. }
  210. for (my $i = 0; $i <= $#versions; $i++) {
  211. pr("");
  212. pr(sprintf("Alternative: %s", $versions[$i]));
  213. pr(sprintf("Priority: %s", $priorities[$i]));
  214. next unless ($#slavenames >= 0);
  215. pr("Slaves:");
  216. for (my $j = 0; $j <= $#slavenames; $j++) {
  217. my $tspath = $slavepath{$i, $j};
  218. next unless length($tspath);
  219. pr(sprintf(" %s %s", $slavenames[$j], $tspath));
  220. }
  221. }
  222. }
  223. sub list_link_group
  224. {
  225. for (my $i = 0; $i <= $#versions; $i++) {
  226. pr("$versions[$i]");
  227. }
  228. }
  229. sub checked_alternative($$$)
  230. {
  231. my ($name, $link, $path) = @_;
  232. $linkname = readlink($link);
  233. if (!defined($linkname) && $! != ENOENT) {
  234. pr(sprintf(_g("warning: %s is supposed to be a symlink to %s, \n".
  235. "or nonexistent; however, readlink failed: %s"),
  236. $link, "$altdir/$name", $!))
  237. if $verbosemode > 0;
  238. } elsif (!defined($linkname) ||
  239. (defined($linkname) && $linkname ne "$altdir/$name")) {
  240. checked_rm("$link.dpkg-tmp");
  241. checked_symlink("$altdir/$name", "$link.dpkg-tmp");
  242. checked_mv("$link.dpkg-tmp", $link);
  243. }
  244. $linkname = readlink("$altdir/$name");
  245. if (defined($linkname) && $linkname eq $path) {
  246. pr(sprintf(_g("Leaving %s (%s) pointing to %s."), $name, $link, $path))
  247. if $verbosemode > 0;
  248. } else {
  249. pr(sprintf(_g("Updating %s (%s) to point to %s."), $name, $link, $path))
  250. if $verbosemode > 0;
  251. }
  252. }
  253. sub set_links($$)
  254. {
  255. my ($spath, $preferred) = (@_);
  256. printf STDOUT _g("Using '%s' to provide '%s' in %s.") . "\n", $spath,
  257. $name, ($mode eq "auto" ? _g("auto mode") : _g("manual mode"));
  258. checked_symlink("$spath","$altdir/$name.dpkg-tmp");
  259. checked_mv("$altdir/$name.dpkg-tmp", "$altdir/$name");
  260. # Link slaves...
  261. for (my $slnum = 0; $slnum < @slavenames; $slnum++) {
  262. my $slave = $slavenames[$slnum];
  263. if ($slavepath{$preferred, $slnum} ne '') {
  264. checked_alternative($slave, $slavelinks[$slnum],
  265. $slavepath{$preferred, $slnum});
  266. checked_symlink($slavepath{$preferred, $slnum},
  267. "$altdir/$slave.dpkg-tmp");
  268. checked_mv("$altdir/$slave.dpkg-tmp", "$altdir/$slave");
  269. } else {
  270. pr(sprintf(_g("Removing %s (%s), not appropriate with %s."), $slave,
  271. $slavelinks[$slnum], $versions[$preferred]))
  272. if $verbosemode > 0;
  273. checked_rm("$altdir/$slave");
  274. }
  275. }
  276. }
  277. sub check_many_actions()
  278. {
  279. return unless $action;
  280. badusage(sprintf(_g("two commands specified: %s and --%s"), $_, $action));
  281. }
  282. sub checked_rm($)
  283. {
  284. my ($f) = @_;
  285. unlink($f) || $! == ENOENT ||
  286. quit(sprintf(_g("unable to remove %s: %s"), $f, $!));
  287. }
  288. #
  289. # Main program
  290. #
  291. $| = 1;
  292. while (@ARGV) {
  293. $_= shift(@ARGV);
  294. last if m/^--$/;
  295. if (!m/^--/) {
  296. quit(sprintf(_g("unknown argument \`%s'"), $_));
  297. } elsif (m/^--help$/) {
  298. usage();
  299. exit(0);
  300. } elsif (m/^--version$/) {
  301. version();
  302. exit(0);
  303. } elsif (m/^--verbose$/) {
  304. $verbosemode= +1;
  305. } elsif (m/^--quiet$/) {
  306. $verbosemode= -1;
  307. } elsif (m/^--install$/) {
  308. check_many_actions();
  309. @ARGV >= 4 || badusage(_g("--install needs <link> <name> <path> <priority>"));
  310. ($alink,$name,$apath,$apriority,@ARGV) = @ARGV;
  311. $apriority =~ m/^[-+]?\d+/ || badusage(_g("priority must be an integer"));
  312. $action = 'install';
  313. } elsif (m/^--(remove|set)$/) {
  314. check_many_actions();
  315. @ARGV >= 2 || badusage(sprintf(_g("--%s needs <name> <path>"), $1));
  316. ($name,$apath,@ARGV) = @ARGV;
  317. $action = $1;
  318. } elsif (m/^--(display|query|auto|config|list|remove-all)$/) {
  319. check_many_actions();
  320. @ARGV || badusage(sprintf(_g("--%s needs <name>"), $1));
  321. $action = $1;
  322. $name= shift(@ARGV);
  323. } elsif (m/^--slave$/) {
  324. @ARGV >= 3 || badusage(_g("--slave needs <link> <name> <path>"));
  325. ($slink,$sname,$spath,@ARGV) = @ARGV;
  326. defined($aslavelink{$sname}) && badusage(sprintf(_g("slave name %s duplicated"), $sname));
  327. $aslavelinkcount{$slink}++ && badusage(sprintf(_g("slave link %s duplicated"), $slink));
  328. $aslavelink{$sname}= $slink;
  329. $aslavepath{$sname}= $spath;
  330. } elsif (m/^--altdir$/) {
  331. @ARGV || badusage(sprintf(_g("--%s needs a <directory> argument"), "altdir"));
  332. $altdir= shift(@ARGV);
  333. } elsif (m/^--admindir$/) {
  334. @ARGV || badusage(sprintf(_g("--%s needs a <directory> argument"), "admindir"));
  335. $admindir= shift(@ARGV);
  336. } elsif (m/^--skip-auto$/) {
  337. $skip = '--skip-auto';
  338. } elsif (m/^--all$/) {
  339. $action = 'all';
  340. } else {
  341. badusage(sprintf(_g("unknown option \`%s'"), $_));
  342. }
  343. }
  344. defined($name) && defined($aslavelink{$name}) &&
  345. badusage(sprintf(_g("name %s is both primary and slave"), $name));
  346. defined($alink) && $aslavelinkcount{$alink} &&
  347. badusage(sprintf(_g("link %s is both primary and slave"), $alink));
  348. $action ||
  349. badusage(_g("need --display, --query, --config, --set, --install, --remove, --all, --remove-all or --auto"));
  350. $action eq 'install' || !%aslavelink ||
  351. badusage(_g("--slave only allowed with --install"));
  352. if ($action eq 'all') {
  353. config_all();
  354. exit 0;
  355. }
  356. if (read_link_group()) {
  357. if ($action eq 'remove') {
  358. # FIXME: Be consistent for now with the case when we try to remove a
  359. # non-existing path from an existing link group file.
  360. exit 0;
  361. } elsif ($action ne 'install') {
  362. pr(sprintf(_g("No alternatives for %s."), $name));
  363. exit 1;
  364. }
  365. }
  366. if ($action eq 'display') {
  367. find_best_version();
  368. display_link_group();
  369. exit 0;
  370. }
  371. if ($action eq 'query') {
  372. find_best_version();
  373. query_link_group();
  374. exit 0;
  375. }
  376. if ($action eq 'list') {
  377. list_link_group();
  378. exit 0;
  379. }
  380. find_best_version();
  381. if ($action eq 'config') {
  382. config_alternatives($name);
  383. }
  384. if ($action eq 'set') {
  385. set_alternatives($name);
  386. }
  387. if (defined($linkname= readlink("$altdir/$name"))) {
  388. if ($linkname eq $best) {
  389. $state= 'expected';
  390. } elsif (defined(readlink("$altdir/$name.dpkg-tmp"))) {
  391. $state= 'expected-inprogress';
  392. } else {
  393. if (-e $linkname) {
  394. $state = 'unexpected';
  395. } else {
  396. $state = 'nonexistent';
  397. }
  398. }
  399. } elsif ($! == ENOENT) {
  400. $state= 'nonexistent';
  401. } else {
  402. $state= 'unexpected';
  403. }
  404. # Possible values for:
  405. # $mode manual, auto
  406. # $state expected, expected-inprogress, unexpected, nonexistent
  407. # $action auto, install, remove, remove-all
  408. # all independent
  409. if ($action eq 'auto') {
  410. pr(sprintf(_g("Setting up automatic selection of %s."), $name))
  411. if $verbosemode > 0;
  412. checked_rm("$altdir/$name.dpkg-tmp");
  413. checked_rm("$altdir/$name");
  414. $state= 'nonexistent';
  415. $mode = 'auto';
  416. }
  417. # $mode manual, auto
  418. # $state expected, expected-inprogress, unexpected, nonexistent
  419. # $action auto, install, remove
  420. # action=auto <=> state=nonexistent
  421. if ($state eq 'unexpected' && $mode eq 'auto') {
  422. pr(sprintf(_g("%s has been changed (manually or by a script).\n" .
  423. "Switching to manual updates only."), "$altdir/$name"))
  424. if $verbosemode >= 0;
  425. $mode = 'manual';
  426. }
  427. # $mode manual, auto
  428. # $state expected, expected-inprogress, unexpected, nonexistent
  429. # $action auto, install, remove
  430. # action=auto <=> state=nonexistent
  431. # state=unexpected => mode=manual
  432. pr(sprintf(_g("Checking available versions of %s, updating links in %s ..."),
  433. $name, $altdir))
  434. if $verbosemode > 0;
  435. if ($action eq 'install') {
  436. if (defined($link) && $link ne $alink) {
  437. pr(sprintf(_g("Renaming %s link from %s to %s."), $name, $link, $alink))
  438. if $verbosemode > 0;
  439. rename_mv($link, $alink, 1) ||
  440. quit(sprintf(_g("unable to rename %s to %s: see error above"), $link, $alink));
  441. }
  442. $link= $alink;
  443. my $i;
  444. if (!defined($i= $versionnum{$apath})) {
  445. push(@versions,$apath);
  446. $versionnum{$apath}= $i= $#versions;
  447. }
  448. $priorities[$i]= $apriority;
  449. for $sname (keys %aslavelink) {
  450. my $j;
  451. if (!defined($j= $slavenum{$sname})) {
  452. push(@slavenames,$sname);
  453. $slavenum{$sname}= $j= $#slavenames;
  454. }
  455. my $oldslavelink = $slavelinks[$j];
  456. my $newslavelink = $aslavelink{$sname};
  457. $slavelinkcount{$oldslavelink}-- if defined($oldslavelink);
  458. $slavelinkcount{$newslavelink}++ &&
  459. quit(sprintf(_g("slave link name %s duplicated"), $newslavelink));
  460. if (defined($oldslavelink) && $newslavelink ne $oldslavelink) {
  461. pr(sprintf(_g("Renaming %s slave link from %s to %s."), $sname, $oldslavelink, $newslavelink))
  462. if $verbosemode > 0;
  463. rename_mv($oldslavelink, $newslavelink, 1) ||
  464. quit(sprintf(_g("unable to rename %s to %s: see error above"), $oldslavelink, $newslavelink));
  465. }
  466. $slavelinks[$j]= $newslavelink;
  467. }
  468. for (my $j = 0; $j <= $#slavenames; $j++) {
  469. $slavepath{$i,$j}= $aslavepath{$slavenames[$j]};
  470. }
  471. fill_missing_slavepaths();
  472. }
  473. if ($action eq 'remove') {
  474. my $hits = 0;
  475. if ($mode eq "manual" and $state ne "expected" and (map { $hits += $apath eq $_ } @versions) and $hits and $linkname eq $apath) {
  476. pr(_g("Removing manually selected alternative - switching to auto mode"));
  477. $mode = "auto";
  478. }
  479. if (defined(my $i = $versionnum{$apath})) {
  480. my $k = $#versions;
  481. $versionnum{$versions[$k]}= $i;
  482. delete $versionnum{$versions[$i]};
  483. $versions[$i]= $versions[$k]; $#versions--;
  484. $priorities[$i]= $priorities[$k]; $#priorities--;
  485. for (my $j = 0; $j <= $#slavenames; $j++) {
  486. $slavepath{$i,$j}= $slavepath{$k,$j};
  487. delete $slavepath{$k,$j};
  488. }
  489. } else {
  490. pr(sprintf(_g("Alternative %s for %s not registered, not removing."), $apath, $name))
  491. if $verbosemode > 0;
  492. }
  493. }
  494. if ($action eq 'remove-all') {
  495. $mode = "auto";
  496. my $k = $#versions;
  497. for (my $i = 0; $i <= $#versions; $i++) {
  498. $k--;
  499. delete $versionnum{$versions[$i]};
  500. $#priorities--;
  501. for (my $j = 0; $j <= $#slavenames; $j++) {
  502. $slavepath{$i,$j}= $slavepath{$k,$j};
  503. delete $slavepath{$k,$j};
  504. }
  505. }
  506. $#versions=$k;
  507. }
  508. for (my $j = 0; $j <= $#slavenames; $j++) {
  509. my $i;
  510. for ($i = 0; $i <= $#versions; $i++) {
  511. last if $slavepath{$i,$j} ne '';
  512. }
  513. if ($i > $#versions) {
  514. pr(sprintf(_g("Discarding obsolete slave link %s (%s)."), $slavenames[$j], $slavelinks[$j]))
  515. if $verbosemode > 0;
  516. checked_rm("$altdir/$slavenames[$j]");
  517. checked_rm($slavelinks[$j]);
  518. my $k = $#slavenames;
  519. $slavenum{$slavenames[$k]}= $j;
  520. delete $slavenum{$slavenames[$j]};
  521. $slavelinkcount{$slavelinks[$j]}--;
  522. $slavenames[$j]= $slavenames[$k]; $#slavenames--;
  523. $slavelinks[$j]= $slavelinks[$k]; $#slavelinks--;
  524. for (my $i = 0; $i <= $#versions; $i++) {
  525. $slavepath{$i,$j}= $slavepath{$i,$k};
  526. delete $slavepath{$i,$k};
  527. }
  528. $j--;
  529. }
  530. }
  531. if ($mode eq 'manual') {
  532. pr(sprintf(_g("Automatic updates of %s are disabled, leaving it alone."), "$altdir/$name"))
  533. if $verbosemode > 0;
  534. pr(sprintf(_g("To return to automatic updates use \`update-alternatives --auto %s'."), $name))
  535. if $verbosemode > 0;
  536. } else {
  537. if ($state eq 'expected-inprogress') {
  538. pr(sprintf(_g("Recovering from previous failed update of %s ..."), $name));
  539. checked_mv("$altdir/$name.dpkg-tmp", "$altdir/$name");
  540. $state= 'expected';
  541. }
  542. }
  543. # $mode manual, auto
  544. # $state expected, expected-inprogress, unexpected, nonexistent
  545. # $action auto, install, remove
  546. # action=auto <=> state=nonexistent
  547. # state=unexpected => mode=manual
  548. # mode=auto => state!=expected-inprogress && state!=unexpected
  549. open(AF,">$admindir/$name.dpkg-new") ||
  550. quit(sprintf(_g("unable to open %s for write: %s"), "$admindir/$name.dpkg-new", $!));
  551. paf($mode);
  552. paf($link);
  553. for (my $j = 0; $j <= $#slavenames; $j++) {
  554. paf($slavenames[$j]);
  555. paf($slavelinks[$j]);
  556. }
  557. find_best_version();
  558. paf('');
  559. for (my $i = 0; $i <= $#versions; $i++) {
  560. paf($versions[$i]);
  561. paf($priorities[$i]);
  562. for (my $j = 0; $j <= $#slavenames; $j++) {
  563. paf($slavepath{$i,$j});
  564. }
  565. }
  566. paf('');
  567. close(AF) || quit(sprintf(_g("unable to close %s: %s"), "$admindir/$name.dpkg-new", $!));
  568. if ($mode eq 'auto') {
  569. if ($best eq '') {
  570. pr(sprintf(_g("Last package providing %s (%s) removed, deleting it."), $name, $link))
  571. if $verbosemode > 0;
  572. checked_rm("$altdir/$name");
  573. checked_rm("$link");
  574. checked_rm("$admindir/$name.dpkg-new");
  575. checked_rm("$admindir/$name");
  576. exit(0);
  577. } else {
  578. checked_alternative($name, $link, $best);
  579. checked_rm("$altdir/$name.dpkg-tmp");
  580. symlink($best,"$altdir/$name.dpkg-tmp");
  581. }
  582. }
  583. checked_mv("$admindir/$name.dpkg-new", "$admindir/$name");
  584. if ($mode eq 'auto') {
  585. checked_mv("$altdir/$name.dpkg-tmp", "$altdir/$name");
  586. for (my $j = 0; $j <= $#slavenames; $j++) {
  587. $sname= $slavenames[$j];
  588. $slink= $slavelinks[$j];
  589. $spath= $slavepath{$bestnum,$j};
  590. checked_rm("$altdir/$sname.dpkg-tmp");
  591. if ($spath eq '') {
  592. pr(sprintf(_g("Removing %s (%s), not appropriate with %s."), $sname, $slink, $best))
  593. if $verbosemode > 0;
  594. checked_rm("$altdir/$sname");
  595. checked_rm("$slink");
  596. } else {
  597. checked_alternative($sname, $slink, $spath);
  598. checked_symlink("$spath", "$altdir/$sname.dpkg-tmp");
  599. checked_mv("$altdir/$sname.dpkg-tmp", "$altdir/$sname");
  600. }
  601. }
  602. }
  603. sub config_message {
  604. if ($#versions < 0) {
  605. print "\n";
  606. printf _g("There is no program which provides %s.\n".
  607. "Nothing to configure.\n"), $name;
  608. return -1;
  609. }
  610. if ($skip && $mode eq 'auto' && $best eq readlink("$altdir/$name")) {
  611. print "\n";
  612. display_link_group();
  613. return -1;
  614. }
  615. if ($#versions == 0 && $mode eq 'auto' && $best eq readlink("$altdir/$name")) {
  616. print "\n";
  617. printf _g("There is only 1 program which provides %s properly in auto mode\n".
  618. "(%s). Nothing to configure.\n"), $name, $versions[0];
  619. return -1;
  620. }
  621. print STDOUT "\n";
  622. printf(STDOUT _g("There are %s alternatives which provide \`%s'.\n\n".
  623. " Selection Alternative\n".
  624. "-----------------------------------------------\n"),
  625. $#versions+1, $name);
  626. printf STDOUT "%s 0 %s (%s)\n",
  627. ($mode eq "auto" && readlink("$altdir/$name") eq $best) ? '*' : ' ',
  628. $best, _g("auto mode");
  629. for (my $i = 0; $i <= $#versions; $i++) {
  630. printf STDOUT "%s %8s %s (%s) priority=%s\n",
  631. (readlink("$altdir/$name") eq $versions[$i] && $mode eq "manual") ? '*' : ' ',
  632. $i+1, $versions[$i], _g("manual mode"), $priorities[$i];
  633. }
  634. printf(STDOUT "\n"._g("Press enter to keep the default[*], or type selection number: "));
  635. return 0;
  636. }
  637. sub config_alternatives {
  638. my $preferred;
  639. do {
  640. return if config_message() < 0;
  641. $preferred=<STDIN>;
  642. chop($preferred);
  643. } until $preferred eq '' || $preferred>=0 && $preferred<=$#versions+1 &&
  644. ($preferred =~ m/[0-9]*/);
  645. if ($preferred ne '') {
  646. if ($preferred == 0) {
  647. $action = "auto";
  648. $mode = "auto";
  649. } else {
  650. $mode = "manual";
  651. $preferred--;
  652. my $spath = $versions[$preferred];
  653. set_links($spath, $preferred);
  654. }
  655. }
  656. }
  657. sub set_alternatives {
  658. $mode = "manual";
  659. # Get prefered number
  660. my $preferred = -1;
  661. for (my $i = 0; $i <= $#versions; $i++) {
  662. if($versions[$i] eq $apath) {
  663. $preferred = $i;
  664. last;
  665. }
  666. }
  667. if($preferred == -1){
  668. quit(sprintf(_g("Cannot find alternative `%s'."), $apath)."\n");
  669. }
  670. set_links($apath, $preferred);
  671. }
  672. sub pr
  673. {
  674. print(STDOUT "@_\n") ||
  675. quit(sprintf(_g("error writing stdout: %s"), $!));
  676. }
  677. sub paf {
  678. $_[0] =~ m/\n/ && quit(sprintf(_g("newlines prohibited in update-alternatives files (%s)"), $_[0]));
  679. print(AF "$_[0]\n") || quit(sprintf(_g("error writing stdout: %s"), $!));
  680. }
  681. sub gl {
  682. $!=0; $_= <AF>;
  683. defined($_) || quit(sprintf(_g("error or eof reading %s for %s (%s)"),
  684. "$admindir/$name", $_[0], $!));
  685. s/\n$// || badfmt(sprintf(_g("missing newline after %s"), $_[0]));
  686. $_;
  687. }
  688. sub badfmt {
  689. quit(sprintf(_g("internal error: %s corrupt: %s"), "$admindir/$name", $_[0]));
  690. }
  691. sub rename_mv {
  692. my ($source, $dest, $ignore_enoent) = @_;
  693. $ignore_enoent = 0 unless defined($ignore_enoent);
  694. return 1 if ($ignore_enoent and not -e $source);
  695. if (not rename($source, $dest)) {
  696. if (system("mv", $source, $dest) != 0) {
  697. return $ignore_enoent if $! == ENOENT;
  698. return 0;
  699. }
  700. }
  701. return 1;
  702. }
  703. sub checked_symlink {
  704. my ($filename, $linkname) = @_;
  705. symlink($filename, $linkname) ||
  706. quit(sprintf(_g("unable to make %s a symlink to %s: %s"), $linkname, $filename, $!));
  707. }
  708. sub checked_mv {
  709. my ($source, $dest) = @_;
  710. rename_mv($source, $dest) ||
  711. quit(sprintf(_g("unable to install %s as %s: %s"), $source, $dest, $!));
  712. }
  713. sub config_all {
  714. opendir ADMINDIR, $admindir or die sprintf(_g("Serious problem: %s"), $!);
  715. my @filenames = grep !/^\.\.?$/, readdir ADMINDIR;
  716. close ADMINDIR;
  717. foreach my $name (@filenames) {
  718. system "$0 $skip --config $name";
  719. exit $? if $?;
  720. }
  721. }
  722. exit(0);
  723. # vim: nowrap ts=8 sw=4