# Buggy code
def _load(self):
    try:
        mtime = getmtime(self.cache_path_json)
    except (IOError, OSError):
        log.debug(
            "No local cache found for %s at %s", self.url_w_subdir, self.cache_path_json
        )
        if context.use_index_cache or (
            context.offline and not self.url_w_subdir.startswith("file://")
        ):
            log.debug(
                "Using cached data for %s at %s forced. Returning empty repodata.",
                self.url_w_subdir,
                self.cache_path_json,
            )
            return {
                "_package_records": (),
                "_package_dists": (),
                "_names_index": defaultdict(list),
                "_track_features_index": defaultdict(list),
            }
        else:
            mod_etag_headers = {}
    else:
        mod_etag_headers = read_mod_and_etag(self.cache_path_json)

        if context.use_index_cache:
            log.debug(
                "Using cached repodata for %s at %s because use_cache=True",
                self.url_w_subdir,
                self.cache_path_json,
            )

            _internal_state = self._read_local_repdata(
                mod_etag_headers.get("_etag"), mod_etag_headers.get("_mod")
            )
            return _internal_state

        if context.local_repodata_ttl > 1:
            max_age = context.local_repodata_ttl
        elif context.local_repodata_ttl == 1:
            max_age = get_cache_control_max_age(
                mod_etag_headers.get("_cache_control", "")
            )
        else:
            max_age = 0

        timeout = mtime + max_age - time()
        if (timeout > 0 or context.offline) and not self.url_w_subdir.startswith(
            "file://"
        ):
            log.debug(
                "Using cached repodata for %s at %s. Timeout in %d sec",
                self.url_w_subdir,
                self.cache_path_json,
                timeout,
            )
            _internal_state = self._read_local_repdata(
                mod_etag_headers.get("_etag"), mod_etag_headers.get("_mod")
            )
            return _internal_state

        log.debug(
            "Local cache timed out for %s at %s",
            self.url_w_subdir,
            self.cache_path_json,
        )

    try:
        raw_repodata_str = fetch_repodata_remote_request(
            self.url_w_credentials,
            mod_etag_headers.get("_etag"),
            mod_etag_headers.get("_mod"),
        )
    except Response304ContentUnchanged:
        log.debug(
            "304 NOT MODIFIED for '%s'. Updating mtime and loading from disk",
            self.url_w_subdir,
        )
        touch(self.cache_path_json)
        _internal_state = self._read_local_repdata(
            mod_etag_headers.get("_etag"), mod_etag_headers.get("_mod")
        )
        return _internal_state
    else:
        if not isdir(dirname(self.cache_path_json)):
            mkdir_p(dirname(self.cache_path_json))
        with open(self.cache_path_json, "w") as fh:
            fh.write(raw_repodata_str or "{}")
        _internal_state = self._process_raw_repodata_str(raw_repodata_str)
        self._internal_state = _internal_state
        self._pickle_me()
        return _internal_state


# Traceback (PermissionError)
error: PermissionError(13, 'Permission denied')
command: /Users/apple/.conda/envs/cafe/bin/conda uninstall protobuf
user_agent: conda/4.4.3 requests/2.18.4 CPython/3.6.1 Darwin/16.1.0 OSX/10.12.1
_messageid: -9223372036845375609
_messagetime: 1514148478933 / 2017-12-24 14:47:58
_receipttime: 1514148478933 / 2017-12-24 14:47:58

