Просмотр исходного кода

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
Rayyan Ansari 2 лет назад
Родитель
Сommit
9e9b94615e
2 измененных файлов с 12 добавлено и 0 удалено
  1. 2 0
      src/borg/archiver/key_cmds.py
  2. 10 0
      src/borg/testsuite/archiver/key_cmds.py

+ 2 - 0
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:

+ 10 - 0
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)