cleaned up file deletion code slightly

This commit is contained in:
Jeremy Long
2016-09-06 06:23:55 -04:00
parent ffa846c05a
commit e868ce8328
5 changed files with 25 additions and 29 deletions

View File

@@ -209,8 +209,6 @@ public class AssemblyAnalyzer extends AbstractFileTypeAnalyzer {
IOUtils.copy(is, fos); IOUtils.copy(is, fos);
grokAssemblyExe = tempFile; grokAssemblyExe = tempFile;
// Set the temp file to get deleted when we're done
grokAssemblyExe.deleteOnExit();
LOGGER.debug("Extracted GrokAssembly.exe to {}", grokAssemblyExe.getPath()); LOGGER.debug("Extracted GrokAssembly.exe to {}", grokAssemblyExe.getPath());
} catch (IOException ioe) { } catch (IOException ioe) {
this.setEnabled(false); this.setEnabled(false);
@@ -295,10 +293,12 @@ public class AssemblyAnalyzer extends AbstractFileTypeAnalyzer {
super.close(); super.close();
try { try {
if (grokAssemblyExe != null && !grokAssemblyExe.delete()) { if (grokAssemblyExe != null && !grokAssemblyExe.delete()) {
LOGGER.debug("Unable to delete temporary GrokAssembly.exe; attempting delete on exit");
grokAssemblyExe.deleteOnExit(); grokAssemblyExe.deleteOnExit();
} }
} catch (SecurityException se) { } catch (SecurityException se) {
LOGGER.debug("Can't delete temporary GrokAssembly.exe"); LOGGER.debug("Can't delete temporary GrokAssembly.exe");
grokAssemblyExe.deleteOnExit();
} }
} }

View File

@@ -229,7 +229,8 @@ public class CentralAnalyzer extends AbstractFileTypeAnalyzer {
LOGGER.warn("Unable to download pom.xml for {} from Central; " LOGGER.warn("Unable to download pom.xml for {} from Central; "
+ "this could result in undetected CPE/CVEs.", dependency.getFileName()); + "this could result in undetected CPE/CVEs.", dependency.getFileName());
} finally { } finally {
if (pomFile != null && !FileUtils.deleteQuietly(pomFile)) { if (pomFile != null && pomFile.exists() && !FileUtils.deleteQuietly(pomFile)) {
LOGGER.debug("Failed to delete temporary pom file {}", pomFile.toString());
pomFile.deleteOnExit(); pomFile.deleteOnExit();
} }
} }

View File

@@ -245,7 +245,8 @@ public class NexusAnalyzer extends AbstractFileTypeAnalyzer {
LOGGER.warn("Unable to download pom.xml for {} from Nexus repository; " LOGGER.warn("Unable to download pom.xml for {} from Nexus repository; "
+ "this could result in undetected CPE/CVEs.", dependency.getFileName()); + "this could result in undetected CPE/CVEs.", dependency.getFileName());
} finally { } finally {
if (pomFile != null && !FileUtils.deleteQuietly(pomFile)) { if (pomFile != null && pomFile.exists() && !FileUtils.deleteQuietly(pomFile)) {
LOGGER.debug("Failed to delete temporary pom file {}", pomFile.toString());
pomFile.deleteOnExit(); pomFile.deleteOnExit();
} }
} }

View File

@@ -158,6 +158,7 @@ public class CpeUpdater extends BaseUpdater implements CachedWebDataSource {
final String originalPath = file.getPath(); final String originalPath = file.getPath();
final File gzip = new File(originalPath + ".gz"); final File gzip = new File(originalPath + ".gz");
if (gzip.isFile() && !gzip.delete()) { if (gzip.isFile() && !gzip.delete()) {
LOGGER.debug("Failed to delete intial temporary file {}", gzip.toString());
gzip.deleteOnExit(); gzip.deleteOnExit();
} }
if (!file.renameTo(gzip)) { if (!file.renameTo(gzip)) {
@@ -192,8 +193,9 @@ public class CpeUpdater extends BaseUpdater implements CachedWebDataSource {
LOGGER.trace("ignore", ex); LOGGER.trace("ignore", ex);
} }
} }
if (gzip.isFile()) { if (gzip.isFile() && !FileUtils.deleteQuietly(gzip)) {
FileUtils.deleteQuietly(gzip); LOGGER.debug("Failed to delete temporary file {}", gzip.toString());
gzip.deleteOnExit();
} }
} }
} }

