update-alternatives.pl 32 KB

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