Browse Source

Merge pull request #3847 from ThomasWaldmann/config-repo-without-key--master

Config repo without key (fwd port to master)
TW 7 years ago
parent
commit
036b501d82
1 changed files with 39 additions and 30 deletions
  1. 39 30
      src/borg/archiver.py

+ 39 - 30
src/borg/archiver.py

@@ -1522,8 +1522,8 @@ class Archiver:
             # see issue #1867.
             repository.commit()
 
-    @with_repository(exclusive=True, cache=True, compatibility=(Manifest.Operation.WRITE,))
-    def do_config(self, args, repository, manifest, key, cache):
+    @with_repository(exclusive=True, manifest=False)
+    def do_config(self, args, repository):
         """get, set, and delete values in a repository or cache config file"""
 
         def repo_validate(section, name, value=None, check_value=True):
@@ -1574,34 +1574,43 @@ class Archiver:
             name = args.name
 
         if args.cache:
-            cache.cache_config.load()
-            config = cache.cache_config._config
-            save = cache.cache_config.save
-            validate = cache_validate
-        else:
-            config = repository.config
-            save = lambda: repository.save_config(repository.path, repository.config)
-            validate = repo_validate
-
-        if args.delete:
-            validate(section, name, check_value=False)
-            config.remove_option(section, name)
-            if len(config.options(section)) == 0:
-                config.remove_section(section)
-            save()
-        elif args.value:
-            validate(section, name, args.value)
-            if section not in config.sections():
-                config.add_section(section)
-            config.set(section, name, args.value)
-            save()
-        else:
-            try:
-                print(config.get(section, name))
-            except (configparser.NoOptionError, configparser.NoSectionError) as e:
-                print(e, file=sys.stderr)
-                return EXIT_WARNING
-        return EXIT_SUCCESS
+            manifest, key = Manifest.load(repository, (Manifest.Operation.WRITE,))
+            assert_secure(repository, manifest)
+            cache = Cache(repository, key, manifest, lock_wait=self.lock_wait)
+
+        try:
+            if args.cache:
+                cache.cache_config.load()
+                config = cache.cache_config._config
+                save = cache.cache_config.save
+                validate = cache_validate
+            else:
+                config = repository.config
+                save = lambda: repository.save_config(repository.path, repository.config)
+                validate = repo_validate
+
+            if args.delete:
+                validate(section, name, check_value=False)
+                config.remove_option(section, name)
+                if len(config.options(section)) == 0:
+                    config.remove_section(section)
+                save()
+            elif args.value:
+                validate(section, name, args.value)
+                if section not in config.sections():
+                    config.add_section(section)
+                config.set(section, name, args.value)
+                save()
+            else:
+                try:
+                    print(config.get(section, name))
+                except (configparser.NoOptionError, configparser.NoSectionError) as e:
+                    print(e, file=sys.stderr)
+                    return EXIT_WARNING
+            return EXIT_SUCCESS
+        finally:
+            if args.cache:
+                cache.close()
 
     def do_debug_info(self, args):
         """display system information for debugging / bug reports"""