ebuild: fetch: Flatten conditionals in _get_fetch_resume_size
[portage.git] / pym / portage / package / ebuild / fetch.py
1 # Copyright 2010-2013 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 from __future__ import print_function
5
6 __all__ = ['fetch']
7
8 import errno
9 import io
10 import logging
11 import random
12 import re
13 import stat
14 import sys
15 import tempfile
16
17 try:
18         from urllib.parse import urlparse, urlunparse
19 except ImportError:
20         from urlparse import urlparse, urlunparse
21
22 import portage
23 portage.proxy.lazyimport.lazyimport(globals(),
24         'portage.package.ebuild.config:check_config_instance,config',
25         'portage.package.ebuild.doebuild:doebuild_environment,' + \
26                 '_doebuild_spawn',
27         'portage.package.ebuild.prepare_build_dirs:prepare_build_dirs',
28 )
29
30 from portage import OrderedDict, os, selinux, shutil, _encodings, \
31         _shell_quote, _unicode_encode
32 from portage.checksum import (hashfunc_map, perform_md5, verify_all,
33         _filter_unaccelarated_hashes, _hash_filter, _apply_hash_filter)
34 from portage.const import BASH_BINARY, CUSTOM_MIRRORS_FILE, \
35         GLOBAL_CONFIG_PATH
36 from portage.data import portage_gid, portage_uid, secpass, userpriv_groups
37 from portage.exception import FileNotFound, OperationNotPermitted, \
38         PortageException, TryAgain
39 from portage.localization import _
40 from portage.locks import lockfile, unlockfile
41 from portage.output import colorize, EOutput
42 from portage.util import apply_recursive_permissions, \
43         apply_secpass_permissions, ensure_dirs, grabdict, shlex_split, \
44         varexpand, writemsg, writemsg_level, writemsg_stdout
45 from portage.process import spawn
46
47 _userpriv_spawn_kwargs = (
48         ("uid",    portage_uid),
49         ("gid",    portage_gid),
50         ("groups", userpriv_groups),
51         ("umask",  0o02),
52 )
53
54 def _hide_url_passwd(url):
55         return re.sub(r'//(.+):.+@(.+)', r'//\1:*password*@\2', url)
56
57 def _spawn_fetch(settings, args, **kwargs):
58         """
59         Spawn a process with appropriate settings for fetching, including
60         userfetch and selinux support.
61         """
62
63         global _userpriv_spawn_kwargs
64
65         # Redirect all output to stdout since some fetchers like
66         # wget pollute stderr (if portage detects a problem then it
67         # can send it's own message to stderr).
68         if "fd_pipes" not in kwargs:
69
70                 kwargs["fd_pipes"] = {
71                         0 : portage._get_stdin().fileno(),
72                         1 : sys.__stdout__.fileno(),
73                         2 : sys.__stdout__.fileno(),
74                 }
75
76         if "userfetch" in settings.features and \
77                 os.getuid() == 0 and portage_gid and portage_uid and \
78                 hasattr(os, "setgroups"):
79                 kwargs.update(_userpriv_spawn_kwargs)
80
81         spawn_func = spawn
82
83         if settings.selinux_enabled():
84                 spawn_func = selinux.spawn_wrapper(spawn_func,
85                         settings["PORTAGE_FETCH_T"])
86
87                 # bash is an allowed entrypoint, while most binaries are not
88                 if args[0] != BASH_BINARY:
89                         args = [BASH_BINARY, "-c", "exec \"$@\"", args[0]] + args
90
91         # Ensure that EBUILD_PHASE is set to fetch, so that config.environ()
92         # does not filter the calling environment (which may contain needed
93         # proxy variables, as in bug #315421).
94         phase_backup = settings.get('EBUILD_PHASE')
95         settings['EBUILD_PHASE'] = 'fetch'
96         try:
97                 rval = spawn_func(args, env=settings.environ(), **kwargs)
98         finally:
99                 if phase_backup is None:
100                         settings.pop('EBUILD_PHASE', None)
101                 else:
102                         settings['EBUILD_PHASE'] = phase_backup
103
104         return rval
105
106 _userpriv_test_write_file_cache = {}
107 _userpriv_test_write_cmd_script = ">> %(file_path)s 2>/dev/null ; rval=$? ; " + \
108         "rm -f  %(file_path)s ; exit $rval"
109
110 def _userpriv_test_write_file(settings, file_path):
111         """
112         Drop privileges and try to open a file for writing. The file may or
113         may not exist, and the parent directory is assumed to exist. The file
114         is removed before returning.
115
116         @param settings: A config instance which is passed to _spawn_fetch()
117         @param file_path: A file path to open and write.
118         @return: True if write succeeds, False otherwise.
119         """
120
121         global _userpriv_test_write_file_cache, _userpriv_test_write_cmd_script
122         rval = _userpriv_test_write_file_cache.get(file_path)
123         if rval is not None:
124                 return rval
125
126         args = [BASH_BINARY, "-c", _userpriv_test_write_cmd_script % \
127                 {"file_path" : _shell_quote(file_path)}]
128
129         returncode = _spawn_fetch(settings, args)
130
131         rval = returncode == os.EX_OK
132         _userpriv_test_write_file_cache[file_path] = rval
133         return rval
134
135 def _checksum_failure_temp_file(distdir, basename):
136         """
137         First try to find a duplicate temp file with the same checksum and return
138         that filename if available. Otherwise, use mkstemp to create a new unique
139         filename._checksum_failure_.$RANDOM, rename the given file, and return the
140         new filename. In any case, filename will be renamed or removed before this
141         function returns a temp filename.
142         """
143
144         filename = os.path.join(distdir, basename)
145         size = os.stat(filename).st_size
146         checksum = None
147         tempfile_re = re.compile(re.escape(basename) + r'\._checksum_failure_\..*')
148         for temp_filename in os.listdir(distdir):
149                 if not tempfile_re.match(temp_filename):
150                         continue
151                 temp_filename = os.path.join(distdir, temp_filename)
152                 try:
153                         if size != os.stat(temp_filename).st_size:
154                                 continue
155                 except OSError:
156                         continue
157                 try:
158                         temp_checksum = perform_md5(temp_filename)
159                 except FileNotFound:
160                         # Apparently the temp file disappeared. Let it go.
161                         continue
162                 if checksum is None:
163                         checksum = perform_md5(filename)
164                 if checksum == temp_checksum:
165                         os.unlink(filename)
166                         return temp_filename
167
168         fd, temp_filename = \
169                 tempfile.mkstemp("", basename + "._checksum_failure_.", distdir)
170         os.close(fd)
171         os.rename(filename, temp_filename)
172         return temp_filename
173
174 def _check_digests(filename, digests, show_errors=1):
175         """
176         Check digests and display a message if an error occurs.
177         @return True if all digests match, False otherwise.
178         """
179         verified_ok, reason = verify_all(filename, digests)
180         if not verified_ok:
181                 if show_errors:
182                         writemsg(_("!!! Previously fetched"
183                                 " file: '%s'\n") % filename, noiselevel=-1)
184                         writemsg(_("!!! Reason: %s\n") % reason[0],
185                                 noiselevel=-1)
186                         writemsg(_("!!! Got:      %s\n"
187                                 "!!! Expected: %s\n") % \
188                                 (reason[1], reason[2]), noiselevel=-1)
189                 return False
190         return True
191
192 def _check_distfile(filename, digests, eout, show_errors=1, hash_filter=None):
193         """
194         @return a tuple of (match, stat_obj) where match is True if filename
195         matches all given digests (if any) and stat_obj is a stat result, or
196         None if the file does not exist.
197         """
198         if digests is None:
199                 digests = {}
200         size = digests.get("size")
201         if size is not None and len(digests) == 1:
202                 digests = None
203
204         try:
205                 st = os.stat(filename)
206         except OSError:
207                 return (False, None)
208         if size is not None and size != st.st_size:
209                 return (False, st)
210         if not digests:
211                 if size is not None:
212                         eout.ebegin(_("%s size ;-)") % os.path.basename(filename))
213                         eout.eend(0)
214                 elif st.st_size == 0:
215                         # Zero-byte distfiles are always invalid.
216                         return (False, st)
217         else:
218                 digests = _filter_unaccelarated_hashes(digests)
219                 if hash_filter is not None:
220                         digests = _apply_hash_filter(digests, hash_filter)
221                 if _check_digests(filename, digests, show_errors=show_errors):
222                         eout.ebegin("%s %s ;-)" % (os.path.basename(filename),
223                                 " ".join(sorted(digests))))
224                         eout.eend(0)
225                 else:
226                         return (False, st)
227         return (True, st)
228
229 _fetch_resume_size_re = re.compile('(^[\d]+)([KMGTPEZY]?$)')
230
231 _size_suffix_map = {
232         ''  : 0,
233         'K' : 10,
234         'M' : 20,
235         'G' : 30,
236         'T' : 40,
237         'P' : 50,
238         'E' : 60,
239         'Z' : 70,
240         'Y' : 80,
241 }
242
243
244 def _get_checksum_failure_max_tries(settings, default=5):
245         """
246         Get the maximum number of failed download attempts.
247
248         Generally, downloading the same file repeatedly from
249         every single available mirror is a waste of bandwidth
250         and time, so there needs to be a cap.
251         """
252         key = 'PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS'
253         v = default
254         try:
255                 v = int(settings.get(key, default))
256         except (ValueError, OverflowError):
257                 writemsg(_("!!! Variable %s contains "
258                         "non-integer value: '%s'\n")
259                         % (key, settings[key]),
260                         noiselevel=-1)
261                 writemsg(_("!!! Using %s default value: %s\n")
262                         % (key, default),
263                         noiselevel=-1)
264                 v = default
265         if v < 1:
266                 writemsg(_("!!! Variable %s contains "
267                         "value less than 1: '%s'\n")
268                         % (key, v),
269                         noiselevel=-1)
270                 writemsg(_("!!! Using %s default value: %s\n")
271                         % (key, default),
272                         noiselevel=-1)
273                 v = default
274         return v
275
276
277 def _get_fetch_resume_size(settings, default='350K'):
278         key = 'PORTAGE_FETCH_RESUME_MIN_SIZE'
279         v = settings.get(key, default)
280         if v is not None:
281                 v = "".join(v.split())
282         if not v:
283                 # If it's empty, silently use the default.
284                 v = default
285         match = _fetch_resume_size_re.match(v)
286         if (match is None or
287                         match.group(2).upper() not in _size_suffix_map):
288                 writemsg(_("!!! Variable %s contains "
289                         "an unrecognized format: '%s'\n")
290                         % (key, settings[key]),
291                         noiselevel=-1)
292                 writemsg(_("!!! Using %s default value: %s\n")
293                         % (key, default),
294                         noiselevel=-1)
295                 v = default
296                 match = _fetch_resume_size_re.match(v)
297         v = int(match.group(1)) * \
298                 2 ** _size_suffix_map[match.group(2).upper()]
299         return v
300
301
302 def _get_file_uri_tuples(uris):
303         """Return a list of (filename, URI) tuples."""
304         file_uri_tuples = []
305         # Check for 'items' attribute since OrderedDict is not a dict.
306         if hasattr(uris, 'items'):
307                 for filename, uri_set in uris.items():
308                         for uri in uri_set:
309                                 file_uri_tuples.append((filename, uri))
310                         if not uri_set:
311                                 file_uri_tuples.append((filename, None))
312         else:
313                 for uri in uris:
314                         if urlparse(uri).scheme:
315                                 file_uri_tuples.append(
316                                         (os.path.basename(uri), uri))
317                         else:
318                                 file_uri_tuples.append(
319                                         (os.path.basename(uri), None))
320         return file_uri_tuples
321
322
323 def _expand_mirror(uri, custom_mirrors=(), third_party_mirrors=()):
324         """
325         Replace the 'mirror://' scheme and netloc in the URI.
326
327         Returns an iterable listing expanded (group, URI) tuples,
328         where the group is either 'custom' or 'third-party'.
329         """
330         parsed = urlparse(uri)
331         mirror = parsed.netloc
332         path = parsed.path
333         if path:
334                 # Try user-defined mirrors first
335                 if mirror in custom_mirrors:
336                         for cmirr in custom_mirrors[mirror]:
337                                 m_uri = urlparse(cmirr)
338                                 yield ('custom', urlunparse((
339                                         m_uri.scheme, m_uri.netloc, path) +
340                                         parsed[3:]))
341
342                 # now try the official mirrors
343                 if mirror in third_party_mirrors:
344                         uris = []
345                         for locmirr in third_party_mirrors[mirror]:
346                                 m_uri = urlparse(locmirr)
347                                 uris.append(urlunparse((
348                                         m_uri.scheme, m_uri.netloc, path) +
349                                         parsed[3:]))
350                         random.shuffle(uris)
351                         for uri in uris:
352                                 yield ('third-party', uri)
353
354                 if (not custom_mirrors.get(mirror, []) and
355                                 not third_party_mirrors.get(mirror, [])):
356                         writemsg(
357                                 _("No known mirror by the name: %s\n")
358                                 % mirror)
359         else:
360                 writemsg(_("Invalid mirror definition in SRC_URI:\n"),
361                          noiselevel=-1)
362                 writemsg("  %s\n" % uri, noiselevel=-1)
363
364
365 def _get_uris(uris, settings, custom_mirrors=(), locations=()):
366         restrict = settings.get("PORTAGE_RESTRICT", "").split()
367         restrict_fetch = "fetch" in restrict
368         restrict_mirror = "mirror" in restrict or "nomirror" in restrict
369         force_mirror = (
370                 "force-mirror" in settings.features and
371                 not restrict_mirror)
372
373         third_party_mirrors = settings.thirdpartymirrors()
374         third_party_mirror_uris = {}
375         filedict = OrderedDict()
376         primaryuri_dict = {}
377         for filename, uri in _get_file_uri_tuples(uris=uris):
378                 if filename not in filedict:
379                         filedict[filename] = [
380                                 os.path.join(location, 'distfiles', filename)
381                                 for location in locations]
382                 if uri is None:
383                         continue
384                 if uri.startswith('mirror://'):
385                         expanded_uris = _expand_mirror(
386                                 uri=uri, custom_mirrors=custom_mirrors,
387                                 third_party_mirrors=third_party_mirrors)
388                         filedict[filename].extend(
389                                 uri for _, uri in expanded_uris)
390                         third_party_mirror_uris.setdefault(filename, []).extend(
391                                 uri for group, uri in expanded_uris
392                                 if group == 'third-party')
393                 else:
394                         if restrict_fetch or force_mirror:
395                                 # Only fetch from specific mirrors is allowed.
396                                 continue
397                         primaryuris = primaryuri_dict.get(filename)
398                         if primaryuris is None:
399                                 primaryuris = []
400                                 primaryuri_dict[filename] = primaryuris
401                         primaryuris.append(uri)
402
403         # Order primaryuri_dict values to match that in SRC_URI.
404         for uris in primaryuri_dict.values():
405                 uris.reverse()
406
407         # Prefer third_party_mirrors over normal mirrors in cases when
408         # the file does not yet exist on the normal mirrors.
409         for filename, uris in third_party_mirror_uris.items():
410                 primaryuri_dict.setdefault(filename, []).extend(uris)
411
412         # Now merge primaryuri values into filedict (includes mirrors
413         # explicitly referenced in SRC_URI).
414         if "primaryuri" in restrict:
415                 for filename, uris in filedict.items():
416                         filedict[filename] = primaryuri_dict.get(filename, []) + uris
417         else:
418                 for filename in filedict:
419                         filedict[filename] += primaryuri_dict.get(filename, [])
420
421         return filedict, primaryuri_dict
422
423
424 def fetch(myuris, mysettings, listonly=0, fetchonly=0,
425         locks_in_subdir=".locks", use_locks=1, try_mirrors=1, digests=None,
426         allow_missing_digests=True):
427         "fetch files.  Will use digest file if available."
428
429         if not myuris:
430                 return 1
431
432         features = mysettings.features
433         restrict = mysettings.get("PORTAGE_RESTRICT","").split()
434
435         userfetch = secpass >= 2 and "userfetch" in features
436         userpriv = secpass >= 2 and "userpriv" in features
437
438         # 'nomirror' is bad/negative logic. You Restrict mirroring, not no-mirroring.
439         restrict_mirror = "mirror" in restrict or "nomirror" in restrict
440         if restrict_mirror:
441                 if ("mirror" in features) and ("lmirror" not in features):
442                         # lmirror should allow you to bypass mirror restrictions.
443                         # XXX: This is not a good thing, and is temporary at best.
444                         print(_(">>> \"mirror\" mode desired and \"mirror\" restriction found; skipping fetch."))
445                         return 1
446
447         checksum_failure_max_tries = _get_checksum_failure_max_tries(
448                 settings=mysettings)
449         fetch_resume_size = _get_fetch_resume_size(settings=mysettings)
450
451         # Behave like the package has RESTRICT="primaryuri" after a
452         # couple of checksum failures, to increase the probablility
453         # of success before checksum_failure_max_tries is reached.
454         checksum_failure_primaryuri = 2
455
456         # In the background parallel-fetch process, it's safe to skip checksum
457         # verification of pre-existing files in $DISTDIR that have the correct
458         # file size. The parent process will verify their checksums prior to
459         # the unpack phase.
460
461         parallel_fetchonly = "PORTAGE_PARALLEL_FETCHONLY" in mysettings
462         if parallel_fetchonly:
463                 fetchonly = 1
464
465         check_config_instance(mysettings)
466
467         custommirrors = grabdict(os.path.join(mysettings["PORTAGE_CONFIGROOT"],
468                 CUSTOM_MIRRORS_FILE), recursive=1)
469
470         mymirrors=[]
471
472         if listonly or ("distlocks" not in features):
473                 use_locks = 0
474
475         fetch_to_ro = 0
476         if "skiprocheck" in features:
477                 fetch_to_ro = 1
478
479         if not os.access(mysettings["DISTDIR"],os.W_OK) and fetch_to_ro:
480                 if use_locks:
481                         writemsg(colorize("BAD",
482                                 _("!!! For fetching to a read-only filesystem, "
483                                 "locking should be turned off.\n")), noiselevel=-1)
484                         writemsg(_("!!! This can be done by adding -distlocks to "
485                                 "FEATURES in /etc/portage/make.conf\n"), noiselevel=-1)
486 #                       use_locks = 0
487
488         # local mirrors are always added
489         if "local" in custommirrors:
490                 mymirrors += custommirrors["local"]
491
492         if restrict_mirror:
493                 # We don't add any mirrors.
494                 pass
495         else:
496                 if try_mirrors:
497                         mymirrors += [x.rstrip("/") for x in mysettings["GENTOO_MIRRORS"].split() if x]
498
499         hash_filter = _hash_filter(mysettings.get("PORTAGE_CHECKSUM_FILTER", ""))
500         if hash_filter.transparent:
501                 hash_filter = None
502         skip_manifest = mysettings.get("EBUILD_SKIP_MANIFEST") == "1"
503         if skip_manifest:
504                 allow_missing_digests = True
505         pkgdir = mysettings.get("O")
506         if digests is None and not (pkgdir is None or skip_manifest):
507                 mydigests = mysettings.repositories.get_repo_for_location(
508                         os.path.dirname(os.path.dirname(pkgdir))).load_manifest(
509                         pkgdir, mysettings["DISTDIR"]).getTypeDigests("DIST")
510         elif digests is None or skip_manifest:
511                 # no digests because fetch was not called for a specific package
512                 mydigests = {}
513         else:
514                 mydigests = digests
515
516         ro_distdirs = [x for x in \
517                 shlex_split(mysettings.get("PORTAGE_RO_DISTDIRS", "")) \
518                 if os.path.isdir(x)]
519
520         fsmirrors = []
521         for x in range(len(mymirrors)-1,-1,-1):
522                 if mymirrors[x] and mymirrors[x][0]=='/':
523                         fsmirrors += [mymirrors[x]]
524                         del mymirrors[x]
525
526         restrict_fetch = "fetch" in restrict
527         custom_local_mirrors = custommirrors.get("local", [])
528         if restrict_fetch:
529                 # With fetch restriction, a normal uri may only be fetched from
530                 # custom local mirrors (if available).  A mirror:// uri may also
531                 # be fetched from specific mirrors (effectively overriding fetch
532                 # restriction, but only for specific mirrors).
533                 locations = custom_local_mirrors
534         else:
535                 locations = mymirrors
536
537         filedict, primaryuri_dict = _get_uris(
538                 uris=myuris, settings=mysettings,
539                 custom_mirrors=custommirrors, locations=locations)
540
541         can_fetch=True
542
543         if listonly:
544                 can_fetch = False
545
546         if can_fetch and not fetch_to_ro:
547                 global _userpriv_test_write_file_cache
548                 dirmode  = 0o070
549                 filemode =   0o60
550                 modemask =    0o2
551                 dir_gid = portage_gid
552                 if "FAKED_MODE" in mysettings:
553                         # When inside fakeroot, directories with portage's gid appear
554                         # to have root's gid. Therefore, use root's gid instead of
555                         # portage's gid to avoid spurrious permissions adjustments
556                         # when inside fakeroot.
557                         dir_gid = 0
558                 distdir_dirs = [""]
559                 try:
560                         
561                         for x in distdir_dirs:
562                                 mydir = os.path.join(mysettings["DISTDIR"], x)
563                                 write_test_file = os.path.join(
564                                         mydir, ".__portage_test_write__")
565
566                                 try:
567                                         st = os.stat(mydir)
568                                 except OSError:
569                                         st = None
570
571                                 if st is not None and stat.S_ISDIR(st.st_mode):
572                                         if not (userfetch or userpriv):
573                                                 continue
574                                         if _userpriv_test_write_file(mysettings, write_test_file):
575                                                 continue
576
577                                 _userpriv_test_write_file_cache.pop(write_test_file, None)
578                                 if ensure_dirs(mydir, gid=dir_gid, mode=dirmode, mask=modemask):
579                                         if st is None:
580                                                 # The directory has just been created
581                                                 # and therefore it must be empty.
582                                                 continue
583                                         writemsg(_("Adjusting permissions recursively: '%s'\n") % mydir,
584                                                 noiselevel=-1)
585                                         def onerror(e):
586                                                 raise # bail out on the first error that occurs during recursion
587                                         if not apply_recursive_permissions(mydir,
588                                                 gid=dir_gid, dirmode=dirmode, dirmask=modemask,
589                                                 filemode=filemode, filemask=modemask, onerror=onerror):
590                                                 raise OperationNotPermitted(
591                                                         _("Failed to apply recursive permissions for the portage group."))
592                 except PortageException as e:
593                         if not os.path.isdir(mysettings["DISTDIR"]):
594                                 writemsg("!!! %s\n" % str(e), noiselevel=-1)
595                                 writemsg(_("!!! Directory Not Found: DISTDIR='%s'\n") % mysettings["DISTDIR"], noiselevel=-1)
596                                 writemsg(_("!!! Fetching will fail!\n"), noiselevel=-1)
597
598         if can_fetch and \
599                 not fetch_to_ro and \
600                 not os.access(mysettings["DISTDIR"], os.W_OK):
601                 writemsg(_("!!! No write access to '%s'\n") % mysettings["DISTDIR"],
602                         noiselevel=-1)
603                 can_fetch = False
604
605         distdir_writable = can_fetch and not fetch_to_ro
606         failed_files = set()
607         restrict_fetch_msg = False
608
609         for myfile in filedict:
610                 """
611                 fetched  status
612                 0        nonexistent
613                 1        partially downloaded
614                 2        completely downloaded
615                 """
616                 fetched = 0
617
618                 orig_digests = mydigests.get(myfile, {})
619
620                 if not (allow_missing_digests or listonly):
621                         verifiable_hash_types = set(orig_digests).intersection(hashfunc_map)
622                         verifiable_hash_types.discard("size")
623                         if not verifiable_hash_types:
624                                 expected = set(hashfunc_map)
625                                 expected.discard("size")
626                                 expected = " ".join(sorted(expected))
627                                 got = set(orig_digests)
628                                 got.discard("size")
629                                 got = " ".join(sorted(got))
630                                 reason = (_("Insufficient data for checksum verification"),
631                                         got, expected)
632                                 writemsg(_("!!! Fetched file: %s VERIFY FAILED!\n") % myfile,
633                                         noiselevel=-1)
634                                 writemsg(_("!!! Reason: %s\n") % reason[0],
635                                         noiselevel=-1)
636                                 writemsg(_("!!! Got:      %s\n!!! Expected: %s\n") % \
637                                         (reason[1], reason[2]), noiselevel=-1)
638
639                                 if fetchonly:
640                                         failed_files.add(myfile)
641                                         continue
642                                 else:
643                                         return 0
644
645                 size = orig_digests.get("size")
646                 if size == 0:
647                         # Zero-byte distfiles are always invalid, so discard their digests.
648                         del mydigests[myfile]
649                         orig_digests.clear()
650                         size = None
651                 pruned_digests = orig_digests
652                 if parallel_fetchonly:
653                         pruned_digests = {}
654                         if size is not None:
655                                 pruned_digests["size"] = size
656
657                 myfile_path = os.path.join(mysettings["DISTDIR"], myfile)
658                 has_space = True
659                 has_space_superuser = True
660                 file_lock = None
661                 if listonly:
662                         writemsg_stdout("\n", noiselevel=-1)
663                 else:
664                         # check if there is enough space in DISTDIR to completely store myfile
665                         # overestimate the filesize so we aren't bitten by FS overhead
666                         vfs_stat = None
667                         if size is not None and hasattr(os, "statvfs"):
668                                 try:
669                                         vfs_stat = os.statvfs(mysettings["DISTDIR"])
670                                 except OSError as e:
671                                         writemsg_level("!!! statvfs('%s'): %s\n" %
672                                                 (mysettings["DISTDIR"], e),
673                                                 noiselevel=-1, level=logging.ERROR)
674                                         del e
675
676                         if vfs_stat is not None:
677                                 try:
678                                         mysize = os.stat(myfile_path).st_size
679                                 except OSError as e:
680                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
681                                                 raise
682                                         del e
683                                         mysize = 0
684                                 if (size - mysize + vfs_stat.f_bsize) >= \
685                                         (vfs_stat.f_bsize * vfs_stat.f_bavail):
686
687                                         if (size - mysize + vfs_stat.f_bsize) >= \
688                                                 (vfs_stat.f_bsize * vfs_stat.f_bfree):
689                                                 has_space_superuser = False
690
691                                         if not has_space_superuser:
692                                                 has_space = False
693                                         elif secpass < 2:
694                                                 has_space = False
695                                         elif userfetch:
696                                                 has_space = False
697
698                         if distdir_writable and use_locks:
699
700                                 lock_kwargs = {}
701                                 if fetchonly:
702                                         lock_kwargs["flags"] = os.O_NONBLOCK
703
704                                 try:
705                                         file_lock = lockfile(myfile_path,
706                                                 wantnewlockfile=1, **lock_kwargs)
707                                 except TryAgain:
708                                         writemsg(_(">>> File '%s' is already locked by "
709                                                 "another fetcher. Continuing...\n") % myfile,
710                                                 noiselevel=-1)
711                                         continue
712                 try:
713                         if not listonly:
714
715                                 eout = EOutput()
716                                 eout.quiet = mysettings.get("PORTAGE_QUIET") == "1"
717                                 match, mystat = _check_distfile(
718                                         myfile_path, pruned_digests, eout, hash_filter=hash_filter)
719                                 if match:
720                                         # Skip permission adjustment for symlinks, since we don't
721                                         # want to modify anything outside of the primary DISTDIR,
722                                         # and symlinks typically point to PORTAGE_RO_DISTDIRS.
723                                         if distdir_writable and not os.path.islink(myfile_path):
724                                                 try:
725                                                         apply_secpass_permissions(myfile_path,
726                                                                 gid=portage_gid, mode=0o664, mask=0o2,
727                                                                 stat_cached=mystat)
728                                                 except PortageException as e:
729                                                         if not os.access(myfile_path, os.R_OK):
730                                                                 writemsg(_("!!! Failed to adjust permissions:"
731                                                                         " %s\n") % str(e), noiselevel=-1)
732                                                         del e
733                                         continue
734
735                                 if distdir_writable and mystat is None:
736                                         # Remove broken symlinks if necessary.
737                                         try:
738                                                 os.unlink(myfile_path)
739                                         except OSError:
740                                                 pass
741
742                                 if mystat is not None:
743                                         if stat.S_ISDIR(mystat.st_mode):
744                                                 writemsg_level(
745                                                         _("!!! Unable to fetch file since "
746                                                         "a directory is in the way: \n"
747                                                         "!!!   %s\n") % myfile_path,
748                                                         level=logging.ERROR, noiselevel=-1)
749                                                 return 0
750
751                                         if mystat.st_size == 0:
752                                                 if distdir_writable:
753                                                         try:
754                                                                 os.unlink(myfile_path)
755                                                         except OSError:
756                                                                 pass
757                                         elif distdir_writable:
758                                                 if mystat.st_size < fetch_resume_size and \
759                                                         mystat.st_size < size:
760                                                         # If the file already exists and the size does not
761                                                         # match the existing digests, it may be that the
762                                                         # user is attempting to update the digest. In this
763                                                         # case, the digestgen() function will advise the
764                                                         # user to use `ebuild --force foo.ebuild manifest`
765                                                         # in order to force the old digests to be replaced.
766                                                         # Since the user may want to keep this file, rename
767                                                         # it instead of deleting it.
768                                                         writemsg(_(">>> Renaming distfile with size "
769                                                                 "%d (smaller than " "PORTAGE_FETCH_RESU"
770                                                                 "ME_MIN_SIZE)\n") % mystat.st_size)
771                                                         temp_filename = \
772                                                                 _checksum_failure_temp_file(
773                                                                 mysettings["DISTDIR"], myfile)
774                                                         writemsg_stdout(_("Refetching... "
775                                                                 "File renamed to '%s'\n\n") % \
776                                                                 temp_filename, noiselevel=-1)
777                                                 elif mystat.st_size >= size:
778                                                         temp_filename = \
779                                                                 _checksum_failure_temp_file(
780                                                                 mysettings["DISTDIR"], myfile)
781                                                         writemsg_stdout(_("Refetching... "
782                                                                 "File renamed to '%s'\n\n") % \
783                                                                 temp_filename, noiselevel=-1)
784
785                                 if distdir_writable and ro_distdirs:
786                                         readonly_file = None
787                                         for x in ro_distdirs:
788                                                 filename = os.path.join(x, myfile)
789                                                 match, mystat = _check_distfile(
790                                                         filename, pruned_digests, eout, hash_filter=hash_filter)
791                                                 if match:
792                                                         readonly_file = filename
793                                                         break
794                                         if readonly_file is not None:
795                                                 try:
796                                                         os.unlink(myfile_path)
797                                                 except OSError as e:
798                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
799                                                                 raise
800                                                         del e
801                                                 os.symlink(readonly_file, myfile_path)
802                                                 continue
803
804                                 # this message is shown only after we know that
805                                 # the file is not already fetched
806                                 if not has_space:
807                                         writemsg(_("!!! Insufficient space to store %s in %s\n") % \
808                                                 (myfile, mysettings["DISTDIR"]), noiselevel=-1)
809
810                                         if has_space_superuser:
811                                                 writemsg(_("!!! Insufficient privileges to use "
812                                                         "remaining space.\n"), noiselevel=-1)
813                                                 if userfetch:
814                                                         writemsg(_("!!! You may set FEATURES=\"-userfetch\""
815                                                                 " in /etc/portage/make.conf in order to fetch with\n"
816                                                                 "!!! superuser privileges.\n"), noiselevel=-1)
817
818                                 if fsmirrors and not os.path.exists(myfile_path) and has_space:
819                                         for mydir in fsmirrors:
820                                                 mirror_file = os.path.join(mydir, myfile)
821                                                 try:
822                                                         shutil.copyfile(mirror_file, myfile_path)
823                                                         writemsg(_("Local mirror has file: %s\n") % myfile)
824                                                         break
825                                                 except (IOError, OSError) as e:
826                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
827                                                                 raise
828                                                         del e
829
830                                 try:
831                                         mystat = os.stat(myfile_path)
832                                 except OSError as e:
833                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
834                                                 raise
835                                         del e
836                                 else:
837                                         # Skip permission adjustment for symlinks, since we don't
838                                         # want to modify anything outside of the primary DISTDIR,
839                                         # and symlinks typically point to PORTAGE_RO_DISTDIRS.
840                                         if not os.path.islink(myfile_path):
841                                                 try:
842                                                         apply_secpass_permissions(myfile_path,
843                                                                 gid=portage_gid, mode=0o664, mask=0o2,
844                                                                 stat_cached=mystat)
845                                                 except PortageException as e:
846                                                         if not os.access(myfile_path, os.R_OK):
847                                                                 writemsg(_("!!! Failed to adjust permissions:"
848                                                                         " %s\n") % (e,), noiselevel=-1)
849
850                                         # If the file is empty then it's obviously invalid. Remove
851                                         # the empty file and try to download if possible.
852                                         if mystat.st_size == 0:
853                                                 if distdir_writable:
854                                                         try:
855                                                                 os.unlink(myfile_path)
856                                                         except EnvironmentError:
857                                                                 pass
858                                         elif myfile not in mydigests:
859                                                 # We don't have a digest, but the file exists.  We must
860                                                 # assume that it is fully downloaded.
861                                                 continue
862                                         else:
863                                                 if mystat.st_size < mydigests[myfile]["size"] and \
864                                                         not restrict_fetch:
865                                                         fetched = 1 # Try to resume this download.
866                                                 elif parallel_fetchonly and \
867                                                         mystat.st_size == mydigests[myfile]["size"]:
868                                                         eout = EOutput()
869                                                         eout.quiet = \
870                                                                 mysettings.get("PORTAGE_QUIET") == "1"
871                                                         eout.ebegin(
872                                                                 "%s size ;-)" % (myfile, ))
873                                                         eout.eend(0)
874                                                         continue
875                                                 else:
876                                                         digests = _filter_unaccelarated_hashes(mydigests[myfile])
877                                                         if hash_filter is not None:
878                                                                 digests = _apply_hash_filter(digests, hash_filter)
879                                                         verified_ok, reason = verify_all(myfile_path, digests)
880                                                         if not verified_ok:
881                                                                 writemsg(_("!!! Previously fetched"
882                                                                         " file: '%s'\n") % myfile, noiselevel=-1)
883                                                                 writemsg(_("!!! Reason: %s\n") % reason[0],
884                                                                         noiselevel=-1)
885                                                                 writemsg(_("!!! Got:      %s\n"
886                                                                         "!!! Expected: %s\n") % \
887                                                                         (reason[1], reason[2]), noiselevel=-1)
888                                                                 if reason[0] == _("Insufficient data for checksum verification"):
889                                                                         return 0
890                                                                 if distdir_writable:
891                                                                         temp_filename = \
892                                                                                 _checksum_failure_temp_file(
893                                                                                 mysettings["DISTDIR"], myfile)
894                                                                         writemsg_stdout(_("Refetching... "
895                                                                                 "File renamed to '%s'\n\n") % \
896                                                                                 temp_filename, noiselevel=-1)
897                                                         else:
898                                                                 eout = EOutput()
899                                                                 eout.quiet = \
900                                                                         mysettings.get("PORTAGE_QUIET", None) == "1"
901                                                                 if digests:
902                                                                         digests = list(digests)
903                                                                         digests.sort()
904                                                                         eout.ebegin(
905                                                                                 "%s %s ;-)" % (myfile, " ".join(digests)))
906                                                                         eout.eend(0)
907                                                                 continue # fetch any remaining files
908
909                         # Create a reversed list since that is optimal for list.pop().
910                         uri_list = filedict[myfile][:]
911                         uri_list.reverse()
912                         checksum_failure_count = 0
913                         tried_locations = set()
914                         while uri_list:
915                                 loc = uri_list.pop()
916                                 # Eliminate duplicates here in case we've switched to
917                                 # "primaryuri" mode on the fly due to a checksum failure.
918                                 if loc in tried_locations:
919                                         continue
920                                 tried_locations.add(loc)
921                                 if listonly:
922                                         writemsg_stdout(loc+" ", noiselevel=-1)
923                                         continue
924                                 # allow different fetchcommands per protocol
925                                 protocol = loc[0:loc.find("://")]
926
927                                 global_config_path = GLOBAL_CONFIG_PATH
928                                 if portage.const.EPREFIX:
929                                         global_config_path = os.path.join(portage.const.EPREFIX,
930                                                         GLOBAL_CONFIG_PATH.lstrip(os.sep))
931
932                                 missing_file_param = False
933                                 fetchcommand_var = "FETCHCOMMAND_" + protocol.upper()
934                                 fetchcommand = mysettings.get(fetchcommand_var)
935                                 if fetchcommand is None:
936                                         fetchcommand_var = "FETCHCOMMAND"
937                                         fetchcommand = mysettings.get(fetchcommand_var)
938                                         if fetchcommand is None:
939                                                 writemsg_level(
940                                                         _("!!! %s is unset. It should "
941                                                         "have been defined in\n!!! %s/make.globals.\n") \
942                                                         % (fetchcommand_var, global_config_path),
943                                                         level=logging.ERROR, noiselevel=-1)
944                                                 return 0
945                                 if "${FILE}" not in fetchcommand:
946                                         writemsg_level(
947                                                 _("!!! %s does not contain the required ${FILE}"
948                                                 " parameter.\n") % fetchcommand_var,
949                                                 level=logging.ERROR, noiselevel=-1)
950                                         missing_file_param = True
951
952                                 resumecommand_var = "RESUMECOMMAND_" + protocol.upper()
953                                 resumecommand = mysettings.get(resumecommand_var)
954                                 if resumecommand is None:
955                                         resumecommand_var = "RESUMECOMMAND"
956                                         resumecommand = mysettings.get(resumecommand_var)
957                                         if resumecommand is None:
958                                                 writemsg_level(
959                                                         _("!!! %s is unset. It should "
960                                                         "have been defined in\n!!! %s/make.globals.\n") \
961                                                         % (resumecommand_var, global_config_path),
962                                                         level=logging.ERROR, noiselevel=-1)
963                                                 return 0
964                                 if "${FILE}" not in resumecommand:
965                                         writemsg_level(
966                                                 _("!!! %s does not contain the required ${FILE}"
967                                                 " parameter.\n") % resumecommand_var,
968                                                 level=logging.ERROR, noiselevel=-1)
969                                         missing_file_param = True
970
971                                 if missing_file_param:
972                                         writemsg_level(
973                                                 _("!!! Refer to the make.conf(5) man page for "
974                                                 "information about how to\n!!! correctly specify "
975                                                 "FETCHCOMMAND and RESUMECOMMAND.\n"),
976                                                 level=logging.ERROR, noiselevel=-1)
977                                         if myfile != os.path.basename(loc):
978                                                 return 0
979
980                                 if not can_fetch:
981                                         if fetched != 2:
982                                                 try:
983                                                         mysize = os.stat(myfile_path).st_size
984                                                 except OSError as e:
985                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
986                                                                 raise
987                                                         del e
988                                                         mysize = 0
989
990                                                 if mysize == 0:
991                                                         writemsg(_("!!! File %s isn't fetched but unable to get it.\n") % myfile,
992                                                                 noiselevel=-1)
993                                                 elif size is None or size > mysize:
994                                                         writemsg(_("!!! File %s isn't fully fetched, but unable to complete it\n") % myfile,
995                                                                 noiselevel=-1)
996                                                 else:
997                                                         writemsg(_("!!! File %s is incorrect size, "
998                                                                 "but unable to retry.\n") % myfile, noiselevel=-1)
999                                                 return 0
1000                                         else:
1001                                                 continue
1002
1003                                 if fetched != 2 and has_space:
1004                                         #we either need to resume or start the download
1005                                         if fetched == 1:
1006                                                 try:
1007                                                         mystat = os.stat(myfile_path)
1008                                                 except OSError as e:
1009                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
1010                                                                 raise
1011                                                         del e
1012                                                         fetched = 0
1013                                                 else:
1014                                                         if mystat.st_size < fetch_resume_size:
1015                                                                 writemsg(_(">>> Deleting distfile with size "
1016                                                                         "%d (smaller than " "PORTAGE_FETCH_RESU"
1017                                                                         "ME_MIN_SIZE)\n") % mystat.st_size)
1018                                                                 try:
1019                                                                         os.unlink(myfile_path)
1020                                                                 except OSError as e:
1021                                                                         if e.errno not in \
1022                                                                                 (errno.ENOENT, errno.ESTALE):
1023                                                                                 raise
1024                                                                         del e
1025                                                                 fetched = 0
1026                                         if fetched == 1:
1027                                                 #resume mode:
1028                                                 writemsg(_(">>> Resuming download...\n"))
1029                                                 locfetch=resumecommand
1030                                                 command_var = resumecommand_var
1031                                         else:
1032                                                 #normal mode:
1033                                                 locfetch=fetchcommand
1034                                                 command_var = fetchcommand_var
1035                                         writemsg_stdout(_(">>> Downloading '%s'\n") % \
1036                                                 _hide_url_passwd(loc))
1037                                         variables = {
1038                                                 "URI":     loc,
1039                                                 "FILE":    myfile
1040                                         }
1041
1042                                         for k in ("DISTDIR", "PORTAGE_SSH_OPTS"):
1043                                                 try:
1044                                                         variables[k] = mysettings[k]
1045                                                 except KeyError:
1046                                                         pass
1047
1048                                         myfetch = shlex_split(locfetch)
1049                                         myfetch = [varexpand(x, mydict=variables) for x in myfetch]
1050                                         myret = -1
1051                                         try:
1052
1053                                                 myret = _spawn_fetch(mysettings, myfetch)
1054
1055                                         finally:
1056                                                 try:
1057                                                         apply_secpass_permissions(myfile_path,
1058                                                                 gid=portage_gid, mode=0o664, mask=0o2)
1059                                                 except FileNotFound:
1060                                                         pass
1061                                                 except PortageException as e:
1062                                                         if not os.access(myfile_path, os.R_OK):
1063                                                                 writemsg(_("!!! Failed to adjust permissions:"
1064                                                                         " %s\n") % str(e), noiselevel=-1)
1065                                                         del e
1066
1067                                         # If the file is empty then it's obviously invalid.  Don't
1068                                         # trust the return value from the fetcher.  Remove the
1069                                         # empty file and try to download again.
1070                                         try:
1071                                                 if os.stat(myfile_path).st_size == 0:
1072                                                         os.unlink(myfile_path)
1073                                                         fetched = 0
1074                                                         continue
1075                                         except EnvironmentError:
1076                                                 pass
1077
1078                                         if mydigests is not None and myfile in mydigests:
1079                                                 try:
1080                                                         mystat = os.stat(myfile_path)
1081                                                 except OSError as e:
1082                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
1083                                                                 raise
1084                                                         del e
1085                                                         fetched = 0
1086                                                 else:
1087
1088                                                         if stat.S_ISDIR(mystat.st_mode):
1089                                                                 # This can happen if FETCHCOMMAND erroneously
1090                                                                 # contains wget's -P option where it should
1091                                                                 # instead have -O.
1092                                                                 writemsg_level(
1093                                                                         _("!!! The command specified in the "
1094                                                                         "%s variable appears to have\n!!! "
1095                                                                         "created a directory instead of a "
1096                                                                         "normal file.\n") % command_var,
1097                                                                         level=logging.ERROR, noiselevel=-1)
1098                                                                 writemsg_level(
1099                                                                         _("!!! Refer to the make.conf(5) "
1100                                                                         "man page for information about how "
1101                                                                         "to\n!!! correctly specify "
1102                                                                         "FETCHCOMMAND and RESUMECOMMAND.\n"),
1103                                                                         level=logging.ERROR, noiselevel=-1)
1104                                                                 return 0
1105
1106                                                         # no exception?  file exists. let digestcheck() report
1107                                                         # an appropriately for size or checksum errors
1108
1109                                                         # If the fetcher reported success and the file is
1110                                                         # too small, it's probably because the digest is
1111                                                         # bad (upstream changed the distfile).  In this
1112                                                         # case we don't want to attempt to resume. Show a
1113                                                         # digest verification failure to that the user gets
1114                                                         # a clue about what just happened.
1115                                                         if myret != os.EX_OK and \
1116                                                                 mystat.st_size < mydigests[myfile]["size"]:
1117                                                                 # Fetch failed... Try the next one... Kill 404 files though.
1118                                                                 if (mystat[stat.ST_SIZE]<100000) and (len(myfile)>4) and not ((myfile[-5:]==".html") or (myfile[-4:]==".htm")):
1119                                                                         html404=re.compile("<title>.*(not found|404).*</title>",re.I|re.M)
1120                                                                         with io.open(
1121                                                                                 _unicode_encode(myfile_path,
1122                                                                                 encoding=_encodings['fs'], errors='strict'),
1123                                                                                 mode='r', encoding=_encodings['content'], errors='replace'
1124                                                                                 ) as f:
1125                                                                                 if html404.search(f.read()):
1126                                                                                         try:
1127                                                                                                 os.unlink(mysettings["DISTDIR"]+"/"+myfile)
1128                                                                                                 writemsg(_(">>> Deleting invalid distfile. (Improper 404 redirect from server.)\n"))
1129                                                                                                 fetched = 0
1130                                                                                                 continue
1131                                                                                         except (IOError, OSError):
1132                                                                                                 pass
1133                                                                 fetched = 1
1134                                                                 continue
1135                                                         if True:
1136                                                                 # File is the correct size--check the checksums for the fetched
1137                                                                 # file NOW, for those users who don't have a stable/continuous
1138                                                                 # net connection. This way we have a chance to try to download
1139                                                                 # from another mirror...
1140                                                                 digests = _filter_unaccelarated_hashes(mydigests[myfile])
1141                                                                 if hash_filter is not None:
1142                                                                         digests = _apply_hash_filter(digests, hash_filter)
1143                                                                 verified_ok, reason = verify_all(myfile_path, digests)
1144                                                                 if not verified_ok:
1145                                                                         writemsg(_("!!! Fetched file: %s VERIFY FAILED!\n") % myfile,
1146                                                                                 noiselevel=-1)
1147                                                                         writemsg(_("!!! Reason: %s\n") % reason[0],
1148                                                                                 noiselevel=-1)
1149                                                                         writemsg(_("!!! Got:      %s\n!!! Expected: %s\n") % \
1150                                                                                 (reason[1], reason[2]), noiselevel=-1)
1151                                                                         if reason[0] == _("Insufficient data for checksum verification"):
1152                                                                                 return 0
1153                                                                         temp_filename = \
1154                                                                                 _checksum_failure_temp_file(
1155                                                                                 mysettings["DISTDIR"], myfile)
1156                                                                         writemsg_stdout(_("Refetching... "
1157                                                                                 "File renamed to '%s'\n\n") % \
1158                                                                                 temp_filename, noiselevel=-1)
1159                                                                         fetched=0
1160                                                                         checksum_failure_count += 1
1161                                                                         if checksum_failure_count == \
1162                                                                                 checksum_failure_primaryuri:
1163                                                                                 # Switch to "primaryuri" mode in order
1164                                                                                 # to increase the probablility of
1165                                                                                 # of success.
1166                                                                                 primaryuris = \
1167                                                                                         primaryuri_dict.get(myfile)
1168                                                                                 if primaryuris:
1169                                                                                         uri_list.extend(
1170                                                                                                 reversed(primaryuris))
1171                                                                         if checksum_failure_count >= \
1172                                                                                 checksum_failure_max_tries:
1173                                                                                 break
1174                                                                 else:
1175                                                                         eout = EOutput()
1176                                                                         eout.quiet = mysettings.get("PORTAGE_QUIET", None) == "1"
1177                                                                         if digests:
1178                                                                                 eout.ebegin("%s %s ;-)" % \
1179                                                                                         (myfile, " ".join(sorted(digests))))
1180                                                                                 eout.eend(0)
1181                                                                         fetched=2
1182                                                                         break
1183                                         else:
1184                                                 if not myret:
1185                                                         fetched=2
1186                                                         break
1187                                                 elif mydigests!=None:
1188                                                         writemsg(_("No digest file available and download failed.\n\n"),
1189                                                                 noiselevel=-1)
1190                 finally:
1191                         if use_locks and file_lock:
1192                                 unlockfile(file_lock)
1193                                 file_lock = None
1194
1195                 if listonly:
1196                         writemsg_stdout("\n", noiselevel=-1)
1197                 if fetched != 2:
1198                         if restrict_fetch and not restrict_fetch_msg:
1199                                 restrict_fetch_msg = True
1200                                 msg = _("\n!!! %s/%s"
1201                                         " has fetch restriction turned on.\n"
1202                                         "!!! This probably means that this "
1203                                         "ebuild's files must be downloaded\n"
1204                                         "!!! manually.  See the comments in"
1205                                         " the ebuild for more information.\n\n") % \
1206                                         (mysettings["CATEGORY"], mysettings["PF"])
1207                                 writemsg_level(msg,
1208                                         level=logging.ERROR, noiselevel=-1)
1209                         elif restrict_fetch:
1210                                 pass
1211                         elif listonly:
1212                                 pass
1213                         elif not filedict[myfile]:
1214                                 writemsg(_("Warning: No mirrors available for file"
1215                                         " '%s'\n") % (myfile), noiselevel=-1)
1216                         else:
1217                                 writemsg(_("!!! Couldn't download '%s'. Aborting.\n") % myfile,
1218                                         noiselevel=-1)
1219
1220                         if listonly:
1221                                 failed_files.add(myfile)
1222                                 continue
1223                         elif fetchonly:
1224                                 failed_files.add(myfile)
1225                                 continue
1226                         return 0
1227         if failed_files:
1228                 return 0
1229         return 1