prompt: You will be provided with a partial code base and an issue statement explaining a problem to resolve.
  <issue>
  Build context (.tar) is not prepared properly
  Hey,
  
  This morning I've updated to version `3.1.1` however, using this version i'm getting wired error from docker-engine build:
  ```
  ERROR: Error processing tar file(exit status 1): mkdir /foodir/bardir: no such file or directory
  ```
  and the actual building does not start.
  took me some time to realise this is related to update i got this morning, 
  Reverting back to version `3.0.1`, I could build again.
  
  *NOTE*: `/foodir/bardir` is censored  due to security policy in my company,
  so for the sake of this issue, lets assume this is the context:
  - Dockerfile
  - foodir
    - bardir
      - file
  
  Also, path in error did start with `/` so i kept it there.
  
  
  </issue>
  <code>
  [start of docker/utils/build.py]
  1 import os
  2 import re
  3 
  4 from ..constants import IS_WINDOWS_PLATFORM
  5 from fnmatch import fnmatch
  6 from itertools import chain
  7 from .utils import create_archive
  8 
  9 
  10 def tar(path, exclude=None, dockerfile=None, fileobj=None, gzip=False):
  11     root = os.path.abspath(path)
  12     exclude = exclude or []
  13     return create_archive(
  14         files=sorted(exclude_paths(root, exclude, dockerfile=dockerfile)),
  15         root=root, fileobj=fileobj, gzip=gzip
  16     )
  17 
  18 
  19 _SEP = re.compile('/|\\\\') if IS_WINDOWS_PLATFORM else re.compile('/')
  20 
  21 
  22 def exclude_paths(root, patterns, dockerfile=None):
  23     """
  24     Given a root directory path and a list of .dockerignore patterns, return
  25     an iterator of all paths (both regular files and directories) in the root
  26     directory that do *not* match any of the patterns.
  27 
  28     All paths returned are relative to the root.
  29     """
  30 
  31     if dockerfile is None:
  32         dockerfile = 'Dockerfile'
  33 
  34     def split_path(p):
  35         return [pt for pt in re.split(_SEP, p) if pt and pt != '.']
  36 
  37     def normalize(p):
  38         # Leading and trailing slashes are not relevant. Yes,
  39         # "foo.py/" must exclude the "foo.py" regular file. "."
  40         # components are not relevant either, even if the whole
  41         # pattern is only ".", as the Docker reference states: "For
  42         # historical reasons, the pattern . is ignored."
  43         # ".." component must be cleared with the potential previous
  44         # component, regardless of whether it exists: "A preprocessing
  45         # step [...]  eliminates . and .. elements using Go's
  46         # filepath.".
  47         i = 0
  48         split = split_path(p)
  49         while i < len(split):
  50             if split[i] == '..':
  51                 del split[i]
  52                 if i > 0:
  53                     del split[i - 1]
  54                     i -= 1
  55             else:
  56                 i += 1
  57         return split
  58 
  59     patterns = (
  60         (True, normalize(p[1:]))
  61         if p.startswith('!') else
  62         (False, normalize(p))
  63         for p in patterns)
  64     patterns = list(reversed(list(chain(
  65         # Exclude empty patterns such as "." or the empty string.
  66         filter(lambda p: p[1], patterns),
  67         # Always include the Dockerfile and .dockerignore
  68         [(True, split_path(dockerfile)), (True, ['.dockerignore'])]))))
  69     return set(walk(root, patterns))
  70 
  71 
  72 def walk(root, patterns, default=True):
  73     """
  74     A collection of file lying below root that should be included according to
  75     patterns.
  76     """
  77 
  78     def match(p):
  79         if p[1][0] == '**':
  80             rec = (p[0], p[1][1:])
  81             return [p] + (match(rec) if rec[1] else [rec])
  82         elif fnmatch(f, p[1][0]):
  83             return [(p[0], p[1][1:])]
  84         else:
  85             return []
  86 
  87     for f in os.listdir(root):
  88         cur = os.path.join(root, f)
  89         # The patterns if recursing in that directory.
  90         sub = list(chain(*(match(p) for p in patterns)))
  91         # Whether this file is explicitely included / excluded.
  92         hit = next((p[0] for p in sub if not p[1]), None)
  93         # Whether this file is implicitely included / excluded.
  94         matched = default if hit is None else hit
  95         sub = list(filter(lambda p: p[1], sub))
  96         if os.path.isdir(cur):
  97             # Entirely skip directories if there are no chance any subfile will
  98             # be included.
  99             if all(not p[0] for p in sub) and not matched:
  100                 continue
  101             # I think this would greatly speed up dockerignore handling by not
  102             # recursing into directories we are sure would be entirely
  103             # included, and only yielding the directory itself, which will be
  104             # recursively archived anyway. However the current unit test expect
  105             # the full list of subfiles and I'm not 100% sure it would make no
  106             # difference yet.
  107             # if all(p[0] for p in sub) and matched:
  108             #     yield f
  109             #     continue
  110             children = False
  111             for r in (os.path.join(f, p) for p in walk(cur, sub, matched)):
  112                 yield r
  113                 children = True
  114             # The current unit tests expect directories only under those
  115             # conditions. It might be simplifiable though.
  116             if (not sub or not children) and hit or hit is None and default:
  117                 yield f
  118         elif matched:
  119             yield f
  120
  [end of docker/utils/build.py]
  </code>
  
  I need you to solve this issue by generating a single patch file that I can apply directly to this repository using git apply. Please respond with a single patch file in the following format.
  <patch>
  diff --git a/file.py b/file.py
  --- a/file.py
  +++ b/file.py
  @@ -1,27 +1,35 @@
   def euclidean(a, b):
  -    while b:
  -        a, b = b, a % b
  -    return a
  +    if b == 0:
  +        return a
  +    return euclidean(b, a % b)
   
   
   def bresenham(x0, y0, x1, y1):
       points = []
       dx = abs(x1 - x0)
       dy = abs(y1 - y0)
  -    sx = 1 if x0 < x1 else -1
  -    sy = 1 if y0 < y1 else -1
  -    err = dx - dy
  +    x, y = x0, y0
  +    sx = -1 if x0 > x1 else 1
  +    sy = -1 if y0 > y1 else 1
   
  -    while True:
  -        points.append((x0, y0))
  -        if x0 == x1 and y0 == y1:
  -            break
  -        e2 = 2 * err
  -        if e2 > -dy:
  -            err -= dy
  -            x0 += sx
  -        if e2 < dx:
  -            err += dx
  -            y0 += sy
  +    if dx > dy:
  +        err = dx / 2.0
  +        while x != x1:
  +            points.append((x, y))
  +            err -= dy
  +            if err < 0:
  +                y += sy
  +                err += dx
  +            x += sx
  +    else:
  +        err = dy / 2.0
  +        while y != y1:
  +            points.append((x, y))
  +            err -= dx
  +            if err < 0:
  +                x += sx
  +                err += dy
  +            y += sy
  +
  +    points.append((x, y))
       return points
  
  </patch>
