Browse Source

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 năm trước cách đây
mục cha
commit
9e9b94615e

+ 2 - 0
src/borg/archiver/key_cmds.py

@@ -101,6 +101,8 @@ class KeysMixIn:
             manager.export_paperkey(args.path)
             manager.export_paperkey(args.path)
         else:
         else:
             try:
             try:
+                if os.path.isdir(args.path):
+                    raise IsADirectoryError
                 if args.qr:
                 if args.qr:
                     manager.export_qr(args.path)
                     manager.export_qr(args.path)
                 else:
                 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)
         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):
     def test_key_import_errors(self):
         export_file = self.output_path + "/exported"
         export_file = self.output_path + "/exported"
         self.cmd(f"--repo={self.repository_location}", "rcreate", KF_ENCRYPTION)
         self.cmd(f"--repo={self.repository_location}", "rcreate", KF_ENCRYPTION)