update-alternatives.pl 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. if ($current_choice and not $alternative->has_choice($current_choice)) {
  394. $new_choice = $alternative->best();
  395. warning(_g("current alternative %s is unknown, switching to %s " .
  396. "for link group %s."), $current_choice, $new_choice,
  397. $alternative->name());
  398. $current_choice = $new_choice;
  399. $alternative->set_status('auto');
  400. }
  401. $alternative->prepare_install($current_choice) if $current_choice;
  402. }
  403. # Save administrative file if needed
  404. if ($alternative->is_modified()) {
  405. $alternative->save("$admdir/" . $alternative->name() . ".dpkg-tmp");
  406. checked_mv("$admdir/" . $alternative->name() . ".dpkg-tmp",
  407. "$admdir/" . $alternative->name());
  408. }
  409. # Replace all symlinks in one pass
  410. $alternative->commit();
  411. exit 0;
  412. ### FUNCTIONS ####
  413. sub version {
  414. printf _g("Debian %s version %s.\n"), $progname, $version;
  415. printf _g("
  416. Copyright (C) 1995 Ian Jackson.
  417. Copyright (C) 2000-2002 Wichert Akkerman.
  418. Copyright (C) 2009 Raphael Hertzog.");
  419. printf _g("
  420. This is free software; see the GNU General Public Licence version 2 or
  421. later for copying conditions. There is NO warranty.
  422. ");
  423. }
  424. sub usage {
  425. printf _g(
  426. "Usage: %s [<option> ...] <command>
  427. Commands:
  428. --install <link> <name> <path> <priority>
  429. [--slave <link> <name> <path>] ...
  430. add a group of alternatives to the system.
  431. --remove <name> <path> remove <path> from the <name> group alternative.
  432. --remove-all <name> remove <name> group from the alternatives system.
  433. --auto <name> switch the master link <name> to automatic mode.
  434. --display <name> display information about the <name> group.
  435. --query <name> machine parseable version of --display <name>.
  436. --list <name> display all targets of the <name> group.
  437. --config <name> show alternatives for the <name> group and ask the
  438. user to select which one to use.
  439. --set <name> <path> set <path> as alternative for <name>.
  440. --all call --config on all alternatives.
  441. <link> is the symlink pointing to %s/<name>.
  442. (e.g. /usr/bin/pager)
  443. <name> is the master name for this link group.
  444. (e.g. pager)
  445. <path> is the location of one of the alternative target files.
  446. (e.g. /usr/bin/less)
  447. <priority> is an integer; options with higher numbers have higher priority in
  448. automatic mode.
  449. Options:
  450. --altdir <directory> change the alternatives directory.
  451. --admindir <directory> change the administrative directory.
  452. --skip-auto skip prompt for alternatives correctly configured
  453. in automatic mode (relevant for --config only)
  454. --verbose verbose operation, more output.
  455. --quiet quiet operation, minimal output.
  456. --help show this help message.
  457. --version show the version.
  458. "), $progname, $altdir;
  459. }
  460. sub error {
  461. my ($format, @params) = @_;
  462. $! = 2;
  463. die sprintf("%s: %s: %s\n", $progname, _g("error"),
  464. sprintf($format, @params));
  465. }
  466. sub badusage {
  467. my ($format, @params) = @_;
  468. printf STDERR "%s: %s\n\n", $progname, sprintf($format, @params);
  469. usage();
  470. exit(2);
  471. }
  472. sub warning {
  473. my ($format, @params) = @_;
  474. if ($verbosemode >= 0) {
  475. printf STDERR "%s: %s: %s\n", $progname, _g("warning"),
  476. sprintf($format, @params);
  477. }
  478. }
  479. sub msg {
  480. my ($min_level, $format, @params) = @_;
  481. if ($verbosemode >= $min_level) {
  482. printf STDOUT "%s: %s\n", $progname, sprintf($format, @params);
  483. }
  484. }
  485. sub verbose {
  486. msg(1, @_);
  487. }
  488. sub info {
  489. msg(0, @_);
  490. }
  491. sub pr {
  492. my ($format, @params) = @_;
  493. printf ($format . "\n", @params);
  494. }
  495. sub set_action {
  496. my ($value) = @_;
  497. if ($action) {
  498. badusage(_g("two commands specified: --%s and --%s"), $value, $action);
  499. }
  500. $action = $value;
  501. }
  502. {
  503. my $fh_log;
  504. sub log_msg {
  505. my ($msg) = @_;
  506. # XXX: the C rewrite must use the std function to get the
  507. # filename from /etc/dpkg/dpkg.cfg or from command line
  508. if (!defined($fh_log) and -w $log_file) {
  509. open($fh_log, ">>", $log_file) ||
  510. error(_g("Can't append to %s"), $log_file);
  511. }
  512. if (defined($fh_log)) {
  513. $msg = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime()) .
  514. " $progname: $msg\n";
  515. print $fh_log $msg;
  516. }
  517. }
  518. }
  519. sub get_all_alternatives {
  520. opendir(ADMINDIR, $admdir)
  521. or error(_g("can't readdir %s: %s"), $admdir, $!);
  522. my @filenames = grep { !/^\.\.?$/ and !/\.dpkg-tmp$/ } (readdir(ADMINDIR));
  523. close(ADMINDIR);
  524. return sort @filenames;
  525. }
  526. sub config_all {
  527. foreach my $name (get_all_alternatives()) {
  528. system($0, @pass_opts, "--config", $name);
  529. exit $? if $?;
  530. print "\n";
  531. }
  532. }
  533. sub rename_mv {
  534. my ($source, $dest) = @_;
  535. lstat($source);
  536. return 0 if not -e _;
  537. if (not rename($source, $dest)) {
  538. if (system("mv", $source, $dest) != 0) {
  539. return 0;
  540. }
  541. }
  542. return 1;
  543. }
  544. sub checked_symlink {
  545. my ($filename, $linkname) = @_;
  546. symlink($filename, $linkname) ||
  547. error(_g("unable to make %s a symlink to %s: %s"), $linkname, $filename, $!);
  548. }
  549. sub checked_mv {
  550. my ($source, $dest) = @_;
  551. rename_mv($source, $dest) ||
  552. error(_g("unable to install %s as %s: %s"), $source, $dest, $!);
  553. }
  554. sub checked_rm {
  555. my ($f) = @_;
  556. unlink($f) || $! == ENOENT || error(_g("unable to remove %s: %s"), $f, $!);
  557. }
  558. ### OBJECTS ####
  559. package FileSet;
  560. use Dpkg::Gettext;
  561. sub new {
  562. my ($class, $master_file, $prio) = @_;
  563. my $self = {
  564. "master_file" => $master_file,
  565. "priority" => $prio,
  566. "slaves" =>
  567. {
  568. # "slave_name" => "slave_file"
  569. },
  570. };
  571. return bless $self, $class;
  572. }
  573. sub add_slave {
  574. my ($self, $name, $file) = @_;
  575. $self->{slaves}{$name} = $file;
  576. }
  577. sub has_slave {
  578. my ($self, $slave) = @_;
  579. return (exists $self->{"slaves"}{$slave} and $self->{"slaves"}{$slave});
  580. }
  581. sub master {
  582. my ($self, $val) = @_;
  583. return $self->{"master_file"};
  584. }
  585. sub priority {
  586. my ($self) = @_;
  587. return $self->{"priority"};
  588. }
  589. sub slave {
  590. my ($self, $slave) = @_;
  591. return $self->{"slaves"}{$slave};
  592. }
  593. package Alternative;
  594. use Dpkg::Gettext;
  595. use POSIX qw(:errno_h);
  596. sub pr { main::pr(@_) }
  597. sub error { main::error(@_) }
  598. sub new {
  599. my ($class, $name) = @_;
  600. my $self = {};
  601. bless $self, $class;
  602. $self->reset($name);
  603. return $self;
  604. }
  605. sub reset {
  606. my ($self, $name) = @_;
  607. my $new = {
  608. "master_name" => $name,
  609. "master_link" => undef,
  610. "status" => undef,
  611. "slaves" => {
  612. # "slave_name" => "slave_link"
  613. },
  614. "choices" => {
  615. # "master_file" => $fileset
  616. },
  617. "modified" => 0,
  618. "commit_ops" => [],
  619. };
  620. %$self = %$new;
  621. }
  622. sub choices {
  623. my ($self) = @_;
  624. my @choices = sort { $a cmp $b } keys %{$self->{choices}};
  625. return wantarray ? @choices : scalar(@choices);
  626. }
  627. sub slaves {
  628. my ($self) = @_;
  629. my @slaves = sort { $a cmp $b } keys %{$self->{slaves}};
  630. return wantarray ? @slaves : scalar(@slaves);
  631. }
  632. sub name {
  633. my ($self) = @_;
  634. return $self->{master_name};
  635. }
  636. sub link {
  637. my ($self) = @_;
  638. return $self->{master_link};
  639. }
  640. sub status {
  641. my ($self) = @_;
  642. return $self->{status};
  643. }
  644. sub fileset {
  645. my ($self, $id) = @_;
  646. return $self->{choices}{$id} if exists $self->{choices}{$id};
  647. return undef;
  648. }
  649. sub slave_link {
  650. my ($self, $id) = @_;
  651. return $self->{slaves}{$id} if exists $self->{slaves}{$id};
  652. return undef;
  653. }
  654. sub has_slave {
  655. my ($self, $slave) = @_;
  656. return (exists $self->{"slaves"}{$slave} and $self->{"slaves"}{$slave});
  657. }
  658. sub is_modified {
  659. my ($self) = @_;
  660. return $self->{modified};
  661. }
  662. sub has_choice {
  663. my ($self, $id) = @_;
  664. return exists $self->{choices}{$id};
  665. }
  666. sub add_choice {
  667. my ($self, $fileset) = @_;
  668. $self->{choices}{$fileset->master()} = $fileset;
  669. $self->{modified} = 1; # XXX: be smarter in detecting change ?
  670. }
  671. sub add_slave {
  672. my ($self, $slave, $link) = @_;
  673. $self->{slaves}{$slave} = $link;
  674. }
  675. sub set_status {
  676. my ($self, $status) = @_;
  677. if (!defined($self->status()) or $status ne $self->status()) {
  678. $self->{modified} = 1;
  679. }
  680. main::log_msg("status of link group " . $self->name() . " set to $status")
  681. if defined($self->status()) and $status ne $self->status();
  682. $self->{status} = $status;
  683. }
  684. sub set_link {
  685. my ($self, $link) = @_;
  686. if (!defined($self->link()) or $link ne $self->link()) {
  687. $self->{modified} = 1;
  688. }
  689. $self->{master_link} = $link;
  690. }
  691. sub remove_choice {
  692. my ($self, $id) = @_;
  693. if ($self->has_choice($id)) {
  694. delete $self->{choices}{$id};
  695. $self->{modified} = 1;
  696. return 1;
  697. }
  698. return 0;
  699. }
  700. {
  701. # Helper functions for load() and save()
  702. my ($fh, $filename);
  703. sub config_helper {
  704. ($fh, $filename) = @_;
  705. }
  706. sub gl {
  707. undef $!;
  708. my $line = <$fh>;
  709. unless (defined($line)) {
  710. error(_g("while reading %s: %s"), $filename, $!) if $!;
  711. error(_g("unexpected end of file in %s while trying to read %s"),
  712. $filename, $_[0]);
  713. }
  714. chomp($line);
  715. return $line;
  716. }
  717. sub badfmt {
  718. my ($format, @params) = @_;
  719. error(_g("%s corrupt: %s"), $filename, sprintf($format, @params));
  720. }
  721. sub paf {
  722. my $line = shift @_;
  723. if ($line =~ m/\n/) {
  724. error(_g("newlines prohibited in update-alternatives files (%s)"), $line);
  725. }
  726. print $fh "$line\n" || error(_g("while writing %s: %s"), $filename, $!);
  727. }
  728. }
  729. sub load {
  730. my ($self, $file, $must_not_die) = @_;
  731. return 0 unless -s $file;
  732. eval {
  733. open(my $fh, "<", $file) || error(_g("unable to read %s: %s"), $file, $!);
  734. config_helper($fh, $file);
  735. my $status = gl(_g("status"));
  736. badfmt(_g("invalid status")) unless $status =~ /^(?:auto|manual)$/;
  737. my $link = gl("link");
  738. my (%slaves, @slaves);
  739. while ((my $slave_name = gl(_g("slave name"))) ne '') {
  740. my $slave_link = gl(_g("slave link"));
  741. badfmt(_g("duplicate slave %s"), $slave_name)
  742. if exists $slaves{$slave_name};
  743. badfmt(_g("slave link same as main link %s"), $link)
  744. if $slave_link eq $link;
  745. badfmt(_g("duplicate slave link %s"), $slave_link)
  746. if grep { $_ eq $slave_link } values %slaves;
  747. $slaves{$slave_name} = $slave_link;
  748. push @slaves, $slave_name;
  749. }
  750. my @filesets;
  751. my $modified = 0;
  752. while ((my $main_file = gl(_g("master file"))) ne '') {
  753. badfmt(_g("duplicate path %s"), $main_file)
  754. if grep { $_->{master_file} eq $main_file } @filesets;
  755. if (-e $main_file) {
  756. my $priority = gl(_g("priority"));
  757. badfmt(_g("priority of %s: %s"), $main_file, $priority)
  758. unless $priority =~ m/^[-+]?\d+$/;
  759. my $group = FileSet->new($main_file, $priority);
  760. foreach my $slave (@slaves) {
  761. $group->add_slave($slave, gl(_g("slave file")));
  762. }
  763. push @filesets, $group;
  764. } else {
  765. # File not found - remove
  766. main::warning(_g("alternative %s (part of link group %s) " .
  767. "doesn't exist. Removing from list of ".
  768. "alternatives."),
  769. $main_file, $self->name()) unless $must_not_die;
  770. gl(_g("priority"));
  771. foreach my $slave (@slaves) {
  772. gl(_g("slave file"));
  773. }
  774. $modified = 1;
  775. }
  776. }
  777. close($fh);
  778. # We parsed the file without trouble, load data into the object
  779. $self->{master_link} = $link;
  780. $self->{slaves} = \%slaves;
  781. $self->{status} = $status;
  782. $self->{modified} = $modified;
  783. $self->{choices} = {};
  784. foreach my $group (@filesets) {
  785. $self->{choices}{$group->master()} = $group;
  786. }
  787. };
  788. if ($@) {
  789. return 0 if $must_not_die;
  790. die $@;
  791. }
  792. return 1;
  793. }
  794. sub save {
  795. my ($self, $file) = @_;
  796. # Cleanup unused slaves before writing admin file
  797. foreach my $slave ($self->slaves()) {
  798. my $has_slave = 0;
  799. foreach my $choice ($self->choices()) {
  800. my $fileset = $self->fileset($choice);
  801. $has_slave++ if $fileset->has_slave($slave);
  802. }
  803. unless ($has_slave) {
  804. main::verbose(_g("discarding obsolete slave link %s (%s)."),
  805. $slave, $self->slave_link($slave));
  806. delete $self->{"slaves"}{$slave};
  807. }
  808. }
  809. # Write admin file
  810. open(my $fh, ">", $file) || error(_g("unable to write %s: %s"), $file, $!);
  811. config_helper($fh, $file);
  812. paf($self->status());
  813. paf($self->link());
  814. foreach my $slave ($self->slaves()) {
  815. paf($slave);
  816. paf($self->slave_link($slave));
  817. }
  818. paf('');
  819. foreach my $choice ($self->choices()) {
  820. paf($choice);
  821. my $fileset = $self->fileset($choice);
  822. paf($fileset->priority());
  823. foreach my $slave ($self->slaves()) {
  824. if ($fileset->has_slave($slave)) {
  825. paf($fileset->slave($slave));
  826. } else {
  827. paf('');
  828. }
  829. }
  830. }
  831. paf('');
  832. close($fh) || error(_g("unable to close %s: %s"), $file, $!);
  833. }
  834. sub display_query {
  835. my ($self) = @_;
  836. pr("Link: %s", $self->name());
  837. pr("Status: %s", $self->status());
  838. my $best = $self->best();
  839. if (defined($best)) {
  840. pr("Best: %s", $best);
  841. }
  842. if ($self->has_current_link()) {
  843. pr("Value: %s", $self->current());
  844. } else {
  845. pr("Value: none");
  846. }
  847. foreach my $choice ($self->choices()) {
  848. pr("");
  849. pr("Alternative: %s", $choice);
  850. my $fileset = $self->fileset($choice);
  851. pr("Priority: %s", $fileset->priority());
  852. next unless scalar($self->slaves());
  853. pr("Slaves:");
  854. foreach my $slave ($self->slaves()) {
  855. if ($fileset->has_slave($slave)) {
  856. pr(" %s %s", $slave, $fileset->slave($slave));
  857. }
  858. }
  859. }
  860. }
  861. sub display_user {
  862. my ($self) = @_;
  863. pr("%s - %s", $self->name(),
  864. ($self->status() eq "auto") ? _g("auto mode") : _g("manual mode"));
  865. if ($self->has_current_link()) {
  866. pr(_g(" link currently points to %s"), $self->current());
  867. } else {
  868. pr(_g(" link currently absent"));
  869. }
  870. foreach my $choice ($self->choices()) {
  871. my $fileset = $self->fileset($choice);
  872. pr(_g("%s - priority %s"), $choice, $fileset->priority());
  873. foreach my $slave ($self->slaves()) {
  874. if ($fileset->has_slave($slave)) {
  875. pr(_g(" slave %s: %s"), $slave, $fileset->slave($slave));
  876. }
  877. }
  878. }
  879. my $best = $self->best();
  880. if (defined($best) && $best) {
  881. pr(_g("Current 'best' version is '%s'."), $best);
  882. } else {
  883. pr(_g("No versions available."));
  884. }
  885. }
  886. sub display_list {
  887. my ($self) = @_;
  888. pr($_) foreach ($self->choices());
  889. }
  890. sub select_choice {
  891. my ($self) = @_;
  892. while (1) {
  893. my $current = $self->current() || "";
  894. my $best = $self->best();
  895. printf _g("There are %s choices for the alternative %s (providing %s).") . "\n\n",
  896. scalar($self->choices()), $self->name(), $self->link();
  897. my $length = 15;
  898. foreach ($self->choices()) {
  899. $length = (length($_) > $length) ? length($_) + 1 : $length;
  900. }
  901. printf " %-12.12s %-${length}.${length}s %-10.10s %s\n", _g("Selection"),
  902. _g("Path"), _g("Priority"), _g("Status");
  903. print "-" x 60 . "\n";
  904. printf "%s %-12d %-${length}s % -10d %s\n",
  905. ($self->status() eq "auto" and $current eq $best) ? "*" : " ", 0,
  906. $best, $self->fileset($best)->priority(), _g("auto mode");
  907. my $index = 1;
  908. my %sel = ("0" => $best);
  909. foreach my $choice ($self->choices()) {
  910. $sel{$index} = $choice;
  911. $sel{$choice} = $choice;
  912. printf "%s %-12d %-${length}.${length}s % -10d %s\n",
  913. ($self->status() eq "manual" and $current eq $choice) ? "*" : " ",
  914. $index, $choice, $self->fileset($choice)->priority(),
  915. _g("manual mode");
  916. $index++;
  917. }
  918. print "\n";
  919. printf _g("Press enter to keep the current choice[*], or type selection number: ");
  920. my $selection = <STDIN>;
  921. return undef unless defined($selection);
  922. chomp($selection);
  923. return ($current || $best) if $selection eq "";
  924. if (exists $sel{$selection}) {
  925. $self->set_status(($selection eq "0") ? "auto" : "manual");
  926. return $sel{$selection};
  927. }
  928. }
  929. }
  930. sub best {
  931. my ($self) = @_;
  932. my @choices = sort { $self->fileset($b)->priority() <=>
  933. $self->fileset($a)->priority()
  934. } ($self->choices());
  935. if (scalar(@choices)) {
  936. return $choices[0];
  937. } else {
  938. return undef;
  939. }
  940. }
  941. sub has_current_link {
  942. my ($self) = @_;
  943. return -l "$altdir/$self->{master_name}";
  944. }
  945. sub current {
  946. my ($self) = @_;
  947. return undef unless $self->has_current_link();
  948. my $val = readlink("$altdir/$self->{master_name}");
  949. error(_g("readlink(%s) failed: %s"), "$altdir/$self->{master_name}", $!)
  950. unless defined $val;
  951. return $val;
  952. }
  953. sub add_commit_op {
  954. my ($self, $sub) = @_;
  955. push @{$self->{commit_ops}}, $sub;
  956. }
  957. sub prepare_install {
  958. my ($self, $choice) = @_;
  959. my ($link, $name) = ($self->link(), $self->name());
  960. my $fileset = $self->fileset($choice);
  961. main::error("can't install unknown choice %s", $choice)
  962. if not defined($choice);
  963. # Create link in /etc/alternatives
  964. main::checked_rm("$altdir/$name.dpkg-tmp");
  965. main::checked_symlink($choice, "$altdir/$name.dpkg-tmp");
  966. $self->add_commit_op(sub {
  967. main::checked_mv("$altdir/$name.dpkg-tmp", "$altdir/$name");
  968. });
  969. $! = 0; lstat($link);
  970. if (-l _ or $! == ENOENT or $force) {
  971. # Create alternative link
  972. main::checked_rm("$link.dpkg-tmp");
  973. main::checked_symlink("$altdir/$name", "$link.dpkg-tmp");
  974. $self->add_commit_op(sub {
  975. main::checked_mv("$link.dpkg-tmp", $link);
  976. });
  977. } else {
  978. main::warning(_g("not replacing %s with a link."), $link);
  979. }
  980. # Take care of slaves links
  981. foreach my $slave ($self->slaves()) {
  982. my ($slink, $spath) = ($self->slave_link($slave), $fileset->slave($slave));
  983. if ($fileset->has_slave($slave) and -e $spath) {
  984. # Create link in /etc/alternatives
  985. main::checked_rm("$altdir/$slave.dpkg-tmp");
  986. main::checked_symlink($spath, "$altdir/$slave.dpkg-tmp");
  987. $self->add_commit_op(sub {
  988. main::checked_mv("$altdir/$slave.dpkg-tmp", "$altdir/$slave");
  989. });
  990. $! = 0; lstat($slink);
  991. if (-l _ or $! == ENOENT or $force) {
  992. # Create alternative link
  993. main::checked_rm("$slink.dpkg-tmp");
  994. main::checked_symlink("$altdir/$slave", "$slink.dpkg-tmp");
  995. $self->add_commit_op(sub {
  996. main::checked_mv("$slink.dpkg-tmp", $slink);
  997. });
  998. } else {
  999. main::warning(_g("not replacing %s with a link."), $slink);
  1000. }
  1001. } else {
  1002. main::warning(_g("skip creation of %s because associated file " .
  1003. "%s (of link group %s) doesn't exist."),
  1004. $slink, $spath, $self->name())
  1005. if $fileset->has_slave($slave);
  1006. # Drop unused slave
  1007. $self->add_commit_op(sub {
  1008. main::checked_rm($slink);
  1009. main::checked_rm("$altdir/$slave");
  1010. });
  1011. }
  1012. }
  1013. }
  1014. sub remove {
  1015. my ($self) = @_;
  1016. my ($link, $name) = ($self->link(), $self->name());
  1017. main::checked_rm("$link.dpkg-tmp");
  1018. main::checked_rm($link) if -l $link;
  1019. main::checked_rm("$altdir/$name.dpkg-tmp");
  1020. main::checked_rm("$altdir/$name");
  1021. foreach my $slave ($self->slaves()) {
  1022. my $slink = $self->slave_link($slave);
  1023. main::checked_rm("$slink.dpkg-tmp");
  1024. main::checked_rm($slink) if -l $slink;
  1025. main::checked_rm("$altdir/$slave.dpkg-tmp");
  1026. main::checked_rm("$altdir/$slave");
  1027. }
  1028. # Drop admin file
  1029. main::checked_rm("$admdir/$name");
  1030. }
  1031. sub commit {
  1032. my ($self) = @_;
  1033. foreach my $sub (@{$self->{commit_ops}}) {
  1034. &$sub();
  1035. }
  1036. $self->{commit_ops} = [];
  1037. }
  1038. sub is_broken {
  1039. my ($self) = @_;
  1040. my $name = $self->name();
  1041. return 1 if not $self->has_current_link();
  1042. # Check master link
  1043. my $file = readlink($self->link());
  1044. return 1 if not defined($file);
  1045. return 1 if $file ne "$altdir/$name";
  1046. # Stop if we have an unmanaged alternative
  1047. return 0 if not $self->has_choice($self->current());
  1048. # Check slaves
  1049. my $fileset = $self->fileset($self->current());
  1050. foreach my $slave ($self->slaves()) {
  1051. $file = readlink($self->slave_link($slave));
  1052. if ($fileset->has_slave($slave) and -e $fileset->slave($slave)) {
  1053. return 1 if not defined($file);
  1054. return 1 if $file ne "$altdir/$slave";
  1055. $file = readlink("$altdir/$slave");
  1056. return 1 if not defined($file);
  1057. return 1 if $file ne $fileset->slave($slave);
  1058. } else {
  1059. # Slave link must not exist
  1060. return 1 if defined($file);
  1061. $file = readlink("$altdir/$slave");
  1062. return 1 if defined($file);
  1063. }
  1064. }
  1065. return 0;
  1066. }
  1067. # vim: nowrap ts=8 sw=4