From f5b60de2e6f127c731d1c06fdb85d0131bfb7a92 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Thu, 18 Nov 2021 20:03:18 -0800 Subject: [PATCH 1/6] Use high level tempfile classes that clean up after themselves --- cli/onionshare_cli/web/send_base_mode.py | 17 +++++----- cli/onionshare_cli/web/share_mode.py | 43 +++++++++++++----------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index 27de598a..52720f6a 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -42,10 +42,11 @@ class SendBaseModeWeb: self.is_zipped = False self.download_filename = None self.download_filesize = None - self.gzip_filename = None - self.gzip_filesize = None self.zip_writer = None + # Store the tempfile objects here, so when they're garbage collected the files are deleted + self.gzip_files = [] + # If autostop_sharing, only allow one download at a time self.download_in_progress = False @@ -192,12 +193,12 @@ class SendBaseModeWeb: # gzip compress the individual file, if it hasn't already been compressed if use_gzip: if filesystem_path not in self.gzip_individual_files: - gzip_filename = tempfile.mkstemp("wb+")[1] - self._gzip_compress(filesystem_path, gzip_filename, 6, None) - self.gzip_individual_files[filesystem_path] = gzip_filename - - # Make sure the gzip file gets cleaned up when onionshare stops - self.web.cleanup_filenames.append(gzip_filename) + self.gzip_files.append( + tempfile.TemporaryFile("wb+", dir=self.common.build_tmp_dir()) + ) + gzip_file = self.gzip_files[-1] + self._gzip_compress(filesystem_path, gzip_file.name, 6, None) + self.gzip_individual_files[filesystem_path] = gzip_file.name file_to_download = self.gzip_individual_files[filesystem_path] filesize = os.path.getsize(self.gzip_individual_files[filesystem_path]) diff --git a/cli/onionshare_cli/web/share_mode.py b/cli/onionshare_cli/web/share_mode.py index 92a4c9af..09799679 100644 --- a/cli/onionshare_cli/web/share_mode.py +++ b/cli/onionshare_cli/web/share_mode.py @@ -134,8 +134,12 @@ class ShareModeWeb(SendBaseModeWeb): The web app routes for sharing files """ - @self.web.app.route("/", defaults={"path": ""}, methods=["GET"], provide_automatic_options=False) - @self.web.app.route("/", methods=["GET"], provide_automatic_options=False) + @self.web.app.route( + "/", defaults={"path": ""}, methods=["GET"], provide_automatic_options=False + ) + @self.web.app.route( + "/", methods=["GET"], provide_automatic_options=False + ) def index(path): """ Render the template for the onionshare landing page. @@ -159,7 +163,9 @@ class ShareModeWeb(SendBaseModeWeb): return self.render_logic(path) - @self.web.app.route("/download", methods=["GET"], provide_automatic_options=False) + @self.web.app.route( + "/download", methods=["GET"], provide_automatic_options=False + ) def download(): """ Download the zip file. @@ -183,7 +189,7 @@ class ShareModeWeb(SendBaseModeWeb): # and serve that use_gzip = self.should_use_gzip() if use_gzip: - file_to_download = self.gzip_filename + file_to_download = self.gzip_file.name self.filesize = self.gzip_filesize etag = self.gzip_etag else: @@ -286,7 +292,9 @@ class ShareModeWeb(SendBaseModeWeb): if if_unmod: if_date = parse_date(if_unmod) if if_date and not if_date.tzinfo: - if_date = if_date.replace(tzinfo=timezone.utc) # Compatible with Flask < 2.0.0 + if_date = if_date.replace( + tzinfo=timezone.utc + ) # Compatible with Flask < 2.0.0 if if_date and if_date > last_modified: abort(412) elif range_header is None: @@ -484,17 +492,16 @@ class ShareModeWeb(SendBaseModeWeb): self.download_etag = make_etag(f) # Compress the file with gzip now, so we don't have to do it on each request - self.gzip_filename = tempfile.mkstemp("wb+")[1] - self._gzip_compress( - self.download_filename, self.gzip_filename, 6, processed_size_callback + self.gzip_file = tempfile.TemporaryFile( + "wb+", dir=self.common.build_tmp_dir() ) - self.gzip_filesize = os.path.getsize(self.gzip_filename) - with open(self.gzip_filename, "rb") as f: + self._gzip_compress( + self.download_filename, self.gzip_file.name, 6, processed_size_callback + ) + self.gzip_filesize = os.path.getsize(self.gzip_file.name) + with open(self.gzip_file.name, "rb") as f: self.gzip_etag = make_etag(f) - # Make sure the gzip file gets cleaned up when onionshare stops - self.web.cleanup_filenames.append(self.gzip_filename) - self.is_zipped = False else: @@ -519,10 +526,6 @@ class ShareModeWeb(SendBaseModeWeb): with open(self.download_filename, "rb") as f: self.download_etag = make_etag(f) - # Make sure the zip file gets cleaned up when onionshare stops - self.web.cleanup_filenames.append(self.zip_writer.zip_filename) - self.web.cleanup_filenames.append(self.zip_writer.zip_temp_dir) - self.is_zipped = True return True @@ -542,10 +545,10 @@ class ZipWriter(object): if zip_filename: self.zip_filename = zip_filename else: - self.zip_temp_dir = tempfile.mkdtemp() - self.zip_filename = ( - f"{self.zip_temp_dir}/onionshare_{self.common.random_string(4, 6)}.zip" + self.zip_temp_dir = tempfile.TemporaryDirectory( + dir=self.common.build_tmp_dir() ) + self.zip_filename = f"{self.zip_temp_dir.name}/onionshare_{self.common.random_string(4, 6)}.zip" self.z = zipfile.ZipFile(self.zip_filename, "w", allowZip64=True) self.processed_size_callback = processed_size_callback From b659eccfbfb726fe81f6928547c06e28c3f1e51c Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Thu, 18 Nov 2021 20:17:39 -0800 Subject: [PATCH 2/6] Use a NamedTemporaryFile instead of TemporaryFile for streamed gzip files --- cli/onionshare_cli/web/send_base_mode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index 52720f6a..7b587182 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -194,7 +194,7 @@ class SendBaseModeWeb: if use_gzip: if filesystem_path not in self.gzip_individual_files: self.gzip_files.append( - tempfile.TemporaryFile("wb+", dir=self.common.build_tmp_dir()) + tempfile.NamedTemporaryFile("wb+", dir=self.common.build_tmp_dir()) ) gzip_file = self.gzip_files[-1] self._gzip_compress(filesystem_path, gzip_file.name, 6, None) From 109d63a10c90c656409c6a3d66e146becc151b0b Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 1 Dec 2021 20:10:51 -0800 Subject: [PATCH 3/6] Use NamedTemporaryFile instead of TemporaryFile, to fix crash when sharing one file --- cli/onionshare_cli/web/share_mode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/onionshare_cli/web/share_mode.py b/cli/onionshare_cli/web/share_mode.py index 09799679..75c11a83 100644 --- a/cli/onionshare_cli/web/share_mode.py +++ b/cli/onionshare_cli/web/share_mode.py @@ -467,7 +467,7 @@ class ShareModeWeb(SendBaseModeWeb): return self.web.error404(history_id) def build_zipfile_list(self, filenames, processed_size_callback=None): - self.common.log("ShareModeWeb", "build_zipfile_list") + self.common.log("ShareModeWeb", "build_zipfile_list", f"filenames={filenames}") for filename in filenames: info = { "filename": filename, @@ -492,7 +492,7 @@ class ShareModeWeb(SendBaseModeWeb): self.download_etag = make_etag(f) # Compress the file with gzip now, so we don't have to do it on each request - self.gzip_file = tempfile.TemporaryFile( + self.gzip_file = tempfile.NamedTemporaryFile( "wb+", dir=self.common.build_tmp_dir() ) self._gzip_compress( From 8ec9a24af1402b0c86b1e811b5e6925d39d3bcdb Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 1 Dec 2021 20:34:54 -0800 Subject: [PATCH 4/6] Explicitly cleanup temp files and dirs --- cli/onionshare_cli/onionshare.py | 7 +++--- cli/onionshare_cli/web/send_base_mode.py | 3 +++ cli/onionshare_cli/web/share_mode.py | 11 +++++++-- cli/onionshare_cli/web/web.py | 29 +++++++++++++----------- desktop/src/onionshare/tab/tab.py | 13 +++++------ desktop/src/onionshare/tab_widget.py | 1 + 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/cli/onionshare_cli/onionshare.py b/cli/onionshare_cli/onionshare.py index c2711b89..2bb22296 100644 --- a/cli/onionshare_cli/onionshare.py +++ b/cli/onionshare_cli/onionshare.py @@ -40,9 +40,6 @@ class OnionShare(object): self.onion_host = None self.port = None - # files and dirs to delete on shutdown - self.cleanup_filenames = [] - # do not use tor -- for development self.local_only = local_only @@ -75,7 +72,9 @@ class OnionShare(object): if self.local_only: self.onion_host = f"127.0.0.1:{self.port}" if not mode_settings.get("general", "public"): - self.auth_string = "E2GOT5LTUTP3OAMRCRXO4GSH6VKJEUOXZQUC336SRKAHTTT5OVSA" + self.auth_string = ( + "E2GOT5LTUTP3OAMRCRXO4GSH6VKJEUOXZQUC336SRKAHTTT5OVSA" + ) return self.onion_host = self.onion.start_onion_service( diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index 7b587182..d786b8b5 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -202,6 +202,9 @@ class SendBaseModeWeb: file_to_download = 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: file_to_download = filesystem_path filesize = os.path.getsize(filesystem_path) diff --git a/cli/onionshare_cli/web/share_mode.py b/cli/onionshare_cli/web/share_mode.py index 75c11a83..d655255e 100644 --- a/cli/onionshare_cli/web/share_mode.py +++ b/cli/onionshare_cli/web/share_mode.py @@ -504,10 +504,13 @@ class ShareModeWeb(SendBaseModeWeb): self.is_zipped = False + # Cleanup this tempfile + self.web.cleanup_tempfiles.append(self.gzip_file) + else: # Zip up the files and folders 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 for info in self.file_info["files"]: @@ -538,8 +541,9 @@ class ZipWriter(object): 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.web = web self.cancel_compression = False 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" + # Cleanup this temp dir + self.web.cleanup_tempdirs.append(self.zip_temp_dir) + self.z = zipfile.ZipFile(self.zip_filename, "w", allowZip64=True) self.processed_size_callback = processed_size_callback if self.processed_size_callback is None: diff --git a/cli/onionshare_cli/web/web.py b/cli/onionshare_cli/web/web.py index e12fccc7..fe2dee87 100644 --- a/cli/onionshare_cli/web/web.py +++ b/cli/onionshare_cli/web/web.py @@ -155,7 +155,8 @@ class Web: self.socketio.init_app(self.app) self.chat_mode = ChatModeWeb(self.common, self) - self.cleanup_filenames = [] + self.cleanup_tempfiles = [] + self.cleanup_tempdirs = [] def get_mode(self): if self.mode == "share": @@ -199,7 +200,10 @@ class Web: for header, value in self.security_headers: r.headers.set(header, value) # 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( "Content-Security-Policy", "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") - # Cleanup files - try: - for filename in self.cleanup_filenames: - if os.path.isfile(filename): - os.remove(filename) - elif os.path.isdir(filename): - shutil.rmtree(filename) - except Exception: - # Don't crash if file is still in use - pass - self.cleanup_filenames = [] + # Close all of the tempfile.NamedTemporaryFile + for file in self.cleanup_tempfiles: + file.close() + + # Clean up the tempfile.NamedTemporaryDirectory objects + for dir in self.cleanup_tempdirs: + dir.cleanup() + + self.cleanup_tempfiles = [] + self.cleanup_tempdirs = [] diff --git a/desktop/src/onionshare/tab/tab.py b/desktop/src/onionshare/tab/tab.py index 2e592771..b0aa25df 100644 --- a/desktop/src/onionshare/tab/tab.py +++ b/desktop/src/onionshare/tab/tab.py @@ -660,9 +660,6 @@ class Tab(QtWidgets.QWidget): # Close 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 # Cancel else: @@ -671,8 +668,10 @@ class Tab(QtWidgets.QWidget): def cleanup(self): self.common.log("Tab", "cleanup", f"tab_id={self.tab_id}") - if self.get_mode() and self.get_mode().web_thread: - self.get_mode().web.stop(self.get_mode().app.port) - self.get_mode().web_thread.quit() - self.get_mode().web_thread.wait() + if self.get_mode(): + if self.get_mode().web_thread: + self.get_mode().web.stop(self.get_mode().app.port) + self.get_mode().web_thread.quit() + self.get_mode().web_thread.wait() + self.get_mode().web.cleanup() diff --git a/desktop/src/onionshare/tab_widget.py b/desktop/src/onionshare/tab_widget.py index 7162fcc4..c43a9b9a 100644 --- a/desktop/src/onionshare/tab_widget.py +++ b/desktop/src/onionshare/tab_widget.py @@ -316,6 +316,7 @@ class TabWidget(QtWidgets.QTabWidget): self.common.log("TabWidget", "closing a service tab") if tab.close_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 tab.settings.get("persistent", "enabled"): From b3d53ca2f2594fec59b5daea12768984a28e3e66 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 1 Dec 2021 21:01:32 -0800 Subject: [PATCH 5/6] Fix CLI tests --- cli/onionshare_cli/web/share_mode.py | 7 +++-- cli/tests/conftest.py | 25 ++++++++--------- cli/tests/test_cli_settings.py | 4 +-- cli/tests/test_cli_web.py | 42 ++++++++++++++++++---------- 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/cli/onionshare_cli/web/share_mode.py b/cli/onionshare_cli/web/share_mode.py index d655255e..38f3f78d 100644 --- a/cli/onionshare_cli/web/share_mode.py +++ b/cli/onionshare_cli/web/share_mode.py @@ -541,7 +541,9 @@ class ZipWriter(object): filename. """ - def __init__(self, common, web, zip_filename=None, processed_size_callback=None): + def __init__( + self, common, web=None, zip_filename=None, processed_size_callback=None + ): self.common = common self.web = web self.cancel_compression = False @@ -555,7 +557,8 @@ class ZipWriter(object): 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) + if self.web: + self.web.cleanup_tempdirs.append(self.zip_temp_dir) self.z = zipfile.ZipFile(self.zip_filename, "w", allowZip64=True) self.processed_size_callback = processed_size_callback diff --git a/cli/tests/conftest.py b/cli/tests/conftest.py index 37b929ec..f4d51f9d 100644 --- a/cli/tests/conftest.py +++ b/cli/tests/conftest.py @@ -37,7 +37,7 @@ def temp_dir(): """Creates a persistent temporary directory for the CLI tests to use""" global test_temp_dir if not test_temp_dir: - test_temp_dir = tempfile.mkdtemp() + test_temp_dir = tempfile.TemporaryDirectory() return test_temp_dir @@ -47,10 +47,9 @@ def temp_dir_1024(temp_dir): particular size (1024 bytes). """ - new_temp_dir = tempfile.mkdtemp(dir=temp_dir) - tmp_file, tmp_file_path = tempfile.mkstemp(dir=new_temp_dir) - with open(tmp_file, "wb") as f: - f.write(b"*" * 1024) + new_temp_dir = tempfile.TemporaryDirectory(dir=temp_dir.name) + tmp_file = tempfile.NamedTemporaryFile(dir=new_temp_dir.name) + tmp_file.write(b"*" * 1024) return new_temp_dir @@ -61,9 +60,8 @@ def temp_dir_1024_delete(temp_dir): the file inside) will be deleted after fixture usage. """ - with tempfile.TemporaryDirectory(dir=temp_dir) as new_temp_dir: - tmp_file, tmp_file_path = tempfile.mkstemp(dir=new_temp_dir) - with open(tmp_file, "wb") as f: + with tempfile.TemporaryDirectory(dir=temp_dir.name) as new_temp_dir: + with open(os.path.join(new_temp_dir, "file"), "wb") as f: f.write(b"*" * 1024) yield new_temp_dir @@ -72,9 +70,10 @@ def temp_dir_1024_delete(temp_dir): def temp_file_1024(temp_dir): """Create a temporary file of a particular size (1024 bytes).""" - with tempfile.NamedTemporaryFile(delete=False, dir=temp_dir) as tmp_file: - tmp_file.write(b"*" * 1024) - return tmp_file.name + filename = os.path.join(temp_dir.name, "file") + with open(filename, "wb") as f: + f.write(b"*" * 1024) + return filename @pytest.fixture @@ -84,11 +83,11 @@ def temp_file_1024_delete(temp_dir): The temporary file will be deleted after fixture usage. """ - with tempfile.NamedTemporaryFile(dir=temp_dir, delete=False) as tmp_file: + with tempfile.NamedTemporaryFile(dir=temp_dir.name, delete=False) as tmp_file: tmp_file.write(b"*" * 1024) tmp_file.flush() tmp_file.close() - yield tmp_file.name + yield tmp_file @pytest.fixture(scope="session") diff --git a/cli/tests/test_cli_settings.py b/cli/tests/test_cli_settings.py index 9513b013..4a4e6fd0 100644 --- a/cli/tests/test_cli_settings.py +++ b/cli/tests/test_cli_settings.py @@ -54,7 +54,7 @@ class TestSettings: "socks_port": 9999, "use_stealth": True, } - tmp_file, tmp_file_path = tempfile.mkstemp(dir=temp_dir) + tmp_file, tmp_file_path = tempfile.mkstemp(dir=temp_dir.name) with open(tmp_file, "w") as f: json.dump(custom_settings, f) settings_obj.filename = tmp_file_path @@ -69,7 +69,7 @@ class TestSettings: def test_save(self, monkeypatch, temp_dir, settings_obj): settings_filename = "default_settings.json" - new_temp_dir = tempfile.mkdtemp(dir=temp_dir) + new_temp_dir = tempfile.mkdtemp(dir=temp_dir.name) settings_path = os.path.join(new_temp_dir, settings_filename) settings_obj.filename = settings_path settings_obj.save() diff --git a/cli/tests/test_cli_web.py b/cli/tests/test_cli_web.py index 71bfeeeb..e93e3e33 100644 --- a/cli/tests/test_cli_web.py +++ b/cli/tests/test_cli_web.py @@ -50,7 +50,8 @@ def web_obj(temp_dir, common_obj, mode, num_files=0): web = Web(common_obj, False, mode_settings, mode) web.running = True - web.cleanup_filenames == [] + web.cleanup_tempfiles == [] + web.cleanup_tempdirs == [] web.app.testing = True # Share mode @@ -58,7 +59,9 @@ def web_obj(temp_dir, common_obj, mode, num_files=0): # Add files files = [] for _ in range(num_files): - with tempfile.NamedTemporaryFile(delete=False, dir=temp_dir) as tmp_file: + with tempfile.NamedTemporaryFile( + delete=False, dir=temp_dir.name + ) as tmp_file: tmp_file.write(b"*" * 1024) files.append(tmp_file.name) web.share_mode.set_file_info(files) @@ -131,7 +134,9 @@ class TestWeb: with web.app.test_client() as c: # Load / with valid auth - res = c.get("/",) + res = c.get( + "/", + ) res.get_data() assert res.status_code == 200 @@ -169,7 +174,7 @@ class TestWeb: def test_receive_mode_message_no_files(self, temp_dir, common_obj): web = web_obj(temp_dir, common_obj, "receive") - data_dir = os.path.join(temp_dir, "OnionShare") + data_dir = os.path.join(temp_dir.name, "OnionShare") os.makedirs(data_dir, exist_ok=True) web.settings.set("receive", "data_dir", data_dir) @@ -200,7 +205,7 @@ class TestWeb: def test_receive_mode_message_and_files(self, temp_dir, common_obj): web = web_obj(temp_dir, common_obj, "receive") - data_dir = os.path.join(temp_dir, "OnionShare") + data_dir = os.path.join(temp_dir.name, "OnionShare") os.makedirs(data_dir, exist_ok=True) web.settings.set("receive", "data_dir", data_dir) @@ -235,7 +240,7 @@ class TestWeb: def test_receive_mode_files_no_message(self, temp_dir, common_obj): web = web_obj(temp_dir, common_obj, "receive") - data_dir = os.path.join(temp_dir, "OnionShare") + data_dir = os.path.join(temp_dir.name, "OnionShare") os.makedirs(data_dir, exist_ok=True) web.settings.set("receive", "data_dir", data_dir) @@ -267,7 +272,7 @@ class TestWeb: def test_receive_mode_no_message_no_files(self, temp_dir, common_obj): web = web_obj(temp_dir, common_obj, "receive") - data_dir = os.path.join(temp_dir, "OnionShare") + data_dir = os.path.join(temp_dir.name, "OnionShare") os.makedirs(data_dir, exist_ok=True) web.settings.set("receive", "data_dir", data_dir) @@ -300,15 +305,21 @@ class TestWeb: res.get_data() assert res.status_code == 200 - def test_cleanup(self, common_obj, temp_dir_1024, temp_file_1024): + def test_cleanup(self, common_obj, temp_dir_1024): web = web_obj(temp_dir_1024, common_obj, "share", 3) - web.cleanup_filenames = [temp_dir_1024, temp_file_1024] + temp_file = tempfile.NamedTemporaryFile() + temp_dir = tempfile.TemporaryDirectory() + + web.cleanup_tempfiles = [temp_file] + web.cleanup_tempdirs = [temp_dir] web.cleanup() - assert os.path.exists(temp_file_1024) is False - assert os.path.exists(temp_dir_1024) is False - assert web.cleanup_filenames == [] + assert os.path.exists(temp_file.name) is False + assert os.path.exists(temp_dir.name) is False + + assert web.cleanup_tempfiles == [] + assert web.cleanup_tempdirs == [] class TestZipWriterDefault: @@ -339,8 +350,10 @@ class TestZipWriterDefault: assert default_zw.processed_size_callback(None) is None def test_add_file(self, default_zw, temp_file_1024_delete): - default_zw.add_file(temp_file_1024_delete) - zipfile_info = default_zw.z.getinfo(os.path.basename(temp_file_1024_delete)) + default_zw.add_file(temp_file_1024_delete.name) + zipfile_info = default_zw.z.getinfo( + os.path.basename(temp_file_1024_delete.name) + ) assert zipfile_info.compress_type == zipfile.ZIP_DEFLATED assert zipfile_info.file_size == 1024 @@ -568,7 +581,6 @@ class TestRangeRequests: resp = client.get(url, headers=headers) assert resp.status_code == 206 - @pytest.mark.skipif(sys.platform != "linux", reason="requires Linux") @check_unsupported("curl", ["--version"]) def test_curl(self, temp_dir, tmpdir, common_obj): From 1dde6f6debb8c9b21e63b40d5bea81b5217d2461 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Thu, 2 Dec 2021 18:51:06 -0800 Subject: [PATCH 6/6] Add temp file to cleanup list just once when its first created --- cli/onionshare_cli/web/send_base_mode.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index d786b8b5..d0fccf06 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -200,11 +200,11 @@ class SendBaseModeWeb: self._gzip_compress(filesystem_path, gzip_file.name, 6, None) self.gzip_individual_files[filesystem_path] = gzip_file.name + # Cleanup this temp file + self.web.cleanup_tempfiles.append(gzip_file) + file_to_download = 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: file_to_download = filesystem_path filesize = os.path.getsize(filesystem_path)