diff --git a/libarchive/entry.py b/libarchive/entry.py index 863dbc7..508c7d6 100644 --- a/libarchive/entry.py +++ b/libarchive/entry.py @@ -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) @@ -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") diff --git a/libarchive/ffi.py b/libarchive/ffi.py index d960b59..1f7e6a8 100644 --- a/libarchive/ffi.py +++ b/libarchive/ffi.py @@ -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) diff --git a/tests/test_symlink_from_memory.py b/tests/test_symlink_from_memory.py new file mode 100644 index 0000000..6fc14bd --- /dev/null +++ b/tests/test_symlink_from_memory.py @@ -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'