Repository: conda/conda

Issue:
FileNotFoundError when extracting tarball
```
error: FileNotFoundError(2, 'No such file or directory')
command: C:\Users\Anurag\Anaconda3\Scripts\conda install spyder
user_agent: conda/4.4.7 requests/2.13.0 CPython/3.6.2 Windows/10 Windows/10.0.16299
_messageid: -9223372036837325447
_messagetime: 1515540414747 / 2018-01-09 17:26:54

Traceback (most recent call last):
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 270, in _make_single_record
    repodata_record = read_repodata_json(extracted_package_dir)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\gateways\disk\read.py", line 123, in read_repodata_json
    with open(join(extracted_package_directory, 'info', 'repodata_record.json')) as fi:
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\common\compat.py", line 120, in open
    closefd=closefd)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Anurag\\Anaconda3\\pkgs\\pip-9.0.1-py36_1\\info\\repodata_record.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 287, in _make_single_record
    index_json_record = read_index_json(extracted_package_dir)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\gateways\disk\read.py", line 111, in read_index_json
    with open(join(extracted_package_directory, 'info', 'index.json')) as fi:
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\common\compat.py", line 120, in open
    closefd=closefd)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Anurag\\Anaconda3\\pkgs\\pip-9.0.1-py36_1\\info\\index.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\exceptions.py", line 743, in __call__
    return func(*args, **kwargs)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\cli\main.py", line 78, in _main
    exit_code = do_call(args, p)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\cli\conda_argparse.py", line 76, in do_call
    exit_code = getattr(module, func_name)(args, parser)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\cli\main_install.py", line 11, in execute
    install(args, parser, 'install')
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\cli\install.py", line 255, in install
    handle_txn(progressive_fetch_extract, unlink_link_transaction, prefix, args, newenv)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\cli\install.py", line 272, in handle_txn
    unlink_link_transaction.display_actions(progressive_fetch_extract)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\link.py", line 706, in display_actions
    legacy_action_groups = self.make_legacy_action_groups(pfe)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\link.py", line 689, in make_legacy_action_groups
    pfe.prepare()
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\common\io.py", line 415, in decorated
    return f(*args, **kwds)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 540, in prepare
    for prec in self.link_precs)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 540, in <genexpr>
    for prec in self.link_precs)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 434, in make_actions_for_record
    ), None)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 431, in <genexpr>
    pcrec for pcrec in concat(PackageCache(pkgs_dir).query(pref_or_spec)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 432, in <genexpr>
    for pkgs_dir in context.pkgs_dirs)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 118, in query
    return (pcrec for pcrec in itervalues(self._package_cache_records) if pcrec == param)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 211, in _package_cache_records
    self.load()
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 90, in load
    package_cache_record = self._make_single_record(base_name)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 308, in _make_single_record
    extract_tarball(package_tarball_full_path, extracted_package_dir)
  File "C:\Users\Anurag\Anaconda3\lib\site-packages\conda\gateways\disk\create.py", line 145, in extract_tarball
    t.extractall(path=destination_directory, members=members_with_progress())
  File "C:\Users\Anurag\Anaconda3\lib\tarfile.py", line 2007, in extractall
    numeric_owner=numeric_owner)
  File "C:\Users\Anurag\Anaconda3\lib\tarfile.py", line 2049, in extract
    numeric_owner=numeric_owner)
  File "C:\Users\Anurag\Anaconda3\lib\tarfile.py", line 2119, in _extract_member
    self.makefile(tarinfo, targetpath)
  File "C:\Users\Anurag\Anaconda3\lib\tarfile.py", line 2160, in makefile
    with bltn_open(targetpath, "wb") as target:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Anurag\\Anaconda3\\pkgs\\pip-9.0.1-py36_1\\Lib\\site-packages\\pip\\_vendor\\cachecontrol\\__pycache__\\filewrapper.cpython-36.pyc'
```


