Exemplo n.º 1
0
    def __init__(self,
                 p,
                 metadata={},
                 codec_name="none",
                 magic=MAGIC,
                 bad_header_checksum=False,
                 header_extra=""):
        self.f = open(p, "w+b")

        self.hasher = hashlib.sha256()
        assert len(magic) == len(MAGIC)
        self._magic = magic
        self._bad_header_checksum = bad_header_checksum
        self.f.write(INCOMPLETE_MAGIC)
        self._header = {
            "root_index_offset": 2**63 - 1,
            "root_index_length": 0,
            "total_file_length": 0,
            "sha256": b"\x00" * 32,
            "codec": codec_name,
            "metadata": metadata,
        }
        self._header_extra = header_extra
        encoded_header = _encode_header(self._header) + self._header_extra
        self._header_length = len(encoded_header)
        self.f.write(
            struct.pack(header_data_length_format, len(encoded_header)))
        self._header_offset = self.f.tell()
        self.f.write(encoded_header)
        self.f.write(encoded_crc64xz(encoded_header))
        self._have_root = False
Exemplo n.º 2
0
    def __init__(self, p, metadata={}, codec_name="none", magic=MAGIC,
                 bad_header_checksum=False, header_extra=""):
        self.f = open(p, "w+b")

        self.hasher = hashlib.sha256()
        assert len(magic) == len(MAGIC)
        self._magic = magic
        self._bad_header_checksum = bad_header_checksum
        self.f.write(INCOMPLETE_MAGIC)
        self._header = {
            "root_index_offset": 2 ** 63 - 1,
            "root_index_length": 0,
            "total_file_length": 0,
            "sha256": b"\x00" * 32,
            "codec": codec_name,
            "metadata": metadata,
            }
        self._header_extra = header_extra
        encoded_header = _encode_header(self._header) + self._header_extra
        self._header_length = len(encoded_header)
        self.f.write(struct.pack(header_data_length_format,
                                 len(encoded_header)))
        self._header_offset = self.f.tell()
        self.f.write(encoded_header)
        self.f.write(encoded_crc64xz(encoded_header))
        self._have_root = False
Exemplo n.º 3
0
 def close(self, header_overrides={}):
     assert self._have_root
     self.f.seek(0, 2)
     self._header["total_file_length"] = self.f.tell()
     self._header["sha256"] = self.hasher.digest()
     self._header.update(header_overrides)
     encoded_header = _encode_header(self._header) + self._header_extra
     assert len(encoded_header) == self._header_length
     self.f.seek(self._header_offset)
     self.f.write(encoded_header)
     checksum = encoded_crc64xz(encoded_header)
     if self._bad_header_checksum:
         checksum = b"\x00" * len(checksum)
     self.f.write(checksum)
     self.f.seek(0)
     self.f.write(self._magic)
     self.f.flush()
     self.f.close()
Exemplo n.º 4
0
 def close(self, header_overrides={}):
     assert self._have_root
     self.f.seek(0, 2)
     self._header["total_file_length"] = self.f.tell()
     self._header["sha256"] = self.hasher.digest()
     self._header.update(header_overrides)
     encoded_header = _encode_header(self._header) + self._header_extra
     assert len(encoded_header) == self._header_length
     self.f.seek(self._header_offset)
     self.f.write(encoded_header)
     checksum = encoded_crc64xz(encoded_header)
     if self._bad_header_checksum:
         checksum = b"\x00" * len(checksum)
     self.f.write(checksum)
     self.f.seek(0)
     self.f.write(self._magic)
     self.f.flush()
     self.f.close()