Skip to content

Commit

Permalink
Merge pull request #8017 from ThomasWaldmann/do-not-accept-empty-path…
Browse files Browse the repository at this point in the history
…s-master

PATH: do not accept empty strings, fixes #4221
  • Loading branch information
ThomasWaldmann committed Jan 2, 2024
2 parents 1432f54 + 73284db commit 9583540
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/borg/archiver/create_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from ..cache import Cache
from ..constants import * # NOQA
from ..compress import CompressionSpec
from ..helpers import comment_validator, ChunkerParams
from ..helpers import comment_validator, ChunkerParams, PathSpec
from ..helpers import archivename_validator, FilesCacheMode
from ..helpers import eval_escapes
from ..helpers import timestamp, archive_ts_now
Expand Down Expand Up @@ -937,4 +937,6 @@ def build_parser_create(self, subparsers, common_parser, mid_common_parser):
)

subparser.add_argument("name", metavar="NAME", type=archivename_validator, help="specify the archive name")
subparser.add_argument("paths", metavar="PATH", nargs="*", type=str, action="extend", help="paths to archive")
subparser.add_argument(
"paths", metavar="PATH", nargs="*", type=PathSpec, action="extend", help="paths to archive"
)
4 changes: 2 additions & 2 deletions src/borg/archiver/diff_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ._common import with_repository, with_archive, build_matcher, Highlander
from ..archive import Archive
from ..constants import * # NOQA
from ..helpers import BaseFormatter, DiffFormatter, archivename_validator, BorgJsonEncoder
from ..helpers import BaseFormatter, DiffFormatter, archivename_validator, PathSpec, BorgJsonEncoder
from ..manifest import Manifest
from ..logger import create_logger

Expand Down Expand Up @@ -167,7 +167,7 @@ def build_parser_diff(self, subparsers, common_parser, mid_common_parser):
"paths",
metavar="PATH",
nargs="*",
type=str,
type=PathSpec,
help="paths of items inside the archives to compare; patterns are supported",
)
define_exclusion_group(subparser)
4 changes: 2 additions & 2 deletions src/borg/archiver/extract_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ._common import build_filter, build_matcher
from ..archive import BackupError, BackupOSError
from ..constants import * # NOQA
from ..helpers import archivename_validator
from ..helpers import archivename_validator, PathSpec
from ..helpers import remove_surrogates
from ..helpers import HardLinkManager
from ..helpers import ProgressIndicatorPercent
Expand Down Expand Up @@ -177,6 +177,6 @@ def build_parser_extract(self, subparsers, common_parser, mid_common_parser):
)
subparser.add_argument("name", metavar="NAME", type=archivename_validator, help="specify the archive name")
subparser.add_argument(
"paths", metavar="PATH", nargs="*", type=str, help="paths to extract; patterns are supported"
"paths", metavar="PATH", nargs="*", type=PathSpec, help="paths to extract; patterns are supported"
)
define_exclusion_group(subparser, strip_components=True)
5 changes: 3 additions & 2 deletions src/borg/archiver/key_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..crypto.key import AESOCBRepoKey, CHPORepoKey, Blake2AESOCBRepoKey, Blake2CHPORepoKey
from ..crypto.key import AESOCBKeyfileKey, CHPOKeyfileKey, Blake2AESOCBKeyfileKey, Blake2CHPOKeyfileKey
from ..crypto.keymanager import KeyManager
from ..helpers import PathSpec
from ..manifest import Manifest

