Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix large inner files with PAX format #36

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion securetar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,18 @@ def __init__(
def __enter__(self) -> tarfile.TarFile:
"""Start context manager tarfile."""
tar_info = tarfile.TarInfo(name=str(self._name))
tar_info.mtime = int(time.time())
if self.outer_tar.format == tarfile.PAX_FORMAT:
# Ensure we always set mtime as a float to force
# a PAX header to be written.
#
# This is necessary to
# handle large files as TarInfo.tobuf will try to
# use a shorter ustar header if we do not have at
# least one float in the tarinfo.
# https://github.com/python/cpython/blob/53b84e772cac6e4a55cebf908d6bb9c48fe254dc/Lib/tarfile.py#L1066
tar_info.mtime = time.time()
bdraco marked this conversation as resolved.
Show resolved Hide resolved
else:
tar_info.mtime = int(time.time())
self.stream = _add_stream(self.outer_tar, tar_info)
self.stream.__enter__()
return super().__enter__()
Expand Down
36 changes: 20 additions & 16 deletions tests/test_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import time
from dataclasses import dataclass
from pathlib import Path, PurePath

from unittest.mock import patch
import pytest

from securetar import (
Expand Down Expand Up @@ -377,7 +377,10 @@ def test_outer_tar_must_not_be_compressed(tmp_path: Path) -> None:
pass


def test_tar_stream(tmp_path: Path) -> None:
@pytest.mark.parametrize(
"format", [tarfile.PAX_FORMAT, tarfile.GNU_FORMAT, tarfile.USTAR_FORMAT]
)
def test_tar_stream(tmp_path: Path, format: int) -> None:
# Prepare test folder
temp_orig = tmp_path.joinpath("orig")
fixture_data = Path(__file__).parent.joinpath("fixtures/tar_data")
Expand All @@ -386,17 +389,18 @@ def test_tar_stream(tmp_path: Path) -> None:
# Create Tarfile
main_tar = tmp_path.joinpath("backup.tar")

with SecureTarFile(main_tar, "w", gzip=False) as tar_file:
tar_info = tarfile.TarInfo(name="test.txt")
with _add_stream(tar_file, tar_info) as stream:
stream.write(b"test")

# Restore
temp_new = tmp_path.joinpath("new")
with SecureTarFile(main_tar, "r", gzip=False) as tar_file:
tar_file.extractall(path=temp_new)

assert temp_new.is_dir()
test_file = temp_new.joinpath("test.txt")
assert test_file.is_file()
assert test_file.read_bytes() == b"test"
with patch.object(tarfile, "DEFAULT_FORMAT", format):
with SecureTarFile(main_tar, "w", gzip=False) as tar_file:
tar_info = tarfile.TarInfo(name="test.txt")
with _add_stream(tar_file, tar_info) as stream:
stream.write(b"test")

# Restore
temp_new = tmp_path.joinpath("new")
with SecureTarFile(main_tar, "r", gzip=False) as tar_file:
tar_file.extractall(path=temp_new)

assert temp_new.is_dir()
test_file = temp_new.joinpath("test.txt")
assert test_file.is_file()
assert test_file.read_bytes() == b"test"
Loading