Skip to content
Open
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
14 changes: 11 additions & 3 deletions libarchive/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def modify(self, header_codec=None, **attributes):
"""
if header_codec:
self.header_codec = header_codec
if 'filetype' in attributes:
self.filetype = attributes.pop('filetype')
for name, value in attributes.items():
setattr(self, name, value)

Expand Down Expand Up @@ -362,10 +364,16 @@ def linkpath(self):
def linkpath(self, value):
if not isinstance(value, bytes):
value = value.encode(self.header_codec)
if self.header_codec == 'utf-8':
ffi.entry_update_link_utf8(self._entry_p, value)
if self.issym:
if self.header_codec == 'utf-8':
ffi.entry_update_symlink_utf8(self._entry_p, value)
else:
ffi.entry_copy_symlink(self._entry_p, value)
else:
ffi.entry_copy_link(self._entry_p, value)
if self.header_codec == 'utf-8':
ffi.entry_update_hardlink_utf8(self._entry_p, value)
else:
ffi.entry_copy_hardlink(self._entry_p, value)

# aliases for compatibility with the standard `tarfile` module
path = property(pathname.fget, pathname.fset, doc="alias of pathname")
Expand Down
4 changes: 4 additions & 0 deletions libarchive/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ def get_write_filter_function(filter_name):
ffi('entry_update_pathname_utf8', [c_archive_entry_p, c_char_p], c_int, check_int)
ffi('entry_copy_link', [c_archive_entry_p, c_char_p], None)
ffi('entry_update_link_utf8', [c_archive_entry_p, c_char_p], c_int, check_int)
ffi('entry_copy_symlink', [c_archive_entry_p, c_char_p], None)
ffi('entry_update_symlink_utf8', [c_archive_entry_p, c_char_p], c_int, check_int)
ffi('entry_copy_hardlink', [c_archive_entry_p, c_char_p], None)
ffi('entry_update_hardlink_utf8', [c_archive_entry_p, c_char_p], c_int, check_int)
ffi('entry_copy_uname', [c_archive_entry_p, c_char_p], None)
ffi('entry_update_uname_utf8', [c_archive_entry_p, c_char_p], c_int, check_int)
ffi('entry_copy_gname', [c_archive_entry_p, c_char_p], None)
Expand Down
103 changes: 103 additions & 0 deletions tests/test_symlink_from_memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import io
import tarfile

import pytest

from libarchive import custom_writer, memory_reader
from libarchive.entry import ArchiveEntry, FileType


@pytest.mark.parametrize('codec,target', [
('utf-8', 'usr/sbin'),
('utf-8', 'données'),
('utf-8', b'usr/sbin'),
('latin-1', 'données'),
('latin-1', b'donn\xe9es'),
])
def test_new_symlink_target(codec, target):
entry = ArchiveEntry(filetype=FileType.SYMBOLINK_LINK, header_codec=codec)
entry.linkpath = target
expected = target.decode(codec) if isinstance(target, bytes) else target
assert entry.issym
assert not entry.islnk
assert entry.linkpath == expected
entry.linkname = 'replacement'
assert entry.linkpath == 'replacement'


@pytest.mark.parametrize('codec', ['utf-8', 'latin-1'])
def test_new_hardlink_target(codec):
entry = ArchiveEntry(filetype=FileType.REGULAR_FILE, header_codec=codec,
linkpath='target')
assert entry.islnk
assert not entry.issym
assert entry.linkpath == 'target'


def test_symlink_attributes_in_reverse_order():
entry = ArchiveEntry(linkpath='target',
filetype=FileType.SYMBOLINK_LINK)
assert entry.issym
assert not entry.islnk
assert entry.linkpath == 'target'


@pytest.mark.parametrize('codec', ['utf-8', 'latin-1'])
@pytest.mark.parametrize('link_type', [tarfile.SYMTYPE, tarfile.LNKTYPE])
def test_existing_link_target(codec, link_type):
stream = io.BytesIO()
with tarfile.open(fileobj=stream, mode='w') as archive:
info = tarfile.TarInfo('link')
info.type = link_type
info.linkname = 'original'
archive.addfile(info)
with memory_reader(stream.getvalue(), header_codec=codec) as archive:
entry = next(iter(archive))
entry.linkpath = 'replacement'
assert entry.linkpath == 'replacement'
assert entry.issym == (link_type == tarfile.SYMTYPE)
assert entry.islnk == (link_type == tarfile.LNKTYPE)


@pytest.mark.parametrize('format_name', ['ustar', 'gnutar'])
def test_memory_writer_hardlink_target(format_name):
stream = io.BytesIO()
with custom_writer(stream.write, format_name) as archive:
archive.add_file_from_memory('target', 4, b'data')
archive.add_file_from_memory('link', 0, b'', linkpath='target')
with tarfile.open(fileobj=io.BytesIO(stream.getvalue())) as archive:
target = archive.getmember('target')
link = archive.getmember('link')
assert target.isfile()
assert archive.extractfile(target).read() == b'data'
assert link.islnk()
assert not link.issym()
assert link.linkname == 'target'
assert archive.extractfile(link).read() == b'data'


@pytest.mark.parametrize('format_name', ['cpio_newc', 'ustar'])
def test_memory_writer_symlink_target(format_name):
stream = io.BytesIO()
with custom_writer(stream.write, format_name) as archive:
archive.add_file_from_memory(
'sbin', 0, b'', filetype=FileType.SYMBOLINK_LINK,
linkpath='usr/sbin',
)
data = stream.getvalue()
if format_name == 'ustar':
with tarfile.open(fileobj=io.BytesIO(data)) as archive:
entry = archive.getmember('sbin')
assert entry.issym()
assert not entry.islnk()
assert entry.linkname == 'usr/sbin'
else:
# A newc header has thirteen eight-character hexadecimal fields.
assert data[:6] == b'070701'
fields = [int(data[i:i + 8], 16) for i in range(6, 110, 8)]
mode, size, namesize = fields[1], fields[6], fields[11]
assert mode & 0o170000 == FileType.SYMBOLINK_LINK
assert data[110:110 + namesize] == b'sbin\0'
payload_offset = (110 + namesize + 3) & ~3
assert size == len(b'usr/sbin')
assert data[payload_offset:payload_offset + size] == b'usr/sbin'