diff --git a/src/borg/archiver.py b/src/borg/archiver.py index 9eee69f20..b76bbf07d 100644 --- a/src/borg/archiver.py +++ b/src/borg/archiver.py @@ -44,7 +44,7 @@ from .crypto.key import key_creator, tam_required_file, tam_required, RepoKey, P from .crypto.keymanager import KeyManager from .helpers import EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR from .helpers import Error, NoManifestError, set_ec -from .helpers import location_validator, archivename_validator, ChunkerParams +from .helpers import positive_int_validator, location_validator, archivename_validator, ChunkerParams from .helpers import PrefixSpec, SortBySpec, HUMAN_SORT_KEYS from .helpers import BaseFormatter, ItemFormatter, ArchiveFormatter from .helpers import format_timedelta, format_file_size, parse_file_size, format_archive @@ -2394,9 +2394,9 @@ class Archiver: if first_last: group = filters_group.add_mutually_exclusive_group() - group.add_argument('--first', metavar='N', dest='first', default=0, type=int, + group.add_argument('--first', metavar='N', dest='first', default=0, type=positive_int_validator, help='consider first N archives after other filters were applied') - group.add_argument('--last', metavar='N', dest='last', default=0, type=int, + group.add_argument('--last', metavar='N', dest='last', default=0, type=positive_int_validator, help='consider last N archives after other filters were applied') parser = argparse.ArgumentParser(prog=self.prog, description='Borg - Deduplicated Backups', diff --git a/src/borg/helpers.py b/src/borg/helpers.py index cd8f6bb42..70d25d978 100644 --- a/src/borg/helpers.py +++ b/src/borg/helpers.py @@ -236,7 +236,7 @@ class Archives(abc.MutableMapping): if first: archives = archives[:first] elif last: - archives = archives[len(archives) - last:] + archives = archives[max(len(archives) - last, 0):] return archives def list_considering(self, args): @@ -400,6 +400,14 @@ class Manifest: self.repository.put(self.MANIFEST_ID, self.key.encrypt(data)) +def positive_int_validator(value): + """argparse type for positive integers""" + int_value = int(value) + if int_value <= 0: + raise argparse.ArgumentTypeError('A positive integer is required: %s' % value) + return int_value + + def interval(s): """Convert a string representing a valid interval to a number of hours.""" multiplier = {'H': 1, 'd': 24, 'w': 24 * 7, 'm': 24 * 31, 'y': 24 * 365}