Changaco / python-libarchive-c

Python interface to libarchive
Other
70 stars 37 forks source link

Detect valid archives #96

Open arlininger opened 4 years ago

arlininger commented 4 years ago

I'm in the process of migrating from python2 using python-libarchive to python3 using python-libarchive-c. In all, the process has been quite easy (and many thanks to you folks for keeping a similar interface). However, I have run in to a bit of a snag.

I've been detecting whether or not a file is a legitimate archive based on trying to open it and detect errors. This works well for most files, but has been failing when I feed it something that looks like a /etc/passwd file. Previously, when libarchive returns Missing type keyword in mtree specification, the old python-libarchive would raise a ValueError. Now, this simply produces a Warning to the logger and the process continues.

Is there an easy way to make this produce an error that I can catch or otherwise detect invalid archives?

Changaco commented 4 years ago

You should be able to intercept the warning message and turn it into an exception by modifying the libarchive.ffi.logger object. The simplest way to do that is probably to replace the logger's warning method. A cleaner solution could be to create a special Handler and call logger.addHandler(handler).

arlininger commented 4 years ago

Thanks, that got me going in the right direction.

Changaco commented 4 years ago

Let's keep this issue open as a feature request. We could add a function named check_archive or is_valid_archive.