golden_diff: diff --git a/docker/utils/build.py b/docker/utils/build.py
  --- a/docker/utils/build.py
  +++ b/docker/utils/build.py
  @@ -93,7 +93,7 @@
           # Whether this file is implicitely included / excluded.
           matched = default if hit is None else hit
           sub = list(filter(lambda p: p[1], sub))
  -        if os.path.isdir(cur):
  +        if os.path.isdir(cur) and not os.path.islink(cur):
               # Entirely skip directories if there are no chance any subfile will
               # be included.
               if all(not p[0] for p in sub) and not matched:
  
verification_info: {"golden_diff": "diff --git a/docker/utils/build.py b/docker/utils/build.py\n--- a/docker/utils/build.py\n+++ b/docker/utils/build.py\n@@ -93,7 +93,7 @@\n         # Whether this file is implicitely included / excluded.\n         matched = default if hit is None else hit\n         sub = list(filter(lambda p: p[1], sub))\n-        if os.path.isdir(cur):\n+        if os.path.isdir(cur) and not os.path.islink(cur):\n             # Entirely skip directories if there are no chance any subfile will\n             # be included.\n             if all(not p[0] for p in sub) and not matched:\n", "issue": "Build context (.tar) is not prepared properly\nHey,\r\n\r\nThis morning I've updated to version `3.1.1` however, using this version i'm getting wired error from docker-engine build:\r\n```\r\nERROR: Error processing tar file(exit status 1): mkdir /foodir/bardir: no such file or directory\r\n```\r\nand the actual building does not start.\r\ntook me some time to realise this is related to update i got this morning, \r\nReverting back to version `3.0.1`, I could build again.\r\n\r\n*NOTE*: `/foodir/bardir` is censored  due to security policy in my company,\r\nso for the sake of this issue, lets assume this is the context:\r\n- Dockerfile\r\n- foodir\r\n  - bardir\r\n    - file\r\n\r\nAlso, path in error did start with `/` so i kept it there.\r\n\n", "before_files": [{"content": "import os\nimport re\n\nfrom ..constants import IS_WINDOWS_PLATFORM\nfrom fnmatch import fnmatch\nfrom itertools import chain\nfrom .utils import create_archive\n\n\ndef tar(path, exclude=None, dockerfile=None, fileobj=None, gzip=False):\n    root = os.path.abspath(path)\n    exclude = exclude or []\n    return create_archive(\n        files=sorted(exclude_paths(root, exclude, dockerfile=dockerfile)),\n        root=root, fileobj=fileobj, gzip=gzip\n    )\n\n\n_SEP = re.compile('/|\\\\\\\\') if IS_WINDOWS_PLATFORM else re.compile('/')\n\n\ndef exclude_paths(root, patterns, dockerfile=None):\n    \"\"\"\n    Given a root directory path and a list of .dockerignore patterns, return\n    an iterator of all paths (both regular files and directories) in the root\n    directory that do *not* match any of the patterns.\n\n    All paths returned are relative to the root.\n    \"\"\"\n\n    if dockerfile is None:\n        dockerfile = 'Dockerfile'\n\n    def split_path(p):\n        return [pt for pt in re.split(_SEP, p) if pt and pt != '.']\n\n    def normalize(p):\n        # Leading and trailing slashes are not relevant. Yes,\n        # \"foo.py/\" must exclude the \"foo.py\" regular file. \".\"\n        # components are not relevant either, even if the whole\n        # pattern is only \".\", as the Docker reference states: \"For\n        # historical reasons, the pattern . is ignored.\"\n        # \"..\" component must be cleared with the potential previous\n        # component, regardless of whether it exists: \"A preprocessing\n        # step [...]  eliminates . and .. elements using Go's\n        # filepath.\".\n        i = 0\n        split = split_path(p)\n        while i < len(split):\n            if split[i] == '..':\n                del split[i]\n                if i > 0:\n                    del split[i - 1]\n                    i -= 1\n            else:\n                i += 1\n        return split\n\n    patterns = (\n        (True, normalize(p[1:]))\n        if p.startswith('!') else\n        (False, normalize(p))\n        for p in patterns)\n    patterns = list(reversed(list(chain(\n        # Exclude empty patterns such as \".\" or the empty string.\n        filter(lambda p: p[1], patterns),\n        # Always include the Dockerfile and .dockerignore\n        [(True, split_path(dockerfile)), (True, ['.dockerignore'])]))))\n    return set(walk(root, patterns))\n\n\ndef walk(root, patterns, default=True):\n    \"\"\"\n    A collection of file lying below root that should be included according to\n    patterns.\n    \"\"\"\n\n    def match(p):\n        if p[1][0] == '**':\n            rec = (p[0], p[1][1:])\n            return [p] + (match(rec) if rec[1] else [rec])\n        elif fnmatch(f, p[1][0]):\n            return [(p[0], p[1][1:])]\n        else:\n            return []\n\n    for f in os.listdir(root):\n        cur = os.path.join(root, f)\n        # The patterns if recursing in that directory.\n        sub = list(chain(*(match(p) for p in patterns)))\n        # Whether this file is explicitely included / excluded.\n        hit = next((p[0] for p in sub if not p[1]), None)\n        # Whether this file is implicitely included / excluded.\n        matched = default if hit is None else hit\n        sub = list(filter(lambda p: p[1], sub))\n        if os.path.isdir(cur):\n            # Entirely skip directories if there are no chance any subfile will\n            # be included.\n            if all(not p[0] for p in sub) and not matched:\n                continue\n            # I think this would greatly speed up dockerignore handling by not\n            # recursing into directories we are sure would be entirely\n            # included, and only yielding the directory itself, which will be\n            # recursively archived anyway. However the current unit test expect\n            # the full list of subfiles and I'm not 100% sure it would make no\n            # difference yet.\n            # if all(p[0] for p in sub) and matched:\n            #     yield f\n            #     continue\n            children = False\n            for r in (os.path.join(f, p) for p in walk(cur, sub, matched)):\n                yield r\n                children = True\n            # The current unit tests expect directories only under those\n            # conditions. It might be simplifiable though.\n            if (not sub or not children) and hit or hit is None and default:\n                yield f\n        elif matched:\n            yield f\n", "path": "docker/utils/build.py"}]}