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
19 changes: 18 additions & 1 deletion libarchive/entry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from contextlib import contextmanager
from ctypes import create_string_buffer, string_at
from ctypes import byref, c_char_p, create_string_buffer, c_size_t, c_void_p, string_at
from enum import IntEnum
import math

Expand Down Expand Up @@ -493,6 +493,23 @@ def set_stored_digest(self, algorithm_name, value):
f"{ffi.libarchive_path}) doesn't support {algorithm_name} digests"
) from None

def get_xattrs(self):
"""Returns the extended attributes.
"""
xattrs = {}

ffi.entry_xattr_reset(self._entry_p)
xattr_next = ffi.entry_xattr_next

key, value, value_size = c_char_p(), c_void_p(), c_size_t()
key_p, value_p, value_size_p = byref(key), byref(value), byref(value_size)

while True:
return_code = xattr_next(self._entry_p, key_p, value_p, value_size_p)
if return_code != ffi.ARCHIVE_OK:
break
yield key.value, string_at(value.value, value_size.value)


class ConsumedArchiveEntry(ArchiveEntry):

Expand Down
4 changes: 4 additions & 0 deletions libarchive/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ def get_write_filter_function(filter_name):
ffi('entry_uname_w', [c_archive_entry_p], c_wchar_p)
ffi('entry_gname_w', [c_archive_entry_p], c_wchar_p)

ffi('entry_xattr_count', [c_archive_entry_p], c_uint)
ffi('entry_xattr_reset', [c_archive_entry_p], c_uint)
ffi('entry_xattr_next', [c_archive_entry_p, POINTER(c_char_p), POINTER(c_void_p), POINTER(c_size_t)], c_uint)

ffi('entry_set_size', [c_archive_entry_p, c_longlong], None)
ffi('entry_set_filetype', [c_archive_entry_p, c_uint], None)
ffi('entry_set_uid', [c_archive_entry_p, c_longlong], None)
Expand Down
4 changes: 3 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ def get_entries(location):
'isfifo': entry.isfifo,
'isdev': entry.isdev,
'uid': entry.uid,
'gid': entry.gid
'gid': entry.gid,
# Convert to str because JSON cannot differentiate between str and bytes.
'xattrs': {key.decode('utf-8'): value.decode('utf-8') for key,value in entry.get_xattrs()},
}


Expand Down
Binary file added tests/data/file-with-attribute.bsd.tar
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/data/file-with-attribute.bsd.tar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"gid": 1000,
"isblk": false,
"ischr": false,
"isdev": false,
"isdir": false,
"isfifo": false,
"islnk": false,
"isreg": true,
"issym": false,
"linkpath": null,
"mode": "rwx------",
"mtime": 1748164058.1061919,
"path": "foo",
"size": 4,
"uid": 1000,
"xattrs": { "user.tags": "mytag" }
}
]
Loading