Refer to /etc/portage/make.conf in messages.
[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
19 except ImportError:
20         from urlparse import urlparse
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 def fetch(myuris, mysettings, listonly=0, fetchonly=0,
244         locks_in_subdir=".locks", use_locks=1, try_mirrors=1, digests=None,
245         allow_missing_digests=True):
246         "fetch files.  Will use digest file if available."
247
248         if not myuris:
249                 return 1
250
251         features = mysettings.features
252         restrict = mysettings.get("PORTAGE_RESTRICT","").split()
253
254         userfetch = secpass >= 2 and "userfetch" in features
255         userpriv = secpass >= 2 and "userpriv" in features
256
257         # 'nomirror' is bad/negative logic. You Restrict mirroring, not no-mirroring.
258         restrict_mirror = "mirror" in restrict or "nomirror" in restrict
259         if restrict_mirror:
260                 if ("mirror" in features) and ("lmirror" not in features):
261                         # lmirror should allow you to bypass mirror restrictions.
262                         # XXX: This is not a good thing, and is temporary at best.
263                         print(_(">>> \"mirror\" mode desired and \"mirror\" restriction found; skipping fetch."))
264                         return 1
265
266         # Generally, downloading the same file repeatedly from
267         # every single available mirror is a waste of bandwidth
268         # and time, so there needs to be a cap.
269         checksum_failure_max_tries = 5
270         v = checksum_failure_max_tries
271         try:
272                 v = int(mysettings.get("PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS",
273                         checksum_failure_max_tries))
274         except (ValueError, OverflowError):
275                 writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
276                         " contains non-integer value: '%s'\n") % \
277                         mysettings["PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"], noiselevel=-1)
278                 writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
279                         "default value: %s\n") % checksum_failure_max_tries,
280                         noiselevel=-1)
281                 v = checksum_failure_max_tries
282         if v < 1:
283                 writemsg(_("!!! Variable PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS"
284                         " contains value less than 1: '%s'\n") % v, noiselevel=-1)
285                 writemsg(_("!!! Using PORTAGE_FETCH_CHECKSUM_TRY_MIRRORS "
286                         "default value: %s\n") % checksum_failure_max_tries,
287                         noiselevel=-1)
288                 v = checksum_failure_max_tries
289         checksum_failure_max_tries = v
290         del v
291
292         fetch_resume_size_default = "350K"
293         fetch_resume_size = mysettings.get("PORTAGE_FETCH_RESUME_MIN_SIZE")
294         if fetch_resume_size is not None:
295                 fetch_resume_size = "".join(fetch_resume_size.split())
296                 if not fetch_resume_size:
297                         # If it's undefined or empty, silently use the default.
298                         fetch_resume_size = fetch_resume_size_default
299                 match = _fetch_resume_size_re.match(fetch_resume_size)
300                 if match is None or \
301                         (match.group(2).upper() not in _size_suffix_map):
302                         writemsg(_("!!! Variable PORTAGE_FETCH_RESUME_MIN_SIZE"
303                                 " contains an unrecognized format: '%s'\n") % \
304                                 mysettings["PORTAGE_FETCH_RESUME_MIN_SIZE"], noiselevel=-1)
305                         writemsg(_("!!! Using PORTAGE_FETCH_RESUME_MIN_SIZE "
306                                 "default value: %s\n") % fetch_resume_size_default,
307                                 noiselevel=-1)
308                         fetch_resume_size = None
309         if fetch_resume_size is None:
310                 fetch_resume_size = fetch_resume_size_default
311                 match = _fetch_resume_size_re.match(fetch_resume_size)
312         fetch_resume_size = int(match.group(1)) * \
313                 2 ** _size_suffix_map[match.group(2).upper()]
314
315         # Behave like the package has RESTRICT="primaryuri" after a
316         # couple of checksum failures, to increase the probablility
317         # of success before checksum_failure_max_tries is reached.
318         checksum_failure_primaryuri = 2
319         thirdpartymirrors = mysettings.thirdpartymirrors()
320
321         # In the background parallel-fetch process, it's safe to skip checksum
322         # verification of pre-existing files in $DISTDIR that have the correct
323         # file size. The parent process will verify their checksums prior to
324         # the unpack phase.
325
326         parallel_fetchonly = "PORTAGE_PARALLEL_FETCHONLY" in mysettings
327         if parallel_fetchonly:
328                 fetchonly = 1
329
330         check_config_instance(mysettings)
331
332         custommirrors = grabdict(os.path.join(mysettings["PORTAGE_CONFIGROOT"],
333                 CUSTOM_MIRRORS_FILE), recursive=1)
334
335         mymirrors=[]
336
337         if listonly or ("distlocks" not in features):
338                 use_locks = 0
339
340         fetch_to_ro = 0
341         if "skiprocheck" in features:
342                 fetch_to_ro = 1
343
344         if not os.access(mysettings["DISTDIR"],os.W_OK) and fetch_to_ro:
345                 if use_locks:
346                         writemsg(colorize("BAD",
347                                 _("!!! For fetching to a read-only filesystem, "
348                                 "locking should be turned off.\n")), noiselevel=-1)
349                         writemsg(_("!!! This can be done by adding -distlocks to "
350                                 "FEATURES in /etc/portage/make.conf\n"), noiselevel=-1)
351 #                       use_locks = 0
352
353         # local mirrors are always added
354         if "local" in custommirrors:
355                 mymirrors += custommirrors["local"]
356
357         if restrict_mirror:
358                 # We don't add any mirrors.
359                 pass
360         else:
361                 if try_mirrors:
362                         mymirrors += [x.rstrip("/") for x in mysettings["GENTOO_MIRRORS"].split() if x]
363
364         hash_filter = _hash_filter(mysettings.get("PORTAGE_CHECKSUM_FILTER", ""))
365         if hash_filter.transparent:
366                 hash_filter = None
367         skip_manifest = mysettings.get("EBUILD_SKIP_MANIFEST") == "1"
368         if skip_manifest:
369                 allow_missing_digests = True
370         pkgdir = mysettings.get("O")
371         if digests is None and not (pkgdir is None or skip_manifest):
372                 mydigests = mysettings.repositories.get_repo_for_location(
373                         os.path.dirname(os.path.dirname(pkgdir))).load_manifest(
374                         pkgdir, mysettings["DISTDIR"]).getTypeDigests("DIST")
375         elif digests is None or skip_manifest:
376                 # no digests because fetch was not called for a specific package
377                 mydigests = {}
378         else:
379                 mydigests = digests
380
381         ro_distdirs = [x for x in \
382                 shlex_split(mysettings.get("PORTAGE_RO_DISTDIRS", "")) \
383                 if os.path.isdir(x)]
384
385         fsmirrors = []
386         for x in range(len(mymirrors)-1,-1,-1):
387                 if mymirrors[x] and mymirrors[x][0]=='/':
388                         fsmirrors += [mymirrors[x]]
389                         del mymirrors[x]
390
391         restrict_fetch = "fetch" in restrict
392         force_mirror = "force-mirror" in features and not restrict_mirror
393         custom_local_mirrors = custommirrors.get("local", [])
394         if restrict_fetch:
395                 # With fetch restriction, a normal uri may only be fetched from
396                 # custom local mirrors (if available).  A mirror:// uri may also
397                 # be fetched from specific mirrors (effectively overriding fetch
398                 # restriction, but only for specific mirrors).
399                 locations = custom_local_mirrors
400         else:
401                 locations = mymirrors
402
403         file_uri_tuples = []
404         # Check for 'items' attribute since OrderedDict is not a dict.
405         if hasattr(myuris, 'items'):
406                 for myfile, uri_set in myuris.items():
407                         for myuri in uri_set:
408                                 file_uri_tuples.append((myfile, myuri))
409                         if not uri_set:
410                                 file_uri_tuples.append((myfile, None))
411         else:
412                 for myuri in myuris:
413                         if urlparse(myuri).scheme:
414                                 file_uri_tuples.append((os.path.basename(myuri), myuri))
415                         else:
416                                 file_uri_tuples.append((os.path.basename(myuri), None))
417
418         filedict = OrderedDict()
419         primaryuri_dict = {}
420         thirdpartymirror_uris = {}
421         for myfile, myuri in file_uri_tuples:
422                 if myfile not in filedict:
423                         filedict[myfile]=[]
424                         for y in range(0,len(locations)):
425                                 filedict[myfile].append(locations[y]+"/distfiles/"+myfile)
426                 if myuri is None:
427                         continue
428                 if myuri[:9]=="mirror://":
429                         eidx = myuri.find("/", 9)
430                         if eidx != -1:
431                                 mirrorname = myuri[9:eidx]
432                                 path = myuri[eidx+1:]
433
434                                 # Try user-defined mirrors first
435                                 if mirrorname in custommirrors:
436                                         for cmirr in custommirrors[mirrorname]:
437                                                 filedict[myfile].append(
438                                                         cmirr.rstrip("/") + "/" + path)
439
440                                 # now try the official mirrors
441                                 if mirrorname in thirdpartymirrors:
442                                         uris = [locmirr.rstrip("/") + "/" + path \
443                                                 for locmirr in thirdpartymirrors[mirrorname]]
444                                         random.shuffle(uris)
445                                         filedict[myfile].extend(uris)
446                                         thirdpartymirror_uris.setdefault(myfile, []).extend(uris)
447
448                                 if not filedict[myfile]:
449                                         writemsg(_("No known mirror by the name: %s\n") % (mirrorname))
450                         else:
451                                 writemsg(_("Invalid mirror definition in SRC_URI:\n"), noiselevel=-1)
452                                 writemsg("  %s\n" % (myuri), noiselevel=-1)
453                 else:
454                         if restrict_fetch or force_mirror:
455                                 # Only fetch from specific mirrors is allowed.
456                                 continue
457                         primaryuris = primaryuri_dict.get(myfile)
458                         if primaryuris is None:
459                                 primaryuris = []
460                                 primaryuri_dict[myfile] = primaryuris
461                         primaryuris.append(myuri)
462
463         # Order primaryuri_dict values to match that in SRC_URI.
464         for uris in primaryuri_dict.values():
465                 uris.reverse()
466
467         # Prefer thirdpartymirrors over normal mirrors in cases when
468         # the file does not yet exist on the normal mirrors.
469         for myfile, uris in thirdpartymirror_uris.items():
470                 primaryuri_dict.setdefault(myfile, []).extend(uris)
471
472         # Now merge primaryuri values into filedict (includes mirrors
473         # explicitly referenced in SRC_URI).
474         if "primaryuri" in restrict:
475                 for myfile, uris in filedict.items():
476                         filedict[myfile] = primaryuri_dict.get(myfile, []) + uris
477         else:
478                 for myfile in filedict:
479                         filedict[myfile] += primaryuri_dict.get(myfile, [])
480
481         can_fetch=True
482
483         if listonly:
484                 can_fetch = False
485
486         if can_fetch and not fetch_to_ro:
487                 global _userpriv_test_write_file_cache
488                 dirmode  = 0o070
489                 filemode =   0o60
490                 modemask =    0o2
491                 dir_gid = portage_gid
492                 if "FAKED_MODE" in mysettings:
493                         # When inside fakeroot, directories with portage's gid appear
494                         # to have root's gid. Therefore, use root's gid instead of
495                         # portage's gid to avoid spurrious permissions adjustments
496                         # when inside fakeroot.
497                         dir_gid = 0
498                 distdir_dirs = [""]
499                 try:
500                         
501                         for x in distdir_dirs:
502                                 mydir = os.path.join(mysettings["DISTDIR"], x)
503                                 write_test_file = os.path.join(
504                                         mydir, ".__portage_test_write__")
505
506                                 try:
507                                         st = os.stat(mydir)
508                                 except OSError:
509                                         st = None
510
511                                 if st is not None and stat.S_ISDIR(st.st_mode):
512                                         if not (userfetch or userpriv):
513                                                 continue
514                                         if _userpriv_test_write_file(mysettings, write_test_file):
515                                                 continue
516
517                                 _userpriv_test_write_file_cache.pop(write_test_file, None)
518                                 if ensure_dirs(mydir, gid=dir_gid, mode=dirmode, mask=modemask):
519                                         if st is None:
520                                                 # The directory has just been created
521                                                 # and therefore it must be empty.
522                                                 continue
523                                         writemsg(_("Adjusting permissions recursively: '%s'\n") % mydir,
524                                                 noiselevel=-1)
525                                         def onerror(e):
526                                                 raise # bail out on the first error that occurs during recursion
527                                         if not apply_recursive_permissions(mydir,
528                                                 gid=dir_gid, dirmode=dirmode, dirmask=modemask,
529                                                 filemode=filemode, filemask=modemask, onerror=onerror):
530                                                 raise OperationNotPermitted(
531                                                         _("Failed to apply recursive permissions for the portage group."))
532                 except PortageException as e:
533                         if not os.path.isdir(mysettings["DISTDIR"]):
534                                 writemsg("!!! %s\n" % str(e), noiselevel=-1)
535                                 writemsg(_("!!! Directory Not Found: DISTDIR='%s'\n") % mysettings["DISTDIR"], noiselevel=-1)
536                                 writemsg(_("!!! Fetching will fail!\n"), noiselevel=-1)
537
538         if can_fetch and \
539                 not fetch_to_ro and \
540                 not os.access(mysettings["DISTDIR"], os.W_OK):
541                 writemsg(_("!!! No write access to '%s'\n") % mysettings["DISTDIR"],
542                         noiselevel=-1)
543                 can_fetch = False
544
545         distdir_writable = can_fetch and not fetch_to_ro
546         failed_files = set()
547         restrict_fetch_msg = False
548
549         for myfile in filedict:
550                 """
551                 fetched  status
552                 0        nonexistent
553                 1        partially downloaded
554                 2        completely downloaded
555                 """
556                 fetched = 0
557
558                 orig_digests = mydigests.get(myfile, {})
559
560                 if not (allow_missing_digests or listonly):
561                         verifiable_hash_types = set(orig_digests).intersection(hashfunc_map)
562                         verifiable_hash_types.discard("size")
563                         if not verifiable_hash_types:
564                                 expected = set(hashfunc_map)
565                                 expected.discard("size")
566                                 expected = " ".join(sorted(expected))
567                                 got = set(orig_digests)
568                                 got.discard("size")
569                                 got = " ".join(sorted(got))
570                                 reason = (_("Insufficient data for checksum verification"),
571                                         got, expected)
572                                 writemsg(_("!!! Fetched file: %s VERIFY FAILED!\n") % myfile,
573                                         noiselevel=-1)
574                                 writemsg(_("!!! Reason: %s\n") % reason[0],
575                                         noiselevel=-1)
576                                 writemsg(_("!!! Got:      %s\n!!! Expected: %s\n") % \
577                                         (reason[1], reason[2]), noiselevel=-1)
578
579                                 if fetchonly:
580                                         failed_files.add(myfile)
581                                         continue
582                                 else:
583                                         return 0
584
585                 size = orig_digests.get("size")
586                 if size == 0:
587                         # Zero-byte distfiles are always invalid, so discard their digests.
588                         del mydigests[myfile]
589                         orig_digests.clear()
590                         size = None
591                 pruned_digests = orig_digests
592                 if parallel_fetchonly:
593                         pruned_digests = {}
594                         if size is not None:
595                                 pruned_digests["size"] = size
596
597                 myfile_path = os.path.join(mysettings["DISTDIR"], myfile)
598                 has_space = True
599                 has_space_superuser = True
600                 file_lock = None
601                 if listonly:
602                         writemsg_stdout("\n", noiselevel=-1)
603                 else:
604                         # check if there is enough space in DISTDIR to completely store myfile
605                         # overestimate the filesize so we aren't bitten by FS overhead
606                         vfs_stat = None
607                         if size is not None and hasattr(os, "statvfs"):
608                                 try:
609                                         vfs_stat = os.statvfs(mysettings["DISTDIR"])
610                                 except OSError as e:
611                                         writemsg_level("!!! statvfs('%s'): %s\n" %
612                                                 (mysettings["DISTDIR"], e),
613                                                 noiselevel=-1, level=logging.ERROR)
614                                         del e
615
616                         if vfs_stat is not None:
617                                 try:
618                                         mysize = os.stat(myfile_path).st_size
619                                 except OSError as e:
620                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
621                                                 raise
622                                         del e
623                                         mysize = 0
624                                 if (size - mysize + vfs_stat.f_bsize) >= \
625                                         (vfs_stat.f_bsize * vfs_stat.f_bavail):
626
627                                         if (size - mysize + vfs_stat.f_bsize) >= \
628                                                 (vfs_stat.f_bsize * vfs_stat.f_bfree):
629                                                 has_space_superuser = False
630
631                                         if not has_space_superuser:
632                                                 has_space = False
633                                         elif secpass < 2:
634                                                 has_space = False
635                                         elif userfetch:
636                                                 has_space = False
637
638                         if distdir_writable and use_locks:
639
640                                 lock_kwargs = {}
641                                 if fetchonly:
642                                         lock_kwargs["flags"] = os.O_NONBLOCK
643
644                                 try:
645                                         file_lock = lockfile(myfile_path,
646                                                 wantnewlockfile=1, **lock_kwargs)
647                                 except TryAgain:
648                                         writemsg(_(">>> File '%s' is already locked by "
649                                                 "another fetcher. Continuing...\n") % myfile,
650                                                 noiselevel=-1)
651                                         continue
652                 try:
653                         if not listonly:
654
655                                 eout = EOutput()
656                                 eout.quiet = mysettings.get("PORTAGE_QUIET") == "1"
657                                 match, mystat = _check_distfile(
658                                         myfile_path, pruned_digests, eout, hash_filter=hash_filter)
659                                 if match:
660                                         # Skip permission adjustment for symlinks, since we don't
661                                         # want to modify anything outside of the primary DISTDIR,
662                                         # and symlinks typically point to PORTAGE_RO_DISTDIRS.
663                                         if distdir_writable and not os.path.islink(myfile_path):
664                                                 try:
665                                                         apply_secpass_permissions(myfile_path,
666                                                                 gid=portage_gid, mode=0o664, mask=0o2,
667                                                                 stat_cached=mystat)
668                                                 except PortageException as e:
669                                                         if not os.access(myfile_path, os.R_OK):
670                                                                 writemsg(_("!!! Failed to adjust permissions:"
671                                                                         " %s\n") % str(e), noiselevel=-1)
672                                                         del e
673                                         continue
674
675                                 if distdir_writable and mystat is None:
676                                         # Remove broken symlinks if necessary.
677                                         try:
678                                                 os.unlink(myfile_path)
679                                         except OSError:
680                                                 pass
681
682                                 if mystat is not None:
683                                         if stat.S_ISDIR(mystat.st_mode):
684                                                 writemsg_level(
685                                                         _("!!! Unable to fetch file since "
686                                                         "a directory is in the way: \n"
687                                                         "!!!   %s\n") % myfile_path,
688                                                         level=logging.ERROR, noiselevel=-1)
689                                                 return 0
690
691                                         if mystat.st_size == 0:
692                                                 if distdir_writable:
693                                                         try:
694                                                                 os.unlink(myfile_path)
695                                                         except OSError:
696                                                                 pass
697                                         elif distdir_writable:
698                                                 if mystat.st_size < fetch_resume_size and \
699                                                         mystat.st_size < size:
700                                                         # If the file already exists and the size does not
701                                                         # match the existing digests, it may be that the
702                                                         # user is attempting to update the digest. In this
703                                                         # case, the digestgen() function will advise the
704                                                         # user to use `ebuild --force foo.ebuild manifest`
705                                                         # in order to force the old digests to be replaced.
706                                                         # Since the user may want to keep this file, rename
707                                                         # it instead of deleting it.
708                                                         writemsg(_(">>> Renaming distfile with size "
709                                                                 "%d (smaller than " "PORTAGE_FETCH_RESU"
710                                                                 "ME_MIN_SIZE)\n") % mystat.st_size)
711                                                         temp_filename = \
712                                                                 _checksum_failure_temp_file(
713                                                                 mysettings["DISTDIR"], myfile)
714                                                         writemsg_stdout(_("Refetching... "
715                                                                 "File renamed to '%s'\n\n") % \
716                                                                 temp_filename, noiselevel=-1)
717                                                 elif mystat.st_size >= size:
718                                                         temp_filename = \
719                                                                 _checksum_failure_temp_file(
720                                                                 mysettings["DISTDIR"], myfile)
721                                                         writemsg_stdout(_("Refetching... "
722                                                                 "File renamed to '%s'\n\n") % \
723                                                                 temp_filename, noiselevel=-1)
724
725                                 if distdir_writable and ro_distdirs:
726                                         readonly_file = None
727                                         for x in ro_distdirs:
728                                                 filename = os.path.join(x, myfile)
729                                                 match, mystat = _check_distfile(
730                                                         filename, pruned_digests, eout, hash_filter=hash_filter)
731                                                 if match:
732                                                         readonly_file = filename
733                                                         break
734                                         if readonly_file is not None:
735                                                 try:
736                                                         os.unlink(myfile_path)
737                                                 except OSError as e:
738                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
739                                                                 raise
740                                                         del e
741                                                 os.symlink(readonly_file, myfile_path)
742                                                 continue
743
744                                 # this message is shown only after we know that
745                                 # the file is not already fetched
746                                 if not has_space:
747                                         writemsg(_("!!! Insufficient space to store %s in %s\n") % \
748                                                 (myfile, mysettings["DISTDIR"]), noiselevel=-1)
749
750                                         if has_space_superuser:
751                                                 writemsg(_("!!! Insufficient privileges to use "
752                                                         "remaining space.\n"), noiselevel=-1)
753                                                 if userfetch:
754                                                         writemsg(_("!!! You may set FEATURES=\"-userfetch\""
755                                                                 " in /etc/portage/make.conf in order to fetch with\n"
756                                                                 "!!! superuser privileges.\n"), noiselevel=-1)
757
758                                 if fsmirrors and not os.path.exists(myfile_path) and has_space:
759                                         for mydir in fsmirrors:
760                                                 mirror_file = os.path.join(mydir, myfile)
761                                                 try:
762                                                         shutil.copyfile(mirror_file, myfile_path)
763                                                         writemsg(_("Local mirror has file: %s\n") % myfile)
764                                                         break
765                                                 except (IOError, OSError) as e:
766                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
767                                                                 raise
768                                                         del e
769
770                                 try:
771                                         mystat = os.stat(myfile_path)
772                                 except OSError as e:
773                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
774                                                 raise
775                                         del e
776                                 else:
777                                         # Skip permission adjustment for symlinks, since we don't
778                                         # want to modify anything outside of the primary DISTDIR,
779                                         # and symlinks typically point to PORTAGE_RO_DISTDIRS.
780                                         if not os.path.islink(myfile_path):
781                                                 try:
782                                                         apply_secpass_permissions(myfile_path,
783                                                                 gid=portage_gid, mode=0o664, mask=0o2,
784                                                                 stat_cached=mystat)
785                                                 except PortageException as e:
786                                                         if not os.access(myfile_path, os.R_OK):
787                                                                 writemsg(_("!!! Failed to adjust permissions:"
788                                                                         " %s\n") % (e,), noiselevel=-1)
789
790                                         # If the file is empty then it's obviously invalid. Remove
791                                         # the empty file and try to download if possible.
792                                         if mystat.st_size == 0:
793                                                 if distdir_writable:
794                                                         try:
795                                                                 os.unlink(myfile_path)
796                                                         except EnvironmentError:
797                                                                 pass
798                                         elif myfile not in mydigests:
799                                                 # We don't have a digest, but the file exists.  We must
800                                                 # assume that it is fully downloaded.
801                                                 continue
802                                         else:
803                                                 if mystat.st_size < mydigests[myfile]["size"] and \
804                                                         not restrict_fetch:
805                                                         fetched = 1 # Try to resume this download.
806                                                 elif parallel_fetchonly and \
807                                                         mystat.st_size == mydigests[myfile]["size"]:
808                                                         eout = EOutput()
809                                                         eout.quiet = \
810                                                                 mysettings.get("PORTAGE_QUIET") == "1"
811                                                         eout.ebegin(
812                                                                 "%s size ;-)" % (myfile, ))
813                                                         eout.eend(0)
814                                                         continue
815                                                 else:
816                                                         digests = _filter_unaccelarated_hashes(mydigests[myfile])
817                                                         if hash_filter is not None:
818                                                                 digests = _apply_hash_filter(digests, hash_filter)
819                                                         verified_ok, reason = verify_all(myfile_path, digests)
820                                                         if not verified_ok:
821                                                                 writemsg(_("!!! Previously fetched"
822                                                                         " file: '%s'\n") % myfile, noiselevel=-1)
823                                                                 writemsg(_("!!! Reason: %s\n") % reason[0],
824                                                                         noiselevel=-1)
825                                                                 writemsg(_("!!! Got:      %s\n"
826                                                                         "!!! Expected: %s\n") % \
827                                                                         (reason[1], reason[2]), noiselevel=-1)
828                                                                 if reason[0] == _("Insufficient data for checksum verification"):
829                                                                         return 0
830                                                                 if distdir_writable:
831                                                                         temp_filename = \
832                                                                                 _checksum_failure_temp_file(
833                                                                                 mysettings["DISTDIR"], myfile)
834                                                                         writemsg_stdout(_("Refetching... "
835                                                                                 "File renamed to '%s'\n\n") % \
836                                                                                 temp_filename, noiselevel=-1)
837                                                         else:
838                                                                 eout = EOutput()
839                                                                 eout.quiet = \
840                                                                         mysettings.get("PORTAGE_QUIET", None) == "1"
841                                                                 if digests:
842                                                                         digests = list(digests)
843                                                                         digests.sort()
844                                                                         eout.ebegin(
845                                                                                 "%s %s ;-)" % (myfile, " ".join(digests)))
846                                                                         eout.eend(0)
847                                                                 continue # fetch any remaining files
848
849                         # Create a reversed list since that is optimal for list.pop().
850                         uri_list = filedict[myfile][:]
851                         uri_list.reverse()
852                         checksum_failure_count = 0
853                         tried_locations = set()
854                         while uri_list:
855                                 loc = uri_list.pop()
856                                 # Eliminate duplicates here in case we've switched to
857                                 # "primaryuri" mode on the fly due to a checksum failure.
858                                 if loc in tried_locations:
859                                         continue
860                                 tried_locations.add(loc)
861                                 if listonly:
862                                         writemsg_stdout(loc+" ", noiselevel=-1)
863                                         continue
864                                 # allow different fetchcommands per protocol
865                                 protocol = loc[0:loc.find("://")]
866
867                                 global_config_path = GLOBAL_CONFIG_PATH
868                                 if portage.const.EPREFIX:
869                                         global_config_path = os.path.join(portage.const.EPREFIX,
870                                                         GLOBAL_CONFIG_PATH.lstrip(os.sep))
871
872                                 missing_file_param = False
873                                 fetchcommand_var = "FETCHCOMMAND_" + protocol.upper()
874                                 fetchcommand = mysettings.get(fetchcommand_var)
875                                 if fetchcommand is None:
876                                         fetchcommand_var = "FETCHCOMMAND"
877                                         fetchcommand = mysettings.get(fetchcommand_var)
878                                         if fetchcommand is None:
879                                                 writemsg_level(
880                                                         _("!!! %s is unset. It should "
881                                                         "have been defined in\n!!! %s/make.globals.\n") \
882                                                         % (fetchcommand_var, global_config_path),
883                                                         level=logging.ERROR, noiselevel=-1)
884                                                 return 0
885                                 if "${FILE}" not in fetchcommand:
886                                         writemsg_level(
887                                                 _("!!! %s does not contain the required ${FILE}"
888                                                 " parameter.\n") % fetchcommand_var,
889                                                 level=logging.ERROR, noiselevel=-1)
890                                         missing_file_param = True
891
892                                 resumecommand_var = "RESUMECOMMAND_" + protocol.upper()
893                                 resumecommand = mysettings.get(resumecommand_var)
894                                 if resumecommand is None:
895                                         resumecommand_var = "RESUMECOMMAND"
896                                         resumecommand = mysettings.get(resumecommand_var)
897                                         if resumecommand is None:
898                                                 writemsg_level(
899                                                         _("!!! %s is unset. It should "
900                                                         "have been defined in\n!!! %s/make.globals.\n") \
901                                                         % (resumecommand_var, global_config_path),
902                                                         level=logging.ERROR, noiselevel=-1)
903                                                 return 0
904                                 if "${FILE}" not in resumecommand:
905                                         writemsg_level(
906                                                 _("!!! %s does not contain the required ${FILE}"
907                                                 " parameter.\n") % resumecommand_var,
908                                                 level=logging.ERROR, noiselevel=-1)
909                                         missing_file_param = True
910
911                                 if missing_file_param:
912                                         writemsg_level(
913                                                 _("!!! Refer to the make.conf(5) man page for "
914                                                 "information about how to\n!!! correctly specify "
915                                                 "FETCHCOMMAND and RESUMECOMMAND.\n"),
916                                                 level=logging.ERROR, noiselevel=-1)
917                                         if myfile != os.path.basename(loc):
918                                                 return 0
919
920                                 if not can_fetch:
921                                         if fetched != 2:
922                                                 try:
923                                                         mysize = os.stat(myfile_path).st_size
924                                                 except OSError as e:
925                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
926                                                                 raise
927                                                         del e
928                                                         mysize = 0
929
930                                                 if mysize == 0:
931                                                         writemsg(_("!!! File %s isn't fetched but unable to get it.\n") % myfile,
932                                                                 noiselevel=-1)
933                                                 elif size is None or size > mysize:
934                                                         writemsg(_("!!! File %s isn't fully fetched, but unable to complete it\n") % myfile,
935                                                                 noiselevel=-1)
936                                                 else:
937                                                         writemsg(_("!!! File %s is incorrect size, "
938                                                                 "but unable to retry.\n") % myfile, noiselevel=-1)
939                                                 return 0
940                                         else:
941                                                 continue
942
943                                 if fetched != 2 and has_space:
944                                         #we either need to resume or start the download
945                                         if fetched == 1:
946                                                 try:
947                                                         mystat = os.stat(myfile_path)
948                                                 except OSError as e:
949                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
950                                                                 raise
951                                                         del e
952                                                         fetched = 0
953                                                 else:
954                                                         if mystat.st_size < fetch_resume_size:
955                                                                 writemsg(_(">>> Deleting distfile with size "
956                                                                         "%d (smaller than " "PORTAGE_FETCH_RESU"
957                                                                         "ME_MIN_SIZE)\n") % mystat.st_size)
958                                                                 try:
959                                                                         os.unlink(myfile_path)
960                                                                 except OSError as e:
961                                                                         if e.errno not in \
962                                                                                 (errno.ENOENT, errno.ESTALE):
963                                                                                 raise
964                                                                         del e
965                                                                 fetched = 0
966                                         if fetched == 1:
967                                                 #resume mode:
968                                                 writemsg(_(">>> Resuming download...\n"))
969                                                 locfetch=resumecommand
970                                                 command_var = resumecommand_var
971                                         else:
972                                                 #normal mode:
973                                                 locfetch=fetchcommand
974                                                 command_var = fetchcommand_var
975                                         writemsg_stdout(_(">>> Downloading '%s'\n") % \
976                                                 _hide_url_passwd(loc))
977                                         variables = {
978                                                 "URI":     loc,
979                                                 "FILE":    myfile
980                                         }
981
982                                         for k in ("DISTDIR", "PORTAGE_SSH_OPTS"):
983                                                 try:
984                                                         variables[k] = mysettings[k]
985                                                 except KeyError:
986                                                         pass
987
988                                         myfetch = shlex_split(locfetch)
989                                         myfetch = [varexpand(x, mydict=variables) for x in myfetch]
990                                         myret = -1
991                                         try:
992
993                                                 myret = _spawn_fetch(mysettings, myfetch)
994
995                                         finally:
996                                                 try:
997                                                         apply_secpass_permissions(myfile_path,
998                                                                 gid=portage_gid, mode=0o664, mask=0o2)
999                                                 except FileNotFound:
1000                                                         pass
1001                                                 except PortageException as e:
1002                                                         if not os.access(myfile_path, os.R_OK):
1003                                                                 writemsg(_("!!! Failed to adjust permissions:"
1004                                                                         " %s\n") % str(e), noiselevel=-1)
1005                                                         del e
1006
1007                                         # If the file is empty then it's obviously invalid.  Don't
1008                                         # trust the return value from the fetcher.  Remove the
1009                                         # empty file and try to download again.
1010                                         try:
1011                                                 if os.stat(myfile_path).st_size == 0:
1012                                                         os.unlink(myfile_path)
1013                                                         fetched = 0
1014                                                         continue
1015                                         except EnvironmentError:
1016                                                 pass
1017
1018                                         if mydigests is not None and myfile in mydigests:
1019                                                 try:
1020                                                         mystat = os.stat(myfile_path)
1021                                                 except OSError as e:
1022                                                         if e.errno not in (errno.ENOENT, errno.ESTALE):
1023                                                                 raise
1024                                                         del e
1025                                                         fetched = 0
1026                                                 else:
1027
1028                                                         if stat.S_ISDIR(mystat.st_mode):
1029                                                                 # This can happen if FETCHCOMMAND erroneously
1030                                                                 # contains wget's -P option where it should
1031                                                                 # instead have -O.
1032                                                                 writemsg_level(
1033                                                                         _("!!! The command specified in the "
1034                                                                         "%s variable appears to have\n!!! "
1035                                                                         "created a directory instead of a "
1036                                                                         "normal file.\n") % command_var,
1037                                                                         level=logging.ERROR, noiselevel=-1)
1038                                                                 writemsg_level(
1039                                                                         _("!!! Refer to the make.conf(5) "
1040                                                                         "man page for information about how "
1041                                                                         "to\n!!! correctly specify "
1042                                                                         "FETCHCOMMAND and RESUMECOMMAND.\n"),
1043                                                                         level=logging.ERROR, noiselevel=-1)
1044                                                                 return 0
1045
1046                                                         # no exception?  file exists. let digestcheck() report
1047                                                         # an appropriately for size or checksum errors
1048
1049                                                         # If the fetcher reported success and the file is
1050                                                         # too small, it's probably because the digest is
1051                                                         # bad (upstream changed the distfile).  In this
1052                                                         # case we don't want to attempt to resume. Show a
1053                                                         # digest verification failure to that the user gets
1054                                                         # a clue about what just happened.
1055                                                         if myret != os.EX_OK and \
1056                                                                 mystat.st_size < mydigests[myfile]["size"]:
1057                                                                 # Fetch failed... Try the next one... Kill 404 files though.
1058                                                                 if (mystat[stat.ST_SIZE]<100000) and (len(myfile)>4) and not ((myfile[-5:]==".html") or (myfile[-4:]==".htm")):
1059                                                                         html404=re.compile("<title>.*(not found|404).*</title>",re.I|re.M)
1060                                                                         with io.open(
1061                                                                                 _unicode_encode(myfile_path,
1062                                                                                 encoding=_encodings['fs'], errors='strict'),
1063                                                                                 mode='r', encoding=_encodings['content'], errors='replace'
1064                                                                                 ) as f:
1065                                                                                 if html404.search(f.read()):
1066                                                                                         try:
1067                                                                                                 os.unlink(mysettings["DISTDIR"]+"/"+myfile)
1068                                                                                                 writemsg(_(">>> Deleting invalid distfile. (Improper 404 redirect from server.)\n"))
1069                                                                                                 fetched = 0
1070                                                                                                 continue
1071                                                                                         except (IOError, OSError):
1072                                                                                                 pass
1073                                                                 fetched = 1
1074                                                                 continue
1075                                                         if True:
1076                                                                 # File is the correct size--check the checksums for the fetched
1077                                                                 # file NOW, for those users who don't have a stable/continuous
1078                                                                 # net connection. This way we have a chance to try to download
1079                                                                 # from another mirror...
1080                                                                 digests = _filter_unaccelarated_hashes(mydigests[myfile])
1081                                                                 if hash_filter is not None:
1082                                                                         digests = _apply_hash_filter(digests, hash_filter)
1083                                                                 verified_ok, reason = verify_all(myfile_path, digests)
1084                                                                 if not verified_ok:
1085                                                                         writemsg(_("!!! Fetched file: %s VERIFY FAILED!\n") % myfile,
1086                                                                                 noiselevel=-1)
1087                                                                         writemsg(_("!!! Reason: %s\n") % reason[0],
1088                                                                                 noiselevel=-1)
1089                                                                         writemsg(_("!!! Got:      %s\n!!! Expected: %s\n") % \
1090                                                                                 (reason[1], reason[2]), noiselevel=-1)
1091                                                                         if reason[0] == _("Insufficient data for checksum verification"):
1092                                                                                 return 0
1093                                                                         temp_filename = \
1094                                                                                 _checksum_failure_temp_file(
1095                                                                                 mysettings["DISTDIR"], myfile)
1096                                                                         writemsg_stdout(_("Refetching... "
1097                                                                                 "File renamed to '%s'\n\n") % \
1098                                                                                 temp_filename, noiselevel=-1)
1099                                                                         fetched=0
1100                                                                         checksum_failure_count += 1
1101                                                                         if checksum_failure_count == \
1102                                                                                 checksum_failure_primaryuri:
1103                                                                                 # Switch to "primaryuri" mode in order
1104                                                                                 # to increase the probablility of
1105                                                                                 # of success.
1106                                                                                 primaryuris = \
1107                                                                                         primaryuri_dict.get(myfile)
1108                                                                                 if primaryuris:
1109                                                                                         uri_list.extend(
1110                                                                                                 reversed(primaryuris))
1111                                                                         if checksum_failure_count >= \
1112                                                                                 checksum_failure_max_tries:
1113                                                                                 break
1114                                                                 else:
1115                                                                         eout = EOutput()
1116                                                                         eout.quiet = mysettings.get("PORTAGE_QUIET", None) == "1"
1117                                                                         if digests:
1118                                                                                 eout.ebegin("%s %s ;-)" % \
1119                                                                                         (myfile, " ".join(sorted(digests))))
1120                                                                                 eout.eend(0)
1121                                                                         fetched=2
1122                                                                         break
1123                                         else:
1124                                                 if not myret:
1125                                                         fetched=2
1126                                                         break
1127                                                 elif mydigests!=None:
1128                                                         writemsg(_("No digest file available and download failed.\n\n"),
1129                                                                 noiselevel=-1)
1130                 finally:
1131                         if use_locks and file_lock:
1132                                 unlockfile(file_lock)
1133                                 file_lock = None
1134
1135                 if listonly:
1136                         writemsg_stdout("\n", noiselevel=-1)
1137                 if fetched != 2:
1138                         if restrict_fetch and not restrict_fetch_msg:
1139                                 restrict_fetch_msg = True
1140                                 msg = _("\n!!! %s/%s"
1141                                         " has fetch restriction turned on.\n"
1142                                         "!!! This probably means that this "
1143                                         "ebuild's files must be downloaded\n"
1144                                         "!!! manually.  See the comments in"
1145                                         " the ebuild for more information.\n\n") % \
1146                                         (mysettings["CATEGORY"], mysettings["PF"])
1147                                 writemsg_level(msg,
1148                                         level=logging.ERROR, noiselevel=-1)
1149                         elif restrict_fetch:
1150                                 pass
1151                         elif listonly:
1152                                 pass
1153                         elif not filedict[myfile]:
1154                                 writemsg(_("Warning: No mirrors available for file"
1155                                         " '%s'\n") % (myfile), noiselevel=-1)
1156                         else:
1157                                 writemsg(_("!!! Couldn't download '%s'. Aborting.\n") % myfile,
1158                                         noiselevel=-1)
1159
1160                         if listonly:
1161                                 failed_files.add(myfile)
1162                                 continue
1163                         elif fetchonly:
1164                                 failed_files.add(myfile)
1165                                 continue
1166                         return 0
1167         if failed_files:
1168                 return 0
1169         return 1