From 233745c345265827613258c7d03ebcc12f757058 Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Mon, 7 Mar 2022 21:08:56 +0100 Subject: [PATCH] reworking block skipper --- tests/test_blockhasher.py | 52 +++++++++++++++++++++++ tests/{test_check.py => test_zfscheck.py} | 0 zfs_autobackup/BlockHasher.py | 7 +-- zfs_autobackup/ZfsCheck.py | 2 +- 4 files changed, 54 insertions(+), 7 deletions(-) rename tests/{test_check.py => test_zfscheck.py} (100%) diff --git a/tests/test_blockhasher.py b/tests/test_blockhasher.py index ba57b72..f9e43d6 100644 --- a/tests/test_blockhasher.py +++ b/tests/test_blockhasher.py @@ -98,3 +98,55 @@ class TestBlockHasher(unittest2.TestCase): checksums = list(block_hasher.generate("tests/data/whole_whole2_partial")) checksums.reverse() self.assertEqual([], list(block_hasher.compare("tests/data/whole_whole2_partial", checksums))) + + def test_skip1(self): + block_hasher = BlockHasher(count=1, skip=1) + self.assertEqual( + list(block_hasher.generate("tests/data/whole_whole2_partial")), + [ + (0, "3c0bf91170d873b8e327d3bafb6bc074580d11b7"), # whole + # (1, "2e863f1fcccd6642e4e28453eba10d2d3f74d798"), # whole2 + (2, "642027d63bb0afd7e0ba197f2c66ad03e3d70de1") # partial + ] + ) + + #should continue the pattern on the next file: + self.assertEqual( + list(block_hasher.generate("tests/data/whole_whole2_partial")), + [ + # (0, "3c0bf91170d873b8e327d3bafb6bc074580d11b7"), # whole + (1, "2e863f1fcccd6642e4e28453eba10d2d3f74d798"), # whole2 + # (2, "642027d63bb0afd7e0ba197f2c66ad03e3d70de1") # partial + ] + ) + + def test_skip6(self): + block_hasher = BlockHasher(count=1, skip=6) + self.assertEqual( + list(block_hasher.generate("tests/data/whole_whole2_partial")), + [ + (0, "3c0bf91170d873b8e327d3bafb6bc074580d11b7"), # whole + # (1, "2e863f1fcccd6642e4e28453eba10d2d3f74d798"), # whole2 + # (2, "642027d63bb0afd7e0ba197f2c66ad03e3d70de1") # partial + ] + ) + + #all blocks of next file are skipped + self.assertEqual( + list(block_hasher.generate("tests/data/whole_whole2_partial")), + [ + # (0, "3c0bf91170d873b8e327d3bafb6bc074580d11b7"), # whole + # (1, "2e863f1fcccd6642e4e28453eba10d2d3f74d798"), # whole2 + # (2, "642027d63bb0afd7e0ba197f2c66ad03e3d70de1") # partial + ] + ) + + #first block of this one is the 6th to be skipped: + self.assertEqual( + list(block_hasher.generate("tests/data/whole_whole2_partial")), + [ + # (0, "3c0bf91170d873b8e327d3bafb6bc074580d11b7"), # whole + (1, "2e863f1fcccd6642e4e28453eba10d2d3f74d798"), # whole2 + # (2, "642027d63bb0afd7e0ba197f2c66ad03e3d70de1") # partial + ] + ) diff --git a/tests/test_check.py b/tests/test_zfscheck.py similarity index 100% rename from tests/test_check.py rename to tests/test_zfscheck.py diff --git a/zfs_autobackup/BlockHasher.py b/zfs_autobackup/BlockHasher.py index 44bb8a6..64a90ff 100644 --- a/zfs_autobackup/BlockHasher.py +++ b/zfs_autobackup/BlockHasher.py @@ -61,14 +61,9 @@ class BlockHasher(): yields nothing for empty files. """ - with os.open(fname, os.O_RDONLY) as fh: - print (os.lseek(fh, 0, os.SEEK_END)) - with os.openopen(fname, "rb") as fh: - - # print(os.path.getsize(fname)) - print(os.lseek(fh, 0, os.SEEK_END)) + with open(fname, "rb") as fh: fsize = fh.seek(0, os.SEEK_END) fh.seek(0) diff --git a/zfs_autobackup/ZfsCheck.py b/zfs_autobackup/ZfsCheck.py index 86ccac6..677fe23 100644 --- a/zfs_autobackup/ZfsCheck.py +++ b/zfs_autobackup/ZfsCheck.py @@ -39,7 +39,7 @@ class ZfsCheck(CliBase): group.add_argument('--check', '-c', metavar="FILE", default=None, const=True, nargs='?', help="Read hashes from STDIN (or FILE) and compare them") - group.add_argument('--skip', '-s', metavar="NUMBER", default=0, type=float, + group.add_argument('--skip', '-s', metavar="NUMBER", default=0, type=int, help="Skip this number of chunks after every hash. %(default)s") return parser