View File

@@ -55,8 +55,9 @@ public class DownloadTask implements Callable<Future<ProcessTask>> {
* @param nvdCveInfo the NVD CVE info * @param nvdCveInfo the NVD CVE info
* @param processor the processor service to submit the downloaded files to * @param processor the processor service to submit the downloaded files to
* @param cveDB the CVE DB to use to store the vulnerability data * @param cveDB the CVE DB to use to store the vulnerability data
* @param settings a reference to the global settings object; this is necessary so that when the thread is started the * @param settings a reference to the global settings object; this is
* dependencies have a correct reference to the global settings. * necessary so that when the thread is started the dependencies have a
* correct reference to the global settings.
* @throws UpdateException thrown if temporary files could not be created * @throws UpdateException thrown if temporary files could not be created
*/ */
public DownloadTask(NvdCveInfo nvdCveInfo, ExecutorService processor, CveDB cveDB, Settings settings) throws UpdateException { public DownloadTask(NvdCveInfo nvdCveInfo, ExecutorService processor, CveDB cveDB, Settings settings) throws UpdateException {
@@ -205,25 +206,13 @@ public class DownloadTask implements Callable<Future<ProcessTask>> {
* Attempts to delete the files that were downloaded. * Attempts to delete the files that were downloaded.
*/ */
public void cleanup() { public void cleanup() {
boolean deleted = false; if (first != null && first.exists() && first.delete()) {
try { LOGGER.debug("Failed to delete first temporary file {}", second.toString());
if (first != null && first.exists()) { first.deleteOnExit();
deleted = first.delete();
}
} finally {
if (first != null && (first.exists() || !deleted)) {
first.deleteOnExit();
}
} }
try { if (second != null && second.exists() && !second.delete()) {
deleted = false; LOGGER.debug("Failed to delete second temporary file {}", second.toString());
if (second != null && second.exists()) { second.deleteOnExit();
deleted = second.delete();
}
} finally {
if (second != null && (second.exists() || !deleted)) {
second.deleteOnExit();
}
} }
} }
@@ -268,7 +257,8 @@ public class DownloadTask implements Callable<Future<ProcessTask>> {
} }
/** /**
* Extracts the file contained in a gzip archive. The extracted file is placed in the exact same path as the file specified. * Extracts the file contained in a gzip archive. The extracted file is
* placed in the exact same path as the file specified.
* *
* @param file the archive file * @param file the archive file
* @throws FileNotFoundException thrown if the file does not exist * @throws FileNotFoundException thrown if the file does not exist
@@ -278,6 +268,7 @@ public class DownloadTask implements Callable<Future<ProcessTask>> {
final String originalPath = file.getPath(); final String originalPath = file.getPath();
final File gzip = new File(originalPath + ".gz"); final File gzip = new File(originalPath + ".gz");
if (gzip.isFile() && !gzip.delete()) { if (gzip.isFile() && !gzip.delete()) {
LOGGER.debug("Failed to delete initial temporary file when extracting 'gz' {}", gzip.toString());
gzip.deleteOnExit(); gzip.deleteOnExit();
} }
if (!file.renameTo(gzip)) { if (!file.renameTo(gzip)) {
@@ -312,8 +303,9 @@ public class DownloadTask implements Callable<Future<ProcessTask>> {
LOGGER.trace("ignore", ex); LOGGER.trace("ignore", ex);
} }
} }
if (gzip.isFile()) { if (gzip.isFile() && !FileUtils.deleteQuietly(gzip)) {
FileUtils.deleteQuietly(gzip); LOGGER.debug("Failed to delete temporary file when extracting 'gz' {}", gzip.toString());
gzip.deleteOnExit();
} }
} }
} }