瀏覽代碼

Merge pull request #682 from ThomasWaldmann/validate-newname

add a simple archivename validator, fixes #680
TW 9 年之前
父節點
當前提交
fb83f619a0
共有 2 個文件被更改,包括 11 次插入2 次删除
  1. 3 2
      borg/archiver.py
  2. 8 0
      borg/helpers.py

+ 3 - 2
borg/archiver.py

@@ -15,7 +15,7 @@ import textwrap
 import traceback
 
 from . import __version__
-from .helpers import Error, location_validator, format_time, format_file_size, \
+from .helpers import Error, location_validator, archivename_validator, format_time, format_file_size, \
     parse_pattern, PathPrefixPattern, to_localtime, timestamp, \
     get_cache_dir, get_keys_dir, prune_within, prune_split, \
     Manifest, remove_surrogates, update_excludes, format_archive, check_extension_modules, Statistics, \
@@ -1053,7 +1053,8 @@ class Archiver:
         subparser.add_argument('location', metavar='ARCHIVE',
                                type=location_validator(archive=True),
                                help='archive to rename')
-        subparser.add_argument('name', metavar='NEWNAME', type=str,
+        subparser.add_argument('name', metavar='NEWNAME',
+                               type=archivename_validator(),
                                help='the new archive name to use')
 
         delete_epilog = textwrap.dedent("""

+ 8 - 0
borg/helpers.py

@@ -757,6 +757,14 @@ def location_validator(archive=None):
     return validator
 
 
+def archivename_validator():
+    def validator(text):
+        if '/' in text or '::' in text or not text:
+            raise argparse.ArgumentTypeError('Invalid repository name: "%s"' % text)
+        return text
+    return validator
+
+
 def decode_dict(d, keys, encoding='utf-8', errors='surrogateescape'):
     for key in keys:
         if isinstance(d.get(key), bytes):