Skip to content

Commit

Permalink
Better cleanup of temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed May 23, 2023
1 parent ebd7ad0 commit b67d6f1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/qz/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}

Expand Down
1 change: 1 addition & 0 deletions src/qz/common/TrayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ public void actionPerformed(ActionEvent e) {

public void exit(int returnCode) {
prefs.save();
FileUtilities.cleanup();
System.exit(returnCode);
}

Expand Down
3 changes: 3 additions & 0 deletions src/qz/installer/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ public CertificateManager certGen(boolean forceNew, String... hostNames) throws
installer.install(caCert);
FirefoxCertificateInstaller.install(caCert, hostNames);
}
if(!tempCert.delete()) {
tempCert.deleteOnExit();
}
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/qz/installer/certificate/NativeCertificateInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletions src/qz/utils/FileUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -848,9 +849,9 @@ public static void configureAssetFile(String relativeAsset, Path dest, HashMap<S
configureAssetFile(relativeAsset, dest.toFile(), additionalMappings, relativeClass);
}

public static Path getTempDirectory() {
private static Path getTempDirectory() {
try {
return Files.createTempDirectory(Constants.DATA_DIR);
return Files.createTempDirectory(Constants.DATA_DIR + "_data_");
} catch(IOException e) {
log.warn("We couldn't get a temp directory for writing. This could cause some items to break");
}
Expand Down Expand Up @@ -891,4 +892,10 @@ public static void setPermissionsRecursively(Path toRecurse, boolean worldWrite)
log.warn("An error occurred setting permissions: {}", toRecurse);
}
}

public static void cleanup() {
if(FileUtilities.TEMP_DIR != null) {
FileUtils.deleteQuietly(FileUtilities.TEMP_DIR.toFile());
}
}
}
8 changes: 7 additions & 1 deletion src/qz/utils/MacUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package qz.utils;

import com.apple.OSXAdapterWrapper;
import org.apache.commons.io.FileUtils;
import org.dyorgio.jna.platform.mac.*;
import com.github.zafarkhaja.semver.Version;
import com.sun.jna.NativeLong;
Expand Down Expand Up @@ -274,12 +275,13 @@ public static void setFocus() {
}

public static boolean nativeFileCopy(Path source, Path destination) {
Path tempFile = null;
try {
// AppleScript's "duplicate" requires an existing destination
if (!destination.toFile().isDirectory()) {
// To perform this in a single operation in AppleScript, the source and dest
// file names must match. Copy to a temp directory first to retain desired name.
Path tempFile = Files.createTempDirectory("qz_cert_").resolve(destination.getFileName());
tempFile = Files.createTempDirectory("qz_cert_").resolve(destination.getFileName());
log.debug("Copying {} to {} to obtain the desired name", source, tempFile);
source = Files.copy(source, tempFile);
destination = destination.getParent();
Expand All @@ -291,6 +293,10 @@ public static boolean nativeFileCopy(Path source, Path destination) {
"with replacing");
} catch(Throwable t) {
log.warn("Unable to perform native file copy using AppleScript", t);
} finally {
if(tempFile != null) {
FileUtils.deleteQuietly(tempFile.getParent().toFile());
}
}
return false;
}
Expand Down

0 comments on commit b67d6f1

Please sign in to comment.