diff --git a/zfs_autobackup/ZfsCheck.py b/zfs_autobackup/ZfsCheck.py index a1ad9fc..000426e 100644 --- a/zfs_autobackup/ZfsCheck.py +++ b/zfs_autobackup/ZfsCheck.py @@ -1,4 +1,8 @@ +from __future__ import print_function import hashlib +import sys +from builtins import BrokenPipeError +from signal import signal, SIGPIPE, SIG_DFL from .ZfsNode import ZfsNode from .util import * @@ -60,6 +64,10 @@ class ZfsCheck(CliBase): if not self.args.test: for (file, block, hash) in block_hash_tree(mnt, count, bs): print("{}\t{}\t{}".format(file, block, hash)) + sys.stdout.flush() #important, to generate SIGPIPES on ssh disconnect + + except BrokenPipeError: + output_redir() finally: snapshot.unmount() @@ -110,14 +118,17 @@ class ZfsCheck(CliBase): if not self.args.test: for (block, hash) in block_hash(dev, count, bs): 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: @@ -147,4 +158,25 @@ def cli(): if __name__ == "__main__": + # try: + # while True: + # # print("stderr", file=sys.stderr) + # print("loop") + # sys.stdout.flush() + # + # except BrokenPipeError: + # output_redir() + # print("pipe brookn", file=sys.stderr) + # sys.stderr.flush() + # + # devnull = os.open(os.devnull, os.O_WRONLY) + # os.dup2(devnull, sys.stdout.fileno()) + # + # print("stout") + # sys.stdout.flush() + # + # + # print("hier kom ik nie", file=sys.stderr) + # open("yo" ,"w") + cli() diff --git a/zfs_autobackup/util.py b/zfs_autobackup/util.py index 088ec90..7dd7a90 100644 --- a/zfs_autobackup/util.py +++ b/zfs_autobackup/util.py @@ -78,3 +78,12 @@ def get_tmp_clone_name(snapshot): +def output_redir(): + """use this after a BrokenPipeError to prevent further exceptions. + Redirects stdout/err to /dev/null + """ + + devnull = os.open(os.devnull, os.O_WRONLY) + os.dup2(devnull, sys.stdout.fileno()) + os.dup2(devnull, sys.stderr.fileno()) +