Selaa lähdekoodia

catch FileNotFoundError in `borg with-lock`, fixes #8022 (#8025)

borg with-lock: catch exception, print error msg, fixes #8022
kmille 1 vuosi sitten
vanhempi
sitoutus
3551f03af4
2 muutettua tiedostoa jossa 10 lisäystä ja 1 poistoa
  1. 5 1
      src/borg/archiver.py
  2. 5 0
      src/borg/testsuite/archiver.py

+ 5 - 1
src/borg/archiver.py

@@ -1850,7 +1850,10 @@ class Archiver:
         env = prepare_subprocess_env(system=True)
         try:
             # we exit with the return code we get from the subprocess
-            return subprocess.call([args.command] + args.args, env=env)
+            ret = subprocess.call([args.command] + args.args, env=env)
+        except (FileNotFoundError, OSError, ValueError) as e:
+            self.print_error(f"Error while trying to run '{args.command}': {e}")
+            ret = EXIT_ERROR
         finally:
             # we need to commit the "no change" operation we did to the manifest
             # because it created a new segment file in the repository. if we would
@@ -1859,6 +1862,7 @@ class Archiver:
             # any other mechanism relying on existing segment data not changing).
             # see issue #1867.
             repository.commit(compact=False)
+            return ret
 
     @with_repository(manifest=False, exclusive=True)
     def do_compact(self, args, repository):

+ 5 - 0
src/borg/testsuite/archiver.py

@@ -3186,6 +3186,11 @@ class ArchiverTestCase(ArchiverTestCaseBase):
         cmd = 'python3', '-c', 'import os, sys; sys.exit(42 if os.path.exists("%s") else 23)' % lock_path
         self.cmd('with-lock', self.repository_location, *cmd, fork=True, exit_code=42)
 
+    def test_with_lock_non_existent_command(self):
+        self.cmd('init', '--encryption=repokey', self.repository_location)
+        cmd = ['non_existent_command', ]
+        self.cmd('with-lock', self.repository_location, *cmd, fork=True, exit_code=EXIT_ERROR)
+
     def test_recreate_list_output(self):
         self.cmd('init', '--encryption=repokey', self.repository_location)
         self.create_regular_file('file1', size=0)