Skip to content

Commit

Permalink
Merge pull request #8102 from ThomasWaldmann/error-msg-bad-nonce-file…
Browse files Browse the repository at this point in the history
…-master

give clean error msg for invalid nonce file, see #7967
  • Loading branch information
ThomasWaldmann committed Feb 18, 2024
2 parents b65888f + ab0b111 commit 3bfd765
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/borg/helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .fs import HardLinkManager
from .misc import sysinfo, log_multi, consume
from .misc import ChunkIteratorFileWrapper, open_item, chunkit, iter_separated, ErrorIgnoringTextIOWrapper
from .parseformat import bin_to_hex, safe_encode, safe_decode
from .parseformat import bin_to_hex, hex_to_bin, 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 PathSpec, SortBySpec, ChunkerParams, FilesCacheMode, partial_format, DatetimeWrapper
Expand Down
15 changes: 13 additions & 2 deletions src/borg/helpers/parseformat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import abc
import argparse
import base64
import binascii
import hashlib
import json
import os
Expand All @@ -10,7 +11,6 @@
import stat
import uuid
from typing import Dict, Set, Tuple, ClassVar, Any, TYPE_CHECKING, Literal
from binascii import hexlify
from collections import Counter, OrderedDict
from datetime import datetime, timezone
from functools import partial
Expand All @@ -33,7 +33,18 @@


def bin_to_hex(binary):
return hexlify(binary).decode("ascii")
return binascii.hexlify(binary).decode("ascii")


def hex_to_bin(hex, length=None):
try:
binary = binascii.unhexlify(hex)
binary_len = len(binary)
if length is not None and binary_len != length:
raise ValueError(f"Expected {length} bytes ({2 * length} hex digits), got {binary_len} bytes.")
except binascii.Error as e:
raise ValueError(str(e)) from None
return binary


def safe_decode(s, coding="utf-8", errors="surrogateescape"):
Expand Down
8 changes: 6 additions & 2 deletions src/borg/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from . import __version__
from .compress import Compressor
from .constants import * # NOQA
from .helpers import Error, IntegrityError
from .helpers import Error, ErrorWithTraceback, IntegrityError
from .helpers import bin_to_hex
from .helpers import get_limited_unpacker
from .helpers import replace_placeholders
Expand Down Expand Up @@ -766,7 +766,11 @@ def handle_error(unpacked):
error = unpacked["exception_class"]
args = unpacked["exception_args"]

if error == "DoesNotExist":
if error == "Error":
raise Error(args[0])
elif error == "ErrorWithTraceback":
raise ErrorWithTraceback(args[0])
elif error == "DoesNotExist":
raise Repository.DoesNotExist(self.location.processed)
elif error == "AlreadyExists":
raise Repository.AlreadyExists(self.location.processed)
Expand Down

0 comments on commit 3bfd765

Please sign in to comment.