From 33fd639f2ad844031ef664107ec13d234fd7b6b2 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Wed, 9 Feb 2022 18:19:50 -0800 Subject: [PATCH 1/3] Use a TemporaryDirectory instead of NamedTemporaryFile to avoid opening an already open file in Windows --- cli/onionshare_cli/web/send_base_mode.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index e608298b..2e906081 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -44,8 +44,9 @@ class SendBaseModeWeb: self.download_filesize = None self.zip_writer = None - # Store the tempfile objects here, so when they're garbage collected the files are deleted - self.gzip_files = [] + # Create a temporary dir to store gzip files in + self.gzip_tmp_dir = tempfile.TemporaryDirectory(dir=self.common.build_tmp_dir()) + self.gzip_counter = 0 # If autostop_sharing, only allow one download at a time self.download_in_progress = False @@ -193,15 +194,15 @@ 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: - self.gzip_files.append( - tempfile.NamedTemporaryFile("wb+", dir=self.common.build_tmp_dir()) + gzip_filename = os.path.join( + self.gzip_tmp_dir.name, str(self.gzip_counter) ) - 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 + self.gzip_counter += 1 + self._gzip_compress(filesystem_path, gzip_filename, 6, None) + self.gzip_individual_files[filesystem_path] = gzip_filename # Cleanup this temp file - self.web.cleanup_tempfiles.append(gzip_file) + self.web.cleanup_tempfiles.append(gzip_filename) file_to_download = self.gzip_individual_files[filesystem_path] filesize = os.path.getsize(self.gzip_individual_files[filesystem_path]) From 305abff13c8bb72e52c8e0268aa2e5efcf00a6d1 Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 13 Feb 2022 10:40:55 -0800 Subject: [PATCH 2/3] Remove cleanup_tempfiles altogether because they are not being used --- cli/onionshare_cli/web/send_base_mode.py | 3 --- cli/onionshare_cli/web/web.py | 6 ------ cli/tests/test_cli_web.py | 4 ---- 3 files changed, 13 deletions(-) diff --git a/cli/onionshare_cli/web/send_base_mode.py b/cli/onionshare_cli/web/send_base_mode.py index 2e906081..d690c98d 100644 --- a/cli/onionshare_cli/web/send_base_mode.py +++ b/cli/onionshare_cli/web/send_base_mode.py @@ -201,9 +201,6 @@ class SendBaseModeWeb: self._gzip_compress(filesystem_path, gzip_filename, 6, None) self.gzip_individual_files[filesystem_path] = gzip_filename - # Cleanup this temp file - self.web.cleanup_tempfiles.append(gzip_filename) - file_to_download = self.gzip_individual_files[filesystem_path] filesize = os.path.getsize(self.gzip_individual_files[filesystem_path]) else: diff --git a/cli/onionshare_cli/web/web.py b/cli/onionshare_cli/web/web.py index 64844b5c..fdbed567 100644 --- a/cli/onionshare_cli/web/web.py +++ b/cli/onionshare_cli/web/web.py @@ -171,7 +171,6 @@ class Web: self.socketio.init_app(self.app) self.chat_mode = ChatModeWeb(self.common, self) - self.cleanup_tempfiles = [] self.cleanup_tempdirs = [] def get_mode(self): @@ -405,13 +404,8 @@ class Web: """ self.common.log("Web", "cleanup") - # 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/cli/tests/test_cli_web.py b/cli/tests/test_cli_web.py index f6076ef9..aa5d06a7 100644 --- a/cli/tests/test_cli_web.py +++ b/cli/tests/test_cli_web.py @@ -308,17 +308,13 @@ class TestWeb: def test_cleanup(self, common_obj, temp_dir_1024): web = web_obj(temp_dir_1024, common_obj, "share", 3) - 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.name) is False assert os.path.exists(temp_dir.name) is False - assert web.cleanup_tempfiles == [] assert web.cleanup_tempdirs == [] From 5f2b3b58aa4b73ec7af7189065d226bdc49081de Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 13 Feb 2022 10:43:02 -0800 Subject: [PATCH 3/3] Fix CLI web tests --- cli/tests/test_cli_web.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/tests/test_cli_web.py b/cli/tests/test_cli_web.py index aa5d06a7..335c3a1a 100644 --- a/cli/tests/test_cli_web.py +++ b/cli/tests/test_cli_web.py @@ -50,7 +50,6 @@ def web_obj(temp_dir, common_obj, mode, num_files=0): web = Web(common_obj, False, mode_settings, mode) web.running = True - web.cleanup_tempfiles == [] web.cleanup_tempdirs == [] web.app.testing = True