diff --git a/usr/lib/python3.8/spoc/repo_online.py b/usr/lib/python3.8/spoc/repo_online.py index ca11923..05e02c3 100644 --- a/usr/lib/python3.8/spoc/repo_online.py +++ b/usr/lib/python3.8/spoc/repo_online.py @@ -3,6 +3,7 @@ import json import os import requests +import shutil import tarfile import time from cryptography.exceptions import InvalidSignature @@ -57,9 +58,15 @@ def unpack_archive(archive_path, destination, expected_hash, observer): # Verify file object, then seek back and open it as tar without losing handle, preventing possible malicious race conditions verify_fileobj(f, expected_hash) f.seek(0) - tar = tarfile.open(fileobj=f) + # Remove the target directory, if it exists from previous failed installation + dst_dir = os.path.join(destination, os.path.basename(archive_path)[:-7]) + try: + shutil.rmtree(dst_dir) + except FileNotFoundError: + pass # Extract the tar members while counting their size # If this is done as non-root, extractall() from https://github.com/python/cpython/blob/master/Lib/tarfile.py needs to be reimplemented instead + tar = tarfile.open(fileobj=f) for tarinfo in tar: tar.extract(tarinfo, destination, numeric_owner=True) observer.units_done += tarinfo.size