update-alternatives.pl 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. #!/usr/bin/perl
  2. #
  3. # update-alternatives
  4. #
  5. # Copyright © 1995 Ian Jackson <ian@davenant.greenend.org.uk>
  6. # Copyright © 2000-2002 Wichert Akkerman <wakkerma@debian.org>
  7. # Copyright © 2006-2009 Guillem Jover <guillem@debian.org>
  8. # Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
  9. # Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. BEGIN { # Work-around for bug #479711 in perl
  24. $ENV{PERL_DL_NONLAZY} = 1;
  25. }
  26. use strict;
  27. use warnings;
  28. use POSIX qw(:errno_h);
  29. use Dpkg;
  30. use Dpkg::Gettext;
  31. textdomain("dpkg");
  32. # Global variables:
  33. my $altdir = '/etc/alternatives';
  34. my $admdir = $admindir . '/alternatives';
  35. my $action = ''; # Action to perform (display / query / install / remove / auto / config)
  36. my $alternative; # Alternative worked on
  37. my $inst_alt; # Alternative to install
  38. my $fileset; # Set of files to install in the alternative
  39. my $path; # Path of alternative we are offering
  40. my $log_file = "/var/log/dpkg.log";
  41. my $skip_auto = 0; # Skip alternatives properly configured in auto mode (for --config)
  42. my $verbosemode = 0;
  43. my $force = 0;
  44. my @pass_opts;
  45. $| = 1;
  46. #
  47. # Main program
  48. #
  49. my @COPY_ARGV = @ARGV;
  50. while (@ARGV) {
  51. $_ = shift(@ARGV);
  52. last if m/^--$/;
  53. if (!m/^--/) {
  54. error(_g("unknown argument \`%s'"), $_);
  55. } elsif (m/^--help$/) {
  56. usage();
  57. exit(0);
  58. } elsif (m/^--version$/) {
  59. version();
  60. exit(0);
  61. } elsif (m/^--verbose$/) {
  62. $verbosemode= +1;
  63. push @pass_opts, $_;
  64. } elsif (m/^--quiet$/) {
  65. $verbosemode= -1;
  66. push @pass_opts, $_;
  67. } elsif (m/^--install$/) {
  68. set_action("install");
  69. @ARGV >= 4 || badusage(_g("--install needs <link> <name> <path> <priority>"));
  70. my $link = shift @ARGV;
  71. my $name = shift @ARGV;
  72. my $path = shift @ARGV;
  73. my $priority = shift @ARGV;
  74. badusage(_g("<link> and <path> can't be the same")) if $link eq $path;
  75. $priority =~ m/^[-+]?\d+/ || badusage(_g("priority must be an integer"));
  76. $alternative = Alternative->new($name);
  77. $inst_alt = Alternative->new($name);
  78. $inst_alt->set_status("auto");
  79. $inst_alt->set_link($link);
  80. $fileset = FileSet->new($path, $priority);
  81. } elsif (m/^--(remove|set)$/) {
  82. set_action($1);
  83. @ARGV >= 2 || badusage(_g("--%s needs <name> <path>"), $1);
  84. $alternative = Alternative->new(shift(@ARGV));
  85. $path = shift @ARGV;
  86. } elsif (m/^--(display|query|auto|config|list|remove-all)$/) {
  87. set_action($1);
  88. @ARGV || badusage(_g("--%s needs <name>"), $1);
  89. $alternative = Alternative->new(shift(@ARGV));
  90. } elsif (m/^--(all|get-selections|set-selections)$/) {
  91. set_action($1);
  92. } elsif (m/^--slave$/) {
  93. badusage(_g("--slave only allowed with --install"))
  94. unless $action eq "install";
  95. @ARGV >= 3 || badusage(_g("--slave needs <link> <name> <path>"));
  96. my $slink = shift @ARGV;
  97. my $sname = shift @ARGV;
  98. my $spath = shift @ARGV;
  99. badusage(_g("<link> and <path> can't be the same")) if $slink eq $spath;
  100. badusage(_g("name %s is both primary and slave"), $inst_alt->name())
  101. if $sname eq $inst_alt->name();
  102. if ($inst_alt->has_slave($sname)) {
  103. badusage(_g("slave name %s duplicated"), $sname);
  104. }
  105. foreach my $slave ($inst_alt->slaves()) {
  106. my $link = $inst_alt->slave_link($slave) || "";
  107. badusage(_g("slave link %s duplicated"), $slink) if $link eq $slink;
  108. badusage(_g("link %s is both primary and slave"), $slink)
  109. if $link eq $inst_alt->link();
  110. }
  111. $inst_alt->add_slave($sname, $slink);
  112. $fileset->add_slave($sname, $spath);
  113. } elsif (m/^--log$/) {
  114. @ARGV || badusage(_g("--%s needs a <file> argument"), "log");
  115. $log_file = shift @ARGV;
  116. push @pass_opts, $_, $log_file;
  117. } elsif (m/^--altdir$/) {
  118. @ARGV || badusage(_g("--%s needs a <directory> argument"), "altdir");
  119. $altdir = shift @ARGV;
  120. push @pass_opts, $_, $altdir;
  121. } elsif (m/^--admindir$/) {
  122. @ARGV || badusage(_g("--%s needs a <directory> argument"), "admindir");
  123. $admdir = shift @ARGV;
  124. push @pass_opts, $_, $admdir;
  125. } elsif (m/^--skip-auto$/) {
  126. $skip_auto = 1;
  127. push @pass_opts, $_;
  128. } elsif (m/^--force$/) {
  129. $force = 1;
  130. push @pass_opts, $_;
  131. } else {
  132. badusage(_g("unknown option \`%s'"), $_);
  133. }
  134. }
  135. badusage(_g("need --display, --query, --list, --get-selections, --config," .
  136. "--set, --set-selections, --install, --remove, --all, " .
  137. "--remove-all or --auto"))
  138. unless $action;
  139. # Load infos about all alternatives to be able to check for mistakes
  140. my %ALL;
  141. foreach my $alt_name (get_all_alternatives()) {
  142. my $alt = Alternative->new($alt_name);
  143. next unless $alt->load("$admdir/$alt_name", 1);
  144. $ALL{objects}{$alt_name} = $alt;
  145. $ALL{links}{$alt->link()} = $alt_name;
  146. $ALL{parent}{$alt_name} = $alt_name;
  147. foreach my $slave ($alt->slaves()) {
  148. $ALL{links}{$alt->slave_link($slave)} = $slave;
  149. $ALL{parent}{$slave} = $alt_name;
  150. }
  151. }
  152. # Check that caller don't mix links between alternatives and don't mix
  153. # alternatives between slave/master, and that the various parameters
  154. # are fine
  155. if ($action eq "install") {
  156. my ($name, $link, $file) = ($inst_alt->name(), $inst_alt->link(), $fileset->master());
  157. if (exists $ALL{parent}{$name} and $ALL{parent}{$name} ne $name) {
  158. error(_g("alternative %s can't be master: %s"), $name,
  159. sprintf(_g("it is a slave of %s"), $ALL{parent}{$name}));
  160. }
  161. if (exists $ALL{links}{$link} and $ALL{links}{$link} ne $name) {
  162. error(_g("alternative link %s is already managed by %s."),
  163. $link, $ALL{parent}{$ALL{links}{$link}});
  164. }
  165. error(_g("alternative link is not absolute as it should be: %s"),
  166. $link) unless $link =~ m|^/|;
  167. error(_g("alternative path is not absolute as it should be: %s"),
  168. $file) unless $file =~ m|^/|;
  169. error(_g("alternative path %s doesn't exist."), $file)
  170. unless -e $file;
  171. error(_g("alternative name (%s) must not contain '/' and spaces."), $name)
  172. if $name =~ m|[/\s]|;
  173. foreach my $slave ($inst_alt->slaves()) {
  174. $link = $inst_alt->slave_link($slave);
  175. $file = $fileset->slave($slave);
  176. if (exists $ALL{parent}{$slave} and $ALL{parent}{$slave} ne $name) {
  177. error(_g("alternative %s can't be slave of %s: %s"),
  178. $slave, $name, ($ALL{parent}{$slave} eq $slave) ?
  179. _g("it is a master alternative.") :
  180. sprintf(_g("it is a slave of %s"), $ALL{parent}{$slave})
  181. );
  182. }
  183. if (exists $ALL{links}{$link} and $ALL{links}{$link} ne $slave) {
  184. error(_g("alternative link %s is already managed by %s."),
  185. $link, $ALL{parent}{$ALL{links}{$link}});
  186. }
  187. error(_g("alternative link is not absolute as it should be: %s"),
  188. $link) unless $link =~ m|^/|;
  189. error(_g("alternative path is not absolute as it should be: %s"),
  190. $file) unless $file =~ m|^/|;
  191. error(_g("alternative name (%s) must not contain '/' and spaces."), $slave)
  192. if $slave =~ m|[/\s]|;
  193. }
  194. }
  195. # Handle actions
  196. if ($action eq 'all') {
  197. config_all();
  198. exit 0;
  199. } elsif ($action eq 'get-selections') {
  200. foreach my $alt_name (sort keys %{$ALL{objects}}) {
  201. my $obj = $ALL{objects}{$alt_name};
  202. printf "%-30s %-8s %s\n", $alt_name, $obj->status(), $obj->current() || "";
  203. }
  204. exit 0;
  205. } elsif ($action eq 'set-selections') {
  206. log_msg("run with @COPY_ARGV");
  207. my $line;
  208. my $prefix = "[$progname --set-selections] ";
  209. while (defined($line = <STDIN>)) {
  210. chomp($line);
  211. my ($alt_name, $status, $choice) = split(/\s+/, $line, 3);
  212. if (exists $ALL{objects}{$alt_name}) {
  213. my $obj = $ALL{objects}{$alt_name};
  214. if ($status eq "auto") {
  215. pr($prefix . _g("Call %s."), "$0 --auto $alt_name");
  216. system($0, @pass_opts, "--auto", $alt_name);
  217. exit $? if $?;
  218. } else {
  219. if ($obj->has_choice($choice)) {
  220. pr($prefix . _g("Call %s."), "$0 --set $alt_name $choice");
  221. system($0, @pass_opts, "--set", $alt_name, $choice);
  222. exit $? if $?;
  223. } else {
  224. pr($prefix . _g("Alternative %s unchanged because choice " .
  225. "%s is not available."), $alt_name, $choice);
  226. }
  227. }
  228. } else {
  229. pr($prefix . _g("Skip unknown alternative %s."), $alt_name);
  230. }
  231. }
  232. exit 0;
  233. }
  234. # Load the alternative info, stop on failure except for --install
  235. if (not $alternative->load("$admdir/" . $alternative->name())
  236. and $action ne "install")
  237. {
  238. # FIXME: Be consistent for now with the case when we try to remove a
  239. # non-existing path from an existing link group file.
  240. if ($action eq "remove") {
  241. verbose(_g("no alternatives for %s."), $alternative->name());
  242. exit 0;
  243. }
  244. error(_g("no alternatives for %s."), $alternative->name());
  245. }
  246. if ($action eq 'display') {
  247. $alternative->display_user();
  248. exit 0;
  249. } elsif ($action eq 'query') {
  250. $alternative->display_query();
  251. exit 0;
  252. } elsif ($action eq 'list') {
  253. $alternative->display_list();
  254. exit 0;
  255. }
  256. # Actions below might modify the system
  257. log_msg("run with @COPY_ARGV");
  258. my $current_choice = '';
  259. if ($alternative->has_current_link()) {
  260. $current_choice = $alternative->current();
  261. # Detect manually modified alternative, switch to manual
  262. if (not $alternative->has_choice($current_choice)) {
  263. if (not -e $current_choice) {
  264. warning(_g("%s is dangling, it will be updated with best choice."),
  265. "$altdir/" . $alternative->name());
  266. $alternative->set_status('auto');
  267. } elsif ($alternative->status() ne "manual") {
  268. warning(_g("%s has been changed (manually or by a script). " .
  269. "Switching to manual updates only."),
  270. "$altdir/" . $alternative->name());
  271. $alternative->set_status('manual');
  272. }
  273. }
  274. } else {
  275. # Lack of alternative link => automatic mode
  276. verbose(_g("setting up automatic selection of %s."), $alternative->name());
  277. $alternative->set_status('auto');
  278. }
  279. my $new_choice;
  280. if ($action eq 'set') {
  281. if ($alternative->has_choice($path)) {
  282. $new_choice = $path;
  283. } else {
  284. error(_g("alternative %s for %s not registered, not setting."),
  285. $path, $alternative->name());
  286. }
  287. $alternative->set_status('manual');
  288. } elsif ($action eq 'auto') {
  289. $alternative->set_status('auto');
  290. $new_choice = $alternative->best();
  291. } elsif ($action eq 'config') {
  292. if (not scalar($alternative->choices())) {
  293. pr(_g("There is no program which provides %s."), $alternative->name());
  294. pr(_g("Nothing to configure."));
  295. } elsif ($skip_auto && $alternative->status() eq 'auto') {
  296. $alternative->display_user();
  297. } elsif (scalar($alternative->choices()) == 1 and
  298. $alternative->status() eq 'auto' and
  299. $alternative->has_current_link()) {
  300. pr(_g("There is only one alternative in link group %s: %s"),
  301. $alternative->name(), $alternative->current());
  302. pr(_g("Nothing to configure."));
  303. } else {
  304. $new_choice = $alternative->select_choice();
  305. }
  306. } elsif ($action eq 'remove') {
  307. if ($alternative->has_choice($path)) {
  308. $alternative->remove_choice($path);
  309. } else {
  310. verbose(_g("alternative %s for %s not registered, not removing."),
  311. $path, $alternative->name());
  312. }
  313. if ($current_choice eq $path) {
  314. # Current choice is removed
  315. if ($alternative->status() eq "manual") {
  316. # And it was manual, switch to auto
  317. info(_g("removing manually selected alternative - " .
  318. "switching %s to auto mode"), $alternative->name());
  319. $alternative->set_status('auto');
  320. }
  321. $new_choice = $alternative->best();
  322. }
  323. } elsif ($action eq 'remove-all') {
  324. foreach my $choice ($alternative->choices()) {
  325. $alternative->remove_choice($choice);
  326. }
  327. } elsif ($action eq 'install') {
  328. if (defined($alternative->link())) {
  329. # Alternative already exists, check if anything got updated
  330. my ($old, $new) = ($alternative->link(), $inst_alt->link());
  331. $alternative->set_link($new);
  332. if ($old ne $new and -l $old) {
  333. info(_g("renaming %s link from %s to %s."), $inst_alt->name(),
  334. $old, $new);
  335. checked_mv($old, $new);
  336. }
  337. # Check if new slaves have been added, or existing ones renamed
  338. foreach my $slave ($inst_alt->slaves()) {
  339. $new = $inst_alt->slave_link($slave);
  340. if (not $alternative->has_slave($slave)) {
  341. $alternative->add_slave($slave, $new);
  342. next;
  343. }
  344. $old = $alternative->slave_link($slave);
  345. $alternative->add_slave($slave, $new);
  346. my $new_file = ($current_choice eq $fileset->master()) ?
  347. $fileset->slave($slave) :
  348. readlink("$admdir/$slave") || "";
  349. if ($old ne $new and -l $old) {
  350. if (-e $new_file) {
  351. info(_g("renaming %s slave link from %s to %s."),
  352. $slave,$old, $new);
  353. checked_mv($old, $new);
  354. } else {
  355. checked_rm($old);
  356. }
  357. }
  358. }
  359. } else {
  360. # Alternative doesn't exist, create from parameters
  361. $alternative = $inst_alt;
  362. }
  363. $alternative->add_choice($fileset);
  364. if ($alternative->status() eq "auto") {
  365. # Update automatic choice if needed
  366. $new_choice = $alternative->best();
  367. } else {
  368. verbose(_g("automatic updates of %s are disabled, leaving it alone."),
  369. "$altdir/" . $alternative->name());
  370. verbose(_g("to return to automatic updates use ".
  371. "\`update-alternatives --auto %s'."), $alternative->name());
  372. }
  373. }
  374. # No choice left, remove everything
  375. if (not scalar($alternative->choices())) {
  376. log_msg("link group " . $alternative->name() . " fully removed");
  377. $alternative->remove();
  378. exit 0;
  379. }
  380. # New choice wanted
  381. if (defined($new_choice) and ($current_choice ne $new_choice)) {
  382. log_msg("link group " . $alternative->name() .
  383. " updated to point to " . $new_choice);
  384. info(_g("using %s to provide %s (%s) in %s."), $new_choice,
  385. $alternative->link(), $alternative->name(),
  386. ($alternative->status() eq "auto" ? _g("auto mode") : _g("manual mode")));
  387. $alternative->prepare_install($new_choice);
  388. } elsif ($alternative->is_broken()) {
  389. log_msg("auto-repair link group " . $alternative->name());
  390. warning(_g("forcing reinstallation of alternative %s " .
  391. "because link group %s is broken."),
  392. $current_choice, $alternative->name());
  393. $alternative->prepare_install($current_choice) if $current_choice;
  394. }
  395. # Save administrative file if needed
  396. if ($alternative->is_modified()) {
  397. $alternative->save("$admdir/" . $alternative->name() . ".dpkg-tmp");
  398. checked_mv("$admdir/" . $alternative->name() . ".dpkg-tmp",
  399. "$admdir/" . $alternative->name());
  400. }
  401. # Replace all symlinks in one pass
  402. $alternative->commit();
  403. exit 0;
  404. ### FUNCTIONS ####
  405. sub version {
  406. printf _g("Debian %s version %s.\n"), $progname, $version;
  407. printf _g("
  408. Copyright (C) 1995 Ian Jackson.
  409. Copyright (C) 2000-2002 Wichert Akkerman.
  410. Copyright (C) 2009 Raphael Hertzog.");
  411. printf _g("
  412. This is free software; see the GNU General Public Licence version 2 or
  413. later for copying conditions. There is NO warranty.
  414. ");
  415. }
  416. sub usage {
  417. printf _g(
  418. "Usage: %s [<option> ...] <command>
  419. Commands:
  420. --install <link> <name> <path> <priority>
  421. [--slave <link> <name> <path>] ...
  422. add a group of alternatives to the system.
  423. --remove <name> <path> remove <path> from the <name> group alternative.
  424. --remove-all <name> remove <name> group from the alternatives system.
  425. --auto <name> switch the master link <name> to automatic mode.
  426. --display <name> display information about the <name> group.
  427. --query <name> machine parseable version of --display <name>.
  428. --list <name> display all targets of the <name> group.
  429. --config <name> show alternatives for the <name> group and ask the
  430. user to select which one to use.
  431. --set <name> <path> set <path> as alternative for <name>.
  432. --all call --config on all alternatives.
  433. <link> is the symlink pointing to %s/<name>.
  434. (e.g. /usr/bin/pager)
  435. <name> is the master name for this link group.
  436. (e.g. pager)
  437. <path> is the location of one of the alternative target files.
  438. (e.g. /usr/bin/less)
  439. <priority> is an integer; options with higher numbers have higher priority in
  440. automatic mode.
  441. Options:
  442. --altdir <directory> change the alternatives directory.
  443. --admindir <directory> change the administrative directory.
  444. --skip-auto skip prompt for alternatives correctly configured
  445. in automatic mode (relevant for --config only)
  446. --verbose verbose operation, more output.
  447. --quiet quiet operation, minimal output.
  448. --help show this help message.
  449. --version show the version.
  450. "), $progname, $altdir;
  451. }
  452. sub error {
  453. my ($format, @params) = @_;
  454. $! = 2;
  455. die sprintf("%s: %s: %s\n", $progname, _g("error"),
  456. sprintf($format, @params));
  457. }
  458. sub badusage {
  459. my ($format, @params) = @_;
  460. printf STDERR "%s: %s\n\n", $progname, sprintf($format, @params);
  461. usage();
  462. exit(2);
  463. }
  464. sub warning {
  465. my ($format, @params) = @_;
  466. if ($verbosemode >= 0) {
  467. printf STDERR "%s: %s: %s\n", $progname, _g("warning"),
  468. sprintf($format, @params);
  469. }
  470. }
  471. sub msg {
  472. my ($min_level, $format, @params) = @_;
  473. if ($verbosemode >= $min_level) {
  474. printf STDOUT "%s: %s\n", $progname, sprintf($format, @params);
  475. }
  476. }
  477. sub verbose {
  478. msg(1, @_);
  479. }
  480. sub info {
  481. msg(0, @_);
  482. }
  483. sub pr {
  484. my ($format, @params) = @_;
  485. printf ($format . "\n", @params);
  486. }
  487. sub set_action {
  488. my ($value) = @_;
  489. if ($action) {
  490. badusage(_g("two commands specified: --%s and --%s"), $value, $action);
  491. }
  492. $action = $value;
  493. }
  494. {
  495. my $fh_log;
  496. sub log_msg {
  497. my ($msg) = @_;
  498. # XXX: the C rewrite must use the std function to get the
  499. # filename from /etc/dpkg/dpkg.cfg or from command line
  500. if (!defined($fh_log) and -w $log_file) {
  501. open($fh_log, ">>", $log_file) ||
  502. error(_g("Can't append to %s"), $log_file);
  503. }
  504. if (defined($fh_log)) {
  505. $msg = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime()) .
  506. " $progname: $msg\n";
  507. print $fh_log $msg;
  508. }
  509. }
  510. }
  511. sub get_all_alternatives {
  512. opendir(ADMINDIR, $admdir)
  513. or error(_g("can't readdir %s: %s"), $admdir, $!);
  514. my @filenames = grep { !/^\.\.?$/ and !/\.dpkg-tmp$/ } (readdir(ADMINDIR));
  515. close(ADMINDIR);
  516. return sort @filenames;
  517. }
  518. sub config_all {
  519. foreach my $name (get_all_alternatives()) {
  520. system($0, @pass_opts, "--config", $name);
  521. exit $? if $?;
  522. print "\n";
  523. }
  524. }
  525. sub rename_mv {
  526. my ($source, $dest) = @_;
  527. lstat($source);
  528. return 0 if not -e _;
  529. if (not rename($source, $dest)) {
  530. if (system("mv", $source, $dest) != 0) {
  531. return 0;
  532. }
  533. }
  534. return 1;
  535. }
  536. sub checked_symlink {
  537. my ($filename, $linkname) = @_;
  538. symlink($filename, $linkname) ||
  539. error(_g("unable to make %s a symlink to %s: %s"), $linkname, $filename, $!);
  540. }
  541. sub checked_mv {
  542. my ($source, $dest) = @_;
  543. rename_mv($source, $dest) ||
  544. error(_g("unable to install %s as %s: %s"), $source, $dest, $!);
  545. }
  546. sub checked_rm {
  547. my ($f) = @_;
  548. unlink($f) || $! == ENOENT || error(_g("unable to remove %s: %s"), $f, $!);
  549. }
  550. ### OBJECTS ####
  551. package FileSet;
  552. use Dpkg::Gettext;
  553. sub new {
  554. my ($class, $master_file, $prio) = @_;
  555. my $self = {
  556. "master_file" => $master_file,
  557. "priority" => $prio,
  558. "slaves" =>
  559. {
  560. # "slave_name" => "slave_file"
  561. },
  562. };
  563. return bless $self, $class;
  564. }
  565. sub add_slave {
  566. my ($self, $name, $file) = @_;
  567. $self->{slaves}{$name} = $file;
  568. }
  569. sub has_slave {
  570. my ($self, $slave) = @_;
  571. return (exists $self->{"slaves"}{$slave} and $self->{"slaves"}{$slave});
  572. }
  573. sub master {
  574. my ($self, $val) = @_;
  575. return $self->{"master_file"};
  576. }
  577. sub priority {
  578. my ($self) = @_;
  579. return $self->{"priority"};
  580. }
  581. sub slave {
  582. my ($self, $slave) = @_;
  583. return $self->{"slaves"}{$slave};
  584. }
  585. package Alternative;
  586. use Dpkg::Gettext;
  587. use POSIX qw(:errno_h);
  588. sub pr { main::pr(@_) }
  589. sub error { main::error(@_) }
  590. sub new {
  591. my ($class, $name) = @_;
  592. my $self = {};
  593. bless $self, $class;
  594. $self->reset($name);
  595. return $self;
  596. }
  597. sub reset {
  598. my ($self, $name) = @_;
  599. my $new = {
  600. "master_name" => $name,
  601. "master_link" => undef,
  602. "status" => undef,
  603. "slaves" => {
  604. # "slave_name" => "slave_link"
  605. },
  606. "choices" => {
  607. # "master_file" => $fileset
  608. },
  609. "modified" => 0,
  610. "commit_ops" => [],
  611. };
  612. %$self = %$new;
  613. }
  614. sub choices {
  615. my ($self) = @_;
  616. my @choices = sort { $a cmp $b } keys %{$self->{choices}};
  617. return wantarray ? @choices : scalar(@choices);
  618. }
  619. sub slaves {
  620. my ($self) = @_;
  621. my @slaves = sort { $a cmp $b } keys %{$self->{slaves}};
  622. return wantarray ? @slaves : scalar(@slaves);
  623. }
  624. sub name {
  625. my ($self) = @_;
  626. return $self->{master_name};
  627. }
  628. sub link {
  629. my ($self) = @_;
  630. return $self->{master_link};
  631. }
  632. sub status {
  633. my ($self) = @_;
  634. return $self->{status};
  635. }
  636. sub fileset {
  637. my ($self, $id) = @_;
  638. return $self->{choices}{$id} if exists $self->{choices}{$id};
  639. return undef;
  640. }
  641. sub slave_link {
  642. my ($self, $id) = @_;
  643. return $self->{slaves}{$id} if exists $self->{slaves}{$id};
  644. return undef;
  645. }
  646. sub has_slave {
  647. my ($self, $slave) = @_;
  648. return (exists $self->{"slaves"}{$slave} and $self->{"slaves"}{$slave});
  649. }
  650. sub is_modified {
  651. my ($self) = @_;
  652. return $self->{modified};
  653. }
  654. sub has_choice {
  655. my ($self, $id) = @_;
  656. return exists $self->{choices}{$id};
  657. }
  658. sub add_choice {
  659. my ($self, $fileset) = @_;
  660. $self->{choices}{$fileset->master()} = $fileset;
  661. $self->{modified} = 1; # XXX: be smarter in detecting change ?
  662. }
  663. sub add_slave {
  664. my ($self, $slave, $link) = @_;
  665. $self->{slaves}{$slave} = $link;
  666. }
  667. sub set_status {
  668. my ($self, $status) = @_;
  669. if (!defined($self->status()) or $status ne $self->status()) {
  670. $self->{modified} = 1;
  671. }
  672. main::log_msg("status of link group " . $self->name() . " set to $status")
  673. if defined($self->status()) and $status ne $self->status();
  674. $self->{status} = $status;
  675. }
  676. sub set_link {
  677. my ($self, $link) = @_;
  678. if (!defined($self->link()) or $link ne $self->link()) {
  679. $self->{modified} = 1;
  680. }
  681. $self->{master_link} = $link;
  682. }
  683. sub remove_choice {
  684. my ($self, $id) = @_;
  685. if ($self->has_choice($id)) {
  686. delete $self->{choices}{$id};
  687. $self->{modified} = 1;
  688. return 1;
  689. }
  690. return 0;
  691. }
  692. {
  693. # Helper functions for load() and save()
  694. my ($fh, $filename);
  695. sub config_helper {
  696. ($fh, $filename) = @_;
  697. }
  698. sub gl {
  699. undef $!;
  700. my $line = <$fh>;
  701. unless (defined($line)) {
  702. error(_g("while reading %s: %s"), $filename, $!) if $!;
  703. error(_g("unexpected end of file in %s while trying to read %s"),
  704. $filename, $_[0]);
  705. }
  706. chomp($line);
  707. return $line;
  708. }
  709. sub badfmt {
  710. my ($format, @params) = @_;
  711. error(_g("%s corrupt: %s"), $filename, sprintf($format, @params));
  712. }
  713. sub paf {
  714. my $line = shift @_;
  715. if ($line =~ m/\n/) {
  716. error(_g("newlines prohibited in update-alternatives files (%s)"), $line);
  717. }
  718. print $fh "$line\n" || error(_g("while writing %s: %s"), $filename, $!);
  719. }
  720. }
  721. sub load {
  722. my ($self, $file, $must_not_die) = @_;
  723. return 0 unless -s $file;
  724. eval {
  725. open(my $fh, "<", $file) || error(_g("unable to read %s: %s"), $file, $!);
  726. config_helper($fh, $file);
  727. my $status = gl(_g("status"));
  728. badfmt(_g("invalid status")) unless $status =~ /^(?:auto|manual)$/;
  729. my $link = gl("link");
  730. my (%slaves, @slaves);
  731. while ((my $slave_name = gl(_g("slave name"))) ne '') {
  732. my $slave_link = gl(_g("slave link"));
  733. badfmt(_g("duplicate slave %s"), $slave_name)
  734. if exists $slaves{$slave_name};
  735. badfmt(_g("slave link same as main link %s"), $link)
  736. if $slave_link eq $link;
  737. badfmt(_g("duplicate slave link %s"), $slave_link)
  738. if grep { $_ eq $slave_link } values %slaves;
  739. $slaves{$slave_name} = $slave_link;
  740. push @slaves, $slave_name;
  741. }
  742. my @filesets;
  743. my $modified = 0;
  744. while ((my $main_file = gl(_g("master file"))) ne '') {
  745. badfmt(_g("duplicate path %s"), $main_file)
  746. if grep { $_->{master_file} eq $main_file } @filesets;
  747. if (-e $main_file) {
  748. my $priority = gl(_g("priority"));
  749. badfmt(_g("priority of %s: %s"), $main_file, $priority)
  750. unless $priority =~ m/^[-+]?\d+$/;
  751. my $group = FileSet->new($main_file, $priority);
  752. foreach my $slave (@slaves) {
  753. $group->add_slave($slave, gl(_g("slave file")));
  754. }
  755. push @filesets, $group;
  756. } else {
  757. # File not found - remove
  758. main::warning(_g("alternative %s (part of link group %s) " .
  759. "doesn't exist. Removing from list of ".
  760. "alternatives."),
  761. $main_file, $self->name()) unless $must_not_die;
  762. gl(_g("priority"));
  763. foreach my $slave (@slaves) {
  764. gl(_g("slave file"));
  765. }
  766. $modified = 1;
  767. }
  768. }
  769. close($fh);
  770. # We parsed the file without trouble, load data into the object
  771. $self->{master_link} = $link;
  772. $self->{slaves} = \%slaves;
  773. $self->{status} = $status;
  774. $self->{modified} = $modified;
  775. $self->{choices} = {};
  776. foreach my $group (@filesets) {
  777. $self->{choices}{$group->master()} = $group;
  778. }
  779. };
  780. if ($@) {
  781. return 0 if $must_not_die;
  782. die $@;
  783. }
  784. return 1;
  785. }
  786. sub save {
  787. my ($self, $file) = @_;
  788. # Cleanup unused slaves before writing admin file
  789. foreach my $slave ($self->slaves()) {
  790. my $has_slave = 0;
  791. foreach my $choice ($self->choices()) {
  792. my $fileset = $self->fileset($choice);
  793. $has_slave++ if $fileset->has_slave($slave);
  794. }
  795. unless ($has_slave) {
  796. main::verbose(_g("discarding obsolete slave link %s (%s)."),
  797. $slave, $self->slave_link($slave));
  798. delete $self->{"slaves"}{$slave};
  799. }
  800. }
  801. # Write admin file
  802. open(my $fh, ">", $file) || error(_g("unable to write %s: %s"), $file, $!);
  803. config_helper($fh, $file);
  804. paf($self->status());
  805. paf($self->link());
  806. foreach my $slave ($self->slaves()) {
  807. paf($slave);
  808. paf($self->slave_link($slave));
  809. }
  810. paf('');
  811. foreach my $choice ($self->choices()) {
  812. paf($choice);
  813. my $fileset = $self->fileset($choice);
  814. paf($fileset->priority());
  815. foreach my $slave ($self->slaves()) {
  816. if ($fileset->has_slave($slave)) {
  817. paf($fileset->slave($slave));
  818. } else {
  819. paf('');
  820. }
  821. }
  822. }
  823. paf('');
  824. close($fh) || error(_g("unable to close %s: %s"), $file, $!);
  825. }
  826. sub display_query {
  827. my ($self) = @_;
  828. pr("Link: %s", $self->name());
  829. pr("Status: %s", $self->status());
  830. my $best = $self->best();
  831. if (defined($best)) {
  832. pr("Best: %s", $best);
  833. }
  834. if ($self->has_current_link()) {
  835. pr("Value: %s", $self->current());
  836. } else {
  837. pr("Value: none");
  838. }
  839. foreach my $choice ($self->choices()) {
  840. pr("");
  841. pr("Alternative: %s", $choice);
  842. my $fileset = $self->fileset($choice);
  843. pr("Priority: %s", $fileset->priority());
  844. next unless scalar($self->slaves());
  845. pr("Slaves:");
  846. foreach my $slave ($self->slaves()) {
  847. if ($fileset->has_slave($slave)) {
  848. pr(" %s %s", $slave, $fileset->slave($slave));
  849. }
  850. }
  851. }
  852. }
  853. sub display_user {
  854. my ($self) = @_;
  855. pr("%s - %s", $self->name(),
  856. ($self->status() eq "auto") ? _g("auto mode") : _g("manual mode"));
  857. if ($self->has_current_link()) {
  858. pr(_g(" link currently points to %s"), $self->current());
  859. } else {
  860. pr(_g(" link currently absent"));
  861. }
  862. foreach my $choice ($self->choices()) {
  863. my $fileset = $self->fileset($choice);
  864. pr(_g("%s - priority %s"), $choice, $fileset->priority());
  865. foreach my $slave ($self->slaves()) {
  866. if ($fileset->has_slave($slave)) {
  867. pr(_g(" slave %s: %s"), $slave, $fileset->slave($slave));
  868. }
  869. }
  870. }
  871. my $best = $self->best();
  872. if (defined($best) && $best) {
  873. pr(_g("Current 'best' version is '%s'."), $best);
  874. } else {
  875. pr(_g("No versions available."));
  876. }
  877. }
  878. sub display_list {
  879. my ($self) = @_;
  880. pr($_) foreach ($self->choices());
  881. }
  882. sub select_choice {
  883. my ($self) = @_;
  884. while (1) {
  885. my $current = $self->current() || "";
  886. my $best = $self->best();
  887. printf _g("There are %s choices for the alternative %s (providing %s).") . "\n\n",
  888. scalar($self->choices()), $self->name(), $self->link();
  889. my $length = 15;
  890. foreach ($self->choices()) {
  891. $length = (length($_) > $length) ? length($_) + 1 : $length;
  892. }
  893. printf " %-12.12s %-${length}.${length}s %-10.10s %s\n", _g("Selection"),
  894. _g("Path"), _g("Priority"), _g("Status");
  895. print "-" x 60 . "\n";
  896. printf "%s %-12d %-${length}s % -10d %s\n",
  897. ($self->status() eq "auto" and $current eq $best) ? "*" : " ", 0,
  898. $best, $self->fileset($best)->priority(), _g("auto mode");
  899. my $index = 1;
  900. my %sel = ("0" => $best);
  901. foreach my $choice ($self->choices()) {
  902. $sel{$index} = $choice;
  903. $sel{$choice} = $choice;
  904. printf "%s %-12d %-${length}.${length}s % -10d %s\n",
  905. ($self->status() eq "manual" and $current eq $choice) ? "*" : " ",
  906. $index, $choice, $self->fileset($choice)->priority(),
  907. _g("manual mode");
  908. $index++;
  909. }
  910. print "\n";
  911. printf _g("Press enter to keep the current choice[*], or type selection number: ");
  912. my $selection = <STDIN>;
  913. return undef unless defined($selection);
  914. chomp($selection);
  915. return ($current || $best) if $selection eq "";
  916. if (exists $sel{$selection}) {
  917. $self->set_status(($selection eq "0") ? "auto" : "manual");
  918. return $sel{$selection};
  919. }
  920. }
  921. }
  922. sub best {
  923. my ($self) = @_;
  924. my @choices = sort { $self->fileset($b)->priority() <=>
  925. $self->fileset($a)->priority()
  926. } ($self->choices());
  927. if (scalar(@choices)) {
  928. return $choices[0];
  929. } else {
  930. return undef;
  931. }
  932. }
  933. sub has_current_link {
  934. my ($self) = @_;
  935. return -l "$altdir/$self->{master_name}";
  936. }
  937. sub current {
  938. my ($self) = @_;
  939. return undef unless $self->has_current_link();
  940. my $val = readlink("$altdir/$self->{master_name}");
  941. error(_g("readlink(%s) failed: %s"), "$altdir/$self->{master_name}", $!)
  942. unless defined $val;
  943. return $val;
  944. }
  945. sub add_commit_op {
  946. my ($self, $sub) = @_;
  947. push @{$self->{commit_ops}}, $sub;
  948. }
  949. sub prepare_install {
  950. my ($self, $choice) = @_;
  951. my ($link, $name) = ($self->link(), $self->name());
  952. my $fileset = $self->fileset($choice);
  953. main::error("can't install unknown choice %s", $choice)
  954. if not defined($choice);
  955. # Create link in /etc/alternatives
  956. main::checked_rm("$altdir/$name.dpkg-tmp");
  957. main::checked_symlink($choice, "$altdir/$name.dpkg-tmp");
  958. $self->add_commit_op(sub {
  959. main::checked_mv("$altdir/$name.dpkg-tmp", "$altdir/$name");
  960. });
  961. $! = 0; lstat($link);
  962. if (-l _ or $! == ENOENT or $force) {
  963. # Create alternative link
  964. main::checked_rm("$link.dpkg-tmp");
  965. main::checked_symlink("$altdir/$name", "$link.dpkg-tmp");
  966. $self->add_commit_op(sub {
  967. main::checked_mv("$link.dpkg-tmp", $link);
  968. });
  969. } else {
  970. main::warning(_g("not replacing %s with a link."), $link);
  971. }
  972. # Take care of slaves links
  973. foreach my $slave ($self->slaves()) {
  974. my ($slink, $spath) = ($self->slave_link($slave), $fileset->slave($slave));
  975. if ($fileset->has_slave($slave) and -e $spath) {
  976. # Create link in /etc/alternatives
  977. main::checked_rm("$altdir/$slave.dpkg-tmp");
  978. main::checked_symlink($spath, "$altdir/$slave.dpkg-tmp");
  979. $self->add_commit_op(sub {
  980. main::checked_mv("$altdir/$slave.dpkg-tmp", "$altdir/$slave");
  981. });
  982. $! = 0; lstat($slink);
  983. if (-l _ or $! == ENOENT or $force) {
  984. # Create alternative link
  985. main::checked_rm("$slink.dpkg-tmp");
  986. main::checked_symlink("$altdir/$slave", "$slink.dpkg-tmp");
  987. $self->add_commit_op(sub {
  988. main::checked_mv("$slink.dpkg-tmp", $slink);
  989. });
  990. } else {
  991. main::warning(_g("not replacing %s with a link."), $slink);
  992. }
  993. } else {
  994. main::warning(_g("skip creation of %s because associated file " .
  995. "%s (of link group %s) doesn't exist."),
  996. $slink, $spath, $self->name())
  997. if $fileset->has_slave($slave);
  998. # Drop unused slave
  999. $self->add_commit_op(sub {
  1000. main::checked_rm($slink);
  1001. main::checked_rm("$altdir/$slave");
  1002. });
  1003. }
  1004. }
  1005. }
  1006. sub remove {
  1007. my ($self) = @_;
  1008. my ($link, $name) = ($self->link(), $self->name());
  1009. main::checked_rm("$link.dpkg-tmp");
  1010. main::checked_rm($link) if -l $link;
  1011. main::checked_rm("$altdir/$name.dpkg-tmp");
  1012. main::checked_rm("$altdir/$name");
  1013. foreach my $slave ($self->slaves()) {
  1014. my $slink = $self->slave_link($slave);
  1015. main::checked_rm("$slink.dpkg-tmp");
  1016. main::checked_rm($slink) if -l $slink;
  1017. main::checked_rm("$altdir/$slave.dpkg-tmp");
  1018. main::checked_rm("$altdir/$slave");
  1019. }
  1020. # Drop admin file
  1021. main::checked_rm("$admdir/$name");
  1022. }
  1023. sub commit {
  1024. my ($self) = @_;
  1025. foreach my $sub (@{$self->{commit_ops}}) {
  1026. &$sub();
  1027. }
  1028. $self->{commit_ops} = [];
  1029. }
  1030. sub is_broken {
  1031. my ($self) = @_;
  1032. my $name = $self->name();
  1033. return 1 if not $self->has_current_link();
  1034. # Check master link
  1035. my $file = readlink($self->link());
  1036. return 1 if not defined($file);
  1037. return 1 if $file ne "$altdir/$name";
  1038. # Stop if we have an unmanaged alternative
  1039. return 0 if not $self->has_choice($self->current());
  1040. # Check slaves
  1041. my $fileset = $self->fileset($self->current());
  1042. foreach my $slave ($self->slaves()) {
  1043. $file = readlink($self->slave_link($slave));
  1044. if ($fileset->has_slave($slave) and -e $fileset->slave($slave)) {
  1045. return 1 if not defined($file);
  1046. return 1 if $file ne "$altdir/$slave";
  1047. $file = readlink("$altdir/$slave");
  1048. return 1 if not defined($file);
  1049. return 1 if $file ne $fileset->slave($slave);
  1050. } else {
  1051. # Slave link must not exist
  1052. return 1 if defined($file);
  1053. $file = readlink("$altdir/$slave");
  1054. return 1 if defined($file);
  1055. }
  1056. }
  1057. return 0;
  1058. }
  1059. # vim: nowrap ts=8 sw=4