Repository: conda/conda

Issue:
Can't build from a symbolic link -- UNHELPFUL error message
_From @remram44 on July 19, 2018 21:41_

### Actual Behavior

```
conda.CondaError: Downloaded bytes did not match Content-Length
  url: file:///tmp/rr_conda_Wg2K6Htj/usagestats.tar.gz
  target_path: /tmp/rr_conda_Wg2K6Htj/croot/src_cache/usagestats.tar.gz
  Content-Length: 44
  downloaded bytes: 14374
```
when `/tmp/rr_conda_Wg2K6Htj/usagestats.tar.gz` is a symbolic link

### Expected Behavior

`file://` URLs to symbolic links in recipes should not break with this unhelpful error (no Content-Length is involved! This is not even HTTP!)

### Other info

This is probably what I was trying to troubleshoot in https://github.com/conda/conda/issues/4134#issuecomment-314097550. The reported size doesn't match the compressed size or the decompressed size, because it is the length of the path in bytes.

You should probably change an `lstat()` to a `stat()` somewhere in your size-checking logic.

### Steps to Reproduce

Point a recipe's `source.url` to a `file://` URL that is a symbolic link.

### `Output of conda info`

```
     active environment : base
    active env location : /home/remram/miniconda
            shell level : 1
       user config file : /home/remram/.condarc
 populated config files : /home/remram/.condarc
          conda version : 4.5.8
    conda-build version : 3.10.9
         python version : 3.6.4.final.0
       base environment : /home/remram/miniconda  (writable)
           channel URLs : https://conda.anaconda.org/conda-forge/linux-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://conda.anaconda.org/vida-nyu/linux-64
                          https://conda.anaconda.org/vida-nyu/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/linux-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/linux-64
                          https://repo.anaconda.com/pkgs/pro/noarch
          package cache : /home/remram/miniconda/pkgs
                          /home/remram/.conda/pkgs
       envs directories : /home/remram/miniconda/envs
                          /home/remram/.conda/envs
               platform : linux-64
             user-agent : conda/4.5.8 requests/2.18.4 CPython/3.6.4 Linux/4.15.0-24-generic ubuntu/18.04 glibc/2.27
                UID:GID : 1000:1000
             netrc file : None
           offline mode : False
```

_Copied from original issue: conda/conda-build#3031_


Discussion:
Thanks for reporting this.  I think the issue is in conda, though.  Conda has the file download functionality - conda-build does nothing on checking file size when downloading things.  Conda-build just calls out to conda.
_From @remram44 on July 20, 2018 1:0_

Should I duplicate the issue there?

>

@remram44 Can you help us out by sharing a specific set of steps that reproduce the behavior you're seeing?  Start with `docker run --rm -it continuumio/miniconda3 /bin/bash`.
Happy to! This should do:
```
docker run --rm -it continuumio/miniconda3 /bin/bash
conda install conda-build
mkdir /data && cd /data
curl -Ls https://github.com/conda/conda/files/2214610/conda-repro-7559.tar.gz | tar zx
conda build usagestats
```

[conda-repro-7559.tar.gz](https://github.com/conda/conda/files/2214610/conda-repro-7559.tar.gz) (16kB) contains the recipe, tarball and symlink; make sure you uncompress in `/data` because that's what the `file://` URL in the recipe points to.

You will see:
```
conda.CondaError: Downloaded bytes did not match Content-Length
  url: file:///data/usagestats.tar.gz
  target_path: /opt/conda/conda-bld/src_cache/usagestats.tar.gz
  Content-Length: 21
  downloaded bytes: 14369
```

Resolving patch:
diff --git a/conda/gateways/connection/adapters/localfs.py b/conda/gateways/connection/adapters/localfs.py
--- a/conda/gateways/connection/adapters/localfs.py
+++ b/conda/gateways/connection/adapters/localfs.py
@@ -5,7 +5,7 @@
 import json
 from logging import getLogger
 from mimetypes import guess_type
-from os import lstat
+from os import stat
 from tempfile import SpooledTemporaryFile
 
 from .. import BaseAdapter, CaseInsensitiveDict, Response
@@ -25,7 +25,7 @@ def send(self, request, stream=None, timeout=None, verify=None, cert=None, proxi
         resp.url = request.url
 
         try:
-            stats = lstat(pathname)
+            stats = stat(pathname)
         except (IOError, OSError) as exc:
             resp.status_code = 404
             message = {


Test patch: