Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(untar): Use shutil, direct path for single tar #8

Merged
merged 1 commit into from
Jul 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions src/snap_to_bucket/fs_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sys
import time
import json
import shutil
import subprocess
from subprocess import PIPE, Popen

Expand All @@ -28,8 +29,6 @@ class FsHandler:
:ivar device: The device to be mounted
"""

TEN_MB = 10 * (1024 ** 2)

def __init__(self, mount_point, verbose=0):
"""
Initializer for the class attributes.
Expand Down Expand Up @@ -196,23 +195,46 @@ def untar(self, tar_location):
if ".tar.gz" in tar_location:
tar_options.append("--gzip")
self.untar_process = Popen(tar_options, stdin=PIPE)
temp_file_obj = open(tar_location, "rb")
temp_file_obj.seek(0, os.SEEK_END)
file_length = temp_file_obj.tell()
temp_file_obj.seek(0, os.SEEK_SET)
while temp_file_obj.tell() < file_length:
free_mem = psutil.virtual_memory().available
max_chunk = free_mem - self.TEN_MB
self.untar_process.stdin.write(temp_file_obj.read(max_chunk))
temp_file_obj.close()
with open(tar_location, "rb") as temp_file_obj:
shutil.copyfileobj(temp_file_obj, self.untar_process.stdin)
self.untar_process.stdin.flush()
os.unlink(tar_location)

def untar_single_file(self, tar_location):
"""
Start untar process for a single part tars

1. Start untar process
a. If tar failed, raise exception
2. Remove the tar file

:param tar_location: Location of the tar file
:type tar_location: string
:raises Exception: If tar process failes
"""
print(f"Untaring file '{tar_location}' to '{self.mount_point}'")
tar_options = ["tar", "--extract", "--directory", self.mount_point,
"--preserve-permissions", "--preserve-order"]
if ".tar.gz" in tar_location:
tar_options.append("--gzip")
tar_options.extend(["--file", tar_location])
untar_process = Popen(tar_options, stdout=PIPE, stderr=PIPE)
response = untar_process.communicate()
if untar_process.returncode != 0:
output = response[0].decode('UTF-8').strip()
error = response[1].decode('UTF-8').strip()
print(f"Untar failed: {output}\n{error}.", file=sys.stderr)
raise Exception("Tar failed")
os.unlink(tar_location)

def terminate_tar(self):
"""
Close the untar process
"""
self.untar_process.stdin.close()
self.untar_process.wait()
returncode = self.untar_process.wait()
if returncode != 0:
print(f"Untar returned: {returncode}", file=sys.stderr)

def mount_required_folders(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/snap_to_bucket/snap_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def __restore_snapshot(self):
if no_of_objects == 1:
temp_path = self.__s3handler.download_key(self.__restore_key,
-1, self.__restore_dir)
self.__fshandler.untar(temp_path)
self.__fshandler.untar_single_file(temp_path)
else:
for i in range(1, no_of_objects + 1):
temp_path = self.__s3handler.download_key(
Expand Down