1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
package urpm::removable;
# $Id$
use urpm::msg;
use urpm::sys;
use urpm::util;
use urpm::get_pkgs;
use urpm 'file_from_local_medium', 'is_local_medium';
#- 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;
}
sub _file_or_synthesis_dir {
my ($medium, $o_url) = @_;
urpm::media::_valid_synthesis_dir($medium) && !$o_url ?
urpm::media::_synthesis_dir($medium) :
file_from_local_medium($medium, $o_url);
}
#- side-effects: $medium->{mntpoint}
sub _look_for_mounted_cdrom_in_mtab {
my ($urpm, $medium, $o_url) = @_;
my @mntpoints = map { $_->{mntpoint} }
grep { $_->{fs} eq 'iso9660' || $_->{fs} eq 'udf' } urpm::sys::read_mtab();
foreach (@mntpoints) {
# set it, then verify
$medium->{mntpoint} = $_;
if (-r _file_or_synthesis_dir($medium, $o_url)) {
$urpm->{log}("using cdrom mounted in $_");
return 1;
}
}
0;
}
#- side-effects:
#- + those of _try_mounting_medium ($medium->{mntpoint})
sub try_mounting_medium {
my ($urpm, $medium, $o_url) = @_;
my $rc = _try_mounting_medium($urpm, $medium, $o_url);
$rc or $urpm->{error}(N("unable to access medium \"%s\".", $medium->{name}));
$rc;
}
#- side-effects:
#- + those of _look_for_mounted_cdrom_in_mtab ($medium->{mntpoint})
sub _try_mounting_medium {
my ($urpm, $medium, $o_url) = @_;
if (urpm::is_cdrom_url($medium->{url})) {
_look_for_mounted_cdrom_in_mtab($urpm, $medium, $o_url);
} else {
-r _file_or_synthesis_dir($medium, $o_url);
}
}
#- side-effects:
#- + those of try_mounting_ ($urpm->{removable_mounted}, "mount")
#- + those of try_mounting_iso ($urpm->{removable_mounted}, "mount")
sub try_mounting {
my ($urpm, $dir, $o_iso) = @_;
$o_iso ? try_mounting_iso($urpm, $dir, $o_iso) : try_mounting_($urpm, $dir);
}
#- side-effects: $urpm->{removable_mounted}, "mount"
sub try_mounting_iso {
my ($urpm, $dir, $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
my $mntpoint = urpm::sys::trim_until_d($dir);
if ($mntpoint) {
$urpm->{log}(N("mounting %s", $mntpoint));
#- to mount an iso image, grab the first loop device
my $loopdev = urpm::sys::first_free_loopdev();
sys_log("mount iso $mntpoint on $iso");
$loopdev and system('mount', $iso, $mntpoint, '-t', 'iso9660', '-o', "loop=$loopdev");
$iso and $urpm->{removable_mounted}{$mntpoint} = undef;
}
-e $mntpoint;
}
#- side-effects: $urpm->{removable_mounted}, "mount"
sub try_mounting_ {
my ($urpm, $dir) = @_;
my $mntpoint = _non_mounted_mntpoint($dir);
if ($mntpoint) {
$urpm->{log}(N("mounting %s", $mntpoint));
sys_log("mount $mntpoint");
system("mount '$mntpoint' 2>/dev/null");
}
-e $dir;
}
#- side-effects: $urpm->{removable_mounted}, "umount"
sub try_umounting {
my ($urpm, $dir) = @_;
if (my $mntpoint = _mounted_mntpoint($dir)) {
$urpm->{log}(N("unmounting %s", $mntpoint));
sys_log("umount $mntpoint");
system("umount '$mntpoint' 2>/dev/null");
delete $urpm->{removable_mounted}{$mntpoint};
}
! -e $dir;
}
#- side-effects: none
sub _mounted_mntpoint {
my ($dir) = @_;
$dir = reduce_pathname($dir);
my $entry = urpm::sys::find_a_mntpoint($dir);
$entry->{mounted} && $entry->{mntpoint};
}
#- side-effects: none
sub _non_mounted_mntpoint {
my ($dir) = @_;
$dir = reduce_pathname($dir);
my $entry = urpm::sys::find_a_mntpoint($dir);
!$entry->{mounted} && $entry->{mntpoint};
}
#- 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};
}
#- side-effects:
#- + those of try_mounting_medium ($medium->{mntpoint})
sub _mount_and_check_notfound {
my ($urpm, $blist, $medium) = @_;
my ($first_url) = values %{$blist->{list}};
try_mounting_medium($urpm, $medium, $first_url) or return 1;
_check_notfound($blist);
}
#- side-effects: none
sub _check_notfound {
my ($blist) = @_;
foreach (values %{$blist->{list}}) {
my $dir_ = _filepath($blist->{medium}, $_) or next;
-r $dir_ or return 1;
}
0;
}
#- side-effects: "eject"
#- + those of _mount_and_check_notfound ($urpm->{removable_mounted}, "mount")
#- + those of try_umounting ($urpm->{removable_mounted}, "umount")
sub _mount_it {
my ($urpm, $blist, $ask_for_medium) = @_;
my $medium = $blist->{medium};
#- 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 (_mount_and_check_notfound($urpm, $blist, $medium)) {
$ask_for_medium
or $urpm->{fatal}(4, N("medium \"%s\" is not available", $medium->{name}));
my $dir; # TODO
try_umounting($urpm, $dir);
system("/usr/bin/eject '$medium->{removable}' 2>/dev/null");
$ask_for_medium->(remove_internal_name($medium->{name}), $medium->{removable})
or $urpm->{fatal}(4, N("medium \"%s\" is not available", $medium->{name}));
}
}
#- side-effects: none
sub _filepath {
my ($medium, $url) = @_;
chomp $url;
my $filepath = file_from_local_medium($medium, $url) or return;
$filepath =~ m!/.*/! or return; #- is this really needed??
$filepath;
}
#- side-effects: "copy-move-files"
sub _do_the_copy {
my ($urpm, $filepath) = @_;
#- we should assume a possibly buggy removable device...
#- First, copy in partial cache, and if the package is still good,
#- transfer it to the rpms cache.
my $filename = basename($filepath);
unlink "$urpm->{cachedir}/partial/$filename";
$urpm->{log}("copying $filepath");
copy_and_own($filepath, "$urpm->{cachedir}/partial/$filename") or return;
my $f = urpm::get_pkgs::verify_partial_rpm_and_move($urpm, $urpm->{cachedir}, $filename) or return;
$f;
}
#- side-effects: $sources
#- + those of _mount_it ($urpm->{removable_mounted}, "mount", "umount", "eject")
#- + those of _do_the_copy: "copy-move-files"
sub _copy_from_cdrom__if_needed {
my ($urpm, $blist, $sources, $ask_for_medium, $want_copy) = @_;
_mount_it($urpm, $blist, $ask_for_medium);
while (my ($i, $url) = each %{$blist->{list}}) {
my $filepath = _filepath($blist->{medium}, $url) or next;
if (-r $filepath) {
$sources->{$i} = $want_copy ? _do_the_copy($urpm, $filepath) : $filepath;
} else {
#- fallback to use other method for retrieving the file later.
$urpm->{error}(N("unable to read rpm file [%s] from medium \"%s\"", $filepath, $blist->{medium}{name}));
}
}
}
#- side-effects:
#- + those of try_mounting_non_cdrom ($urpm->{removable_mounted}, "mount")
sub try_mounting_non_cdroms {
my ($urpm, $list) = @_;
my $blist = _create_blists($urpm->{media}, $list);
my @used_media = map { $_->{medium} } @$blist;
foreach my $medium (grep { !urpm::is_cdrom_url($_->{url}) } @used_media) {
try_mounting_non_cdrom($urpm, $medium);
}
}
#- side-effects:
#- + those of try_mounting_ ($urpm->{removable_mounted}, "mount")
sub try_mounting_non_cdrom {
my ($urpm, $medium) = @_;
my $dir = file_from_local_medium($medium) or return;
-e $dir || try_mounting($urpm, $dir, $medium->{iso}) or
$urpm->{error}(N("unable to access medium \"%s\"", $medium->{name})), return;
1;
}
#- side-effects: none
sub _create_blists {
my ($media, $list) = @_;
#- make sure everything is correct on input...
$media or return;
@$media == @$list or return;
my $i;
[ grep { %{$_->{list}} }
map { { medium => $_, list => $list->[$i++] } } @$media ];
}
#- side-effects: none
sub _sort_media {
my (@l) = @_;
if (@l > 1) {
@l = sort { values(%{$a->{list}}) <=> values(%{$b->{list}}) } @l;
#- check if a removable device is already mounted (and files present).
if (my ($already_mounted) = grep { !_check_notfound($_) } @l) {
@l = ($already_mounted, grep { $_ != $already_mounted } @l);
}
}
@l;
}
#- $list is a [ { pkg_id1 => url1, ... }, { ... }, ... ]
#- where there is one hash for each medium in {media}
#-
#- side-effects:
#- + those of _copy_from_cdrom__if_needed ($urpm->{removable_mounted}, $sources, "mount", "umount", "eject", "copy-move-files")
sub copy_packages_of_removable_media {
my ($urpm, $list, $sources, $o_ask_for_medium) = @_;
my $blists = _create_blists($urpm->{media}, $list);
#- If more than one media uses this device, we have to sort
#- needed packages to copy the needed rpm files.
my @l = _sort_media(grep { urpm::is_cdrom_url($_->{medium}{url}) } @$blists);
foreach my $blist (@l) {
_copy_from_cdrom__if_needed($urpm, $blist, $sources, $o_ask_for_medium, @l > 1);
}
1;
}
1;
|