From 52bee66c9f2188016f76ab2156f7caf97c100f8c Mon Sep 17 00:00:00 2001 From: yackob03 Date: Fri, 6 Dec 2013 14:23:26 -0500 Subject: [PATCH] Update the checksums to a version that supports empty layers. --- util/checksums.py | 62 ++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/util/checksums.py b/util/checksums.py index 16ba05404..5e77b7ac8 100644 --- a/util/checksums.py +++ b/util/checksums.py @@ -28,38 +28,40 @@ def compute_tarsum(fp, json_data): header_fields = ('name', 'mode', 'uid', 'gid', 'size', 'mtime', 'type', 'linkname', 'uname', 'gname', 'devmajor', 'devminor') - tar = tarfile.open(mode='r|*', fileobj=fp) + tar = None hashes = [] - for member in tar: - header = '' - for field in header_fields: - value = getattr(member, field) - if field == 'type': - field = 'typeflag' - elif field == 'name': - if member.isdir() and not value.endswith('/'): - value += '/' - header += '{0}{1}'.format(field, value) - h = None - try: - if member.size > 0: - f = tar.extractfile(member) - h = sha256_file(f, header) - else: + try: + tar = tarfile.open(mode='r|*', fileobj=fp) + for member in tar: + header = '' + for field in header_fields: + value = getattr(member, field) + if field == 'type': + field = 'typeflag' + elif field == 'name': + if member.isdir() and not value.endswith('/'): + value += '/' + header += '{0}{1}'.format(field, value) + h = None + try: + if member.size > 0: + f = tar.extractfile(member) + h = sha256_file(f, header) + else: + h = sha256_string(header) + except KeyError: h = sha256_string(header) - except KeyError: - h = sha256_string(header) - hashes.append(h) - #log = '\n+ filename: {0}\n'.format(member.name) - #log += '+ header: {0}\n'.format(header) - #log += '+ hash: {0}\n'.format(h) - #log += '*' * 16 - #logger.debug('checksums.compute_tarsum: \n{0}'.format(log)) - tar.close() - hashes.sort() + hashes.append(h) + hashes.sort() + except tarfile.ReadError as e: + if e.message != 'empty file': + # NOTE(samalba): ignore empty tarfiles but still let the tarsum + # compute with json data + raise + finally: + if tar: + tar.close() data = json_data + ''.join(hashes) - #logger.debug('checksums.compute_tarsum: ' - # 'Hashes: \n{0}\n{1}'.format('\n'.join(hashes), '-' * 16)) tarsum = 'tarsum+sha256:{0}'.format(sha256_string(data)) logger.debug('checksums.compute_tarsum: return {0}'.format(tarsum)) return tarsum @@ -86,4 +88,4 @@ if __name__ == '__main__': json_data = file(sys.argv[1]).read() fp = open(sys.argv[2]) print compute_simple(fp, json_data) - print compute_tarsum(fp, json_data) + print compute_tarsum(fp, json_data) \ No newline at end of file