From b67d6f1cda27f82a7e4efbf53013bea69110fa48 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Tue, 23 May 2023 00:53:15 -0400 Subject: [PATCH] Better cleanup of temp files --- src/qz/App.java | 3 ++- src/qz/common/TrayManager.java | 1 + src/qz/installer/Installer.java | 3 +++ .../certificate/NativeCertificateInstaller.java | 7 ++++++- src/qz/utils/FileUtilities.java | 11 +++++++++-- src/qz/utils/MacUtilities.java | 8 +++++++- 6 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/qz/App.java b/src/qz/App.java index e115a07a8..e410a28fc 100644 --- a/src/qz/App.java +++ b/src/qz/App.java @@ -32,6 +32,7 @@ public static void main(String ... args) { ArgParser parser = new ArgParser(args); LibUtilities.getInstance().bind(); if(parser.intercept()) { + FileUtilities.cleanup(); System.exit(parser.getExitCode()); } SingleInstanceChecker.stealWebsocket = parser.hasFlag(ArgValue.STEAL); @@ -74,7 +75,7 @@ public static void main(String ... args) { catch(Exception e) { log.error("Could not start tray manager", e); } - + FileUtilities.cleanup(); log.warn("The web socket server is no longer running"); } diff --git a/src/qz/common/TrayManager.java b/src/qz/common/TrayManager.java index b620b48f0..dc8bc2ab4 100644 --- a/src/qz/common/TrayManager.java +++ b/src/qz/common/TrayManager.java @@ -477,6 +477,7 @@ public void actionPerformed(ActionEvent e) { public void exit(int returnCode) { prefs.save(); + FileUtilities.cleanup(); System.exit(returnCode); } diff --git a/src/qz/installer/Installer.java b/src/qz/installer/Installer.java index d225110b4..e6a9bacf9 100644 --- a/src/qz/installer/Installer.java +++ b/src/qz/installer/Installer.java @@ -268,6 +268,9 @@ public CertificateManager certGen(boolean forceNew, String... hostNames) throws installer.install(caCert); FirefoxCertificateInstaller.install(caCert, hostNames); } + if(!tempCert.delete()) { + tempCert.deleteOnExit(); + } } } } diff --git a/src/qz/installer/certificate/NativeCertificateInstaller.java b/src/qz/installer/certificate/NativeCertificateInstaller.java index 4360f5274..4b765c450 100644 --- a/src/qz/installer/certificate/NativeCertificateInstaller.java +++ b/src/qz/installer/certificate/NativeCertificateInstaller.java @@ -53,8 +53,9 @@ public static NativeCertificateInstaller getInstance(Installer.PrivilegeLevel ty * Install a certificate from memory */ public boolean install(X509Certificate cert) { + File certFile = null; try { - File certFile = File.createTempFile(KeyPairWrapper.getAlias(KeyPairWrapper.Type.CA) + "-", CertificateManager.DEFAULT_CERTIFICATE_EXTENSION); + certFile = File.createTempFile(KeyPairWrapper.getAlias(KeyPairWrapper.Type.CA) + "-", CertificateManager.DEFAULT_CERTIFICATE_EXTENSION); JcaMiscPEMGenerator generator = new JcaMiscPEMGenerator(cert); JcaPEMWriter writer = new JcaPEMWriter(new OutputStreamWriter(Files.newOutputStream(certFile.toPath(), StandardOpenOption.CREATE))); writer.writeObject(generator.generate()); @@ -63,6 +64,10 @@ public boolean install(X509Certificate cert) { return install(certFile); } catch(IOException e) { log.warn("Could not install cert from temp file", e); + } finally { + if(certFile != null && !certFile.delete()) { + certFile.deleteOnExit(); + } } return false; } diff --git a/src/qz/utils/FileUtilities.java b/src/qz/utils/FileUtilities.java index caf988588..788fca336 100644 --- a/src/qz/utils/FileUtilities.java +++ b/src/qz/utils/FileUtilities.java @@ -10,6 +10,7 @@ package qz.utils; import org.apache.commons.io.Charsets; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.text.translate.CharSequenceTranslator; import org.apache.commons.lang3.text.translate.LookupTranslator; @@ -848,9 +849,9 @@ public static void configureAssetFile(String relativeAsset, Path dest, HashMap