Skip to content

Commit 6a565c8

Browse files
pathlib refactor key
1 parent aea3d6a commit 6a565c8

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

src/borg/crypto/key.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import textwrap
55
from hashlib import sha256, pbkdf2_hmac
6+
from pathlib import Path
67
from typing import Literal, ClassVar
78
from collections.abc import Callable
89

@@ -642,11 +643,11 @@ def get_existing_or_new_target(self, args):
642643

643644
def _find_key_in_keys_dir(self):
644645
id = self.repository.id
645-
keys_dir = get_keys_dir()
646-
for name in os.listdir(keys_dir):
647-
filename = os.path.join(keys_dir, name)
646+
keys_path = Path(get_keys_dir())
647+
for entry in keys_path.iterdir():
648+
filename = keys_path / entry.name
648649
try:
649-
return self.sanity_check(filename, id)
650+
return self.sanity_check(str(filename), id)
650651
except (KeyfileInvalidError, KeyfileMismatchError):
651652
pass
652653

@@ -668,12 +669,12 @@ def _find_key_file_from_environment(self):
668669

669670
def _get_new_target_in_keys_dir(self, args):
670671
filename = args.location.to_key_filename()
671-
path = filename
672+
path = Path(filename)
672673
i = 1
673-
while os.path.exists(path):
674+
while path.exists():
674675
i += 1
675-
path = filename + ".%d" % i
676-
return path
676+
path = Path(filename + ".%d" % i)
677+
return str(path)
677678

678679
def load(self, target, passphrase):
679680
if self.STORAGE == KeyBlobStorage.KEYFILE:

0 commit comments

Comments
 (0)