Skip to content

Commit

Permalink
replace the misleadingly named VOID_CB function
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed May 31, 2021
1 parent 0323df6 commit c931af3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 3 additions & 1 deletion libarchive/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
)
OPEN_CALLBACK = CFUNCTYPE(c_int, c_void_p, c_void_p)
CLOSE_CALLBACK = CFUNCTYPE(c_int, c_void_p, c_void_p)
VOID_CB = lambda *_: ARCHIVE_OK

NO_OPEN_CB = ctypes.cast(None, OPEN_CALLBACK)
NO_CLOSE_CB = ctypes.cast(None, CLOSE_CALLBACK)


# Type aliases, for readability
Expand Down
12 changes: 6 additions & 6 deletions libarchive/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from . import ffi
from .ffi import (
ARCHIVE_EOF, OPEN_CALLBACK, READ_CALLBACK, CLOSE_CALLBACK, SEEK_CALLBACK,
VOID_CB, page_size,
NO_OPEN_CB, NO_CLOSE_CB, page_size,
)
from .entry import ArchiveEntry, new_archive_entry

Expand Down Expand Up @@ -57,14 +57,14 @@ def new_archive_read(format_name='all', filter_name='all', passphrase=None):
@contextmanager
def custom_reader(
read_func, format_name='all', filter_name='all',
open_func=VOID_CB, seek_func=None, close_func=VOID_CB,
open_func=None, seek_func=None, close_func=None,
block_size=page_size, archive_read_class=ArchiveRead, passphrase=None,
):
"""Read an archive using a custom function.
"""
open_cb = OPEN_CALLBACK(open_func)
open_cb = OPEN_CALLBACK(open_func) if open_func else NO_OPEN_CB
read_cb = READ_CALLBACK(read_func)
close_cb = CLOSE_CALLBACK(close_func)
close_cb = CLOSE_CALLBACK(close_func) if close_func else NO_CLOSE_CB
with new_archive_read(format_name, filter_name, passphrase) as archive_p:
if seek_func:
ffi.read_set_seek_callback(archive_p, SEEK_CALLBACK(seek_func))
Expand Down Expand Up @@ -140,9 +140,9 @@ def seek_func(archive_p, context, offset, whence):
# tell libarchive the current position
return stream.tell()

open_cb = OPEN_CALLBACK(VOID_CB)
open_cb = NO_OPEN_CB
read_cb = READ_CALLBACK(read_func)
close_cb = CLOSE_CALLBACK(VOID_CB)
close_cb = NO_CLOSE_CB
with new_archive_read(format_name, filter_name, passphrase) as archive_p:
if stream.seekable():
ffi.read_set_seek_callback(archive_p, SEEK_CALLBACK(seek_func))
Expand Down
10 changes: 5 additions & 5 deletions libarchive/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from . import ffi
from .entry import ArchiveEntry, new_archive_entry
from .ffi import (
OPEN_CALLBACK, WRITE_CALLBACK, CLOSE_CALLBACK, VOID_CB, REGULAR_FILE,
DEFAULT_UNIX_PERMISSION, ARCHIVE_EOF,
OPEN_CALLBACK, WRITE_CALLBACK, CLOSE_CALLBACK, NO_OPEN_CB, NO_CLOSE_CB,
REGULAR_FILE, DEFAULT_UNIX_PERMISSION, ARCHIVE_EOF,
page_size, entry_sourcepath, entry_clear, read_disk_new, read_disk_open_w,
read_next_header2, read_disk_descend, read_free, write_header, write_data,
write_finish_entry, entry_set_size, entry_set_filetype, entry_set_perm,
Expand Down Expand Up @@ -187,17 +187,17 @@ def new_archive_write(format_name, filter_name=None, options='', passphrase=None
@contextmanager
def custom_writer(
write_func, format_name, filter_name=None,
open_func=VOID_CB, close_func=VOID_CB, block_size=page_size,
open_func=None, close_func=None, block_size=page_size,
archive_write_class=ArchiveWrite, options='', passphrase=None,
):

def write_cb_internal(archive_p, context, buffer_, length):
data = cast(buffer_, POINTER(c_char * length))[0]
return write_func(data)

open_cb = OPEN_CALLBACK(open_func)
open_cb = OPEN_CALLBACK(open_func) if open_func else NO_OPEN_CB
write_cb = WRITE_CALLBACK(write_cb_internal)
close_cb = CLOSE_CALLBACK(close_func)
close_cb = CLOSE_CALLBACK(close_func) if close_func else NO_CLOSE_CB

with new_archive_write(format_name, filter_name, options,
passphrase) as archive_p:
Expand Down

0 comments on commit c931af3

Please sign in to comment.