package urpm::media; # $Id$ use urpm 'file_from_local_url'; use urpm::msg; use urpm::util; use urpm::removable; use urpm::lock; use MDV::Distribconf; our @PER_MEDIA_OPT = qw( downloader hdlist ignore key-ids list media_info_dir name noreconfigure priority-upgrade removable static synthesis update url verify-rpm virtual with_hdlist with_synthesis ); sub only_media_opts { my ($m) = @_; my %m = map { $_ => $m->{$_} } grep { defined $m->{$_} } @PER_MEDIA_OPT; \%m; } sub read_private_netrc { my ($urpm) = @_; my @words = split(/\s+/, scalar cat_($urpm->{private_netrc})); my @l; my $e; while (@words) { my $keyword = shift @words; if ($keyword eq 'machine') { push @l, $e = { machine => shift(@words) }; } elsif ($keyword eq 'default') { push @l, $e = { default => '' }; } elsif ($keyword eq 'login' || $keyword eq 'password' || $keyword eq 'account') { $e->{$keyword} = shift(@words); } else { $urpm->{error}("unknown netrc command $keyword"); } } @l; } sub read_config_add_passwords { my ($urpm, $config) = @_; my @netrc = read_private_netrc($urpm) or return; foreach (@{$config->{media}}) { my $u = urpm::download::parse_url_with_login($_->{url}) or next; if (my ($e) = grep { ($_->{default} || $_->{machine} eq $u->{machine}) && $_->{login} eq $u->{login} } @netrc) { $_->{url} = sprintf('%s://%s:%s@%s%s', $u->{proto}, $u->{login}, $e->{password}, $u->{machine}, $u->{dir}); } else { $urpm->{log}(sprintf('no password found for %s@%s', $u->{login}, $u->{machine})); } } } sub remove_passwords_and_write_private_netrc { my ($urpm, $config) = @_; my @l; foreach (@{$config->{media}}) { my $u = urpm::download::parse_url_with_login($_->{url}) or next; #- check whether a password is visible $u->{password} or next; push @l, $u; $_->{url} = sprintf('%s://%s@%s%s', $u->{proto}, $u->{login}, $u->{machine}, $u->{dir}); } { my $fh = urpm::sys::open_safe($urpm, '>', $urpm->{private_netrc}) or return; foreach my $u (@l) { printf $fh "machine %s login %s password %s\n", $u->{machine}, $u->{login}, $u->{password}; } } chmod 0600, $urpm->{private_netrc}; } #- handle deprecated way of saving passwords sub recover_url_from_list { my ($urpm, $medium) = @_; my $list = delete $medium->{list} or return; my $statedir_list = "$urpm->{statedir}/$list"; #- /./ is end of url marker in list file (typically generated by a #- find . -name "*.rpm" > list #- for exportable list file. if (my @probe = map { m!^(.*)/\./! || m!^(.*)/[^/]*$! } cat_($statedir_list)) { $urpm->{log}("recovering url from $statedir_list"); ($medium->{url}) = sort { length($a) <=> length($b) } @probe; $urpm->{modified} = 1; #- ensure urpmi.cfg is handled using only partially hidden url + netrc, since file list won't be generated anymore unlink $statedir_list; } } #- Loads /etc/urpmi/urpmi.cfg and performs basic checks. #- Does not handle old format: [with ] #- options : #- - nocheck_access : don't check presence of synthesis and other files sub read_config { my ($urpm, $b_nocheck_access) = @_; return if $urpm->{media}; #- media already loaded $urpm->{media} = []; my $config = urpm::cfg::load_config($urpm->{config}) or $urpm->{fatal}(6, $urpm::cfg::err); #- per-media options read_config_add_passwords($urpm, $config); foreach my $m (@{$config->{media}}) { my $medium = only_media_opts($m); if (!$medium->{url}) { #- recover the url the old deprecated way... #- only useful for migration, new urpmi.cfg will use netrc recover_url_from_list($urpm, $medium); $medium->{url} or $urpm->{error}("unable to find url in list file $medium->{name}, medium ignored"); } add_existing_medium($urpm, $medium, $b_nocheck_access); } eval { require urpm::ldap; urpm::ldap::load_ldap_media($urpm) }; } #- if invalid, set {ignore} sub check_existing_medium { my ($urpm, $medium, $b_nocheck_access) = @_; my $err; if (!$medium->{url}) { $err = $medium->{virtual} ? N("virtual medium \"%s\" should have a clear url, medium ignored", $medium->{name}) : N("unable to access list file of \"%s\", medium ignored", $medium->{name}); } elsif (!$b_nocheck_access && !$medium->{ignore} && !-r any_synthesis($urpm, $medium)) { $err = N("unable to access synthesis file of \"%s\", medium ignored", $medium->{name}); } if ($err) { $medium->{ignore} = 1; $urpm->{error}($err); } } sub _migrate__with_synthesis { my ($medium, $with_synthesis) = @_; #- try to migrate to media_info_dir my $b = basename($with_synthesis); if ($b eq 'synthesis.hdlist.cz' || $b eq 'hdlist.cz') { $medium->{media_info_dir} = dirname($with_synthesis); } else { $with_synthesis =~ s/(synthesis\.)?(hdlist.*\.cz)$/synthesis.$2/; $medium->{with_synthesis} = $with_synthesis; } } #- probe medium to be used, take old medium into account too. sub add_existing_medium { my ($urpm, $medium, $b_nocheck_access) = @_; if (name2medium($urpm, $medium->{name})) { $urpm->{error}(N("trying to override existing medium \"%s\", skipping", $medium->{name})); return; } if ($medium->{with_hdlist}) { _migrate__with_synthesis($medium, delete $medium->{with_hdlist}); $urpm->{modified} = 1; } check_existing_medium($urpm, $medium, $b_nocheck_access); #- probe removable device. probe_removable_device($urpm, $medium); #- clear URLs for trailing /es. $medium->{url} and $medium->{url} =~ s|(.*?)/*$|$1|; push @{$urpm->{media}}, $medium; } sub file_from_file_url { my ($url) = @_; $url =~ m!^(?:file:/)?(/.*)! && $1; } sub _url_with_synthesis_basename { my ($medium) = @_; $medium->{with_synthesis} ? basename($medium->{with_synthesis}) : 'synthesis.hdlist.cz'; } sub _synthesis_dir { my ($medium) = @_; my $base = file_from_local_url($medium->{url}) || $medium->{url}; $medium->{with_synthesis} ? reduce_pathname("$base/$medium->{with_synthesis}/..") : $medium->{media_info_dir} && reduce_pathname("$base/$medium->{media_info_dir}"); } sub _url_with_synthesis { my ($medium) = @_; my $base = file_from_local_url($medium->{url}) || $medium->{url}; $medium->{with_synthesis} ? reduce_pathname("$base/$medium->{with_synthesis}") : _synthesis_dir($medium) . "/" . _url_with_synthesis_basename($medium); } sub synthesis_for_virtual_medium { my ($medium) = @_; file_from_file_url($medium->{url}) && _url_with_synthesis($medium); } sub _synthesis { my ($medium) = @_; $medium->{name} && "synthesis.hdlist.$medium->{name}.cz"; } sub statedir_synthesis { my ($urpm, $medium) = @_; "$urpm->{statedir}/" . _synthesis($medium); } sub statedir_descriptions { my ($urpm, $medium) = @_; $medium->{name} && "$urpm->{statedir}/descriptions.$medium->{name}"; } sub statedir_names { my ($urpm, $medium) = @_; $medium->{name} && "$urpm->{statedir}/names.$medium->{name}"; } sub statedir_MD5SUM { my ($urpm, $medium) = @_; $medium->{name} && "$urpm->{statedir}/MD5SUM.$medium->{name}"; } sub cachedir_with_synthesis { my ($urpm, $medium) = @_; _url_with_synthesis($medium) && "$urpm->{cachedir}/partial/" . _url_with_synthesis_basename($medium); } sub any_synthesis { my ($urpm, $medium) = @_; my $f = $medium->{virtual} ? _url_with_synthesis($medium) : statedir_synthesis($urpm, $medium); -e $f && $f; } sub name2medium { my ($urpm, $name) = @_; my ($medium) = grep { $_->{name} eq $name } @{$urpm->{media}}; $medium; } #- probe device associated with a removable device. sub probe_removable_device { my ($urpm, $medium) = @_; if ($medium->{url} && $medium->{url} =~ /^removable/) { #- try to find device name in url scheme, this is deprecated, use medium option "removable" instead if ($medium->{url} =~ /^removable_?([^_:]*)/) { $medium->{removable} ||= $1 && "/dev/$1"; } } else { delete $medium->{removable}; return; } #- try to find device to open/close for removable medium. if (my $dir = file_from_local_url($medium->{url})) { my %infos; my @mntpoints = urpm::sys::find_mntpoints($dir, \%infos); if (@mntpoints > 1) { #- return value is suitable for an hash. $urpm->{log}(N("too many mount points for removable medium \"%s\"", $medium->{name})); $urpm->{log}(N("taking removable device as \"%s\"", join ',', map { $infos{$_}{device} } @mntpoints)); } if (urpm::removable::is_iso($medium->{removable})) { $urpm->{log}(N("Medium \"%s\" is an ISO image, will be mounted on-the-fly", $medium->{name})); } elsif (@mntpoints) { if ($medium->{removable} && $medium->{removable} ne $infos{$mntpoints[-1]}{device}) { $urpm->{log}(N("using different removable device [%s] for \"%s\"", $infos{$mntpoints[-1]}{device}, $medium->{name})); } $medium->{removable} = $infos{$mntpoints[-1]}{device}; } else { $urpm->{error}(N("unable to retrieve pathname for removable medium \"%s\"", $medium->{name})); } } else { $urpm->{error}(N("unable to retrieve pathname for removable medium \"%s\"", $medium->{name})); } } #- Writes the urpmi.cfg file. sub write_urpmi_cfg { my ($urpm) = @_; #- avoid trashing exiting configuration if it wasn't loaded $urpm->{media} or return; my $config = { #- global config options found in the config file, without the ones #- set from the command-line global => $urpm->{global_config}, media => [ map { only_media_opts($_) } grep { !$_->{external} } @{$urpm->{media}} ], }; remove_passwords_and_write_private_netrc($urpm, $config); urpm::cfg::dump_config($urpm->{config}, $config) or $urpm->{fatal}(6, N("unable to write config file [%s]", $urpm->{config})); $urpm->{log}(N("wrote config file [%s]", $urpm->{config})); #- everything should be synced now. delete $urpm->{modified}; } sub write_config { my ($urpm) = @_; write_urpmi_cfg($urpm); } sub _tempignore { my ($medium, $ignore) = @_; $medium->{ignore} = $ignore; } #- read urpmi.cfg file as well as necessary synthesis files #- options : #- root (deprecated, set directly $urpm->{root}) #- cmdline_skiplist #- nocheck_access (used by read_config) #- #- callback (urpmf) #- need_xml (for urpmf: to be able to have info not available in synthesis) #- nodepslist (for urpmq: we don't need the synthesis) #- no_skiplist (urpmf) #- no_second_pass (urpmf) #- #- synthesis (use this synthesis file, and only this synthesis file) #- #- parallel #- usedistrib (otherwise uses urpmi.cfg) #- media #- excludemedia #- sortmedia #- #- update #- searchmedia sub configure { my ($urpm, %options) = @_; clean($urpm); $options{parallel} && $options{usedistrib} and $urpm->{fatal}(1, N("Can't use parallel mode with use-distrib mode")); if ($options{parallel}) { require urpm::parallel; urpm::parallel::configure($urpm, $options{parallel}); if (!$options{media} && $urpm->{parallel_handler}{media}) { $options{media} = $urpm->{parallel_handler}{media}; $urpm->{log}->(N("using associated media for parallel mode: %s", $options{media})); } } else { #- nb: can't have both parallel and root $urpm->{root} = $options{root} if $options{root}; } if ($urpm->{root} && ! -c "$urpm->{root}/dev/null") { mkdir "$urpm->{root}/dev"; system("/bin/cp", "-a", '/dev/null', "$urpm->{root}/dev"); } if ($options{synthesis}) { if ($options{synthesis} ne 'none') { #- synthesis take precedence over media, update options. $options{media} || $options{excludemedia} || $options{sortmedia} || $options{update} || $options{usedistrib} || $options{parallel} and $urpm->{fatal}(1, N("--synthesis cannot be used with --media, --excludemedia, --sortmedia, --update, --use-distrib or --parallel")); $urpm->parse_synthesis($options{synthesis}); #- synthesis disables the split of transaction (too risky and not useful). $urpm->{options}{'split-length'} = 0; } } else { if ($options{usedistrib}) { $urpm->{media} = []; add_distrib_media($urpm, "Virtual", $options{usedistrib}, %options, 'virtual' => 1); } else { read_config($urpm, $options{nocheck_access}); if (!$options{media} && $urpm->{options}{'default-media'}) { $options{media} = $urpm->{options}{'default-media'}; } } if ($options{media}) { delete $_->{modified} foreach @{$urpm->{media} || []}; select_media($urpm, split /,/, $options{media}); foreach (@{$urpm->{media} || []}) { _tempignore($_, !$_->{modified}); } } if ($options{searchmedia}) { foreach (select_media_by_name($urpm, [ split /,/, $options{searchmedia} ])) { #- Ensure this media is selected $_->{modified} = 1; _tempignore($_, 0); $_->{searchmedia} = 1; } } if ($options{excludemedia}) { delete $_->{modified} foreach @{$urpm->{media} || []}; foreach (select_media_by_name($urpm, [ split /,/, $options{excludemedia} ])) { $_->{modified} = 1; #- this is only a local ignore that will not be saved. _tempignore($_, 1); } } if ($options{sortmedia}) { my @sorted_media = map { select_media_by_name($urpm, [$_]) } split(/,/, $options{sortmedia}); my @remaining = difference2($urpm->{media}, \@sorted_media); $urpm->{media} = [ @sorted_media, @remaining ]; } _parse_media($urpm, \%options) if !$options{nodepslist}; } #- determine package to withdraw (from skip.list file) only if something should be withdrawn. if (!$options{nodepslist}) { _compute_flags_for_skiplist($urpm, $options{cmdline_skiplist}) if !$options{no_skiplist}; _compute_flags_for_instlist($urpm); } } sub _parse_media { my ($urpm, $options) = @_; foreach (grep { !$_->{ignore} && (!$options->{update} || $_->{update}) } @{$urpm->{media} || []}) { our $currentmedia = $_; #- hack for urpmf delete @$_{qw(start end)}; _parse_synthesis_or_ignore($urpm, $_, $options->{callback}); if ($options->{need_xml}) { # TODO # _parse_xml_($urpm, $_, any_xml($urpm, $_), $options->{callback}); } if ($_->{searchmedia}) { $urpm->{searchmedia} = 1; $urpm->{log}(N("Search start: %s end: %s", $_->{start}, $_->{end})); } } } sub _compute_flags_for_skiplist { my ($urpm, $cmdline_skiplist) = @_; my %uniq; $urpm->compute_flags( urpm::sys::get_packages_list($urpm->{skiplist}, $cmdline_skiplist), skip => 1, callback => sub { my ($urpm, $pkg) = @_; $pkg->is_arch_compat && ! exists $uniq{$pkg->fullname} or return; $uniq{$pkg->fullname} = undef; $urpm->{log}(N("skipping package %s", scalar($pkg->fullname))); }, ); } sub _compute_flags_for_instlist { my ($urpm) = @_; my %uniq; $urpm->compute_flags( urpm::sys::get_packages_list($urpm->{instlist}), disable_obsolete => 1, callback => sub { my ($urpm, $pkg) = @_; $pkg->is_arch_compat && ! exists $uniq{$pkg->fullname} or return; $uniq{$pkg->fullname} = undef; $urpm->{log}(N("would install instead of upgrade package %s", scalar($pkg->fullname))); }, ); } #- add a new medium, sync the config file accordingly. #- returns the new medium's name. (might be different from the requested #- name if index_name was specified) #- options: ignore, index_name, nolock, update, virtual, media_info_dir sub add_medium { my ($urpm, $name, $url, $with_synthesis, %options) = @_; #- make sure configuration has been read. $urpm->{media} or die "caller should have used ->read_config or ->configure first"; #- if a medium with that name has already been found, we have to exit now if (defined $options{index_name}) { my ($i, $basename) = ($options{index_name}, $name); while (1) { ++$i; $name = $basename . ($i == 1 ? '' : $i); last if !name2medium($urpm, $name); } } else { name2medium($urpm, $name) and $urpm->{fatal}(5, N("medium \"%s\" already exists", $name)); } $url =~ s,/*$,,; #- clear URLs for trailing /es. #- creating the medium info. my $medium = { name => $name, url => $url, modified => !$options{ignore}, }; foreach (qw(downloader update ignore media_info_dir)) { $medium->{$_} = $options{$_} if exists $options{$_}; } if ($options{virtual}) { file_from_file_url($url) or $urpm->{fatal}(1, N("virtual medium needs to be local")); $medium->{virtual} = 1; } else { probe_removable_device($urpm, $medium); } if ($with_synthesis) { _migrate__with_synthesis($medium, $with_synthesis); } #- local media have priority, other are added at the end. my $inserted; my $ignore_text = $medium->{ignore} ? ' ' . N("(ignored by default)") : ''; if (file_from_file_url($url)) { #- insert before first remote medium @{$urpm->{media}} = map { if (!file_from_file_url($_->{url}) && !$inserted) { $inserted = 1; $urpm->{info}(N("adding medium \"%s\" before remote medium \"%s\"", $name, $_->{name}) . $ignore_text); $medium, $_; } else { $_ } } @{$urpm->{media}}; } if (!$inserted) { $urpm->{info}(N("adding medium \"%s\"", $name) . $ignore_text); push @{$urpm->{media}}, $medium; } $urpm->{modified} = 1; $name; } #- add distribution media, according to url given. #- returns the list of names of added media. #- options : #- - initial_number : when adding several numbered media, start with this number #- - probe_with : force use of rpms instead of using synthesis #- - ask_media : callback to know whether each media should be added #- - only_updates : only add "update" media (used by rpmdrake) #- other options are passed to add_medium(): ignore, nolock, virtual sub add_distrib_media { my ($urpm, $name, $url, %options) = @_; #- make sure configuration has been read. $urpm->{media} or die "caller should have used ->read_config or ->configure first"; my $distribconf; if (my $dir = file_from_local_url($url)) { urpm::removable::try_mounting($urpm, $dir) or $urpm->{error}(N("unable to mount the distribution medium")), return (); $distribconf = MDV::Distribconf->new($dir, undef); $distribconf->load or $urpm->{error}(N("this location doesn't seem to contain any distribution")), return (); } else { unlink "$urpm->{cachedir}/partial/media.cfg"; $distribconf = MDV::Distribconf->new($url, undef); $distribconf->settree('mandriva'); $urpm->{log}(N("retrieving media.cfg file...")); if (urpm::download::sync($urpm, undef, [ reduce_pathname($distribconf->getfullpath(undef, 'infodir') . '/media.cfg') ], quiet => 1)) { $distribconf->parse_mediacfg("$urpm->{cachedir}/partial/media.cfg") or $urpm->{error}(N("unable to parse media.cfg")), return(); } else { $urpm->{error}(N("...retrieving failed: %s", $@)); $urpm->{error}(N("unable to access the distribution medium (no media.cfg file found)")); return (); } } #- cosmetic update of name if it contains spaces. $name =~ /\s/ and $name .= ' '; my @newnames; #- at this point, we have found a media.cfg file, so parse it #- and create all necessary media according to it. my $medium_index = $options{initial_number} || 1; foreach my $media ($distribconf->listmedia) { my $media_name = $distribconf->getvalue($media, 'name') || ''; if (my $media_arch = $distribconf->getvalue($media, 'arch')) { if (!URPM::archscore($media_arch)) { $urpm->{log}(N("skipping non compatible media `%s' (for %s)", $media, $media_arch)); next; } } my $add_by_default = !$distribconf->getvalue($media, 'noauto'); if ($options{ask_media}) { $options{ask_media}->($media_name, $add_by_default) or next; } else { my $simple_rpms = !$distribconf->getvalue($media, 'debug_for') && !$distribconf->getvalue($media, 'rpms'); $add_by_default || $simple_rpms or next; } my $is_update_media = $distribconf->getvalue($media, 'updates_for'); if ($options{only_updates}) { $is_update_media or next; } my $use_copied_synthesis = $urpm->{options}{use_copied_hdlist} || $distribconf->getvalue($media, 'use_copied_hdlist'); my $with_synthesis = $use_copied_synthesis && offset_pathname( $url, $distribconf->getpath($media, 'path'), ) . '/' . $distribconf->getpath($media, 'synthesis'); push @newnames, add_medium($urpm, $name ? "$media_name ($name$medium_index)" : $media_name, reduce_pathname($distribconf->getfullpath($media, 'path')), $with_synthesis, !$use_copied_synthesis ? (media_info_dir => 'media_info') : (), !$use_copied_synthesis && $options{probe_with} ? ($options{probe_with} => 1) : (), index_name => $name ? undef : 0, $add_by_default ? () : (ignore => 1), %options, # the following override %options update => $is_update_media ? 1 : undef, ); ++$medium_index; } return @newnames; } #- deprecated, use select_media_by_name instead sub select_media { my $urpm = shift; my $options = {}; if (ref $_[0]) { $options = shift } foreach (select_media_by_name($urpm, [ @_ ], $options->{strict_match})) { #- select medium by setting the modified flag, do not check ignore. $_->{modified} = 1; } } sub select_media_by_name { my ($urpm, $names, $b_strict_match) = @_; my %wanted = map { $_ => 1 } @$names; #- first the exact matches my @l = grep { delete $wanted{$_->{name}} } @{$urpm->{media}}; #- check if some arguments don't correspond to the medium name. #- in such case, try to find the unique medium (or list candidate #- media found). foreach (keys %wanted) { my $q = quotemeta; my (@found, @foundi); my $regex = $b_strict_match ? qr/^$q$/ : qr/$q/; my $regexi = $b_strict_match ? qr/^$q$/i : qr/$q/i; foreach my $medium (@{$urpm->{media}}) { $medium->{name} =~ $regex and push @found, $medium; $medium->{name} =~ $regexi and push @foundi, $medium; } @found = @foundi if !@found; if (@found == 0) { $urpm->{error}(N("trying to select nonexistent medium \"%s\"", $_)); } else { if (@found > 1) { $urpm->{log}(N("selecting multiple media: %s", join(", ", map { qq("$_->{name}") } @found))); } #- changed behaviour to select all occurences by default. push @l, @found; } } @l; } #- deprecated, use remove_media instead sub remove_selected_media { my ($urpm) = @_; remove_media($urpm, [ grep { $_->{modified} } @{$urpm->{media}} ]); } sub remove_media { my ($urpm, $to_remove) = @_; foreach my $medium (@$to_remove) { $urpm->{info}(N("removing medium \"%s\"", $medium->{name})); #- mark to re-write configuration. $urpm->{modified} = 1; #- remove files associated with this medium. unlink grep { $_ } map { $_->($urpm, $medium) } \&statedir_synthesis, \&statedir_descriptions, \&statedir_names; #- remove proxy settings for this media urpm::download::remove_proxy_media($medium->{name}); } $urpm->{media} = [ difference2($urpm->{media}, $to_remove) ]; } sub _probe_with_try_list { my ($urpm, $medium, $f) = @_; my @media_info_dirs = ('media_info', '.'); my $base = file_from_local_url($medium->{url}) || $medium->{url}; foreach my $media_info_dir (@media_info_dirs) { my $url = reduce_pathname("$base/$media_info_dir") . '/synthesis.hdlist.cz'; if ($f->($url)) { $urpm->{debug} and $urpm->{debug}("found synthesis: $url"); $medium->{media_info_dir} = $media_info_dir; return 1; } } undef; } sub may_reconfig_urpmi { my ($urpm, $medium) = @_; my $f; if (my $dir = file_from_local_url($medium->{url})) { $f = reduce_pathname("$dir/reconfig.urpmi"); } else { unlink($f = "$urpm->{cachedir}/partial/reconfig.urpmi"); urpm::download::sync($urpm, $medium, [ reduce_pathname("$medium->{url}/reconfig.urpmi") ], quiet => 1); } if (-s $f) { reconfig_urpmi($urpm, $f, $medium); } unlink $f if !file_from_local_url($medium->{url}); } #- read a reconfiguration file for urpmi, and reconfigure media accordingly #- $rfile is the reconfiguration file (local), $name is the media name #- #- the format is similar to the RewriteRule of mod_rewrite, so: #- PATTERN REPLACEMENT [FLAG] #- where FLAG can be L or N #- #- example of reconfig.urpmi: #- # this is an urpmi reconfiguration file #- /cooker /cooker/$ARCH sub reconfig_urpmi { my ($urpm, $rfile, $medium) = @_; -r $rfile or return; $urpm->{log}(N("reconfiguring urpmi for media \"%s\"", $medium->{name})); my ($magic, @lines) = cat_($rfile); #- the first line of reconfig.urpmi must be magic, to be sure it's not an error file $magic =~ /^# this is an urpmi reconfiguration file/ or return undef; my @replacements; foreach (@lines) { chomp; s/^\s*//; s/#.*$//; s/\s*$//; $_ or next; my ($p, $r, $f) = split /\s+/, $_, 3; push @replacements, [ quotemeta $p, $r, $f || 1 ]; } my $reconfigured = 0; my @reconfigurable = qw(url with_synthesis media_info_dir); my %orig = %$medium; URLS: foreach my $k (@reconfigurable) { foreach my $r (@replacements) { if ($medium->{$k} =~ s/$r->[0]/$r->[1]/) { $reconfigured = 1; #- Flags stolen from mod_rewrite: L(ast), N(ext) if ($r->[2] =~ /L/) { last; } elsif ($r->[2] =~ /N/) { #- dangerous option redo URLS; } } } #- check that the new url exists before committing changes (local mirrors) my $file = file_from_local_url($medium->{$k}); if ($file && !-e $file) { %$medium = %orig; $reconfigured = 0; $urpm->{log}(N("...reconfiguration failed")); return; } } if ($reconfigured) { $urpm->{log}(N("reconfiguration done")); $urpm->{modified} = 1; } $reconfigured; } sub _guess_synthesis_suffix { my ($url) = @_; $url =~ m!\bmedia/(\w+)/*\Z! && $1; } sub _synthesis_suffix { my ($medium) = @_; $medium->{with_synthesis} =~ /synthesis\.hdlist(.*?)(?:\.src)?\.cz$/ ? $1 : ''; } #- names. is used by external progs (namely for bash-completion) sub generate_medium_names { my ($urpm, $medium) = @_; unlink statedir_names($urpm, $medium); if (my $fh = urpm::sys::open_safe($urpm, ">", statedir_names($urpm, $medium))) { foreach ($medium->{start} .. $medium->{end}) { if (defined $urpm->{depslist}[$_]) { print $fh $urpm->{depslist}[$_]->name . "\n"; } else { $urpm->{error}(N("Error generating names file: dependency %d not found", $_)); } } } else { $urpm->{error}(N("Error generating names file: Can't write to file (%s)", $!)); } } sub _read_existing_synthesis { my ($urpm, $medium) = @_; unlink cachedir_with_synthesis($urpm, $medium); $urpm->{info}(N("medium \"%s\" is up-to-date", $medium->{name})); #- the medium is now considered not modified. $medium->{modified} = 0; #- XXX we could link the new synthesis to the old one. #- (However links need to be managed. see bug #12391.) #- as previously done, just read synthesis file here, this is enough. _parse_synthesis_or_ignore($urpm, $medium); 1; } sub _parse_synthesis { my ($urpm, $medium, $synthesis_file, $o_callback) = @_; -e $synthesis_file or return; $urpm->{log}(N("examining synthesis file [%s]", $synthesis_file)); ($medium->{start}, $medium->{end}) = $urpm->parse_synthesis($synthesis_file, $o_callback ? (callback => $o_callback) : @{[]}); } sub _parse_synthesis_or_ignore { my ($urpm, $medium, $o_callback) = @_; _parse_synthesis($urpm, $medium, any_synthesis($urpm, $medium), $o_callback) or _ignore_medium_on_parse_error($urpm, $medium); } sub is_valid_medium { my ($medium) = @_; defined $medium->{start} && defined $medium->{end}; } sub _ignore_medium_on_parse_error { my ($urpm, $medium) = @_; $urpm->{error}(N("problem reading synthesis file of medium \"%s\"", $medium->{name})); $medium->{ignore} = 1; } sub _get_list_or_pubkey__local { my ($urpm, $medium, $name) = @_; my $path = _synthesis_dir($medium) . "/$name" . _synthesis_suffix($medium); -e $path or $path = file_from_local_url($medium->{url}) . "/media_info/$name"; if (-e $path) { $urpm->{log}(N("copying [%s] for medium \"%s\"...", $path, $medium->{name})); copy_and_own($path, "$urpm->{cachedir}/partial/$name") or $urpm->{error}(N("...copying failed")), return; } 1; } sub _get_list_or_pubkey__remote { my ($urpm, $medium, $name) = @_; my $found; if (_synthesis_suffix($medium)) { my $local_name = $name . _synthesis_suffix($medium); if (urpm::download::sync($urpm, $medium, [_synthesis_dir($medium) . "/$local_name"], quiet => 1)) { rename("$urpm->{cachedir}/partial/$local_name", "$urpm->{cachedir}/partial/$name"); $found = 1; } } if (!$found) { urpm::download::sync($urpm, $medium, [_synthesis_dir($medium) . "/$name"], quiet => 1) or unlink "$urpm->{cachedir}/partial/$name"; } } sub get_descriptions_local { my ($urpm, $medium) = @_; unlink statedir_descriptions($urpm, $medium); my $dir = file_from_local_url($medium->{url}); my $description_file = "$dir/media_info/descriptions"; #- new default location -e $description_file or $description_file = "$dir/../descriptions"; -e $description_file or return; $urpm->{log}(N("copying description file of \"%s\"...", $medium->{name})); if (copy_and_own($description_file, statedir_descriptions($urpm, $medium))) { $urpm->{log}(N("...copying done")); } else { $urpm->{error}(N("...copying failed")); $medium->{ignore} = 1; } } sub get_descriptions_remote { my ($urpm, $medium) = @_; unlink "$urpm->{cachedir}/partial/descriptions"; if (-e statedir_descriptions($urpm, $medium)) { urpm::util::move(statedir_descriptions($urpm, $medium), "$urpm->{cachedir}/partial/descriptions"); } urpm::download::sync($urpm, $medium, [ reduce_pathname("$medium->{url}/media_info/descriptions") ], quiet => 1) or #- try older location urpm::download::sync($urpm, $medium, [ reduce_pathname("$medium->{url}/../descriptions") ], quiet => 1); if (-e "$urpm->{cachedir}/partial/descriptions") { urpm::util::move("$urpm->{cachedir}/partial/descriptions", statedir_descriptions($urpm, $medium)); } } sub get_synthesis__local { my ($urpm, $medium, $callback) = @_; my $f = cachedir_with_synthesis($urpm, $medium); unlink $f; $urpm->{log}(N("copying [%s] for medium \"%s\"...", _url_with_synthesis($medium), $medium->{name})); $callback and $callback->('copy', $medium->{name}); if (copy_and_own(_url_with_synthesis($medium), $f)) { $callback and $callback->('done', $medium->{name}); $urpm->{log}(N("...copying done")); if (file_size($f) < 20) { $urpm->{error}(N("copy of [%s] failed (file is suspiciously small)", $f)); 0; } else { 1; } } else { $callback and $callback->('failed', $medium->{name}); #- force error, reported afterwards unlink $f; 0; } } sub get_synthesis__remote { my ($urpm, $medium, $callback, $quiet) = @_; if (urpm::download::sync($urpm, $medium, [ _url_with_synthesis($medium) ], quiet => $quiet, callback => $callback) && file_size(cachedir_with_synthesis($urpm, $medium)) >= 20) { 1; } else { chomp(my $err = $@); $urpm->{error}(N("...retrieving failed: %s", $err)); 0; } } #- check copied/downloaded file has right signature. sub check_synthesis_md5sum { my ($urpm, $medium) = @_; my $wanted_md5sum = urpm::md5sum::from_MD5SUM__or_warn($urpm, "$urpm->{cachedir}/partial/MD5SUM", 'synthesis.hdlist.cz'); if ($wanted_md5sum) { $urpm->{log}(N("computing md5sum of retrieved source synthesis")); urpm::md5sum::compute(cachedir_with_synthesis($urpm, $medium)) eq $wanted_md5sum or $urpm->{error}(N("copy of [%s] failed (md5sum mismatch)", _url_with_synthesis($medium))), return; } 1; } sub _call_genhdlist2 { my ($urpm, $medium) = @_; !$medium->{with_synthesis} or $urpm->{fatal}(1, 'with_synthesis not handled with --probe-rpms'); my $dir = file_from_local_url($medium->{url}); system('genhdlist2', $urpm->{debug} ? '--verbose' : (), '--no-hdlist', '--media_info-dir', "$urpm->{cachedir}/partial", $dir) == 0 or $urpm->{error}(N("genhdlist2 failed on %s", $dir)), return; 1; } sub _is_statedir_MD5SUM_uptodate { my ($urpm, $medium, $new_MD5SUM) = @_; my $current_MD5SUM = statedir_MD5SUM($urpm, $medium); $urpm->{log}(N("comparing %s and %s", $new_MD5SUM, $current_MD5SUM)); cat_($new_MD5SUM) eq cat_($current_MD5SUM); } #- options: callback, force, nomd5sum, probe_with sub _update_medium__parse_if_unmodified__local { my ($urpm, $medium, $options) = @_; my $dir = file_from_local_url($medium->{url}); if (!-d $dir) { #- the directory given does not exist and may be accessible #- by mounting some other directory. Try to figure it out and mount #- everything that might be necessary. urpm::removable::try_mounting($urpm, $options->{probe_with} ne 'rpms' && _synthesis_dir($medium) ? _synthesis_dir($medium) : $dir, #- in case of an iso image, pass its name urpm::removable::is_iso($medium->{removable}) && $medium->{removable}, ) or $urpm->{error}(N("unable to access medium \"%s\", this could happen if you mounted manually the directory when creating the medium.", $medium->{name})), return; } #- try to probe for possible with_synthesis parameter, unless #- it is already defined (and valid). if (!_synthesis_dir($medium) && $options->{probe_with} ne 'rpms') { _probe_with_try_list($urpm, $medium, sub { my ($url) = @_; -e $url or return; if (file_size($url) >= 20) { 1; } else { $urpm->{error}(N("invalid hdlist file %s for medium \"%s\"", $url, $medium->{name})); 0; } }); } if ($medium->{virtual}) { #- syncing a virtual medium is very simple, just try to read the file in order to #- determine its type, once a with_synthesis has been found (but is mandatory). _parse_synthesis_or_ignore($urpm, $medium); 1; } elsif ($options->{probe_with} eq 'rpms' || !_synthesis_dir($medium)) { _call_genhdlist2($urpm, $medium) or return ''; 1; } elsif (_synthesis_dir($medium)) { my $new_MD5SUM = _synthesis_dir($medium) . '/MD5SUM'; unlink "$urpm->{cachedir}/partial/MD5SUM"; if (!$options->{nomd5sum} && file_size($new_MD5SUM) > 32) { if (!$options->{force} && _is_statedir_MD5SUM_uptodate($urpm, $medium, $new_MD5SUM)) { _read_existing_synthesis($urpm, $medium) and return 'unmodified'; } $urpm->{log}(N("copying MD5SUM file of \"%s\"...", $medium->{name})); copy_and_own($new_MD5SUM, "$urpm->{cachedir}/partial/MD5SUM"); } my $ok = get_synthesis__local($urpm, $medium, $options->{callback}); $ok &&= !$options->{force} || check_synthesis_md5sum($urpm, $medium); if ($ok) { 1; } elsif ($urpm->{options}{'build-hdlist-on-error'}) { #- if copying synthesis has failed, try to build it directly. _call_genhdlist2($urpm, $medium) or return ''; 1; } else { _ignore_medium_on_parse_error($urpm, $medium); ''; } } } #- options: callback, force, nomd5sum, probe_with, quiet sub _update_medium__parse_if_unmodified__remote { my ($urpm, $medium, $options) = @_; #- examine if a distant MD5SUM file is available. #- this will only be done if $with_synthesis is not empty in order to use #- an existing synthesis file, and to check if download was good. #- if no MD5SUM is available, do it as before... if (_synthesis_dir($medium)) { #- we can assume at this point a basename is existing, but it needs #- to be checked for being valid, nothing can be deduced if no MD5SUM #- file is present. my $new_MD5SUM = "$urpm->{cachedir}/partial/MD5SUM"; unlink $new_MD5SUM; if (!$options->{nomd5sum} && urpm::download::sync($urpm, $medium, [ reduce_pathname(_synthesis_dir($medium) . '/MD5SUM') ], quiet => 1) && file_size($new_MD5SUM) > 32) { if ($options->{force} < 2 && _is_statedir_MD5SUM_uptodate($urpm, $medium, $new_MD5SUM)) { _read_existing_synthesis($urpm, $medium) and return 'unmodified'; } } } #- try to probe for possible with_synthesis parameter, unless #- it is already defined (and valid). $urpm->{log}(N("retrieving source synthesis of \"%s\"...", $medium->{name})); $options->{callback} and $options->{callback}('retrieve', $medium->{name}); my $error = sub { my ($msg) = @_; $urpm->{error}($msg); unlink cachedir_with_synthesis($urpm, $medium); $options->{callback} and $options->{callback}('failed', $medium->{name}); }; if (!_synthesis_dir($medium)) { my $err; _probe_with_try_list($urpm, $medium, sub { my ($url) = @_; my $f = "$urpm->{cachedir}/partial/" . basename($url); $options->{force} and unlink $f; if (urpm::download::sync($urpm, $medium, [ $url ], quiet => $options->{quiet}, callback => $options->{callback}) && file_size($f) >= 20) { $urpm->{log}(N("found probed synthesis as %s", $url)); 1; } else { chomp($err = $@); 0; } }) or do { $error->(N("no synthesis file found for medium \"%s\"", $medium->{name})); $urpm->{error}(N("...retrieving failed: %s", $err)); return; }; } else { if ($options->{force}) { unlink cachedir_with_synthesis($urpm, $medium); } else { #- for rsync, try to sync (copy if needed) local copy after restored the previous one. my $previous_synthesis = statedir_synthesis($urpm, $medium); if (-e $previous_synthesis && urpm::protocol_from_url($medium->{url}) eq 'rsync') { copy_and_own( $previous_synthesis, cachedir_with_synthesis($urpm, $medium), ) or $error->(N("...copying failed")), return; } } my $ok = get_synthesis__remote($urpm, $medium, $options->{callback}, $options->{quiet}); $ok &&= !$options->{force} || check_synthesis_md5sum($urpm, $medium); $options->{callback} and $options->{callback}('done', $medium->{name}); if (!$ok) { _ignore_medium_on_parse_error($urpm, $medium); return; } } 1; } sub _get_pubkey_and_descriptions { my ($urpm, $medium, $nopubkey) = @_; my $local = file_from_local_url($medium->{url}); ($local ? \&get_descriptions_local : \&get_descriptions_remote)->($urpm, $medium); #- examine if a pubkey file is available. if (!$nopubkey && !$medium->{'key-ids'}) { ($local ? \&_get_list_or_pubkey__local : \&_get_list_or_pubkey__remote)->($urpm, $medium, 'pubkey'); } } sub _read_cachedir_pubkey { my ($urpm, $medium, $b_wait_lock) = @_; -s "$urpm->{cachedir}/partial/pubkey" or return; $urpm->{log}(N("examining pubkey file of \"%s\"...", $medium->{name})); my $_rpm_lock = urpm::lock::rpm_db($urpm, 'exclusive', wait => $b_wait_lock); my $db = urpm::db_open_or_die($urpm, $urpm->{root}, 'rw'); my %key_ids; URPM::import_needed_pubkeys_from_file($db, "$urpm->{cachedir}/partial/pubkey", sub { my ($id, $imported) = @_; if ($id) { $key_ids{$id} = undef; $imported and $urpm->{log}(N("...imported key %s from pubkey file of \"%s\"", $id, $medium->{name})); $imported or $urpm->{debug}("pubkey $id already imported") if $urpm->{debug}; } else { $urpm->{error}(N("unable to import pubkey file of \"%s\"", $medium->{name})); } }); unlink "$urpm->{cachedir}/partial/pubkey"; join(',', keys %key_ids); } #- options: callback, force, nomd5sum, probe_with, quiet, nopubkey, wait_lock #- (from _update_medium__parse_if_unmodified__local and _update_medium__parse_if_unmodified__remote) sub _update_medium_ { my ($urpm, $medium, %options) = @_; unless ($medium->{modified}) { _parse_synthesis_or_ignore($urpm, $medium); return 'unmodified'; } #- always delete a remaining list file or pubkey file in cache. foreach (qw(list pubkey)) { unlink "$urpm->{cachedir}/partial/$_"; } #- check for a reconfig.urpmi file (if not already reconfigured) if (!$medium->{noreconfigure}) { may_reconfig_urpmi($urpm, $medium); } { my $rc = file_from_local_url($medium->{url}) ? _update_medium__parse_if_unmodified__local($urpm, $medium, \%options) : _update_medium__parse_if_unmodified__remote($urpm, $medium, \%options); if (!$rc || $rc eq 'unmodified') { return $rc; } } if (!$medium->{virtual}) { if (file_size(cachedir_with_synthesis($urpm, $medium)) < 20) { $urpm->{error}(N("no synthesis file found for medium \"%s\"", $medium->{name})); return; } $options{callback} and $options{callback}('parse', $medium->{name}); if (_parse_synthesis($urpm, $medium, cachedir_with_synthesis($urpm, $medium))) { $options{callback} && $options{callback}('done', $medium->{name}); } else { $urpm->{error}(N("unable to parse synthesis file of \"%s\"", $medium->{name})); $options{callback} and $options{callback}('failed', $medium->{name}); #- we have to read back the current synthesis file unmodified. _parse_synthesis_or_ignore($urpm, $medium); return; } if (-e statedir_synthesis($urpm, $medium)) { $urpm->{info}(N("updated medium \"%s\"", $medium->{name})); } #- use new files unlink statedir_synthesis($urpm, $medium); urpm::util::move(cachedir_with_synthesis($urpm, $medium), statedir_synthesis($urpm, $medium)); unlink statedir_MD5SUM($urpm, $medium); if (!$medium->{with_synthesis}) { # no MD5SUM when using with_synthesis, urpmi.update will update everytime! urpm::util::move("$urpm->{cachedir}/partial/MD5SUM", statedir_MD5SUM($urpm, $medium)) if -e "$urpm->{cachedir}/partial/MD5SUM"; } } $medium->{modified} = 0; _get_pubkey_and_descriptions($urpm, $medium, $options{nopubkey}); $medium->{'key-ids'} ||= _read_cachedir_pubkey($urpm, $medium, $options{wait_lock}); generate_medium_names($urpm, $medium); 1; } sub _update_medium { my ($urpm, $medium, %options) = @_; my $rc = _update_medium_($urpm, $medium, %options); if (!$rc && !$medium->{virtual}) { #- an error has occured for updating the medium, we have to remove temporary files. unlink(glob("$urpm->{cachedir}/partial/*")); } $rc; } sub _update_media__handle_some_flags { my ($urpm, $forcekey, $all) = @_; foreach my $medium (grep { !$_->{ignore} } @{$urpm->{media}}) { $forcekey and delete $medium->{'key-ids'}; if ($medium->{static}) { #- don't ever update static media $medium->{modified} = 0; } elsif ($all) { #- if we're rebuilding all media, mark them as modified (except removable ones) $medium->{modified} ||= $medium->{url} !~ m!^removable!; } } } #- Update the urpmi database w.r.t. the current configuration. #- Takes care of modifications, and tries some tricks to bypass #- the recomputation of base files. #- Recognized options : #- all : all medias are being rebuilt #- callback : UI callback #- forcekey : force retrieval of pubkey #- force : try to force rebuilding base files #- nomd5sum : don't verify MD5SUM of retrieved files #- nopubkey : don't use rpm pubkeys #- probe_with : probe synthesis or rpms #- quiet : download synthesis quietly #- wait_lock : block until lock can be acquired sub update_media { my ($urpm, %options) = @_; $urpm->{media} or return; # verify that configuration has been read $options{nopubkey} ||= $urpm->{options}{nopubkey}; #- examine each medium to see if one of them needs to be updated. #- if this is the case and if not forced, try to use a pre-calculated #- synthesis file, else build it from rpm files. clean($urpm); _update_media__handle_some_flags($urpm, $options{forcekey}, $options{all}); my %updates_result; foreach my $medium (grep { !$_->{ignore} } @{$urpm->{media}}) { my $rc = _update_medium($urpm, $medium, %options); $updates_result{$rc || 'error'}++; } $urpm->{debug} and $urpm->{debug}('update_medium: ' . join(' ', map { "$_=$updates_result{$_}" } keys %updates_result)); if ($updates_result{1} == 0) { #- only errors/unmodified, leave now #- (this ensures buggy added medium is not added to urpmi.cfg) return $updates_result{error} == 0; } if ($urpm->{modified}) { #- write config files in any case write_config($urpm); urpm::download::dump_proxy_config(); } $updates_result{error} == 0; } #- clean params and depslist computation zone. sub clean { my ($urpm) = @_; $urpm->{depslist} = []; $urpm->{provides} = {}; foreach (@{$urpm->{media} || []}) { delete $_->{start}; delete $_->{end}; } } 1;