From ed61f03b4b1fcd45a052424d9b06c99dda118823 Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Mon, 21 Feb 2022 11:40:40 +0100 Subject: [PATCH] zfs-check fixes and tests --- tests/test_check.py | 16 ++++++++++++++++ zfs_autobackup/ZfsAutobackup.py | 5 +++++ zfs_autobackup/ZfsAutoverify.py | 5 +++++ zfs_autobackup/ZfsCheck.py | 12 ++++++------ zfs_autobackup/util.py | 2 +- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/tests/test_check.py b/tests/test_check.py index d05870c..5c6f77d 100644 --- a/tests/test_check.py +++ b/tests/test_check.py @@ -85,6 +85,16 @@ class TestZfsCheck(unittest2.TestCase): prepare_zpools() shelltest("cp tests/data/whole /test_source1/testfile") + shelltest("mkdir /test_source1/emptydir") + shelltest("mkdir /test_source1/dir") + shelltest("cp tests/data/whole2 /test_source1/dir/testfile") + + #it should ignore these: + shelltest("ln -s / /test_source1/symlink") + shelltest("mknod /test_source1/c c 1 1") + shelltest("mknod /test_source1/b b 1 1") + shelltest("mkfifo /test_source1/f") + shelltest("zfs snapshot test_source1@test") with OutputIO() as buf: @@ -93,5 +103,11 @@ class TestZfsCheck(unittest2.TestCase): print(buf.getvalue()) self.assertEqual("""testfile 0 3c0bf91170d873b8e327d3bafb6bc074580d11b7 +dir/testfile 0 2e863f1fcccd6642e4e28453eba10d2d3f74d798 """, buf.getvalue()) + + # def test_brokenpipe_cleanup_filesystem(self): + # """test if stuff is cleaned up correctly, in debugging mode , when a pipe breaks. """ + + diff --git a/zfs_autobackup/ZfsAutobackup.py b/zfs_autobackup/ZfsAutobackup.py index 5e80d5c..37fcfe5 100644 --- a/zfs_autobackup/ZfsAutobackup.py +++ b/zfs_autobackup/ZfsAutobackup.py @@ -1,6 +1,9 @@ import time import argparse +from signal import signal, SIGPIPE +from .util import output_redir + from .ZfsAuto import ZfsAuto from . import compressors @@ -489,6 +492,8 @@ class ZfsAutobackup(ZfsAuto): def cli(): import sys + signal(SIGPIPE, output_redir) + sys.exit(ZfsAutobackup(sys.argv[1:], False).run()) diff --git a/zfs_autobackup/ZfsAutoverify.py b/zfs_autobackup/ZfsAutoverify.py index 37f75c7..7e8c158 100644 --- a/zfs_autobackup/ZfsAutoverify.py +++ b/zfs_autobackup/ZfsAutoverify.py @@ -1,4 +1,7 @@ # from util import activate_volume_snapshot, create_mountpoints, cleanup_mountpoint +from signal import signal, SIGPIPE +from .util import output_redir + from .ZfsAuto import ZfsAuto from .ZfsNode import ZfsNode import sys @@ -302,6 +305,8 @@ class ZfsAutoverify(ZfsAuto): def cli(): import sys + signal(SIGPIPE, output_redir) + sys.exit(ZfsAutoverify(sys.argv[1:], False).run()) diff --git a/zfs_autobackup/ZfsCheck.py b/zfs_autobackup/ZfsCheck.py index bc4d1d2..69789be 100644 --- a/zfs_autobackup/ZfsCheck.py +++ b/zfs_autobackup/ZfsCheck.py @@ -1,5 +1,8 @@ from __future__ import print_function +from signal import signal, SIGPIPE + + from .ZfsNode import ZfsNode from .util import * from .CliBase import CliBase @@ -62,8 +65,6 @@ class ZfsCheck(CliBase): print("{}\t{}\t{}".format(file, block, hash)) sys.stdout.flush() #important, to generate SIGPIPES on ssh disconnect - # except BrokenPipeError: - # output_redir() finally: snapshot.unmount() @@ -89,7 +90,7 @@ class ZfsCheck(CliBase): start_time = time.time() while time.time() - start_time < 10: for location in locations: - if pathlib.Path(location).is_block_device(): + if os.path.exists(location): return location # fake it in testmode @@ -116,15 +117,12 @@ class ZfsCheck(CliBase): print("{}\t{}".format(block, hash)) sys.stdout.flush() #important, to generate SIGPIPES on ssh disconnect - except BrokenPipeError: - output_redir() finally: self.deacitvate_volume_snapshot(snapshot) def run(self): - snapshot = self.node.get_dataset(self.args.snapshot) if not snapshot.exists: @@ -150,6 +148,8 @@ class ZfsCheck(CliBase): def cli(): import sys + signal(SIGPIPE, output_redir) + sys.exit(ZfsCheck(sys.argv[1:], False).run()) diff --git a/zfs_autobackup/util.py b/zfs_autobackup/util.py index 17927e0..0e27e7d 100644 --- a/zfs_autobackup/util.py +++ b/zfs_autobackup/util.py @@ -64,7 +64,7 @@ def block_hash_tree(start_path, count=10000, bs=4096): try: for (dirpath, dirnames, filenames) in os.walk(".", onerror=walkerror): for f in filenames: - file_path=os.path.join(dirpath, f) + file_path=os.path.join(dirpath, f)[2:] if (not os.path.islink(file_path)) and os.path.isfile(file_path): for (chunk_nr, hash) in block_hash(file_path, count, bs):