pkggzip.py revision 3339
461N/A#!/usr/bin/python
461N/A#
461N/A# CDDL HEADER START
461N/A#
461N/A# The contents of this file are subject to the terms of the
461N/A# Common Development and Distribution License (the "License").
461N/A# You may not use this file except in compliance with the License.
461N/A#
461N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
461N/A# or http://www.opensolaris.org/os/licensing.
461N/A# See the License for the specific language governing permissions
461N/A# and limitations under the License.
461N/A#
461N/A# When distributing Covered Code, include this CDDL HEADER in each
461N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
461N/A# If applicable, add the following below this CDDL HEADER, with the
461N/A# fields enclosed by brackets "[]" replaced with your own identifying
461N/A# information: Portions Copyright [yyyy] [name of copyright owner]
461N/A#
461N/A# CDDL HEADER END
461N/A#
461N/A
461N/A#
3339N/A# Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
461N/A#
461N/A#ident "%Z%%M% %I% %E% SMI"
461N/A
461N/Aimport gzip
461N/A
461N/Aclass PkgGzipFile(gzip.GzipFile):
461N/A """This is a version of GzipFile that does not include a file
461N/A pathname or timestamp in the gzip header. This allows us to get
461N/A deterministic gzip files on compression, so that we can reliably
542N/A use a cryptographic hash on the compressed content."""
461N/A
461N/A def __init__(self, filename=None, mode=None, compresslevel=9,
461N/A fileobj=None):
461N/A
461N/A gzip.GzipFile.__init__(self, filename, mode, compresslevel,
461N/A fileobj)
461N/A
542N/A #
542N/A # This is a gzip header conforming to RFC1952. The first two bytes
542N/A # (\037,\213) are the gzip magic number. The third byte is the
542N/A # compression method (8, deflate). The fourth byte is the flag byte
542N/A # (0), which indicates that no FNAME, FCOMMENT or other extended data
542N/A # is present. Bytes 5-8 are the MTIME field, zeroed in this case.
542N/A # Byte 9 is the XFL (Extra Flags) field, set to 2 (compressor used
542N/A # max compression). The final bit is the OS type, set to 255 (for
542N/A # "unknown").
3339N/A magic = b"\037\213\010\000\000\000\000\000\002\377"
542N/A
461N/A def _write_gzip_header(self):
542N/A self.fileobj.write(self.magic)
542N/A
542N/A @staticmethod
542N/A def test_is_pkggzipfile(path):
542N/A f = open(path, "rb")
542N/A hdrstr = f.read(len(PkgGzipFile.magic))
542N/A f.close()
542N/A return (hdrstr == PkgGzipFile.magic)