python-libarchive-c icon indicating copy to clipboard operation
python-libarchive-c copied to clipboard

Detect valid archives

Open arlininger opened this issue 5 years ago • 3 comments

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?

arlininger avatar Nov 21 '19 22:11 arlininger

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).

Changaco avatar Nov 22 '19 09:11 Changaco

Thanks, that got me going in the right direction.

arlininger avatar Nov 22 '19 19:11 arlininger

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

Changaco avatar Nov 23 '19 10:11 Changaco