ngclient: simplify storing a downloaded file

Replace the usage of securesystemslib.util.persist_temp_file() with
shutil.copyfileobj() as file system abstraction is not used in the
client.
This way we prevent securesystemslib.exception.StorageError from
leaking through client API calls.

Note: with those changes we are no longer do fsync.

Signed-off-by: Martin Vrachev <mvrachev@vmware.com>
This commit is contained in:
Martin Vrachev 2022-01-26 15:58:26 +02:00
parent d95ead6280
commit 3fa0668f89

View file

@ -35,12 +35,11 @@
import logging
import os
import shutil
import tempfile
from typing import Optional, Set
from urllib import parse
from securesystemslib import util as sslib_util
from tuf.api import exceptions
from tuf.api.metadata import (
Metadata,
@ -218,8 +217,7 @@ def download_target(
ValueError: Invalid arguments
DownloadError: Download of the target file failed in some way
RepositoryError: Downloaded target failed to be verified in some way
exceptions.StorageError: Downloaded target could not be written
to disk
OSError: Failed to write target to file
Returns:
Local path to downloaded file
@ -252,7 +250,9 @@ def download_target(
) as target_file:
targetinfo.verify_length_and_hashes(target_file)
sslib_util.persist_temp_file(target_file, filepath)
target_file.seek(0)
with open(filepath, "wb") as destination_file:
shutil.copyfileobj(target_file, destination_file)
logger.info("Downloaded target %s", targetinfo.path)
return filepath