Skip to content

Commit 6fecf8b

Browse files
nvinsonChangaco
authored andcommitted
Add bindings to change symlink read behavior.
Add bindings to change symlink read behavior. Unfortunately, libarchive does not seem to have a way of getting the current setting, so only the setter was implemented. Closes: #132 Signed-off-by: Nicholas Vinson <nvinson234@gmail.com>
1 parent 1a5b505 commit 6fecf8b

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

libarchive/ffi.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
ARCHIVE_FAILED = -25 # Current operation cannot complete.
3636
ARCHIVE_FATAL = -30 # No more operations are possible.
3737

38+
ARCHIVE_SYMLINK_MODE_HYBRID = 'H'
39+
ARCHIVE_SYMLINK_MODE_LOGICAL = 'L'
40+
ARCHIVE_SYMLINK_MODE_PHYSICAL = 'P'
3841

3942
# Callback types
4043

@@ -286,6 +289,9 @@ def get_write_filter_function(filter_name):
286289
ffi('read_disk_open', [c_archive_p, c_char_p], c_int, check_int)
287290
ffi('read_disk_open_w', [c_archive_p, c_wchar_p], c_int, check_int)
288291
ffi('read_disk_descend', [c_archive_p], c_int, check_int)
292+
ffi('read_disk_set_symlink_hybrid', [c_archive_p], c_int)
293+
ffi('read_disk_set_symlink_logical', [c_archive_p], c_int)
294+
ffi('read_disk_set_symlink_physical', [c_archive_p], c_int)
289295

290296
# archive_read_data
291297

libarchive/write.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def add_entries(self, entries):
4646

4747
def add_files(
4848
self, *paths, flags=0, lookup=False, pathname=None, recursive=True,
49-
**attributes
49+
symlink_mode=ffi.ARCHIVE_SYMLINK_MODE_PHYSICAL, **attributes
5050
):
5151
"""Read files through the OS and add them to the archive.
5252
@@ -63,6 +63,11 @@ def add_files(
6363
recursive (bool):
6464
when False, if a path in `paths` is a directory,
6565
only the directory itself is added.
66+
symlink_mode (enum):
67+
Determines how symlinks are traversed. Valid options are
68+
ARCHIVE_SYMLINK_MODE_HYBRID, ARCHIVE_SYMLINK_MODE_LOGICAL, and
69+
ARCHIVE_SYMLINK_MODE_PHYSICAL as defined in the ffi module.
70+
Default value matches default from libarchive.
6671
attributes (dict): passed to `ArchiveEntry.modify()`
6772
6873
Raises:
@@ -79,6 +84,14 @@ def add_files(
7984
entry_p = entry._entry_p
8085
for path in paths:
8186
with new_archive_read_disk(path, flags, lookup) as read_p:
87+
if (symlink_mode == ffi.ARCHIVE_SYMLINK_MODE_PHYSICAL):
88+
ffi.read_disk_set_symlink_physical(read_p)
89+
elif (symlink_mode == ffi.ARCHIVE_SYMLINK_MODE_LOGICAL):
90+
ffi.read_disk_set_symlink_logical(read_p)
91+
elif (symlink_mode == ffi.ARCHIVE_SYMLINK_MODE_HYBRID):
92+
ffi.read_disk_set_symlink_hybrid(read_p)
93+
else:
94+
raise ValueError(f"Bad symlink mode value {symlink_mode}")
8295
while 1:
8396
r = read_next_header2(read_p, entry_p)
8497
if r == ARCHIVE_EOF:

0 commit comments

Comments
 (0)