Skip to content

Commit

Permalink
Improve keyfile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hakavlad committed Jun 17, 2024
1 parent bb2d999 commit a8a74f1
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions tird/tird.py
Original file line number Diff line number Diff line change
Expand Up @@ -990,65 +990,85 @@ def get_keyfile_digest(f_path: str) -> Optional[bytes]:
def get_keyfile_digest_list(d_path: str) -> Optional[list]:
"""
"""
f_tuple_list: list = []
def walk_error_handler(error: Any) -> None:
"""Handle walk error."""
print(f'{ERR}E: {error}{RES}')
raise UserWarning

size_sum: int = 0
# ----------------------------------------------------------------------- #

print(f'{ITA}I: scanning the directory "{d_path}"{RES}')

for root, _, files in walk(d_path):
for fp in files:
f_path: str = path.join(root, fp)
f_path_list: list = []

if DEBUG:
print(f'{ITA}D: getting the size of "{f_path}" '
f'(real path: "{path.realpath(f_path)}"){RES}')
try:
for root, _, files in walk(d_path, onerror=walk_error_handler):
for fp in files:
f_path: str = path.join(root, fp)
f_path_list.append(f_path)
except UserWarning:
return None

opt_f_size: Optional[int] = get_file_size(f_path)
list_len: int = len(f_path_list)

if opt_f_size is None:
return None
print(f'{ITA}I: found {list_len} files{RES}')

f_size: int = opt_f_size
if list_len == 0:
return []

if DEBUG:
print(f'{ITA}D: size: {string_size(f_size)}{RES}')
# ----------------------------------------------------------------------- #

f_tuple_list: list = []

size_sum += f_size
size_sum: int = 0

for f_path in f_path_list:
if DEBUG:
print(f'{ITA}D: getting the size of "{f_path}" '
f'(real path: "{path.realpath(f_path)}"){RES}')

f_tuple: tuple = (f_path, f_size)
opt_f_size: Optional[int] = get_file_size(f_path)

f_tuple_list.append(f_tuple)
if opt_f_size is None:
return None

f_tuple_list_len: int = len(f_tuple_list)
f_size: int = opt_f_size

if f_tuple_list_len == 0:
return []
if DEBUG:
print(f'{ITA}D: size: {string_size(f_size)}{RES}')

size_sum += f_size

f_tuple: tuple = (f_path, f_size)
f_tuple_list.append(f_tuple)

for f_tuple in f_tuple_list:
f_path, f_size = f_tuple
print(f'{ITA} - found "{f_path}", {string_size(f_size)}{RES}')

print(f'{ITA}I: found {f_tuple_list_len} files; '
print(f'{ITA}I: found {list_len} files; '
f'total size: {string_size(size_sum)}{RES}')

# ----------------------------------------------------------------------- #

print(f'{ITA}I: hashing files in the directory "{d_path}"{RES}')

digest_list: list = []

for f_tuple in f_tuple_list:

f_path, f_size = f_tuple

if DEBUG:
print(f'{ITA}D: hashing "{f_path}"{RES}')

f = open_file(f_path, 'rb')
f: Any = open_file(f_path, 'rb')

if f is None:
return None

f_digest = blake2b_keyfile_digest(f, f_size, salt=sd['blake2_salt'])
f_digest: Optional[bytes] = blake2b_keyfile_digest(
f, f_size, salt=sd['blake2_salt']
)

close_file(f)

Expand Down

0 comments on commit a8a74f1

Please sign in to comment.