Skip to content

Commit

Permalink
Merge pull request #110 from Changaco/clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco authored May 25, 2021
2 parents 8b37a36 + 1b35361 commit 9cf50ca
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion libarchive/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def format_time(seconds, nanos):
return int(seconds)


class ArchiveEntry(object):
class ArchiveEntry:

__slots__ = ('_archive_p', '_entry_p')

Expand Down
36 changes: 22 additions & 14 deletions libarchive/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from os import fstat, stat

from . import ffi
from .ffi import (ARCHIVE_EOF, OPEN_CALLBACK, READ_CALLBACK, CLOSE_CALLBACK,
SEEK_CALLBACK, VOID_CB, page_size)
from .ffi import (
ARCHIVE_EOF, OPEN_CALLBACK, READ_CALLBACK, CLOSE_CALLBACK, SEEK_CALLBACK,
VOID_CB, page_size,
)
from .entry import ArchiveEntry, new_archive_entry


class ArchiveRead(object):
class ArchiveRead:

def __init__(self, archive_p):
self._pointer = archive_p
Expand Down Expand Up @@ -54,9 +56,9 @@ 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, close_func=VOID_CB, block_size=page_size,
archive_read_class=ArchiveRead, passphrase=None,
read_func, format_name='all', filter_name='all',
open_func=VOID_CB, close_func=VOID_CB, block_size=page_size,
archive_read_class=ArchiveRead, passphrase=None,
):
"""Read an archive using a custom function.
"""
Expand All @@ -69,8 +71,9 @@ def custom_reader(


@contextmanager
def fd_reader(fd, format_name='all', filter_name='all', block_size=4096,
passphrase=None):
def fd_reader(
fd, format_name='all', filter_name='all', block_size=4096, passphrase=None,
):
"""Read an archive from a file descriptor.
"""
with new_archive_read(format_name, filter_name, passphrase) as archive_p:
Expand All @@ -83,8 +86,9 @@ def fd_reader(fd, format_name='all', filter_name='all', block_size=4096,


@contextmanager
def file_reader(path, format_name='all', filter_name='all', block_size=4096,
passphrase=None):
def file_reader(
path, format_name='all', filter_name='all', block_size=4096, passphrase=None,
):
"""Read an archive from a file.
"""
with new_archive_read(format_name, filter_name, passphrase) as archive_p:
Expand All @@ -106,8 +110,10 @@ def memory_reader(buf, format_name='all', filter_name='all', passphrase=None):


@contextmanager
def stream_reader(stream, format_name='all', filter_name='all',
block_size=page_size, passphrase=None):
def stream_reader(
stream, format_name='all', filter_name='all', block_size=page_size,
passphrase=None,
):
"""Read an archive from a stream.
The `stream` object must support the standard `readinto` method.
Expand All @@ -133,8 +139,10 @@ def read_func(archive_p, context, ptrptr):


@contextmanager
def seekable_stream_reader(stream, format_name='all', filter_name='all',
block_size=page_size, passphrase=None):
def seekable_stream_reader(
stream, format_name='all', filter_name='all', block_size=page_size,
passphrase=None,
):
"""Read an archive from a seekable stream.
The `stream` object must support the standard `readinto`, 'seek' and
Expand Down
31 changes: 14 additions & 17 deletions libarchive/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def new_archive_read_disk(path, flags=0, lookup=False):
read_free(archive_p)


class ArchiveWrite(object):
class ArchiveWrite:

def __init__(self, archive_p):
self._pointer = archive_p
Expand Down Expand Up @@ -75,9 +75,9 @@ def add_files(self, *paths, **kw):
entry_clear(entry_p)

def add_file_from_memory(
self, entry_path, entry_size, entry_data,
filetype=REGULAR_FILE, permission=DEFAULT_UNIX_PERMISSION,
atime=None, mtime=None, ctime=None, birthtime=None,
self, entry_path, entry_size, entry_data,
filetype=REGULAR_FILE, permission=DEFAULT_UNIX_PERMISSION,
atime=None, mtime=None, ctime=None, birthtime=None,
):
""""Add file from memory to archive.
Expand Down Expand Up @@ -146,10 +146,7 @@ def add_file_from_memory(


@contextmanager
def new_archive_write(format_name,
filter_name=None,
options='',
passphrase=None):
def new_archive_write(format_name, filter_name=None, options='', passphrase=None):
archive_p = ffi.write_new()
try:
ffi.get_write_format_function(format_name)(archive_p)
Expand Down Expand Up @@ -189,9 +186,9 @@ def new_archive_write(format_name,

@contextmanager
def custom_writer(
write_func, format_name, filter_name=None,
open_func=VOID_CB, close_func=VOID_CB, block_size=page_size,
archive_write_class=ArchiveWrite, options='', passphrase=None,
write_func, format_name, filter_name=None,
open_func=VOID_CB, close_func=VOID_CB, block_size=page_size,
archive_write_class=ArchiveWrite, options='', passphrase=None,
):

def write_cb_internal(archive_p, context, buffer_, length):
Expand All @@ -212,8 +209,8 @@ def write_cb_internal(archive_p, context, buffer_, length):

@contextmanager
def fd_writer(
fd, format_name, filter_name=None,
archive_write_class=ArchiveWrite, options='', passphrase=None,
fd, format_name, filter_name=None,
archive_write_class=ArchiveWrite, options='', passphrase=None,
):
with new_archive_write(format_name, filter_name, options,
passphrase) as archive_p:
Expand All @@ -223,8 +220,8 @@ def fd_writer(

@contextmanager
def file_writer(
filepath, format_name, filter_name=None,
archive_write_class=ArchiveWrite, options='', passphrase=None,
filepath, format_name, filter_name=None,
archive_write_class=ArchiveWrite, options='', passphrase=None,
):
with new_archive_write(format_name, filter_name, options,
passphrase) as archive_p:
Expand All @@ -234,8 +231,8 @@ def file_writer(

@contextmanager
def memory_writer(
buf, format_name, filter_name=None,
archive_write_class=ArchiveWrite, options='', passphrase=None,
buf, format_name, filter_name=None,
archive_write_class=ArchiveWrite, options='', passphrase=None,
):
with new_archive_write(format_name, filter_name, options,
passphrase) as archive_p:
Expand Down
15 changes: 8 additions & 7 deletions tests/test_security_flags.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Test security-related extraction flags."""

from __future__ import division, print_function, unicode_literals
import pytest
import os

from libarchive import extract_file
from libarchive.ffi import version_number
from libarchive.extract import EXTRACT_SECURE_NOABSOLUTEPATHS, \
EXTRACT_SECURE_NODOTDOT
from libarchive.extract import (
EXTRACT_SECURE_NOABSOLUTEPATHS, EXTRACT_SECURE_NODOTDOT,
)
from libarchive.exception import ArchiveError
from . import data_dir

Expand All @@ -24,13 +24,14 @@ def run_test(flag, filename):


def test_no_dot_dot():
run_test(EXTRACT_SECURE_NODOTDOT,
'../python-libarchive-c-test-dot-dot-file')
run_test(EXTRACT_SECURE_NODOTDOT, '../python-libarchive-c-test-dot-dot-file')


def test_absolute():
# EXTRACT_SECURE_NOABSOLUTEPATHS was only added in 3.1.900
# 3.1.900 -> 3001009
if version_number() >= 3001009:
run_test(EXTRACT_SECURE_NOABSOLUTEPATHS,
'/tmp/python-libarchive-c-test-absolute-file')
run_test(
EXTRACT_SECURE_NOABSOLUTEPATHS,
'/tmp/python-libarchive-c-test-absolute-file'
)

0 comments on commit 9cf50ca

Please sign in to comment.