update-alternatives.pl 32 KB

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