Skip to content

Commit c90c437

Browse files
committed
add support for setting the uid and gid of entries
closes #115
1 parent 0c06347 commit c90c437

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

libarchive/entry.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ def filetype(self):
4242
def uid(self):
4343
return ffi.entry_uid(self._entry_p)
4444

45+
@uid.setter
46+
def uid(self, uid):
47+
ffi.entry_set_uid(self._entry_p, uid)
48+
4549
@property
4650
def gid(self):
4751
return ffi.entry_gid(self._entry_p)
4852

53+
@gid.setter
54+
def gid(self, gid):
55+
ffi.entry_set_gid(self._entry_p, gid)
56+
4957
def get_blocks(self, block_size=ffi.page_size):
5058
archive_p = self._archive_p
5159
buf = create_string_buffer(block_size)

libarchive/ffi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ def get_write_filter_function(filter_name):
195195

196196
ffi('entry_set_size', [c_archive_entry_p, c_longlong], None)
197197
ffi('entry_set_filetype', [c_archive_entry_p, c_uint], None)
198+
ffi('entry_set_uid', [c_archive_entry_p, c_longlong], None)
199+
ffi('entry_set_gid', [c_archive_entry_p, c_longlong], None)
198200
ffi('entry_set_perm', [c_archive_entry_p, c_int], None)
199201
ffi('entry_set_atime', [c_archive_entry_p, c_time_t, c_long], None)
200202
ffi('entry_set_mtime', [c_archive_entry_p, c_time_t, c_long], None)

libarchive/write.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def add_file_from_memory(
7878
self, entry_path, entry_size, entry_data,
7979
filetype=REGULAR_FILE, permission=DEFAULT_UNIX_PERMISSION,
8080
atime=None, mtime=None, ctime=None, birthtime=None,
81+
uid=None, gid=None,
8182
):
8283
""""Add file from memory to archive.
8384
@@ -99,6 +100,8 @@ def add_file_from_memory(
99100
birthtime (int | Tuple[int]):
100101
the file's birth time (for archive formats that support it),
101102
in seconds or as a tuple (seconds, nanoseconds)
103+
uid (int): the file owner's identifier
104+
gid (int): the file group's identifier
102105
"""
103106
archive_pointer = self._pointer
104107

@@ -117,6 +120,11 @@ def add_file_from_memory(
117120
entry_set_filetype(archive_entry_pointer, filetype)
118121
entry_set_perm(archive_entry_pointer, permission)
119122

123+
if uid is not None:
124+
archive_entry.uid = uid
125+
if gid is not None:
126+
archive_entry.gid = gid
127+
120128
if atime is not None:
121129
if not isinstance(atime, tuple):
122130
atime = (atime, 0)

tests/test_rwx.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ def write_callback(data):
161161
with libarchive.custom_writer(write_callback, archfmt) as archive:
162162
archive.add_file_from_memory(
163163
entry_path, entry_size, entry_data,
164-
atime=atime, mtime=mtime, ctime=ctime, birthtime=btime
164+
atime=atime, mtime=mtime, ctime=ctime, birthtime=btime,
165+
uid=1000, gid=1000,
165166
)
166167

167168
buf = b''.join(blocks)
@@ -178,3 +179,5 @@ def write_callback(data):
178179
assert archive_entry.birthtime in (
179180
btime[0], format_time(*btime)
180181
)
182+
assert archive_entry.uid == 1000
183+
assert archive_entry.gid == 1000

0 commit comments

Comments
 (0)