Make unpacking remove target directory, if it exists from previous failed installation

This commit is contained in:
Disassembler 2020-04-03 14:36:49 +02:00
parent 5f5bad4c20
commit eeab3cb54c
No known key found for this signature in database
GPG Key ID: 524BD33A0EE29499

View File

@ -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