| 1 | """시스템 libarchive를 시험에서만 사용해 7z 상호 운용을 확인함.""" |
| 2 | import ctypes as c |
| 3 | import ctypes.util |
| 4 | from pathlib import Path |
| 5 | import sys |
| 6 | |
| 7 | |
| 8 | def library(): |
| 9 | lib = c.CDLL(ctypes.util.find_library("archive")) |
| 10 | functions = { |
| 11 | "archive_write_new": (c.c_void_p, []), |
| 12 | "archive_read_new": (c.c_void_p, []), |
| 13 | "archive_entry_new": (c.c_void_p, []), |
| 14 | "archive_error_string": (c.c_char_p, [c.c_void_p]), |
| 15 | "archive_write_set_format_7zip": (c.c_int, [c.c_void_p]), |
| 16 | "archive_write_set_format_option": (c.c_int, [c.c_void_p, c.c_char_p, c.c_char_p, c.c_char_p]), |
| 17 | "archive_write_open_memory": (c.c_int, [c.c_void_p, c.c_void_p, c.c_size_t, c.POINTER(c.c_size_t)]), |
| 18 | "archive_entry_set_pathname": (None, [c.c_void_p, c.c_char_p]), |
| 19 | "archive_entry_set_filetype": (None, [c.c_void_p, c.c_uint]), |
| 20 | "archive_entry_set_perm": (None, [c.c_void_p, c.c_uint]), |
| 21 | "archive_entry_set_size": (None, [c.c_void_p, c.c_longlong]), |
| 22 | "archive_entry_set_mtime": (None, [c.c_void_p, c.c_longlong, c.c_long]), |
| 23 | "archive_write_header": (c.c_int, [c.c_void_p, c.c_void_p]), |
| 24 | "archive_write_data": (c.c_ssize_t, [c.c_void_p, c.c_void_p, c.c_size_t]), |
| 25 | "archive_entry_free": (None, [c.c_void_p]), |
| 26 | "archive_write_close": (c.c_int, [c.c_void_p]), |
| 27 | "archive_write_free": (c.c_int, [c.c_void_p]), |
| 28 | "archive_read_support_filter_all": (c.c_int, [c.c_void_p]), |
| 29 | "archive_read_support_format_7zip": (c.c_int, [c.c_void_p]), |
| 30 | "archive_read_open_filename": (c.c_int, [c.c_void_p, c.c_char_p, c.c_size_t]), |
| 31 | "archive_read_next_header": (c.c_int, [c.c_void_p, c.POINTER(c.c_void_p)]), |
| 32 | "archive_entry_pathname": (c.c_char_p, [c.c_void_p]), |
| 33 | "archive_read_data": (c.c_ssize_t, [c.c_void_p, c.c_void_p, c.c_size_t]), |
| 34 | "archive_read_free": (c.c_int, [c.c_void_p]), |
| 35 | } |
| 36 | for name, (result, args) in functions.items(): |
| 37 | function = getattr(lib, name) |
| 38 | function.restype = result |
| 39 | function.argtypes = args |
| 40 | return lib |
| 41 | |
| 42 | |
| 43 | def create(method, entries): |
| 44 | lib = library() |
| 45 | archive = lib.archive_write_new() |
| 46 | buffer = c.create_string_buffer(4 * 1024 * 1024) |
| 47 | used = c.c_size_t() |
| 48 | def check(result): |
| 49 | assert result == 0, lib.archive_error_string(archive) |
| 50 | try: |
| 51 | check(lib.archive_write_set_format_7zip(archive)) |
| 52 | check(lib.archive_write_set_format_option(archive, b"7zip", b"compression", method.encode())) |
| 53 | check(lib.archive_write_open_memory(archive, buffer, len(buffer), c.byref(used))) |
| 54 | for name, data in entries: |
| 55 | entry = lib.archive_entry_new() |
| 56 | try: |
| 57 | lib.archive_entry_set_pathname(entry, name.encode()) |
| 58 | lib.archive_entry_set_filetype(entry, 0o100000) |
| 59 | lib.archive_entry_set_perm(entry, 0o600) |
| 60 | lib.archive_entry_set_size(entry, len(data)) |
| 61 | lib.archive_entry_set_mtime(entry, 0, 0) |
| 62 | check(lib.archive_write_header(archive, entry)) |
| 63 | assert lib.archive_write_data(archive, data, len(data)) == len(data) |
| 64 | finally: |
| 65 | lib.archive_entry_free(entry) |
| 66 | check(lib.archive_write_close(archive)) |
| 67 | return buffer.raw[:used.value] |
| 68 | finally: |
| 69 | lib.archive_write_free(archive) |
| 70 | |
| 71 | |
| 72 | def verify(path): |
| 73 | lib = library() |
| 74 | archive = lib.archive_read_new() |
| 75 | result = {} |
| 76 | try: |
| 77 | assert lib.archive_read_support_filter_all(archive) == 0 |
| 78 | assert lib.archive_read_support_format_7zip(archive) == 0 |
| 79 | assert lib.archive_read_open_filename(archive, str(path).encode(), 65536) == 0 |
| 80 | entry = c.c_void_p() |
| 81 | while True: |
| 82 | status = lib.archive_read_next_header(archive, c.byref(entry)) |
| 83 | if status == 1: |
| 84 | return result |
| 85 | assert status == 0, lib.archive_error_string(archive) |
| 86 | name = lib.archive_entry_pathname(entry).decode() |
| 87 | data = bytearray() |
| 88 | buffer = c.create_string_buffer(65536) |
| 89 | while True: |
| 90 | size = lib.archive_read_data(archive, buffer, len(buffer)) |
| 91 | assert size >= 0, lib.archive_error_string(archive) |
| 92 | if size == 0: |
| 93 | break |
| 94 | data.extend(buffer.raw[:size]) |
| 95 | result[name] = bytes(data) |
| 96 | finally: |
| 97 | lib.archive_read_free(archive) |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
| 101 | root = Path(__file__).resolve().parent / "fixtures/7z" |
| 102 | payload = b"Zipelo 7z interoperability\n" * 100 |
| 103 | if sys.argv[1:] == ["--generate"]: |
| 104 | for method in ("lzma1", "lzma2"): |
| 105 | path = root / f"libarchive-{method}.7z" |
| 106 | path.write_bytes(create(method, [("folder/hello.txt", payload), ("second", payload[::-1])])) |
| 107 | assert verify(path) == {"folder/hello.txt": payload, "second": payload[::-1]} |
| 108 | else: |
| 109 | for name in ("copy.7z", "lzma.7z", "lzma2.7z", "encoded-solid.7z", "long-match.7z", "lzma2-chunks.7z"): |
| 110 | assert verify(root / name) |