from ._common import with_repository
Expand Down Expand Up @@ -194,7 +195,7 @@ def build_parser_keys(self, subparsers, common_parser, mid_common_parser):
help="export repository key for backup",
)
subparser.set_defaults(func=self.do_key_export)
subparser.add_argument("path", metavar="PATH", nargs="?", type=str, help="where to store the backup")
subparser.add_argument("path", metavar="PATH", nargs="?", type=PathSpec, help="where to store the backup")
subparser.add_argument(
"--paper",
dest="paper",
Expand Down Expand Up @@ -237,7 +238,7 @@ def build_parser_keys(self, subparsers, common_parser, mid_common_parser):
)
subparser.set_defaults(func=self.do_key_import)
subparser.add_argument(
"path", metavar="PATH", nargs="?", type=str, help="path to the backup ('-' to read from stdin)"
"path", metavar="PATH", nargs="?", type=PathSpec, help="path to the backup ('-' to read from stdin)"
)
subparser.add_argument(
"--paper",
Expand Down
4 changes: 2 additions & 2 deletions src/borg/archiver/list_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..archive import Archive
from ..cache import Cache
from ..constants import * # NOQA
from ..helpers import ItemFormatter, BaseFormatter, archivename_validator
from ..helpers import ItemFormatter, BaseFormatter, archivename_validator, PathSpec
from ..manifest import Manifest

from ..logger import create_logger
Expand Down Expand Up @@ -119,6 +119,6 @@ def build_parser_list(self, subparsers, common_parser, mid_common_parser):
)
subparser.add_argument("name", metavar="NAME", type=archivename_validator, help="specify the archive name")
subparser.add_argument(
"paths", metavar="PATH", nargs="*", type=str, help="paths to list; patterns are supported"
"paths", metavar="PATH", nargs="*", type=PathSpec, help="paths to list; patterns are supported"
)
define_exclusion_group(subparser)
3 changes: 2 additions & 1 deletion src/borg/archiver/mount_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ._common import with_repository, Highlander
from ..constants import * # NOQA
from ..helpers import EXIT_ERROR
from ..helpers import PathSpec
from ..helpers import umount
from ..manifest import Manifest
from ..remote import cache_if_remote
Expand Down Expand Up @@ -180,6 +181,6 @@ def _define_borg_mount(self, parser):
)
define_archive_filters_group(parser)
parser.add_argument(
"paths", metavar="PATH", nargs="*", type=str, help="paths to extract; patterns are supported"
"paths", metavar="PATH", nargs="*", type=PathSpec, help="paths to extract; patterns are supported"
)
define_exclusion_group(parser, strip_components=True)
4 changes: 2 additions & 2 deletions src/borg/archiver/recreate_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..archive import ArchiveRecreater
from ..constants import * # NOQA
from ..compress import CompressionSpec
from ..helpers import archivename_validator, comment_validator, ChunkerParams
from ..helpers import archivename_validator, comment_validator, PathSpec, ChunkerParams
from ..helpers import timestamp
from ..manifest import Manifest

Expand Down Expand Up @@ -205,5 +205,5 @@ def build_parser_recreate(self, subparsers, common_parser, mid_common_parser):
)

subparser.add_argument(
"paths", metavar="PATH", nargs="*", type=str, help="paths to recreate; patterns are supported"
"paths", metavar="PATH", nargs="*", type=PathSpec, help="paths to recreate; patterns are supported"
)
4 changes: 2 additions & 2 deletions src/borg/archiver/tar_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..helpers import msgpack
from ..helpers import create_filter_process
from ..helpers import ChunkIteratorFileWrapper
from ..helpers import archivename_validator, comment_validator, ChunkerParams
from ..helpers import archivename_validator, comment_validator, PathSpec, ChunkerParams
from ..helpers import remove_surrogates
from ..helpers import timestamp, archive_ts_now
from ..helpers import basic_json_data, json_print
Expand Down Expand Up @@ -418,7 +418,7 @@ def build_parser_tar(self, subparsers, common_parser, mid_common_parser):
subparser.add_argument("name", metavar="NAME", type=archivename_validator, help="specify the archive name")
subparser.add_argument("tarfile", metavar="FILE", help='output tar file. "-" to write to stdout instead.')
subparser.add_argument(
"paths", metavar="PATH", nargs="*", type=str, help="paths to extract; patterns are supported"
"paths", metavar="PATH", nargs="*", type=PathSpec, help="paths to extract; patterns are supported"
)
define_exclusion_group(subparser, strip_components=True)

Expand Down
2 changes: 1 addition & 1 deletion src/borg/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from .parseformat import bin_to_hex, safe_encode, safe_decode
from .parseformat import text_to_json, binary_to_json, remove_surrogates, join_cmd
from .parseformat import eval_escapes, decode_dict, positive_int_validator, interval
from .parseformat import SortBySpec, ChunkerParams, FilesCacheMode, partial_format, DatetimeWrapper
from .parseformat import PathSpec, SortBySpec, ChunkerParams, FilesCacheMode, partial_format, DatetimeWrapper
from .parseformat import format_file_size, parse_file_size, FileSize, parse_storage_quota
from .parseformat import sizeof_fmt, sizeof_fmt_iec, sizeof_fmt_decimal, Location, text_validator
from .parseformat import format_line, replace_placeholders, PlaceholderError, relative_time_marker_validator
Expand Down
6 changes: 6 additions & 0 deletions src/borg/helpers/parseformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ def __call__(self, text, overrides=None):
replace_placeholders = PlaceholderReplacer()


def PathSpec(text):
if not text:
raise argparse.ArgumentTypeError("Empty strings are not accepted as paths.")
return text


def SortBySpec(text):
from ..manifest import AI_HUMAN_SORT_KEYS

Expand Down

0 comments on commit 9583540

Please sign in to comment.