summaryrefslogtreecommitdiffstats
path: root/perl-install/standalone/man/C
ModeNameSize
d---------man545logstatsplain
d---------man841logstatsplain
='#n126'>126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
package urpm::removable;

# $Id$

use urpm::msg;
use urpm::sys;
use urpm::util;
use urpm::get_pkgs;
use urpm 'file_from_local_url';



#- returns the removable device name if it corresponds to an iso image, '' otherwise
#-
#- side-effects: none
sub is_iso {
    my ($removable_dev) = @_;
    $removable_dev && $removable_dev =~ /\.iso$/i;
}

#- side-effects: $urpm->{removable_mounted}, mount
sub try_mounting {
    my ($urpm, $dir, $o_removable) = @_;

    my $is_iso = is_iso($o_removable);
    my @mntpoints = $is_iso
	#- note: for isos, we don't parse the fstab because it might not be declared in it.
	#- so we try to remove suffixes from the dir name until the dir exists
	? ($dir = urpm::sys::trim_until_d($dir))
	: _non_mounted_mntpoints($dir);

    foreach (@mntpoints) {
	$urpm->{log}(N("mounting %s", $_));
	if ($is_iso) {
	    #- to mount an iso image, grab the first loop device
	    my $loopdev = urpm::sys::first_free_loopdev();
	    sys_log("mount iso $_ on $o_removable");
	    $loopdev and system('mount', $o_removable, $_, '-t', 'iso9660', '-o', "loop=$loopdev");
	    $o_removable and $urpm->{removable_mounted}{$_} = undef;
	} else {
	    sys_log("mount $_");
	    system("mount '$_' 2>/dev/null");
	}
    }
    -e $dir;
}

#- side-effects: $urpm->{removable_mounted}, umount
sub try_umounting {
    my ($urpm, $dir) = @_;

    foreach (reverse _mounted_mntpoints($dir)) {
	$urpm->{log}(N("unmounting %s", $_));
	sys_log("umount $_");
	system("umount '$_' 2>/dev/null");
	delete $urpm->{removable_mounted}{$_};
    }
    ! -e $dir;
}

#- side-effects: none
sub _mounted_mntpoints {
    my ($dir) = @_;
    my %infos;
    $dir = reduce_pathname($dir);
    grep { $infos{$_}{mounted} } urpm::sys::find_mntpoints($dir, \%infos);
}
#- side-effects: none
sub _non_mounted_mntpoints {
    my ($dir) = @_;
    my %infos;
    $dir = reduce_pathname($dir);
    grep { !$infos{$_}{mounted} } urpm::sys::find_mntpoints($dir, \%infos);
}

#- side-effects: $urpm->{removable_mounted}
#-   + those of try_umounting ($urpm->{removable_mounted}, umount)
sub try_umounting_removables {
    my ($urpm) = @_;
    foreach (keys %{$urpm->{removable_mounted}}) {
	try_umounting($urpm, $_);
    }
    delete $urpm->{removable_mounted};
}

#- examine if given medium is already inside a removable device.
#-
#- side-effects:
#-   + those of try_mounting ($urpm->{removable_mounted}, mount)
sub _check_notfound {
    my ($urpm, $medium_list, $dir, $removable) = @_;
	if ($dir) {
	    try_mounting($urpm, $dir, $removable);
	    -e $dir or return 2;
	}
	foreach (values %$medium_list) {
	    my $dir_ = _filepath($_) or next;
	    if (!$dir) {
		$dir = $dir_;
		try_mounting($urpm, $dir, $removable);
	    }
	    -r $dir_ or return 1;
	}
	0;
}

#- removable media have to be examined to keep mounted the one that has
#- more packages than others.
sub _examine_removable_medium {
    my ($urpm, $blist, $sources, $o_ask_for_medium) = @_;

    my $medium = $blist->{medium};

    if (file_from_local_url($medium->{url})) {
	_examine_removable_medium_($urpm, $medium, $blist->{list}, $sources, $o_ask_for_medium);
    } else {
	#- we have a removable device that is not removable, well...
	$urpm->{error}(N("inconsistent medium \"%s\" marked removable but not really", $medium->{name}));
    }
}

sub _mount_it {
    my ($urpm, $medium, $medium_list, $o_ask_for_medium) = @_;

    my $dir = file_from_local_url($medium->{url});

    #- 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.
    while (_check_notfound($urpm, $medium_list, $dir, $medium->{removable})) {
	if (is_iso($medium->{removable})) {
	    try_umounting($urpm, $dir);
	} else {
	    $o_ask_for_medium 
	      or $urpm->{fatal}(4, N("medium \"%s\" is not available", $medium->{name}));

	    try_umounting($urpm, $dir);