1
0
Fork 0
mirror of https://github.com/borgbackup/borg.git synced 2025-03-04 10:39:50 +00:00

debug id-hash: implement file content id-hash computation, see #7406

This commit is contained in:
Thomas Waldmann 2023-03-04 21:42:13 +01:00
parent 086a4237ca
commit eb8ccd22d4
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
2 changed files with 26 additions and 2 deletions

View file

@ -2207,6 +2207,15 @@ class Archiver:
print("object %s fetched." % hex_id)
return EXIT_SUCCESS
@with_repository(compatibility=Manifest.NO_OPERATION_CHECK)
def do_debug_id_hash(self, args, repository, manifest, key):
"""compute id-hash for file contents"""
with open(args.path, "rb") as f:
data = f.read()
id = key.id_hash(data)
print(id.hex())
return EXIT_SUCCESS
@with_repository(manifest=False, exclusive=True)
def do_debug_put_obj(self, args, repository):
"""put file contents into the repository"""
@ -3733,6 +3742,21 @@ class Archiver:
subparser.add_argument('wanted', metavar='WANTED', type=str,
help='term to search the repo for, either 0x1234abcd hex term or a string')
debug_id_hash_epilog = process_epilog("""
This command computes the id-hash for some file content.
""")
subparser = debug_parsers.add_parser('id-hash', parents=[common_parser], add_help=False,
description=self.do_debug_id_hash.__doc__,
epilog=debug_id_hash_epilog,
formatter_class=argparse.RawDescriptionHelpFormatter,
help='compute id-hash for some file content (debug)')
subparser.set_defaults(func=self.do_debug_id_hash)
subparser.add_argument('location', metavar='REPOSITORY',
type=location_validator(archive=False),
help='repository to use')
subparser.add_argument('path', metavar='PATH', type=str,
help='content for which the id-hash shall get computed')
debug_get_obj_epilog = process_epilog("""
This command gets an object from the repository.
""")

View file

@ -2909,8 +2909,8 @@ class ArchiverTestCase(ArchiverTestCaseBase):
self.cmd('init', '--encryption=repokey', self.repository_location)
data = b'some data'
self.create_regular_file('file', contents=data)
# TODO: to compute the correct hexkey, we need: borg debug id-hash file
id_hash = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" # 256bit = 64 hex digits
output = self.cmd('debug', 'id-hash', self.repository_location, 'input/file')
id_hash = output.strip()
output = self.cmd('debug', 'put-obj', self.repository_location, id_hash, 'input/file')
assert id_hash in output
output = self.cmd('debug', 'get-obj', self.repository_location, id_hash, 'output/file')