update-alternatives.pl 35 KB

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