Traceback (most recent call last):
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/exceptions.py", line 722, in __call__
return func(*args, **kwargs)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/cli/main.py", line 78, in _main
exit_code = do_call(args, p)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/cli/conda_argparse.py", line 75, in do_call
exit_code = getattr(module, func_name)(args, parser)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/cli/main_remove.py", line 82, in execute
txn = solver.solve_for_transaction(force_remove=args.force)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 501, in solve_for_transaction
force_remove, force_reinstall)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 434, in solve_for_diff
final_precs = self.solve_final_state(deps_modifier, prune, ignore_pinned, force_remove)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 178, in solve_final_state
index, r = self._prepare(prepared_specs)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/solve.py", line 557, in _prepare
self.subdirs, prepared_specs)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/index.py", line 214, in get_reduced_index
new_records = query_all(spec)
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/index.py", line 187, in query_all
return tuple(concat(future.result() for future in as_completed(futures)))
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/repodata.py", line 99, in query
self.load()
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/repodata.py", line 143, in load
_internal_state = self._load()
File "/Users/apple/miniconda3/lib/python3.6/site-packages/conda/core/repodata.py", line 222, in _load
with open(self.cache_path_json, 'w') as fh:
PermissionError: [Errno 13] Permission denied: '/Users/apple/miniconda3/pkgs/cache/809318c1.json'

# Fixed code
def _load(self):
    try:
        mtime = getmtime(self.cache_path_json)
    except (IOError, OSError):
        log.debug(
            "No local cache found for %s at %s", self.url_w_subdir, self.cache_path_json
        )
        if context.use_index_cache or (
            context.offline and not self.url_w_subdir.startswith("file://")
        ):
            log.debug(
                "Using cached data for %s at %s forced. Returning empty repodata.",
                self.url_w_subdir,
                self.cache_path_json,
            )
            return {
                "_package_records": (),
                "_package_dists": (),
                "_names_index": defaultdict(list),
                "_track_features_index": defaultdict(list),
            }
        else:
            mod_etag_headers = {}
    else:
        mod_etag_headers = read_mod_and_etag(self.cache_path_json)

        if context.use_index_cache:
            log.debug(
                "Using cached repodata for %s at %s because use_cache=True",
                self.url_w_subdir,
                self.cache_path_json,
            )

            _internal_state = self._read_local_repdata(
                mod_etag_headers.get("_etag"), mod_etag_headers.get("_mod")
            )
            return _internal_state

        if context.local_repodata_ttl > 1:
            max_age = context.local_repodata_ttl
        elif context.local_repodata_ttl == 1:
            max_age = get_cache_control_max_age(
                mod_etag_headers.get("_cache_control", "")
            )
        else:
            max_age = 0

        timeout = mtime + max_age - time()
        if (timeout > 0 or context.offline) and not self.url_w_subdir.startswith(
            "file://"
        ):
            log.debug(
                "Using cached repodata for %s at %s. Timeout in %d sec",
                self.url_w_subdir,
                self.cache_path_json,
                timeout,
            )
            _internal_state = self._read_local_repdata(
                mod_etag_headers.get("_etag"), mod_etag_headers.get("_mod")
            )
            return _internal_state

        log.debug(
            "Local cache timed out for %s at %s",
            self.url_w_subdir,
            self.cache_path_json,
        )

    try:
        raw_repodata_str = fetch_repodata_remote_request(
            self.url_w_credentials,
            mod_etag_headers.get("_etag"),
            mod_etag_headers.get("_mod"),
        )
    except Response304ContentUnchanged:
        log.debug(
            "304 NOT MODIFIED for '%s'. Updating mtime and loading from disk",
            self.url_w_subdir,
        )
        touch(self.cache_path_json)
        _internal_state = self._read_local_repdata(
            mod_etag_headers.get("_etag"), mod_etag_headers.get("_mod")
        )
        return _internal_state
    else:
        if not isdir(dirname(self.cache_path_json)):
            mkdir_p(dirname(self.cache_path_json))
        try:
            with open(self.cache_path_json, "w") as fh:
                fh.write(raw_repodata_str or "{}")
        except (IOError, OSError) as e:
            if e.errno in (EACCES, EPERM):
                raise NotWritableError(self.cache_path_json, e.errno, caused_by=e)
            else:
                raise
        _internal_state = self._process_raw_repodata_str(raw_repodata_str)
        self._internal_state = _internal_state
        self._pickle_me()
        return _internal_state