update-alternatives.pl 34 KB

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