Skip to content

Commit

Permalink
Merge pull request #105 from goldmann/gh-104-broken-symlinks
Browse files Browse the repository at this point in the history
Better handling of symlinks pointing to non-existing locations
  • Loading branch information
goldmann authored Jul 22, 2016
2 parents e40565b + d568d17 commit afd81ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
12 changes: 11 additions & 1 deletion docker_squash/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,22 @@ def _squash_layers(self, layers_to_squash, layers_to_move):
skipped_hard_links.append(member)
continue

# If we have a symlink to a location that does not exist in the
# squashed tar (yet), we don't know if it's a file or a directory.
# In case it's a directory - we cannot add any new files under
# this directory because extration will fail at the import time.
# Assume here the target is directory (if it's a file it won't
# break) and skip all files that should be added under this directory.
if member.issym() and member.linkname not in squashed_files:
to_skip.append(os.path.join(member.name, "/"))
continue

if member.isfile():
# Finally add the file to archive
squashed_tar.addfile(
member, layer_tar.extractfile(member))
else:
# Special cases: symlinks and hard links, and other files, we skip the file itself
# Special case: other(?) files, we skip the file itself
squashed_tar.addfile(member)

# We added a file to the squashed tar, so let's note it
Expand Down
15 changes: 15 additions & 0 deletions tests/test_integ_squash.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,21 @@ def test_should_squash_correctly_hardlinks(self):
squashed_image.assertFileExists('usr/libexec/git-core/git-remote-ftp')
squashed_image.assertFileExists('usr/libexec/git-core/git-remote-http')

# https://github.com/goldmann/docker-squash/issues/104
def test_should_handle_symlinks_to_nonexisting_locations(self):
dockerfile = '''
FROM %s
RUN mkdir -p /var/log
RUN touch /var/log/somelog
RUN mv /var/log /var/log-removed && ln -sf /data/var/log /var/log
RUN rm -rf /var/log-removed
''' % TestIntegSquash.BUSYBOX_IMAGE

with self.Image(dockerfile) as image:
with self.SquashedImage(image, 3, numeric=True) as squashed_image:
self.assertEqual(
len(squashed_image.layers), len(image.layers) - 2)

def test_should_squash_every_layer_from_an_image_from_docker_hub(self):
dockerfile = '''
FROM python:3.5.1-alpine
Expand Down

0 comments on commit afd81ae

Please sign in to comment.