update-alternatives.pl 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  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 ($alternative->status() ne "manual") {
  243. warning(_g("%s has been changed (manually or by a script). " .
  244. "Switching to manual updates only."),
  245. "$altdir/" . $alternative->name());
  246. $alternative->set_status('manual');
  247. }
  248. }
  249. } else {
  250. # Lack of alternative link => automatic mode
  251. verbose(_g("setting up automatic selection of %s."), $alternative->name());
  252. $alternative->set_status('auto');
  253. }
  254. my $new_choice;
  255. if ($action eq 'set') {
  256. $alternative->set_status('manual');
  257. $new_choice = $path;
  258. } elsif ($action eq 'auto') {
  259. $alternative->set_status('auto');
  260. $new_choice = $alternative->best();
  261. } elsif ($action eq 'config') {
  262. if (not scalar($alternative->choices())) {
  263. pr(_g("There is no program which provides %s."), $alternative->name());
  264. pr(_g("Nothing to configure."));
  265. } elsif ($skip_auto && $alternative->status() eq 'auto') {
  266. $alternative->display_user();
  267. } elsif (scalar($alternative->choices()) == 1 and
  268. $alternative->status() eq 'auto' and
  269. $alternative->has_current_link()) {
  270. pr(_g("There is only one alternative in link group %s: %s"),
  271. $alternative->name(), $alternative->current());
  272. pr(_g("Nothing to configure."));
  273. } else {
  274. $new_choice = $alternative->select_choice();
  275. }
  276. } elsif ($action eq 'remove') {
  277. if ($alternative->has_choice($path)) {
  278. $alternative->remove_choice($path);
  279. } else {
  280. verbose(_g("alternative %s for %s not registered, not removing."),
  281. $path, $alternative->name());
  282. }
  283. if ($current_choice eq $path) {
  284. # Current choice is removed
  285. if ($alternative->status() eq "manual") {
  286. # And it was manual, switch to auto
  287. info(_g("removing manually selected alternative - " .
  288. "switching %s to auto mode"), $alternative->name());
  289. $alternative->set_status('auto');
  290. }
  291. $new_choice = $alternative->best();
  292. }
  293. } elsif ($action eq 'remove-all') {
  294. foreach my $choice ($alternative->choices()) {
  295. $alternative->remove_choice($choice);
  296. }
  297. } elsif ($action eq 'install') {
  298. if (defined($alternative->link())) {
  299. # Alternative already exists, check if anything got updated
  300. my ($old, $new) = ($alternative->link(), $inst_alt->link());
  301. $alternative->set_link($new);
  302. if ($old ne $new and -l $old) {
  303. info(_g("renaming %s link from %s to %s."), $inst_alt->name(),
  304. $old, $new);
  305. checked_mv($old, $new);
  306. }
  307. # Check if new slaves have been added, or existing ones renamed
  308. foreach my $slave ($inst_alt->slaves()) {
  309. $new = $inst_alt->slave_link($slave);
  310. if (not $alternative->has_slave($slave)) {
  311. $alternative->add_slave($slave, $new);
  312. next;
  313. }
  314. $old = $alternative->slave_link($slave);
  315. $alternative->add_slave($slave, $new);
  316. my $new_file = ($current_choice eq $fileset->master()) ?
  317. $fileset->slave($slave) :
  318. readlink("$admdir/$slave") || "";
  319. if ($old ne $new and -l $old) {
  320. if (-e $new_file) {
  321. info(_g("renaming %s slave link from %s to %s."),
  322. $slave,$old, $new);
  323. checked_mv($old, $new);
  324. } else {
  325. checked_rm($old);
  326. }
  327. }
  328. }
  329. } else {
  330. # Alternative doesn't exist, create from parameters
  331. $alternative = $inst_alt;
  332. }
  333. $alternative->add_choice($fileset);
  334. if ($alternative->status() eq "auto") {
  335. # Update automatic choice if needed
  336. $new_choice = $alternative->best();
  337. } else {
  338. verbose(_g("automatic updates of %s are disabled, leaving it alone."),
  339. "$altdir/" . $alternative->name());
  340. verbose(_g("to return to automatic updates use ".
  341. "\`update-alternatives --auto %s'."), $alternative->name());
  342. }
  343. }
  344. # No choice left, remove everything
  345. if (not scalar($alternative->choices())) {
  346. log_msg("link group " . $alternative->name() . " fully removed");
  347. $alternative->remove();
  348. exit 0;
  349. }
  350. # New choice wanted
  351. if (defined($new_choice) and ($current_choice ne $new_choice)) {
  352. log_msg("link group " . $alternative->name() .
  353. " updated to point to " . $new_choice);
  354. info(_g("using %s to provide %s (%s) in %s."), $new_choice,
  355. $alternative->link(), $alternative->name(),
  356. ($alternative->status() eq "auto" ? _g("auto mode") : _g("manual mode")));
  357. $alternative->prepare_install($new_choice);
  358. } elsif ($alternative->is_broken()) {
  359. log_msg("auto-repair link group " . $alternative->name());
  360. warning(_g("forcing reinstallation of alternative %s " .
  361. "because link group %s is broken."),
  362. $current_choice, $alternative->name());
  363. $alternative->prepare_install($current_choice) if $current_choice;
  364. }
  365. # Save administrative file if needed
  366. if ($alternative->is_modified()) {
  367. $alternative->save("$admdir/" . $alternative->name() . ".dpkg-tmp");
  368. checked_mv("$admdir/" . $alternative->name() . ".dpkg-tmp",
  369. "$admdir/" . $alternative->name());
  370. }
  371. # Replace all symlinks in one pass
  372. $alternative->commit();
  373. exit 0;
  374. ### FUNCTIONS ####
  375. sub version {
  376. printf _g("Debian %s version %s.\n"), $progname, $version;
  377. printf _g("
  378. Copyright (C) 1995 Ian Jackson.
  379. Copyright (C) 2000-2002 Wichert Akkerman.
  380. Copyright (C) 2009 Raphael Hertzog.");
  381. printf _g("
  382. This is free software; see the GNU General Public Licence version 2 or
  383. later for copying conditions. There is NO warranty.
  384. ");
  385. }
  386. sub usage {
  387. printf _g(
  388. "Usage: %s [<option> ...] <command>
  389. Commands:
  390. --install <link> <name> <path> <priority>
  391. [--slave <link> <name> <path>] ...
  392. add a group of alternatives to the system.
  393. --remove <name> <path> remove <path> from the <name> group alternative.
  394. --remove-all <name> remove <name> group from the alternatives system.
  395. --auto <name> switch the master link <name> to automatic mode.
  396. --display <name> display information about the <name> group.
  397. --query <name> machine parseable version of --display <name>.
  398. --list <name> display all targets of the <name> group.
  399. --config <name> show alternatives for the <name> group and ask the
  400. user to select which one to use.
  401. --set <name> <path> set <path> as alternative for <name>.
  402. --all call --config on all alternatives.
  403. <link> is the symlink pointing to %s/<name>.
  404. (e.g. /usr/bin/pager)
  405. <name> is the master name for this link group.
  406. (e.g. pager)
  407. <path> is the location of one of the alternative target files.
  408. (e.g. /usr/bin/less)
  409. <priority> is an integer; options with higher numbers have higher priority in
  410. automatic mode.
  411. Options:
  412. --altdir <directory> change the alternatives directory.
  413. --admindir <directory> change the administrative directory.
  414. --skip-auto skip prompt for alternatives correctly configured
  415. in automatic mode (relevant for --config only)
  416. --verbose verbose operation, more output.
  417. --quiet quiet operation, minimal output.
  418. --help show this help message.
  419. --version show the version.
  420. "), $progname, $altdir;
  421. }
  422. sub error {
  423. my ($format, @params) = @_;
  424. $! = 2;
  425. die sprintf("%s: %s: %s\n", $progname, _g("error"),
  426. sprintf($format, @params));
  427. }
  428. sub badusage {
  429. my ($format, @params) = @_;
  430. printf STDERR "%s: %s\n\n", $progname, sprintf($format, @params);
  431. usage();
  432. exit(2);
  433. }
  434. sub warning {
  435. my ($format, @params) = @_;
  436. if ($verbosemode >= 0) {
  437. printf STDERR "%s: %s: %s\n", $progname, _g("warning"),
  438. sprintf($format, @params);
  439. }
  440. }
  441. sub msg {
  442. my ($min_level, $format, @params) = @_;
  443. if ($verbosemode >= $min_level) {
  444. printf STDOUT "%s: %s\n", $progname, sprintf($format, @params);
  445. }
  446. }
  447. sub verbose {
  448. msg(1, @_);
  449. }
  450. sub info {
  451. msg(0, @_);
  452. }
  453. sub pr {
  454. my ($format, @params) = @_;
  455. printf ($format . "\n", @params);
  456. }
  457. sub set_action {
  458. my ($value) = @_;
  459. if ($action) {
  460. badusage(_g("two commands specified: --%s and --%s"), $value, $action);
  461. }
  462. $action = $value;
  463. }
  464. {
  465. my $fh_log;
  466. sub log_msg {
  467. my ($msg) = @_;
  468. # XXX: the C rewrite must use the std function to get the
  469. # filename from /etc/dpkg/dpkg.cfg or from command line
  470. if (!defined($fh_log) and -w $log_file) {
  471. open($fh_log, ">>", $log_file) ||
  472. error(_g("Can't append to %s"), $log_file);
  473. }
  474. if (defined($fh_log)) {
  475. $msg = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime()) .
  476. " $progname: $msg\n";
  477. print $fh_log $msg;
  478. }
  479. }
  480. }
  481. sub get_all_alternatives {
  482. opendir(ADMINDIR, $admdir)
  483. or error(_g("can't readdir %s: %s"), $admdir, $!);
  484. my @filenames = grep { !/^\.\.?$/ and !/\.dpkg-tmp$/ } (readdir(ADMINDIR));
  485. close(ADMINDIR);
  486. return sort @filenames;
  487. }
  488. sub config_all {
  489. foreach my $name (get_all_alternatives()) {
  490. system($0, @pass_opts, "--config", $name);
  491. exit $? if $?;
  492. print "\n";
  493. }
  494. }
  495. sub rename_mv {
  496. my ($source, $dest) = @_;
  497. lstat($source);
  498. return 0 if not -e _;
  499. if (not rename($source, $dest)) {
  500. if (system("mv", $source, $dest) != 0) {
  501. return 0;
  502. }
  503. }
  504. return 1;
  505. }
  506. sub checked_symlink {
  507. my ($filename, $linkname) = @_;
  508. symlink($filename, $linkname) ||
  509. error(_g("unable to make %s a symlink to %s: %s"), $linkname, $filename, $!);
  510. }
  511. sub checked_mv {
  512. my ($source, $dest) = @_;
  513. rename_mv($source, $dest) ||
  514. error(_g("unable to install %s as %s: %s"), $source, $dest, $!);
  515. }
  516. sub checked_rm {
  517. my ($f) = @_;
  518. unlink($f) || $! == ENOENT || error(_g("unable to remove %s: %s"), $f, $!);
  519. }
  520. ### OBJECTS ####
  521. package FileSet;
  522. use Dpkg::Gettext;
  523. sub new {
  524. my ($class, $master_file, $prio) = @_;
  525. my $self = {
  526. "master_file" => $master_file,
  527. "priority" => $prio,
  528. "slaves" =>
  529. {
  530. # "slave_name" => "slave_file"
  531. },
  532. };
  533. return bless $self, $class;
  534. }
  535. sub add_slave {
  536. my ($self, $name, $file) = @_;
  537. $self->{slaves}{$name} = $file;
  538. }
  539. sub has_slave {
  540. my ($self, $slave) = @_;
  541. return (exists $self->{"slaves"}{$slave} and $self->{"slaves"}{$slave});
  542. }
  543. sub master {
  544. my ($self, $val) = @_;
  545. return $self->{"master_file"};
  546. }
  547. sub priority {
  548. my ($self) = @_;
  549. return $self->{"priority"};
  550. }
  551. sub slave {
  552. my ($self, $slave) = @_;
  553. return $self->{"slaves"}{$slave};
  554. }
  555. package Alternative;
  556. use Dpkg::Gettext;
  557. use POSIX qw(:errno_h);
  558. sub pr { main::pr(@_) }
  559. sub error { main::error(@_) }
  560. sub new {
  561. my ($class, $name) = @_;
  562. my $self = {};
  563. bless $self, $class;
  564. $self->reset($name);
  565. return $self;
  566. }
  567. sub reset {
  568. my ($self, $name) = @_;
  569. my $new = {
  570. "master_name" => $name,
  571. "master_link" => undef,
  572. "status" => undef,
  573. "slaves" => {
  574. # "slave_name" => "slave_link"
  575. },
  576. "choices" => {
  577. # "master_file" => $fileset
  578. },
  579. "modified" => 0,
  580. "commit_ops" => [],
  581. };
  582. %$self = %$new;
  583. }
  584. sub choices {
  585. my ($self) = @_;
  586. my @choices = sort { $a cmp $b } keys %{$self->{choices}};
  587. return wantarray ? @choices : scalar(@choices);
  588. }
  589. sub slaves {
  590. my ($self) = @_;
  591. my @slaves = sort { $a cmp $b } keys %{$self->{slaves}};
  592. return wantarray ? @slaves : scalar(@slaves);
  593. }
  594. sub name {
  595. my ($self) = @_;
  596. return $self->{master_name};
  597. }
  598. sub link {
  599. my ($self) = @_;
  600. return $self->{master_link};
  601. }
  602. sub status {
  603. my ($self) = @_;
  604. return $self->{status};
  605. }
  606. sub fileset {
  607. my ($self, $id) = @_;
  608. return $self->{choices}{$id} if exists $self->{choices}{$id};
  609. return undef;
  610. }
  611. sub slave_link {
  612. my ($self, $id) = @_;
  613. return $self->{slaves}{$id} if exists $self->{slaves}{$id};
  614. return undef;
  615. }
  616. sub has_slave {
  617. my ($self, $slave) = @_;
  618. return (exists $self->{"slaves"}{$slave} and $self->{"slaves"}{$slave});
  619. }
  620. sub is_modified {
  621. my ($self) = @_;
  622. return $self->{modified};
  623. }
  624. sub has_choice {
  625. my ($self, $id) = @_;
  626. return exists $self->{choices}{$id};
  627. }
  628. sub add_choice {
  629. my ($self, $fileset) = @_;
  630. $self->{choices}{$fileset->master()} = $fileset;
  631. $self->{modified} = 1; # XXX: be smarter in detecting change ?
  632. }
  633. sub add_slave {
  634. my ($self, $slave, $link) = @_;
  635. $self->{slaves}{$slave} = $link;
  636. }
  637. sub set_status {
  638. my ($self, $status) = @_;
  639. if (!defined($self->status()) or $status ne $self->status()) {
  640. $self->{modified} = 1;
  641. }
  642. main::log_msg("status of link group " . $self->name() . " set to $status")
  643. if defined($self->status()) and $status ne $self->status();
  644. $self->{status} = $status;
  645. }
  646. sub set_link {
  647. my ($self, $link) = @_;
  648. if (!defined($self->link()) or $link ne $self->link()) {
  649. $self->{modified} = 1;
  650. }
  651. $self->{master_link} = $link;
  652. }
  653. sub remove_choice {
  654. my ($self, $id) = @_;
  655. if ($self->has_choice($id)) {
  656. delete $self->{choices}{$id};
  657. $self->{modified} = 1;
  658. return 1;
  659. }
  660. return 0;
  661. }
  662. {
  663. # Helper functions for load() and save()
  664. my ($fh, $filename);
  665. sub config_helper {
  666. ($fh, $filename) = @_;
  667. }
  668. sub gl {
  669. undef $!;
  670. my $line = <$fh>;
  671. unless (defined($line)) {
  672. error(_g("while reading %s: %s"), $filename, $!) if $!;
  673. error(_g("unexpected end of file in %s while trying to read %s"),
  674. $filename, $_[0]);
  675. }
  676. chomp($line);
  677. return $line;
  678. }
  679. sub badfmt {
  680. my ($format, @params) = @_;
  681. error(_g("%s corrupt: %s"), $filename, sprintf($format, @params));
  682. }
  683. sub paf {
  684. my $line = shift @_;
  685. if ($line =~ m/\n/) {
  686. error(_g("newlines prohibited in update-alternatives files (%s)"), $line);
  687. }
  688. print $fh "$line\n" || error(_g("while writing %s: %s"), $filename, $!);
  689. }
  690. }
  691. sub load {
  692. my ($self, $file, $must_not_die) = @_;
  693. return 0 unless -s $file;
  694. eval {
  695. open(my $fh, "<", $file) || error(_g("unable to read %s: %s"), $file, $!);
  696. config_helper($fh, $file);
  697. my $status = gl(_g("status"));
  698. badfmt(_g("invalid status")) unless $status =~ /^(?:auto|manual)$/;
  699. my $link = gl("link");
  700. my (%slaves, @slaves);
  701. while ((my $slave_name = gl(_g("slave name"))) ne '') {
  702. my $slave_link = gl(_g("slave link"));
  703. badfmt(_g("duplicate slave %s"), $slave_name)
  704. if exists $slaves{$slave_name};
  705. badfmt(_g("slave link same as main link %s"), $link)
  706. if $slave_link eq $link;
  707. badfmt(_g("duplicate slave link %s"), $slave_link)
  708. if grep { $_ eq $slave_link } values %slaves;
  709. $slaves{$slave_name} = $slave_link;
  710. push @slaves, $slave_name;
  711. }
  712. my @filesets;
  713. my $modified = 0;
  714. while ((my $main_file = gl(_g("master file"))) ne '') {
  715. badfmt(_g("duplicate path %s"), $main_file)
  716. if grep { $_->{master_file} eq $main_file } @filesets;
  717. if (-e $main_file) {
  718. my $priority = gl(_g("priority"));
  719. badfmt(_g("priority of %s: %s"), $main_file, $priority)
  720. unless $priority =~ m/^[-+]?\d+$/;
  721. my $group = FileSet->new($main_file, $priority);
  722. foreach my $slave (@slaves) {
  723. $group->add_slave($slave, gl(_g("slave file")));
  724. }
  725. push @filesets, $group;
  726. } else {
  727. # File not found - remove
  728. main::warning(_g("alternative %s (part of link group %s) " .
  729. "doesn't exist. Removing from list of ".
  730. "alternatives."),
  731. $main_file, $self->name()) unless $must_not_die;
  732. gl(_g("priority"));
  733. foreach my $slave (@slaves) {
  734. gl(_g("slave file"));
  735. }
  736. $modified = 1;
  737. }
  738. }
  739. close($fh);
  740. # We parsed the file without trouble, load data into the object
  741. $self->{master_link} = $link;
  742. $self->{slaves} = \%slaves;
  743. $self->{status} = $status;
  744. $self->{modified} = $modified;
  745. $self->{choices} = {};
  746. foreach my $group (@filesets) {
  747. $self->{choices}{$group->master()} = $group;
  748. }
  749. };
  750. if ($@) {
  751. return 0 if $must_not_die;
  752. die $@;
  753. }
  754. return 1;
  755. }
  756. sub save {
  757. my ($self, $file) = @_;
  758. # Cleanup unused slaves before writing admin file
  759. foreach my $slave ($self->slaves()) {
  760. my $has_slave = 0;
  761. foreach my $choice ($self->choices()) {
  762. my $fileset = $self->fileset($choice);
  763. $has_slave++ if $fileset->has_slave($slave);
  764. }
  765. unless ($has_slave) {
  766. main::verbose(_g("discarding obsolete slave link %s (%s)."),
  767. $slave, $self->slave_link($slave));
  768. delete $self->{"slaves"}{$slave};
  769. }
  770. }
  771. # Write admin file
  772. open(my $fh, ">", $file) || error(_g("unable to write %s: %s"), $file, $!);
  773. config_helper($fh, $file);
  774. paf($self->status());
  775. paf($self->link());
  776. foreach my $slave ($self->slaves()) {
  777. paf($slave);
  778. paf($self->slave_link($slave));
  779. }
  780. paf('');
  781. foreach my $choice ($self->choices()) {
  782. paf($choice);
  783. my $fileset = $self->fileset($choice);
  784. paf($fileset->priority());
  785. foreach my $slave ($self->slaves()) {
  786. if ($fileset->has_slave($slave)) {
  787. paf($fileset->slave($slave));
  788. } else {
  789. paf('');
  790. }
  791. }
  792. }
  793. paf('');
  794. close($fh) || error(_g("unable to close %s: %s"), $file, $!);
  795. }
  796. sub display_query {
  797. my ($self) = @_;
  798. pr("Link: %s", $self->name());
  799. pr("Status: %s", $self->status());
  800. my $best = $self->best();
  801. if (defined($best)) {
  802. pr("Best: %s", $best);
  803. }
  804. if ($self->has_current_link()) {
  805. pr("Value: %s", $self->current());
  806. } else {
  807. pr("Value: none");
  808. }
  809. foreach my $choice ($self->choices()) {
  810. pr("");
  811. pr("Alternative: %s", $choice);
  812. my $fileset = $self->fileset($choice);
  813. pr("Priority: %s", $fileset->priority());
  814. next unless scalar($self->slaves());
  815. pr("Slaves:");
  816. foreach my $slave ($self->slaves()) {
  817. if ($fileset->has_slave($slave)) {
  818. pr(" %s %s", $slave, $fileset->slave($slave));
  819. }
  820. }
  821. }
  822. }
  823. sub display_user {
  824. my ($self) = @_;
  825. pr("%s - %s", $self->name(),
  826. ($self->status() eq "auto") ? _g("auto mode") : _g("manual mode"));
  827. if ($self->has_current_link()) {
  828. pr(_g(" link currently points to %s"), $self->current());
  829. } else {
  830. pr(_g(" link currently absent"));
  831. }
  832. foreach my $choice ($self->choices()) {
  833. my $fileset = $self->fileset($choice);
  834. pr(_g("%s - priority %s"), $choice, $fileset->priority());
  835. foreach my $slave ($self->slaves()) {
  836. if ($fileset->has_slave($slave)) {
  837. pr(_g(" slave %s: %s"), $slave, $fileset->slave($slave));
  838. }
  839. }
  840. }
  841. my $best = $self->best();
  842. if (defined($best) && $best) {
  843. pr(_g("Current \`best' version is %s."), $best);
  844. } else {
  845. pr(_g("No versions available."));
  846. }
  847. }
  848. sub display_list {
  849. my ($self) = @_;
  850. pr($_) foreach ($self->choices());
  851. }
  852. sub select_choice {
  853. my ($self) = @_;
  854. while (1) {
  855. my $current = $self->current() || "";
  856. my $best = $self->best();
  857. printf _g("There are %s choices for the alternative %s (providing %s).") . "\n\n",
  858. scalar($self->choices()), $self->name(), $self->link();
  859. my $length = 15;
  860. foreach ($self->choices()) {
  861. $length = (length($_) > $length) ? length($_) + 1 : $length;
  862. }
  863. printf " %-12.12s %-${length}.${length}s %-10.10s %s\n", _g("Selection"),
  864. _g("Path"), _g("Priority"), _g("Status");
  865. print "-" x 60 . "\n";
  866. printf "%s %-12d %-${length}s % -10d %s\n",
  867. ($self->status() eq "auto" and $current eq $best) ? "*" : " ", 0,
  868. $best, $self->fileset($best)->priority(), _g("auto mode");
  869. my $index = 1;
  870. my %sel = ("0" => $best);
  871. foreach my $choice ($self->choices()) {
  872. $sel{$index} = $choice;
  873. $sel{$choice} = $choice;
  874. printf "%s %-12d %-${length}.${length}s % -10d %s\n",
  875. ($self->status() eq "manual" and $current eq $choice) ? "*" : " ",
  876. $index, $choice, $self->fileset($choice)->priority(),
  877. _g("manual mode");
  878. $index++;
  879. }
  880. print "\n";
  881. printf _g("Press enter to keep the current choice[*], or type selection number: ");
  882. my $selection = <STDIN>;
  883. return undef unless defined($selection);
  884. chomp($selection);
  885. return ($current || $best) if $selection eq "";
  886. if (exists $sel{$selection}) {
  887. $self->set_status(($selection eq "0") ? "auto" : "manual");
  888. return $sel{$selection};
  889. }
  890. }
  891. }
  892. sub best {
  893. my ($self) = @_;
  894. my @choices = sort { $self->fileset($b)->priority() <=>
  895. $self->fileset($a)->priority()
  896. } ($self->choices());
  897. if (scalar(@choices)) {
  898. return $choices[0];
  899. } else {
  900. return undef;
  901. }
  902. }
  903. sub has_current_link {
  904. my ($self) = @_;
  905. return -l "$altdir/$self->{master_name}";
  906. }
  907. sub current {
  908. my ($self) = @_;
  909. return undef unless $self->has_current_link();
  910. my $val = readlink("$altdir/$self->{master_name}");
  911. error(_g("readlink(%s) failed: %s"), "$altdir/$self->{master_name}", $!)
  912. unless defined $val;
  913. return $val;
  914. }
  915. sub add_commit_op {
  916. my ($self, $sub) = @_;
  917. push @{$self->{commit_ops}}, $sub;
  918. }
  919. sub prepare_install {
  920. my ($self, $choice) = @_;
  921. my ($link, $name) = ($self->link(), $self->name());
  922. my $fileset = $self->fileset($choice);
  923. # Create link in /etc/alternatives
  924. main::checked_rm("$altdir/$name.dpkg-tmp");
  925. main::checked_symlink($choice, "$altdir/$name.dpkg-tmp");
  926. $self->add_commit_op(sub {
  927. main::checked_mv("$altdir/$name.dpkg-tmp", "$altdir/$name");
  928. });
  929. $! = 0; lstat($link);
  930. if (-l _ or $! == ENOENT or $force) {
  931. # Create alternative link
  932. main::checked_rm("$link.dpkg-tmp");
  933. main::checked_symlink("$altdir/$name", "$link.dpkg-tmp");
  934. $self->add_commit_op(sub {
  935. main::checked_mv("$link.dpkg-tmp", $link);
  936. });
  937. } else {
  938. main::warning(_g("not replacing %s with a link."), $link);
  939. }
  940. # Take care of slaves links
  941. foreach my $slave ($self->slaves()) {
  942. my ($slink, $spath) = ($self->slave_link($slave), $fileset->slave($slave));
  943. if ($fileset->has_slave($slave) and -e $spath) {
  944. # Create link in /etc/alternatives
  945. main::checked_rm("$altdir/$slave.dpkg-tmp");
  946. main::checked_symlink($spath, "$altdir/$slave.dpkg-tmp");
  947. $self->add_commit_op(sub {
  948. main::checked_mv("$altdir/$slave.dpkg-tmp", "$altdir/$slave");
  949. });
  950. $! = 0; lstat($slink);
  951. if (-l _ or $! == ENOENT or $force) {
  952. # Create alternative link
  953. main::checked_rm("$slink.dpkg-tmp");
  954. main::checked_symlink("$altdir/$slave", "$slink.dpkg-tmp");
  955. $self->add_commit_op(sub {
  956. main::checked_mv("$slink.dpkg-tmp", $slink);
  957. });
  958. } else {
  959. main::warning(_g("not replacing %s with a link."), $link);
  960. }
  961. } else {
  962. main::warning(_g("skip creation of %s because associated file " .
  963. "%s (of link group %s) doesn't exist."),
  964. $slink, $spath, $self->name())
  965. if $fileset->has_slave($slave);
  966. # Drop unused slave
  967. $self->add_commit_op(sub {
  968. main::checked_rm($slink);
  969. main::checked_rm("$altdir/$slave");
  970. });
  971. }
  972. }
  973. }
  974. sub remove {
  975. my ($self) = @_;
  976. my ($link, $name) = ($self->link(), $self->name());
  977. main::checked_rm("$link.dpkg-tmp");
  978. main::checked_rm($link);
  979. main::checked_rm("$altdir/$name.dpkg-tmp");
  980. main::checked_rm("$altdir/$name");
  981. foreach my $slave ($self->slaves()) {
  982. my $slink = $self->slave_link($slave);
  983. main::checked_rm("$slink.dpkg-tmp");
  984. main::checked_rm($slink);
  985. main::checked_rm("$altdir/$slave.dpkg-tmp");
  986. main::checked_rm("$altdir/$slave");
  987. }
  988. # Drop admin file
  989. main::checked_rm("$admdir/$name");
  990. }
  991. sub commit {
  992. my ($self) = @_;
  993. foreach my $sub (@{$self->{commit_ops}}) {
  994. &$sub();
  995. }
  996. $self->{commit_ops} = [];
  997. }
  998. sub is_broken {
  999. my ($self) = @_;
  1000. my $name = $self->name();
  1001. return 1 if not $self->has_current_link();
  1002. # Check master link
  1003. my $file = readlink($self->link());
  1004. return 1 if not defined($file);
  1005. return 1 if $file ne "$altdir/$name";
  1006. # Stop if we have an unmanaged alternative
  1007. return 0 if not $self->has_choice($self->current());
  1008. # Check slaves
  1009. my $fileset = $self->fileset($self->current());
  1010. foreach my $slave ($self->slaves()) {
  1011. $file = readlink($self->slave_link($slave));
  1012. if ($fileset->has_slave($slave) and -e $fileset->slave($slave)) {
  1013. return 1 if not defined($file);
  1014. return 1 if $file ne "$altdir/$slave";
  1015. $file = readlink("$altdir/$slave");
  1016. return 1 if not defined($file);
  1017. return 1 if $file ne $fileset->slave($slave);
  1018. } else {
  1019. # Slave link must not exist
  1020. return 1 if defined($file);
  1021. $file = readlink("$altdir/$slave");
  1022. return 1 if defined($file);
  1023. }
  1024. }
  1025. return 0;
  1026. }
  1027. # vim: nowrap ts=8 sw=4