2014-09-16 04:18:57 +00:00
|
|
|
import os
|
2018-03-21 20:05:27 +00:00
|
|
|
import tarfile
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2018-03-21 20:05:27 +00:00
|
|
|
import marisa_trie
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2018-03-21 20:05:27 +00:00
|
|
|
from util.registry.aufs import is_aufs_metadata, get_deleted_prefix
|
|
|
|
from util.registry.tarlayerformat import TarLayerFormat
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-10-15 19:51:34 +00:00
|
|
|
class StreamLayerMerger(TarLayerFormat):
|
2014-09-16 04:18:57 +00:00
|
|
|
""" Class which creates a generator of the combined TAR data for a set of Docker layers. """
|
2018-03-23 18:39:38 +00:00
|
|
|
def __init__(self, get_tar_stream_iterator, path_prefix=None, reporter=None):
|
|
|
|
super(StreamLayerMerger, self).__init__(get_tar_stream_iterator, path_prefix, reporter=reporter)
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-10-15 01:40:02 +00:00
|
|
|
self.path_trie = marisa_trie.Trie()
|
2018-03-21 20:05:27 +00:00
|
|
|
self.path_encountered = set()
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2018-03-21 20:05:27 +00:00
|
|
|
self.deleted_prefix_trie = marisa_trie.Trie()
|
|
|
|
self.deleted_prefixes_encountered = set()
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2018-03-21 20:05:27 +00:00
|
|
|
def after_tar_layer(self):
|
|
|
|
# Update the tries.
|
|
|
|
self.path_trie = marisa_trie.Trie(self.path_encountered)
|
|
|
|
self.deleted_prefix_trie = marisa_trie.Trie(self.deleted_prefixes_encountered)
|
2014-11-24 21:07:38 +00:00
|
|
|
|
2018-03-21 20:05:27 +00:00
|
|
|
@staticmethod
|
|
|
|
def _normalize_path(path):
|
|
|
|
return os.path.relpath(path.decode('utf-8'), './')
|
|
|
|
|
|
|
|
def _check_deleted(self, absolute):
|
|
|
|
ubsolute = unicode(absolute)
|
|
|
|
for prefix in self.deleted_prefix_trie.iter_prefixes(ubsolute):
|
|
|
|
if not os.path.relpath(ubsolute, prefix).startswith('..'):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_skipped_file(self, filename):
|
|
|
|
absolute = StreamLayerMerger._normalize_path(filename)
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2014-09-18 19:56:59 +00:00
|
|
|
# Skip metadata.
|
|
|
|
if is_aufs_metadata(absolute):
|
2018-03-21 20:05:27 +00:00
|
|
|
return True
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2018-03-21 20:05:27 +00:00
|
|
|
# Check if the file is under a deleted path.
|
|
|
|
if self._check_deleted(absolute):
|
|
|
|
return True
|
2014-09-16 04:18:57 +00:00
|
|
|
|
|
|
|
# Check if this file has already been encountered somewhere. If so,
|
|
|
|
# skip it.
|
2014-10-15 01:40:02 +00:00
|
|
|
ubsolute = unicode(absolute)
|
2014-10-15 15:57:54 +00:00
|
|
|
if ubsolute in self.path_trie:
|
2018-03-21 20:05:27 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
def should_append_file(self, filename):
|
|
|
|
if self.is_skipped_file(filename):
|
2014-09-19 16:22:54 +00:00
|
|
|
return False
|
2014-09-16 04:18:57 +00:00
|
|
|
|
2018-03-21 20:05:27 +00:00
|
|
|
absolute = StreamLayerMerger._normalize_path(filename)
|
|
|
|
|
|
|
|
# Add any prefix of deleted paths to the prefix list.
|
|
|
|
deleted_prefix = get_deleted_prefix(absolute)
|
|
|
|
if deleted_prefix is not None:
|
|
|
|
self.deleted_prefixes_encountered.add(deleted_prefix)
|
|
|
|
return False
|
2014-10-15 15:57:54 +00:00
|
|
|
|
2014-09-18 19:56:59 +00:00
|
|
|
# Otherwise, add the path to the encountered list and return it.
|
2018-03-21 20:05:27 +00:00
|
|
|
self.path_encountered.add(absolute)
|
2014-09-19 16:22:54 +00:00
|
|
|
return True
|