Merge pull request #74 from chenxiaolong/sparse

Produce sparse output files
This commit is contained in:
Andrew Gunnerson
2023-03-02 16:04:13 -05:00
committed by GitHub
2 changed files with 26 additions and 1 deletions
+5 -1
View File
@@ -659,7 +659,11 @@ class _TeeFileDescriptor:
self.capture.write(data)
else:
for stream in self.streams:
stream.write(data)
# Naive hole punching to create sparse files
if stream is self.backing and util.is_zero(data):
stream.seek(len(data), os.SEEK_CUR)
else:
stream.write(data)
return len(data)
+21
View File
@@ -3,6 +3,9 @@ import os
import tempfile
_ZERO_BLOCK = memoryview(b'\0' * 16384)
@contextlib.contextmanager
def open_output_file(path):
'''
@@ -145,3 +148,21 @@ def read_exact(f, size: int) -> bytes:
return bytes(data)
else:
return data
def is_zero(data):
'''
Check if all bytes in the bytes-like object are null bytes.
'''
view = memoryview(data)
while view:
n = min(len(view), len(_ZERO_BLOCK))
if view[:n] != _ZERO_BLOCK[:n]:
return False
view = view[n:]
return True