Skip to content

Commit

Permalink
improve the new errors and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed May 24, 2021
1 parent f220276 commit 33c28ef
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
19 changes: 8 additions & 11 deletions libarchive/ffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ def get_write_filter_function(filter_name):

# FFI declarations

# library version
version_number = ffi('version_number', [], c_int, check_int)

# archive_util

errno = ffi('errno', [c_archive_p], c_int)
Expand Down Expand Up @@ -317,19 +320,13 @@ def get_write_filter_function(filter_name):
ffi('write_close', [c_archive_p], c_int, check_int)
ffi('write_free', [c_archive_p], c_int, check_int)

# library version
ffi('version_number', [], c_int, check_int)
# archive encryption

try:
ffi('read_add_passphrase', [c_archive_p, c_char_p], c_int, check_int)
ffi('write_set_passphrase', [c_archive_p, c_char_p], c_int, check_int)
except AttributeError:
logger.warning('archive encryption is not supported')

def read_add_passphrase(*args, **kwargs):
raise NotImplementedError(
"undefined symbol: archive_read_add_passphrase")

def write_set_passphrase(*args, **kwargs):
raise NotImplementedError(
"undefined symbol: archive_write_set_passphrase")
logger.info(
f"the libarchive being used (version {version_number()}, "
f"path {ffi.libarchive_path}) doesn't support encryption"
)
8 changes: 7 additions & 1 deletion libarchive/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ def new_archive_read(format_name='all', filter_name='all', passphrase=None):
if passphrase:
if not isinstance(passphrase, bytes):
passphrase = passphrase.encode('utf-8')
ffi.read_add_passphrase(archive_p, passphrase)
try:
ffi.read_add_passphrase(archive_p, passphrase)
except AttributeError:
raise NotImplementedError(
f"the libarchive being used (version {ffi.version_number()}, "
f"path {ffi.libarchive_path}) doesn't support encryption"
)
ffi.get_read_filter_function(filter_name)(archive_p)
ffi.get_read_format_function(format_name)(archive_p)
yield archive_p
Expand Down
28 changes: 19 additions & 9 deletions libarchive/write.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from contextlib import contextmanager
from ctypes import byref, cast, c_char, c_size_t, c_void_p, POINTER
import warnings

from . import ffi
from .entry import ArchiveEntry, new_archive_entry
Expand Down Expand Up @@ -154,20 +155,29 @@ def new_archive_write(format_name,
ffi.get_write_format_function(format_name)(archive_p)
if filter_name:
ffi.get_write_filter_function(filter_name)(archive_p)
if passphrase:
if format_name != 'zip':
raise RuntimeError('encryption is only support by zip format')
if 'encryption' not in options:
raise RuntimeError(
'option "encryption=type" is required to enable encryption'
if passphrase and 'encryption' not in options:
if format_name == 'zip':
warnings.warn(
"The default encryption scheme of zip archives is weak. "
"Use `options='encryption=$type'` to specify the encryption "
"type you want to use. The supported values are 'zipcrypt' "
"(the weak default), 'aes128' and 'aes256'."
)
if not isinstance(passphrase, bytes):
passphrase = passphrase.encode('utf-8')
ffi.write_set_passphrase(archive_p, passphrase)
options += ',encryption' if options else 'encryption'
if options:
if not isinstance(options, bytes):
options = options.encode('utf-8')
ffi.write_set_options(archive_p, options)
if passphrase:
if not isinstance(passphrase, bytes):
passphrase = passphrase.encode('utf-8')
try:
ffi.write_set_passphrase(archive_p, passphrase)
except AttributeError:
raise NotImplementedError(
f"the libarchive being used (version {ffi.version_number()}, "
f"path {ffi.libarchive_path}) doesn't support encryption"
)
yield archive_p
ffi.write_close(archive_p)
ffi.write_free(archive_p)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ universal = 1
[flake8]
exclude=.?*,env*/
ignore = E226,E731,W504
max-line-length = 80
max-line-length = 85

0 comments on commit 33c28ef

Please sign in to comment.