From 9e9b94615eb5071968ea8124df3aa370eeabce31 Mon Sep 17 00:00:00 2001 From: Rayyan Ansari Date: Tue, 29 Nov 2022 17:33:29 +0000 Subject: [PATCH] archiver: key_cmds: check if key path is a directory before opening On Windows, Python raises PermissionError instead of IsADirectoryError (like on Unix) if the file to open is actually a directory. See https://github.com/python/cpython/issues/87261 --- src/borg/archiver/key_cmds.py | 2 ++ src/borg/testsuite/archiver/key_cmds.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/borg/archiver/key_cmds.py b/src/borg/archiver/key_cmds.py index fa89d1da9..abb9d1ee2 100644 --- a/src/borg/archiver/key_cmds.py +++ b/src/borg/archiver/key_cmds.py @@ -101,6 +101,8 @@ class KeysMixIn: manager.export_paperkey(args.path) else: try: + if os.path.isdir(args.path): + raise IsADirectoryError if args.qr: manager.export_qr(args.path) else: diff --git a/src/borg/testsuite/archiver/key_cmds.py b/src/borg/testsuite/archiver/key_cmds.py index c866fc3ae..94dadcab0 100644 --- a/src/borg/testsuite/archiver/key_cmds.py +++ b/src/borg/testsuite/archiver/key_cmds.py @@ -163,6 +163,16 @@ class ArchiverTestCase(ArchiverTestCaseBase): self.cmd(f"--repo={self.repository_location}", "key", "export", export_directory, exit_code=EXIT_ERROR) + def test_key_export_qr_directory(self): + export_directory = self.output_path + "/exported" + os.mkdir(export_directory) + + self.cmd(f"--repo={self.repository_location}", "rcreate", RK_ENCRYPTION) + + self.cmd( + f"--repo={self.repository_location}", "key", "export", "--qr-html", export_directory, exit_code=EXIT_ERROR + ) + def test_key_import_errors(self): export_file = self.output_path + "/exported" self.cmd(f"--repo={self.repository_location}", "rcreate", KF_ENCRYPTION)