|
1 | 1 | from contextlib import contextmanager |
2 | | -from ctypes import create_string_buffer |
| 2 | +from ctypes import create_string_buffer, string_at |
3 | 3 | from enum import IntEnum |
4 | 4 | import math |
5 | 5 |
|
6 | 6 | from . import ffi |
| 7 | +from .exception import ArchiveError |
7 | 8 |
|
8 | 9 |
|
9 | 10 | class FileType(IntEnum): |
@@ -86,6 +87,7 @@ def modify(self, header_codec=None, **attributes): |
86 | 87 | rdev (int | Tuple[int, int]): device number, if the file is a device |
87 | 88 | rdevmajor (int): major part of the device number |
88 | 89 | rdevminor (int): minor part of the device number |
| 90 | + stored_digests (dict[str, bytes]): hashes of the file's contents |
89 | 91 | """ |
90 | 92 | if header_codec: |
91 | 93 | self.header_codec = header_codec |
@@ -433,6 +435,64 @@ def rdevminor(self): |
433 | 435 | def rdevminor(self, value): |
434 | 436 | ffi.entry_set_rdevminor(self._entry_p, value) |
435 | 437 |
|
| 438 | + @property |
| 439 | + def stored_digests(self): |
| 440 | + """The file's hashes stored in the archive. |
| 441 | +
|
| 442 | + libarchive only supports reading and writing digests from and to 'mtree' |
| 443 | + files. Setting the digests requires at least version 3.8.0 of libarchive |
| 444 | + (released in May 2025). It also requires including the names of the |
| 445 | + digest algorithms in the string of options passed to the archive writer |
| 446 | + (e.g. `file_writer(archive_path, 'mtree', options='md5,rmd160,sha256')`). |
| 447 | + """ |
| 448 | + return {name: self.get_stored_digest(name) for name in ffi.DIGEST_ALGORITHMS} |
| 449 | + |
| 450 | + @stored_digests.setter |
| 451 | + def stored_digests(self, values): |
| 452 | + for name, value in values.items(): |
| 453 | + self.set_stored_digest(name, value) |
| 454 | + |
| 455 | + def get_stored_digest(self, algorithm_name): |
| 456 | + algorithm = ffi.DIGEST_ALGORITHMS[algorithm_name] |
| 457 | + try: |
| 458 | + ptr = ffi.entry_digest(self._entry_p, algorithm.libarchive_id) |
| 459 | + except AttributeError: |
| 460 | + raise NotImplementedError( |
| 461 | + f"the libarchive being used (version {ffi.version_number()}, path " |
| 462 | + f"{ffi.libarchive_path}) doesn't support reading entry digests" |
| 463 | + ) from None |
| 464 | + except ArchiveError: |
| 465 | + raise NotImplementedError( |
| 466 | + f"the libarchive being used (version {ffi.version_number()}, path " |
| 467 | + f"{ffi.libarchive_path}) doesn't support {algorithm_name} digests" |
| 468 | + ) from None |
| 469 | + return string_at(ptr, algorithm.bytes_length) |
| 470 | + |
| 471 | + def set_stored_digest(self, algorithm_name, value): |
| 472 | + algorithm = ffi.DIGEST_ALGORITHMS[algorithm_name] |
| 473 | + expected_length = algorithm.bytes_length |
| 474 | + if len(value) != expected_length: |
| 475 | + raise ValueError( |
| 476 | + f"invalid input digest: expected {expected_length} bytes, " |
| 477 | + f"got {len(value)}" |
| 478 | + ) |
| 479 | + try: |
| 480 | + retcode = ffi.entry_set_digest( |
| 481 | + self._entry_p, |
| 482 | + algorithm.libarchive_id, |
| 483 | + (expected_length * ffi.c_ubyte).from_buffer_copy(value) |
| 484 | + ) |
| 485 | + except AttributeError: |
| 486 | + raise NotImplementedError( |
| 487 | + f"the libarchive being used (version {ffi.version_number()}, path " |
| 488 | + f"{ffi.libarchive_path}) doesn't support writing entry digests" |
| 489 | + ) from None |
| 490 | + if retcode < 0: |
| 491 | + raise NotImplementedError( |
| 492 | + f"the libarchive being used (version {ffi.version_number()}, path " |
| 493 | + f"{ffi.libarchive_path}) doesn't support {algorithm_name} digests" |
| 494 | + ) from None |
| 495 | + |
436 | 496 |
|
437 | 497 | class ConsumedArchiveEntry(ArchiveEntry): |
438 | 498 |
|
|
0 commit comments