Explicitly cleanup temp files and dirs

This commit is contained in:
Micah Lee 2021-12-01 20:34:54 -08:00
parent 109d63a10c
commit 8ec9a24af1
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
6 changed files with 38 additions and 26 deletions

View File

@ -40,9 +40,6 @@ class OnionShare(object):
self.onion_host = None self.onion_host = None
self.port = None self.port = None
# files and dirs to delete on shutdown
self.cleanup_filenames = []
# do not use tor -- for development # do not use tor -- for development
self.local_only = local_only self.local_only = local_only
@ -75,7 +72,9 @@ class OnionShare(object):
if self.local_only: if self.local_only:
self.onion_host = f"127.0.0.1:{self.port}" self.onion_host = f"127.0.0.1:{self.port}"
if not mode_settings.get("general", "public"): if not mode_settings.get("general", "public"):
self.auth_string = "E2GOT5LTUTP3OAMRCRXO4GSH6VKJEUOXZQUC336SRKAHTTT5OVSA" self.auth_string = (
"E2GOT5LTUTP3OAMRCRXO4GSH6VKJEUOXZQUC336SRKAHTTT5OVSA"
)
return return
self.onion_host = self.onion.start_onion_service( self.onion_host = self.onion.start_onion_service(

View File

@ -202,6 +202,9 @@ class SendBaseModeWeb:
file_to_download = self.gzip_individual_files[filesystem_path] file_to_download = self.gzip_individual_files[filesystem_path]
filesize = os.path.getsize(self.gzip_individual_files[filesystem_path]) filesize = os.path.getsize(self.gzip_individual_files[filesystem_path])
# Cleanup this temp file
self.web.cleanup_tempfiles.append(gzip_file)
else: else:
file_to_download = filesystem_path file_to_download = filesystem_path
filesize = os.path.getsize(filesystem_path) filesize = os.path.getsize(filesystem_path)

View File

@ -504,10 +504,13 @@ class ShareModeWeb(SendBaseModeWeb):
self.is_zipped = False self.is_zipped = False
# Cleanup this tempfile
self.web.cleanup_tempfiles.append(self.gzip_file)
else: else:
# Zip up the files and folders # Zip up the files and folders
self.zip_writer = ZipWriter( self.zip_writer = ZipWriter(
self.common, processed_size_callback=processed_size_callback self.common, self.web, processed_size_callback=processed_size_callback
) )
self.download_filename = self.zip_writer.zip_filename self.download_filename = self.zip_writer.zip_filename
for info in self.file_info["files"]: for info in self.file_info["files"]:
@ -538,8 +541,9 @@ class ZipWriter(object):
filename. filename.
""" """
def __init__(self, common, zip_filename=None, processed_size_callback=None): def __init__(self, common, web, zip_filename=None, processed_size_callback=None):
self.common = common self.common = common
self.web = web
self.cancel_compression = False self.cancel_compression = False
if zip_filename: if zip_filename:
@ -550,6 +554,9 @@ class ZipWriter(object):
) )
self.zip_filename = f"{self.zip_temp_dir.name}/onionshare_{self.common.random_string(4, 6)}.zip" self.zip_filename = f"{self.zip_temp_dir.name}/onionshare_{self.common.random_string(4, 6)}.zip"
# Cleanup this temp dir
self.web.cleanup_tempdirs.append(self.zip_temp_dir)
self.z = zipfile.ZipFile(self.zip_filename, "w", allowZip64=True) self.z = zipfile.ZipFile(self.zip_filename, "w", allowZip64=True)
self.processed_size_callback = processed_size_callback self.processed_size_callback = processed_size_callback
if self.processed_size_callback is None: if self.processed_size_callback is None:

View File

@ -155,7 +155,8 @@ class Web:
self.socketio.init_app(self.app) self.socketio.init_app(self.app)
self.chat_mode = ChatModeWeb(self.common, self) self.chat_mode = ChatModeWeb(self.common, self)
self.cleanup_filenames = [] self.cleanup_tempfiles = []
self.cleanup_tempdirs = []
def get_mode(self): def get_mode(self):
if self.mode == "share": if self.mode == "share":
@ -199,7 +200,10 @@ class Web:
for header, value in self.security_headers: for header, value in self.security_headers:
r.headers.set(header, value) r.headers.set(header, value)
# Set a CSP header unless in website mode and the user has disabled it # Set a CSP header unless in website mode and the user has disabled it
if not self.settings.get("website", "disable_csp") or self.mode != "website": if (
not self.settings.get("website", "disable_csp")
or self.mode != "website"
):
r.headers.set( r.headers.set(
"Content-Security-Policy", "Content-Security-Policy",
"default-src 'self'; frame-ancestors 'none'; form-action 'self'; base-uri 'self'; img-src 'self' data:;", "default-src 'self'; frame-ancestors 'none'; form-action 'self'; base-uri 'self'; img-src 'self' data:;",
@ -380,14 +384,13 @@ class Web:
""" """
self.common.log("Web", "cleanup") self.common.log("Web", "cleanup")
# Cleanup files # Close all of the tempfile.NamedTemporaryFile
try: for file in self.cleanup_tempfiles:
for filename in self.cleanup_filenames: file.close()
if os.path.isfile(filename):
os.remove(filename) # Clean up the tempfile.NamedTemporaryDirectory objects
elif os.path.isdir(filename): for dir in self.cleanup_tempdirs:
shutil.rmtree(filename) dir.cleanup()
except Exception:
# Don't crash if file is still in use self.cleanup_tempfiles = []
pass self.cleanup_tempdirs = []
self.cleanup_filenames = []

View File

@ -660,9 +660,6 @@ class Tab(QtWidgets.QWidget):
# Close # Close
if self.close_dialog.clickedButton() == self.close_dialog.accept_button: if self.close_dialog.clickedButton() == self.close_dialog.accept_button:
self.common.log("Tab", "close_tab", "close, closing tab")
self.get_mode().stop_server()
self.get_mode().web.cleanup()
return True return True
# Cancel # Cancel
else: else:
@ -671,8 +668,10 @@ class Tab(QtWidgets.QWidget):
def cleanup(self): def cleanup(self):
self.common.log("Tab", "cleanup", f"tab_id={self.tab_id}") self.common.log("Tab", "cleanup", f"tab_id={self.tab_id}")
if self.get_mode() and self.get_mode().web_thread: if self.get_mode():
if self.get_mode().web_thread:
self.get_mode().web.stop(self.get_mode().app.port) self.get_mode().web.stop(self.get_mode().app.port)
self.get_mode().web_thread.quit() self.get_mode().web_thread.quit()
self.get_mode().web_thread.wait() self.get_mode().web_thread.wait()
self.get_mode().web.cleanup() self.get_mode().web.cleanup()

View File

@ -316,6 +316,7 @@ class TabWidget(QtWidgets.QTabWidget):
self.common.log("TabWidget", "closing a service tab") self.common.log("TabWidget", "closing a service tab")
if tab.close_tab(): if tab.close_tab():
self.common.log("TabWidget", "user is okay with closing the tab") self.common.log("TabWidget", "user is okay with closing the tab")
tab.cleanup()
# If the tab is persistent, delete the settings file from disk # If the tab is persistent, delete the settings file from disk
if tab.settings.get("persistent", "enabled"): if tab.settings.get("persistent", "enabled"):