update-alternatives.pl 36 KB

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