update-alternatives.pl 36 KB

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