update-alternatives.pl 34 KB

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