update-alternatives.pl 36 KB

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