Discussion:
```
error: FileNotFoundError(2, 'No such file or directory')
command: C:\Anaconda3\Scripts\conda install --yes --json --force-pscheck --prefix C:\Anaconda3 jupyterlab==0.30.6 --dry-run
user_agent: conda/4.4.7 requests/2.18.4 CPython/3.6.3 Windows/10 Windows/10.0.16299
_messageid: -9223372036837325446
_messagetime: 1515541455356 / 2018-01-09 17:44:15

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 270, in _make_single_record
    repodata_record = read_repodata_json(extracted_package_dir)
  File "C:\Anaconda3\lib\site-packages\conda\gateways\disk\read.py", line 123, in read_repodata_json
    with open(join(extracted_package_directory, 'info', 'repodata_record.json')) as fi:
  File "C:\Anaconda3\lib\site-packages\conda\common\compat.py", line 120, in open
    closefd=closefd)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Anaconda3\\pkgs\\nbformat-4.3.0-py36_0\\info\\repodata_record.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 287, in _make_single_record
    index_json_record = read_index_json(extracted_package_dir)
  File "C:\Anaconda3\lib\site-packages\conda\gateways\disk\read.py", line 111, in read_index_json
    with open(join(extracted_package_directory, 'info', 'index.json')) as fi:
  File "C:\Anaconda3\lib\site-packages\conda\common\compat.py", line 120, in open
    closefd=closefd)
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Anaconda3\\pkgs\\nbformat-4.3.0-py36_0\\info\\index.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Anaconda3\lib\site-packages\conda\exceptions.py", line 743, in __call__
    return func(*args, **kwargs)
  File "C:\Anaconda3\lib\site-packages\conda\cli\main.py", line 78, in _main
    exit_code = do_call(args, p)
  File "C:\Anaconda3\lib\site-packages\conda\cli\conda_argparse.py", line 76, in do_call
    exit_code = getattr(module, func_name)(args, parser)
  File "C:\Anaconda3\lib\site-packages\conda\cli\main_install.py", line 11, in execute
    install(args, parser, 'install')
  File "C:\Anaconda3\lib\site-packages\conda\cli\install.py", line 255, in install
    handle_txn(progressive_fetch_extract, unlink_link_transaction, prefix, args, newenv)
  File "C:\Anaconda3\lib\site-packages\conda\cli\install.py", line 276, in handle_txn
    actions = unlink_link_transaction.make_legacy_action_groups(progressive_fetch_extract)[0]
  File "C:\Anaconda3\lib\site-packages\conda\core\link.py", line 689, in make_legacy_action_groups
    pfe.prepare()
  File "C:\Anaconda3\lib\site-packages\conda\common\io.py", line 415, in decorated
    return f(*args, **kwds)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 540, in prepare
    for prec in self.link_precs)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 540, in <genexpr>
    for prec in self.link_precs)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 434, in make_actions_for_record
    ), None)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 431, in <genexpr>
    pcrec for pcrec in concat(PackageCache(pkgs_dir).query(pref_or_spec)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 432, in <genexpr>
    for pkgs_dir in context.pkgs_dirs)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 118, in query
    return (pcrec for pcrec in itervalues(self._package_cache_records) if pcrec == param)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 211, in _package_cache_records
    self.load()
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 90, in load
    package_cache_record = self._make_single_record(base_name)
  File "C:\Anaconda3\lib\site-packages\conda\core\package_cache.py", line 308, in _make_single_record
    extract_tarball(package_tarball_full_path, extracted_package_dir)
  File "C:\Anaconda3\lib\site-packages\conda\gateways\disk\create.py", line 145, in extract_tarball
    t.extractall(path=destination_directory, members=members_with_progress())
  File "C:\Anaconda3\lib\tarfile.py", line 2007, in extractall
    numeric_owner=numeric_owner)
  File "C:\Anaconda3\lib\tarfile.py", line 2049, in extract
    numeric_owner=numeric_owner)
  File "C:\Anaconda3\lib\tarfile.py", line 2119, in _extract_member
    self.makefile(tarinfo, targetpath)
  File "C:\Anaconda3\lib\tarfile.py", line 2160, in makefile
    with bltn_open(targetpath, "wb") as target:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Anaconda3\\pkgs\\nbformat-4.3.0-py36_0\\Lib\\site-packages\\nbformat-4.3.0-py3.6.egg-info'
```

Resolving patch:
diff --git a/conda/core/package_cache.py b/conda/core/package_cache.py
--- a/conda/core/package_cache.py
+++ b/conda/core/package_cache.py
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 from __future__ import absolute_import, division, print_function, unicode_literals
 
-from errno import EACCES, EPERM
+from errno import EACCES, ENOENT, EPERM
 from functools import reduce
 from logging import getLogger
 from os import listdir
@@ -305,7 +305,16 @@ def _make_single_record(self, package_filename):
                             # We have a partially unpacked conda package directory. Best thing
                             # to do is remove it and try extracting.
                             rm_rf(extracted_package_dir)
-                        extract_tarball(package_tarball_full_path, extracted_package_dir)
+                        try:
+                            extract_tarball(package_tarball_full_path, extracted_package_dir)
+                        except EnvironmentError as e:
+                            if e.errno == ENOENT:
+                                # FileNotFoundError(2, 'No such file or directory')
+                                # At this point, we can assume the package tarball is bad.
+                                # Remove everything and move on.
+                                # see https://github.com/conda/conda/issues/6707
+                                rm_rf(package_tarball_full_path)
+                                rm_rf(extracted_package_dir)
                         try:
                             index_json_record = read_index_json(extracted_package_dir)
                         except (IOError, OSError, JSONDecodeError):


Test patch: